diff --git a/guecs.hpp b/guecs.hpp index 47547e1..f1dc26d 100644 --- a/guecs.hpp +++ b/guecs.hpp @@ -5,7 +5,7 @@ #include #include #include -#include "textures2.hpp" +#include "textures.hpp" #include #include "events.hpp" #include "constants.hpp" diff --git a/main.cpp b/main.cpp index dbbbdbc..e340ae7 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,5 @@ #include "gui_fsm.hpp" -#include "textures2.hpp" +#include "textures.hpp" int main() { textures::init(); diff --git a/meson.build b/meson.build index 9016c6c..c39ad6f 100644 --- a/meson.build +++ b/meson.build @@ -84,8 +84,7 @@ sources = [ 'stats.cpp', 'status_ui.cpp', 'systems.cpp', - 'texture.cpp', - 'textures2.cpp', + 'textures.cpp', 'tilemap.cpp', 'worldbuilder.cpp', ] diff --git a/raycaster.cpp b/raycaster.cpp index 53147c0..7a953ce 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -8,7 +8,7 @@ #include #include #include "components.hpp" -#include "textures2.hpp" +#include "textures.hpp" using namespace fmt; using std::make_unique; diff --git a/raycaster.hpp b/raycaster.hpp index e2c6bac..0f114db 100644 --- a/raycaster.hpp +++ b/raycaster.hpp @@ -5,7 +5,7 @@ #include "animator.hpp" #include "spatialmap.hpp" #include "levelmanager.hpp" -#include "texture.hpp" +#include "textures.hpp" using matrix::Matrix; using RGBA = uint32_t; @@ -37,7 +37,7 @@ struct Raycaster { int $screen_pos_y = RAY_VIEW_Y; GameLevel $level; Matrix $map; - std::unordered_map $sprites; + std::unordered_map $sprites; std::vector $zbuffer; // width Animator $anim; diff --git a/status_ui.cpp b/status_ui.cpp index 556de47..fb56153 100644 --- a/status_ui.cpp +++ b/status_ui.cpp @@ -12,12 +12,12 @@ namespace gui { { $gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT); $gui.layout( - "[*%(100,300)log_view]" - "[_]" - "[_]" "[button1 | button2 | button3]" "[button4 | button5 | button6]" "[button7 | button8 | button9]" + "[*%(100,300)log_view]" + "[_]" + "[_]" ); } diff --git a/status_ui.hpp b/status_ui.hpp index fc494a5..f350e1d 100644 --- a/status_ui.hpp +++ b/status_ui.hpp @@ -2,7 +2,7 @@ #include "levelmanager.hpp" #include "constants.hpp" #include -#include "textures2.hpp" +#include "textures.hpp" #include "guecs.hpp" namespace gui { diff --git a/tests/guecs.cpp b/tests/guecs.cpp index d9914a1..4efaa47 100644 --- a/tests/guecs.cpp +++ b/tests/guecs.cpp @@ -2,7 +2,7 @@ #include #include "constants.hpp" #include "guecs.hpp" -#include "texture.hpp" +#include "textures.hpp" using namespace guecs; diff --git a/tests/textures.cpp b/tests/textures.cpp index c12c66e..4901447 100644 --- a/tests/textures.cpp +++ b/tests/textures.cpp @@ -1,7 +1,7 @@ #include #include #include -#include "textures2.hpp" +#include "textures.hpp" using namespace fmt; diff --git a/texture.cpp b/texture.cpp deleted file mode 100644 index cd8c7f9..0000000 --- a/texture.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include "texture.hpp" -#include -#include "dbc.hpp" -#include -#include "config.hpp" -#include "constants.hpp" - -using std::shared_ptr, std::make_shared; - -sf::Image TextureManager::load_image(std::string filename) { - sf::Image texture; - bool good = texture.loadFromFile(filename); - dbc::check(good, fmt::format("failed to load {}", filename)); - return texture; -} - -void TextureManager::load_sprites() { - Config assets("assets/config.json"); - - for(auto& el : assets["sprites"].items()) { - string path = el.value(); - auto texture = make_shared(path); - - texture->setSmooth(false); - auto sprite = make_shared(*texture); - - string name = el.key(); - sprite_textures[name] = {sprite, texture}; - } - - floor = load_image(assets["sprites"]["floor"]); - ceiling = load_image(assets["sprites"]["ceiling"]); -} - -void TextureManager::load_tiles() { - Config assets("assets/tiles.json"); - auto &tiles = assets.json(); - - for(auto &el : tiles.items()) { - auto &config = el.value(); - surfaces.emplace_back(load_image(config["texture"])); - - std::wstring display = assets.wstring(el.key(), "display"); - int surface_i = surfaces.size() - 1; - wchar_t tid = display[0]; - - char_to_texture[tid] = surface_i; - } -} - -const uint32_t* TextureManager::get_surface(size_t num) { - return (const uint32_t *)surfaces[num].getPixelsPtr(); -} - -matrix::Matrix TextureManager::convert_char_to_texture(matrix::Matrix &tile_ids) { - auto result = matrix::make(matrix::width(tile_ids), matrix::height(tile_ids)); - - for(matrix::each_cell it(tile_ids); it.next();) { - wchar_t tid = tile_ids[it.y][it.x]; - result[it.y][it.x] = char_to_texture.at(tid); - } - - return result; -} - -SpriteTexture TextureManager::get(std::string name) { - dbc::check(sprite_textures.contains(name), - fmt::format("!!!!! texture pack does not contain {} sprite", name)); - return sprite_textures.at(name); -} diff --git a/texture.hpp b/texture.hpp deleted file mode 100644 index 6703ba9..0000000 --- a/texture.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include "matrix.hpp" - -struct SpriteTexture { - std::shared_ptr sprite = nullptr; - std::shared_ptr texture = nullptr; -}; - -struct TextureManager { - std::vector surfaces; - std::unordered_map sprite_textures; - std::unordered_map char_to_texture; - sf::Image floor; - sf::Image ceiling; - - void load_tiles(); - void load_sprites(); - SpriteTexture get(std::string name); - sf::Image load_image(std::string filename); - const uint32_t* get_surface(size_t num); - - // ZED: this is ugly so maybe you should like rewrite it or something - matrix::Matrix convert_char_to_texture(matrix::Matrix &from); -}; diff --git a/textures.cpp b/textures.cpp new file mode 100644 index 0000000..8eb8314 --- /dev/null +++ b/textures.cpp @@ -0,0 +1,92 @@ +#include "textures.hpp" +#include +#include "dbc.hpp" +#include +#include "config.hpp" +#include "constants.hpp" +#include + +namespace textures { + using std::shared_ptr, std::make_shared; + + static TextureManager TMGR; + static bool initialized = false; + + void load_sprites() { + Config assets("assets/config.json"); + + for(auto& el : assets["sprites"].items()) { + string path = el.value(); + auto texture = make_shared(path); + + texture->setSmooth(false); + auto sprite = make_shared(*texture); + + string name = el.key(); + TMGR.sprite_textures[name] = {sprite, texture}; + } + + TMGR.floor = load_image(assets["sprites"]["floor"]); + TMGR.ceiling = load_image(assets["sprites"]["ceiling"]); + } + + void load_tiles() { + Config assets("assets/tiles.json"); + auto &tiles = assets.json(); + + for(auto &el : tiles.items()) { + auto &config = el.value(); + TMGR.surfaces.emplace_back(load_image(config["texture"])); + + std::wstring display = assets.wstring(el.key(), "display"); + int surface_i = TMGR.surfaces.size() - 1; + wchar_t tid = display[0]; + + TMGR.char_to_texture[tid] = surface_i; + } + } + + void init() { + if(!initialized) { + load_tiles(); + load_sprites(); + initialized = true; + } + } + + SpriteTexture get(std::string name) { + dbc::check(TMGR.sprite_textures.contains(name), + fmt::format("!!!!! texture pack does not contain {} sprite", name)); + return TMGR.sprite_textures.at(name); + } + + sf::Image load_image(std::string filename) { + sf::Image texture; + bool good = texture.loadFromFile(filename); + dbc::check(good, fmt::format("failed to load {}", filename)); + return texture; + } + + const uint32_t* get_surface(size_t num) { + return (const uint32_t *)TMGR.surfaces[num].getPixelsPtr(); + } + + matrix::Matrix convert_char_to_texture(matrix::Matrix &tile_ids) { + auto result = matrix::make(matrix::width(tile_ids), matrix::height(tile_ids)); + + for(matrix::each_cell it(tile_ids); it.next();) { + wchar_t tid = tile_ids[it.y][it.x]; + result[it.y][it.x] = TMGR.char_to_texture.at(tid); + } + + return result; + } + + const uint32_t* get_floor() { + return (const uint32_t *)TMGR.floor.getPixelsPtr(); + } + + const uint32_t* get_ceiling() { + return (const uint32_t *)TMGR.ceiling.getPixelsPtr(); + } +}; diff --git a/textures.hpp b/textures.hpp new file mode 100644 index 0000000..7c2fdb0 --- /dev/null +++ b/textures.hpp @@ -0,0 +1,38 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include "matrix.hpp" + +namespace textures { + + struct SpriteTexture { + std::shared_ptr sprite = nullptr; + std::shared_ptr texture = nullptr; + }; + + struct TextureManager { + std::vector surfaces; + std::unordered_map sprite_textures; + std::unordered_map char_to_texture; + sf::Image floor; + sf::Image ceiling; + }; + + void init(); + + SpriteTexture get(std::string name); + + sf::Image load_image(std::string filename); + + const uint32_t* get_surface(size_t num); + + matrix::Matrix convert_char_to_texture(matrix::Matrix &from); + + const uint32_t* get_floor(); + + const uint32_t* get_ceiling(); +} diff --git a/textures2.cpp b/textures2.cpp deleted file mode 100644 index 3f07bb7..0000000 --- a/textures2.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "textures2.hpp" - -namespace textures { - static TextureManager textures; - static bool initialized = false; - - void init() { - if(!initialized) { - textures.load_tiles(); - textures.load_sprites(); - initialized = true; - } - } - - SpriteTexture get(std::string name) { - return textures.get(name); - } - - sf::Image load_image(std::string filename) { - return textures.load_image(filename); - } - - const uint32_t* get_surface(size_t num) { - return textures.get_surface(num); - } - - matrix::Matrix convert_char_to_texture(matrix::Matrix &from) { - return textures.convert_char_to_texture(from); - } - - const uint32_t* get_floor() { - return (const uint32_t *)textures.floor.getPixelsPtr(); - } - - const uint32_t* get_ceiling() { - return (const uint32_t *)textures.ceiling.getPixelsPtr(); - } -}; diff --git a/textures2.hpp b/textures2.hpp deleted file mode 100644 index d831c7d..0000000 --- a/textures2.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "texture.hpp" - -namespace textures { - void init(); - - SpriteTexture get(std::string name); - - sf::Image load_image(std::string filename); - - const uint32_t* get_surface(size_t num); - - matrix::Matrix convert_char_to_texture(matrix::Matrix &from); - - const uint32_t* get_floor(); - - const uint32_t* get_ceiling(); -}