diff --git a/source/code/main.cpp b/source/code/main.cpp index 7e90f0e..9a43d94 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp @@ -36,6 +36,7 @@ https://blockattack.net #include "sago/SagoSpriteHolder.hpp" #include "sago/SagoTextBox.hpp" +#include "sago/SagoMisc.hpp" #include #include #include //Used for srand() @@ -259,6 +260,10 @@ void ResetFullscreen() { dataHolder.invalidateAll(globalData.screen); globalData.spriteHolder.reset(new sago::SagoSpriteHolder( dataHolder ) ); globalData.spriteHolder->ReadSprites(globalData.modinfo.getModSpriteFiles()); + if (sago::FileExists("sprites/custom_backgrounds.sprite")) { + std::vector custom_backgrounds = { "custom_backgrounds" }; + globalData.spriteHolder->ReadSprites(custom_backgrounds); + } InitImages(*(globalData.spriteHolder.get()) ); SDL_ShowCursor(SDL_DISABLE); } diff --git a/source/code/sago/SagoSpriteHolder.cpp b/source/code/sago/SagoSpriteHolder.cpp index 94c4c1c..88047d4 100644 --- a/source/code/sago/SagoSpriteHolder.cpp +++ b/source/code/sago/SagoSpriteHolder.cpp @@ -148,6 +148,7 @@ const sago::SagoSprite& SagoSpriteHolder::GetSprite(const std::string& spritenam } } + const SagoDataHolder& SagoSpriteHolder::GetDataHolder() const { return *data->tex; } diff --git a/source/code/themes.cpp b/source/code/themes.cpp index 4899875..1053502 100644 --- a/source/code/themes.cpp +++ b/source/code/themes.cpp @@ -25,6 +25,7 @@ https://www.blockattack.net #include "themes.hpp" #include "sago/SagoMisc.hpp" #include +#include #include #include "nlohmann/json.hpp" #include @@ -231,6 +232,7 @@ void ThemesInit() { } initialized = true; ThemesInitBackGroundData(); + ThemesInitCustomBackgrounds(); themes.resize(1); //Add the default theme ThemesFillMissingFields(themes[0]); const std::vector& theme_files = sago::GetFileList("themes"); @@ -309,3 +311,38 @@ std::string ThemesGetNextBoardBackground(const std::string& current) { } return ret; } + +void ThemesInitCustomBackgrounds() { + std::vector custom_backgrounds = sago::GetFileList("textures/backgrounds"); + std::stringstream sprite_stream; + sprite_stream << "{\n"; + bool first = true; + for (const std::string& filename : custom_backgrounds) { + std::cout << "Found custom background " << filename << "\n"; + if (boost::algorithm::ends_with(filename, ".png") || boost::algorithm::ends_with(filename, ".jpg")) { + BackGroundData bg; + bg.name = filename; + std::string texture_name = fmt::format("custom_background_{}",filename);; + bg.background_sprite = texture_name; + bg.background_sprite_16x9 = texture_name; + bg.background_scale = ImgScale::Resize; + background_data[bg.name] = bg; + if (!first) { + sprite_stream << ",\n"; + } + sprite_stream << "\"" << texture_name << "\": {\n"; + sprite_stream << " \"texture\": \"" << "backgrounds/" << filename.substr(0, filename.size()-4) << "\",\n"; + sprite_stream << " \"topx\": 0,\n"; + sprite_stream << " \"topy\": 0,\n"; + sprite_stream << " \"height\": 7680,\n"; + sprite_stream << " \"width\": 10240,\n"; + sprite_stream << " \"number_of_frames\": 1,\n"; + sprite_stream << " \"frame_time\": 1\n"; + sprite_stream << "}\n"; + first = false; + + } + } + sprite_stream << "}\n"; + sago::WriteFileContent("sprites/custom_backgrounds.sprite", sprite_stream.str()); +} \ No newline at end of file diff --git a/source/code/themes.hpp b/source/code/themes.hpp index f5d9dd1..f74f837 100644 --- a/source/code/themes.hpp +++ b/source/code/themes.hpp @@ -93,4 +93,6 @@ size_t ThemeGetNumber(const std::string& name); BackGroundData ThemesGetNextBackground(const std::string& current); -std::string ThemesGetNextBoardBackground(const std::string& current); \ No newline at end of file +std::string ThemesGetNextBoardBackground(const std::string& current); + +void ThemesInitCustomBackgrounds(); \ No newline at end of file