1
0
mirror of synced 2025-02-09 07:09:42 +01:00

Implement JacketPack

This commit is contained in:
Stepland 2019-10-23 23:39:10 +02:00
parent aa19fff3dd
commit ac50015866
9 changed files with 138 additions and 8 deletions

View File

@ -10,8 +10,24 @@ foreach module : ['system', 'window', 'graphics', 'audio']
sfml += [dependency('sfml-'+module, version : '>=2.5.1')]
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(
'jujube',
'src/Main.cpp',
dependencies: sfml
sources,
dependencies: [sfml, filesystem]
)

View File

@ -9,15 +9,24 @@
#include "Screens/Result.hpp"
int main(int argc, char const *argv[]) {
sf::RenderWindow window(sf::VideoMode(800,600), "jujube");
window.setVerticalSyncEnabled(true);
Screen::MusicSelect music_select;
while (true) {
Chart& selected_chart = music_select.display(window);
Chart& selected_chart = music_select.select_chart(window);
Screen::Gameplay gameplay(selected_chart);
Score score = gameplay.display(window);
Score score = gameplay.play_chart(window);
Screen::Result result(score);
result.display(window);
}
return 0;
}

View File

@ -1,6 +1,20 @@
#pragma once
#include <string>
class Chart {
public:
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
View File

@ -0,0 +1,3 @@
#include "MusicList.hpp"
MusicList::MusicList()

View File

@ -1,6 +1,12 @@
#pragma once
#include <vector>
#include "../Textures/JacketPack.hpp"
class MusicList {
public:
MusicList();
private:
Textures::JacketPack jacket_previews;
};

View File

@ -9,7 +9,7 @@ namespace Screen {
const Chart& chart;
public:
explicit Gameplay(const Chart& selected_chart);
Score display(sf::Window& window) const;
Score play_chart(sf::Window& window) const;
};
};

View File

@ -5,10 +5,14 @@
#include "../Model/Chart.hpp"
namespace Screen {
/*
The music select screen is created only once and loads a cache of available songs
in the music_list attribute
*/
class MusicSelect {
MusicList music_list;
public:
MusicSelect();
Chart& display(sf::Window& window) const;
MusicSelect() = default;
Chart& select_chart(sf::Window& window) const;
};
};

View 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};
}

View 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);
}