working asset manager !
All checks were successful
Linux arm64 / Build (push) Successful in 1m58s

This commit is contained in:
2025-05-27 21:29:56 +02:00
parent 3d1feb6295
commit d50714ef8c
8 changed files with 175 additions and 19 deletions

View File

@@ -1,12 +1,12 @@
#include "AppMenu.h" #include "AppMenu.h"
#include "../AssetManager.h" #include "../../Utils/AssetManager.h"
AppMenu::AppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow) : menuStack(menuStack), AppMenu::AppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow) : menuStack(menuStack),
settings(settings), settings(settings),
renderWindow(renderWindow) renderWindow(renderWindow)
{ {
Asset file = getResource(AssetName::data_fonts_pressstart_prstartk_ttf); const Asset& file = getResource(AssetName::data_fonts_pressstart_prstartk_ttf);
this->pressStartFont = sf::Font(file.data, file.size); this->pressStartFont = sf::Font(file.data, file.size);
} }

View File

@@ -2,6 +2,7 @@
#include "AppMenu.h" #include "AppMenu.h"
#include "../PlayerCursor.h" #include "../PlayerCursor.h"
#include "../../Utils/AssetManager.h"
#include <stack> #include <stack>
#include <memory> #include <memory>
@@ -22,8 +23,9 @@ SettingsKeybindsAppMenu::SettingsKeybindsAppMenu(std::shared_ptr<MenuStack> menu
std::string textureName = ACTION_NAMES[action]; std::string textureName = ACTION_NAMES[action];
textureName = std::regex_replace(textureName, std::regex(" "), ""); textureName = std::regex_replace(textureName, std::regex(" "), "");
std::filesystem::path texturePath("data/images/keybinds/" + textureName + ".png"); const Asset& textureData = getResource("data/images/keybinds/" + textureName + ".png");
this->iconTextures[action] = sf::Texture(texturePath, false, {{0, 0}, {16, 16}});
this->iconTextures[action] = sf::Texture(textureData.data, textureData.size, false, {{0, 0}, {16, 16}});
} }
} }

View File

@@ -70,6 +70,7 @@ int main() {
void resetSettingsFile() { void resetSettingsFile() {
std::filesystem::create_directories("data/config");
std::ofstream settingsFile("data/config/settings.bin", std::ios::trunc | std::ios::binary); std::ofstream settingsFile("data/config/settings.bin", std::ios::trunc | std::ios::binary);
char byte; char byte;
@@ -137,6 +138,7 @@ void resetSettingsFile() {
void resetKeybindFile(int layout) { void resetKeybindFile(int layout) {
if (layout < 0 || layout > 4) return; if (layout < 0 || layout > 4) return;
std::filesystem::create_directories("data/config/keybinds/layout");
std::ofstream layoutFile("data/config/keybinds/layout" + std::to_string(layout) + ".bin", std::ios::trunc | std::ios::binary); std::ofstream layoutFile("data/config/keybinds/layout" + std::to_string(layout) + ".bin", std::ios::trunc | std::ios::binary);
std::map<Action, sfKey> keybinds; std::map<Action, sfKey> keybinds;

View File

@@ -10,6 +10,8 @@
#include <filesystem> #include <filesystem>
#include <algorithm> #include <algorithm>
namespace fs = std::filesystem;
PiecesFiles::PiecesFiles() { PiecesFiles::PiecesFiles() {
} }
@@ -132,7 +134,12 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector<Piece>& pieces, std:
bool PiecesFiles::getFilePath(int polyominoSize, std::string& filePath) const { bool PiecesFiles::getFilePath(int polyominoSize, std::string& filePath) const {
std::string dataFolderPath = "data/pieces/"; std::string dataFolderPath = "data/pieces/";
if (!std::filesystem::is_directory(dataFolderPath)) {
if (!fs::exists(dataFolderPath)) {
fs::create_directories(dataFolderPath);
}
if (!fs::is_directory(dataFolderPath)) {
return false; return false;
} }

View File

@@ -0,0 +1,97 @@
#include "./AssetManager.h"
#include <map>
static const unsigned char data_fonts_pressstart_prstartk_ttf[] = {
#include <data/fonts/pressstart/prstartk.ttf.h>
};
static const unsigned char data_fonts_pressstart_prstart_ttf[] = {
#include <data/fonts/pressstart/prstart.ttf.h>
};
static const unsigned char data_images_keybinds_Rotate0_png[] = {
#include <data/images/keybinds/Rotate0.png.h>
};
static const unsigned char data_images_keybinds_Moveright_png[] = {
#include <data/images/keybinds/Moveright.png.h>
};
static const unsigned char data_images_keybinds_RotateCW_png[] = {
#include <data/images/keybinds/RotateCW.png.h>
};
static const unsigned char data_images_keybinds_Pause_png[] = {
#include <data/images/keybinds/Pause.png.h>
};
static const unsigned char data_images_keybinds_Hold_png[] = {
#include <data/images/keybinds/Hold.png.h>
};
static const unsigned char data_images_keybinds_Softdrop_png[] = {
#include <data/images/keybinds/Softdrop.png.h>
};
static const unsigned char data_images_keybinds_RotateCCW_png[] = {
#include <data/images/keybinds/RotateCCW.png.h>
};
static const unsigned char data_images_keybinds_Moveleft_png[] = {
#include <data/images/keybinds/Moveleft.png.h>
};
static const unsigned char data_images_keybinds_Rotate180_png[] = {
#include <data/images/keybinds/Rotate180.png.h>
};
static const unsigned char data_images_keybinds_Retry_png[] = {
#include <data/images/keybinds/Retry.png.h>
};
static const unsigned char data_images_keybinds_Harddrop_png[] = {
#include <data/images/keybinds/Harddrop.png.h>
};
static const Asset assets[] = {
{data_fonts_pressstart_prstartk_ttf, sizeof(data_fonts_pressstart_prstartk_ttf)},
{data_fonts_pressstart_prstart_ttf, sizeof(data_fonts_pressstart_prstart_ttf)},
{data_images_keybinds_Rotate0_png, sizeof(data_images_keybinds_Rotate0_png)},
{data_images_keybinds_Moveright_png, sizeof(data_images_keybinds_Moveright_png)},
{data_images_keybinds_RotateCW_png, sizeof(data_images_keybinds_RotateCW_png)},
{data_images_keybinds_Pause_png, sizeof(data_images_keybinds_Pause_png)},
{data_images_keybinds_Hold_png, sizeof(data_images_keybinds_Hold_png)},
{data_images_keybinds_Softdrop_png, sizeof(data_images_keybinds_Softdrop_png)},
{data_images_keybinds_RotateCCW_png, sizeof(data_images_keybinds_RotateCCW_png)},
{data_images_keybinds_Moveleft_png, sizeof(data_images_keybinds_Moveleft_png)},
{data_images_keybinds_Rotate180_png, sizeof(data_images_keybinds_Rotate180_png)},
{data_images_keybinds_Retry_png, sizeof(data_images_keybinds_Retry_png)},
{data_images_keybinds_Harddrop_png, sizeof(data_images_keybinds_Harddrop_png)},
};
static const std::map<std::string, AssetName> assetMap = {
{"data/fonts/pressstart/prstartk.ttf", AssetName::data_fonts_pressstart_prstartk_ttf},
{"data/fonts/pressstart/prstart.ttf", AssetName::data_fonts_pressstart_prstart_ttf},
{"data/images/keybinds/Rotate0.png", AssetName::data_images_keybinds_Rotate0_png},
{"data/images/keybinds/Moveright.png", AssetName::data_images_keybinds_Moveright_png},
{"data/images/keybinds/RotateCW.png", AssetName::data_images_keybinds_RotateCW_png},
{"data/images/keybinds/Pause.png", AssetName::data_images_keybinds_Pause_png},
{"data/images/keybinds/Hold.png", AssetName::data_images_keybinds_Hold_png},
{"data/images/keybinds/Softdrop.png", AssetName::data_images_keybinds_Softdrop_png},
{"data/images/keybinds/RotateCCW.png", AssetName::data_images_keybinds_RotateCCW_png},
{"data/images/keybinds/Moveleft.png", AssetName::data_images_keybinds_Moveleft_png},
{"data/images/keybinds/Rotate180.png", AssetName::data_images_keybinds_Rotate180_png},
{"data/images/keybinds/Retry.png", AssetName::data_images_keybinds_Retry_png},
{"data/images/keybinds/Harddrop.png", AssetName::data_images_keybinds_Harddrop_png},
};
const Asset& getResource(AssetName fileName) {
return assets[static_cast<std::size_t>(fileName)];
}
const Asset& getResource(const std::string& fileName) {
return getResource(assetMap.at(fileName));
}

30
src/Utils/AssetManager.h Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#include <cstdint>
#include <string>
struct Asset {
const unsigned char* data;
std::size_t size;
};
enum class AssetName {
data_fonts_pressstart_prstartk_ttf,
data_fonts_pressstart_prstart_ttf,
data_images_keybinds_Rotate0_png,
data_images_keybinds_Moveright_png,
data_images_keybinds_RotateCW_png,
data_images_keybinds_Pause_png,
data_images_keybinds_Hold_png,
data_images_keybinds_Softdrop_png,
data_images_keybinds_RotateCCW_png,
data_images_keybinds_Moveleft_png,
data_images_keybinds_Rotate180_png,
data_images_keybinds_Retry_png,
data_images_keybinds_Harddrop_png,
};
const Asset& getResource(AssetName fileName);
const Asset& getResource(const std::string& fileName);

View File

@@ -17,8 +17,8 @@ target("graph")
set_default(true) set_default(true)
add_rules("bin2c", { add_rules("bin2c", {
extensions = {".png", ".ttf"}, extensions = {".png", ".ttf"},
outputSource = {"src/GraphicalUI/AssetManager.cpp"}, outputSource = {"src/Utils/AssetManager.cpp"},
outputHeader = {"src/GraphicalUI/AssetManager.h"} outputHeader = {"src/Utils/AssetManager.h"}
}) })
set_kind("binary") set_kind("binary")
add_files("./src/GraphicalUI/**.cpp") add_files("./src/GraphicalUI/**.cpp")

View File

@@ -1,21 +1,18 @@
buildAsset = function(target, batchcmds, sourcefile_bin, opt)
end
rule("bin2c") rule("bin2c")
set_extensions(".bin") set_extensions(".bin")
on_load(function (target) on_load(function (target)
local headerdir = path.join(target:autogendir(), "rules", "bin2c") local headerdir = path.join(target:autogendir(), "rules", "bin2c")
local outputSource = table.unpack(target:extraconf("rules", "bin2c", "outputSource"))
if not os.isdir(headerdir) then if not os.isdir(headerdir) then
os.mkdir(headerdir) os.mkdir(headerdir)
end end
target:add("includedirs", headerdir) target:add("includedirs", headerdir)
target:add("files", outputSource)
end) end)
before_buildcmd_files(function (target, batchcmds, sourcebatch, opt) before_buildcmd_files(function (target, batchcmds, sourcebatch, opt)
@@ -44,31 +41,52 @@ enum class AssetName {
}; };
const Asset& getResource(AssetName fileName); const Asset& getResource(AssetName fileName);
const Asset& getResource(const std::string& fileName);
]], outputHeaderEnumContent) ]], outputHeaderEnumContent)
local outputSource = table.unpack(target:extraconf("rules", "bin2c", "outputSource")) local outputSource = table.unpack(target:extraconf("rules", "bin2c", "outputSource"))
local outputSourceContent = string.format("#include \"%s\"\n", "AssetManager.h") local relativePath = path.join(path.relative(path.directory(outputHeader), path.directory(outputSource)), path.filename(outputHeader))
local outputSourceContent = string.format([[
#include "%s"
#include <map>
]], relativePath)
local outputSourceArrayVars = "" local outputSourceArrayVars = ""
local outputSourceMapVars = ""
for _, filePath in ipairs(sourcebatch.sourcefiles) do for _, filePath in ipairs(sourcebatch.sourcefiles) do
local escapedName = string.gsub(filePath, "[/|.]", "_") local escapedName = string.gsub(filePath, "[/|.]", "_")
local varDecl = string.format("static const unsigned char %s[] = {\n\t#include <%s>\n};\n\n", escapedName, filePath .. ".h") local varDecl = string.format("static const unsigned char %s[] = {\n\t#include <%s>\n};\n\n", escapedName, filePath .. ".h")
outputSourceContent = outputSourceContent .. varDecl outputSourceContent = outputSourceContent .. varDecl
outputSourceArrayVars = outputSourceArrayVars .. string.format("\t{%s, sizeof(%s)},\n", escapedName, escapedName) outputSourceArrayVars = outputSourceArrayVars .. string.format("\t{%s, sizeof(%s)},\n", escapedName, escapedName)
outputSourceMapVars = outputSourceMapVars .. string.format("\t{\"%s\", AssetName::%s},\n", filePath, escapedName)
end end
outputSourceContent = outputSourceContent .. string.format([[ outputSourceContent = outputSourceContent .. string.format([[
static const Asset assets[] = { static const Asset assets[] = {
%s %s
}; };
]], outputSourceArrayVars)
static const std::map<std::string, AssetName> assetMap = {
%s
};
]], outputSourceArrayVars, outputSourceMapVars)
outputSourceContent = outputSourceContent .. [[ outputSourceContent = outputSourceContent .. [[
const Asset& getResource(AssetName fileName) { const Asset& getResource(AssetName fileName) {
return assets[static_cast<std::size_t>(fileName)]; return assets[static_cast<std::size_t>(fileName)];
} }
const Asset& getResource(const std::string& fileName) {
return getResource(assetMap.at(fileName));
}
]] ]]
for _, sourcefile_bin in ipairs(sourcebatch.sourcefiles) do for _, sourcefile_bin in ipairs(sourcebatch.sourcefiles) do