Implement JacketPack
This commit is contained in:
parent
aa19fff3dd
commit
ac50015866
20
meson.build
20
meson.build
@ -10,8 +10,24 @@ foreach module : ['system', 'window', 'graphics', 'audio']
|
|||||||
sfml += [dependency('sfml-'+module, version : '>=2.5.1')]
|
sfml += [dependency('sfml-'+module, version : '>=2.5.1')]
|
||||||
endforeach
|
endforeach
|
||||||
|
|
||||||
|
cpp = meson.get_compiler('cpp')
|
||||||
|
filesystem = cpp.find_library('stdc++fs')
|
||||||
|
|
||||||
|
sources = [
|
||||||
|
'src/Main.cpp',
|
||||||
|
'src/Model/Chart.hpp',
|
||||||
|
'src/Model/MusicList.hpp',
|
||||||
|
'src/Model/MusicList.cpp',
|
||||||
|
'src/Model/Score.hpp',
|
||||||
|
'src/Screens/Gameplay.hpp',
|
||||||
|
'src/Screens/MusicSelect.hpp',
|
||||||
|
'src/Screens/Result.hpp',
|
||||||
|
'src/Textures/TexturePack.hpp',
|
||||||
|
'src/Textures/TexturePack.cpp',
|
||||||
|
]
|
||||||
|
|
||||||
executable(
|
executable(
|
||||||
'jujube',
|
'jujube',
|
||||||
'src/Main.cpp',
|
sources,
|
||||||
dependencies: sfml
|
dependencies: [sfml, filesystem]
|
||||||
)
|
)
|
13
src/Main.cpp
13
src/Main.cpp
@ -9,15 +9,24 @@
|
|||||||
#include "Screens/Result.hpp"
|
#include "Screens/Result.hpp"
|
||||||
|
|
||||||
int main(int argc, char const *argv[]) {
|
int main(int argc, char const *argv[]) {
|
||||||
|
|
||||||
sf::RenderWindow window(sf::VideoMode(800,600), "jujube");
|
sf::RenderWindow window(sf::VideoMode(800,600), "jujube");
|
||||||
window.setVerticalSyncEnabled(true);
|
window.setVerticalSyncEnabled(true);
|
||||||
|
|
||||||
Screen::MusicSelect music_select;
|
Screen::MusicSelect music_select;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
Chart& selected_chart = music_select.display(window);
|
|
||||||
|
Chart& selected_chart = music_select.select_chart(window);
|
||||||
|
|
||||||
Screen::Gameplay gameplay(selected_chart);
|
Screen::Gameplay gameplay(selected_chart);
|
||||||
Score score = gameplay.display(window);
|
Score score = gameplay.play_chart(window);
|
||||||
|
|
||||||
Screen::Result result(score);
|
Screen::Result result(score);
|
||||||
result.display(window);
|
result.display(window);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,20 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
class Chart {
|
class Chart {
|
||||||
public:
|
public:
|
||||||
Chart();
|
Chart();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ChartMetadata {
|
||||||
|
public:
|
||||||
|
ChartMetadata(
|
||||||
|
std::string title,
|
||||||
|
std::string artist
|
||||||
|
);
|
||||||
|
private:
|
||||||
|
std::string title;
|
||||||
|
std::string artist;
|
||||||
|
|
||||||
|
}
|
3
src/Model/MusicList.cpp
Normal file
3
src/Model/MusicList.cpp
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#include "MusicList.hpp"
|
||||||
|
|
||||||
|
MusicList::MusicList()
|
@ -1,6 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "../Textures/JacketPack.hpp"
|
||||||
|
|
||||||
class MusicList {
|
class MusicList {
|
||||||
public:
|
public:
|
||||||
MusicList();
|
MusicList();
|
||||||
|
private:
|
||||||
|
Textures::JacketPack jacket_previews;
|
||||||
};
|
};
|
||||||
|
@ -9,7 +9,7 @@ namespace Screen {
|
|||||||
const Chart& chart;
|
const Chart& chart;
|
||||||
public:
|
public:
|
||||||
explicit Gameplay(const Chart& selected_chart);
|
explicit Gameplay(const Chart& selected_chart);
|
||||||
Score display(sf::Window& window) const;
|
Score play_chart(sf::Window& window) const;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -5,10 +5,14 @@
|
|||||||
#include "../Model/Chart.hpp"
|
#include "../Model/Chart.hpp"
|
||||||
|
|
||||||
namespace Screen {
|
namespace Screen {
|
||||||
|
/*
|
||||||
|
The music select screen is created only once and loads a cache of available songs
|
||||||
|
in the music_list attribute
|
||||||
|
*/
|
||||||
class MusicSelect {
|
class MusicSelect {
|
||||||
MusicList music_list;
|
MusicList music_list;
|
||||||
public:
|
public:
|
||||||
MusicSelect();
|
MusicSelect() = default;
|
||||||
Chart& display(sf::Window& window) const;
|
Chart& select_chart(sf::Window& window) const;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
44
src/Textures/JacketPack.cpp
Normal file
44
src/Textures/JacketPack.cpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#include "JacketPack.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cassert>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
sf::Sprite Textures::JacketPack::operator[](const std::string& path) const{
|
||||||
|
auto location = get_detailed_location(locations.at(path));
|
||||||
|
sf::IntRect rect(location.column, location.row, 128, 128);
|
||||||
|
sf::Sprite jacket;
|
||||||
|
jacket.setTexture(textures.at(location.texture_index)->getTexture());
|
||||||
|
jacket.setTextureRect(rect);
|
||||||
|
return jacket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Textures::JacketPack::push_back(const std::filesystem::path& jacket) {
|
||||||
|
|
||||||
|
sf::Texture jacket_texture;
|
||||||
|
if (!jacket_texture.loadFromFile(jacket)) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Unable to load jacket image : " << jacket;
|
||||||
|
throw std::runtime_error(ss.str());
|
||||||
|
} else {
|
||||||
|
locations[jacket] = next_location;
|
||||||
|
}
|
||||||
|
auto size = jacket_texture.getSize();
|
||||||
|
auto location = get_detailed_location(next_location);
|
||||||
|
sf::Sprite new_jacket;
|
||||||
|
new_jacket.setTexture(jacket_texture);
|
||||||
|
new_jacket.setScale(128.0f/size.x, 128.0f/size.y);
|
||||||
|
new_jacket.setPosition(128.0f * location.column, 128.0f * location.row);
|
||||||
|
if (location.column == 0 and location.row == 0) {
|
||||||
|
// first jacket means it's a new texture
|
||||||
|
textures.push_back(std::make_shared<sf::RenderTexture>());
|
||||||
|
textures.back()->create(1024, 1024);
|
||||||
|
}
|
||||||
|
textures.at(location.texture_index)->draw(new_jacket);
|
||||||
|
textures.at(location.texture_index)->display();
|
||||||
|
next_location++;
|
||||||
|
}
|
||||||
|
|
||||||
|
Textures::DetailedLocation Textures::get_detailed_location(unsigned int location) {
|
||||||
|
return {location / 64, (location % 64) / 8, location % 8};
|
||||||
|
}
|
34
src/Textures/JacketPack.hpp
Normal file
34
src/Textures/JacketPack.hpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
namespace Textures {
|
||||||
|
|
||||||
|
/*
|
||||||
|
A JacketPack stores 128x128 jacket previews in 1024x1024 textures
|
||||||
|
*/
|
||||||
|
class JacketPack {
|
||||||
|
public:
|
||||||
|
JacketPack() = default;
|
||||||
|
sf::Sprite operator[](const std::string& path) const;
|
||||||
|
void push_back(const std::filesystem::path& jacket);
|
||||||
|
//private:
|
||||||
|
std::unordered_map<std::filesystem::path, unsigned int> locations;
|
||||||
|
std::vector<std::shared_ptr<sf::RenderTexture>> textures;
|
||||||
|
unsigned int next_location = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DetailedLocation {
|
||||||
|
unsigned int texture_index;
|
||||||
|
unsigned int row;
|
||||||
|
unsigned int column;
|
||||||
|
};
|
||||||
|
|
||||||
|
DetailedLocation get_detailed_location(unsigned int location);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user