This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
#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),
|
||||
settings(settings),
|
||||
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);
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "AppMenu.h"
|
||||
#include "../PlayerCursor.h"
|
||||
#include "../../Utils/AssetManager.h"
|
||||
|
||||
#include <stack>
|
||||
#include <memory>
|
||||
@@ -22,8 +23,9 @@ SettingsKeybindsAppMenu::SettingsKeybindsAppMenu(std::shared_ptr<MenuStack> menu
|
||||
std::string textureName = ACTION_NAMES[action];
|
||||
textureName = std::regex_replace(textureName, std::regex(" "), "");
|
||||
|
||||
std::filesystem::path texturePath("data/images/keybinds/" + textureName + ".png");
|
||||
this->iconTextures[action] = sf::Texture(texturePath, false, {{0, 0}, {16, 16}});
|
||||
const Asset& textureData = getResource("data/images/keybinds/" + textureName + ".png");
|
||||
|
||||
this->iconTextures[action] = sf::Texture(textureData.data, textureData.size, false, {{0, 0}, {16, 16}});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -70,6 +70,7 @@ int main() {
|
||||
|
||||
|
||||
void resetSettingsFile() {
|
||||
std::filesystem::create_directories("data/config");
|
||||
std::ofstream settingsFile("data/config/settings.bin", std::ios::trunc | std::ios::binary);
|
||||
char byte;
|
||||
|
||||
@@ -137,6 +138,7 @@ void resetSettingsFile() {
|
||||
void resetKeybindFile(int layout) {
|
||||
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::map<Action, sfKey> keybinds;
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
#include <filesystem>
|
||||
#include <algorithm>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
97
src/Utils/AssetManager.cpp
Normal file
97
src/Utils/AssetManager.cpp
Normal 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
30
src/Utils/AssetManager.h
Normal 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);
|
||||
@@ -17,8 +17,8 @@ target("graph")
|
||||
set_default(true)
|
||||
add_rules("bin2c", {
|
||||
extensions = {".png", ".ttf"},
|
||||
outputSource = {"src/GraphicalUI/AssetManager.cpp"},
|
||||
outputHeader = {"src/GraphicalUI/AssetManager.h"}
|
||||
outputSource = {"src/Utils/AssetManager.cpp"},
|
||||
outputHeader = {"src/Utils/AssetManager.h"}
|
||||
})
|
||||
set_kind("binary")
|
||||
add_files("./src/GraphicalUI/**.cpp")
|
||||
|
||||
@@ -1,21 +1,18 @@
|
||||
|
||||
buildAsset = function(target, batchcmds, sourcefile_bin, opt)
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
rule("bin2c")
|
||||
set_extensions(".bin")
|
||||
|
||||
on_load(function (target)
|
||||
local headerdir = path.join(target:autogendir(), "rules", "bin2c")
|
||||
|
||||
local outputSource = table.unpack(target:extraconf("rules", "bin2c", "outputSource"))
|
||||
|
||||
if not os.isdir(headerdir) then
|
||||
os.mkdir(headerdir)
|
||||
end
|
||||
|
||||
|
||||
target:add("includedirs", headerdir)
|
||||
|
||||
target:add("files", outputSource)
|
||||
end)
|
||||
|
||||
before_buildcmd_files(function (target, batchcmds, sourcebatch, opt)
|
||||
@@ -44,32 +41,53 @@ enum class AssetName {
|
||||
};
|
||||
|
||||
const Asset& getResource(AssetName fileName);
|
||||
]], outputHeaderEnumContent)
|
||||
|
||||
const Asset& getResource(const std::string& fileName);
|
||||
]], outputHeaderEnumContent)
|
||||
|
||||
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 outputSourceMapVars = ""
|
||||
|
||||
for _, filePath in ipairs(sourcebatch.sourcefiles) do
|
||||
local escapedName = string.gsub(filePath, "[/|.]", "_")
|
||||
local varDecl = string.format("static const unsigned char %s[] = {\n\t#include <%s>\n};\n\n", escapedName, filePath .. ".h")
|
||||
outputSourceContent = outputSourceContent .. varDecl
|
||||
outputSourceArrayVars = outputSourceArrayVars .. string.format("\t{%s, sizeof(%s)},\n", escapedName, escapedName)
|
||||
outputSourceMapVars = outputSourceMapVars .. string.format("\t{\"%s\", AssetName::%s},\n", filePath, escapedName)
|
||||
end
|
||||
|
||||
outputSourceContent = outputSourceContent .. string.format([[
|
||||
static const Asset assets[] = {
|
||||
%s
|
||||
};
|
||||
]], outputSourceArrayVars)
|
||||
|
||||
static const std::map<std::string, AssetName> assetMap = {
|
||||
%s
|
||||
};
|
||||
|
||||
]], outputSourceArrayVars, outputSourceMapVars)
|
||||
|
||||
outputSourceContent = outputSourceContent .. [[
|
||||
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));
|
||||
}
|
||||
]]
|
||||
|
||||
for _, sourcefile_bin in ipairs(sourcebatch.sourcefiles) do
|
||||
-- get header file
|
||||
|
||||
Reference in New Issue
Block a user