diff --git a/Makefile b/Makefile index a1533ce..d4a9c62 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ tracy_build: meson compile -j 10 -C builddir test: build - ./builddir/runtests + ./builddir/runtests "[textures]" run: build test powershell "cp ./builddir/zedcaster.exe ." diff --git a/combat_ui.cpp b/combat_ui.cpp index de9be61..eb43103 100644 --- a/combat_ui.cpp +++ b/combat_ui.cpp @@ -15,7 +15,7 @@ namespace gui { "[ >.%(100,50)label_hp | *%.(198,50)bar_hp | _ ]"); } - void CombatUI::render(TexturePack& textures) { + void CombatUI::render() { auto& world = $gui.world(); world.set_the({$gui.$parser}); @@ -39,7 +39,7 @@ namespace gui { } } - $gui.init(textures); + $gui.init(); } void CombatUI::draw(sf::RenderWindow& window) { diff --git a/combat_ui.hpp b/combat_ui.hpp index 5d0ac47..e8d3cd4 100644 --- a/combat_ui.hpp +++ b/combat_ui.hpp @@ -13,7 +13,7 @@ namespace gui { CombatUI(GameLevel level); - void render(TexturePack& texture); + void render(); void draw(sf::RenderWindow& window); void update_level(GameLevel &level) { $level = level; } void set_damage(float percent); diff --git a/guecs.cpp b/guecs.cpp index e940391..b80736d 100644 --- a/guecs.cpp +++ b/guecs.cpp @@ -28,7 +28,7 @@ namespace guecs { } } - void UI::init(TexturePack& textures) { + void UI::init() { if($world.has_the()) { auto& bg = $world.get_the(); bg.init(); @@ -59,7 +59,7 @@ namespace guecs { }); $world.query([&](auto, auto &cell, auto &sprite) { - sprite.init(cell, textures); + sprite.init(cell); }); } diff --git a/guecs.hpp b/guecs.hpp index 651963c..47547e1 100644 --- a/guecs.hpp +++ b/guecs.hpp @@ -5,7 +5,7 @@ #include #include #include -#include "texture.hpp" +#include "textures2.hpp" #include #include "events.hpp" #include "constants.hpp" @@ -57,8 +57,8 @@ namespace guecs { std::shared_ptr sprite = nullptr; std::shared_ptr texture = nullptr; - void init(lel::Cell &cell, TexturePack &textures) { - auto sprite_texture = textures.get(name); + void init(lel::Cell &cell) { + auto sprite_texture = textures::get(name); texture = sprite_texture.texture; sprite = make_shared(*texture); sprite->setPosition({ @@ -145,7 +145,7 @@ namespace guecs { return $world; } - void init(TexturePack& textures); + void init(); void render(sf::RenderWindow& window); void mouse(float x, float y); diff --git a/gui_fsm.cpp b/gui_fsm.cpp index 91f7e56..d85d9bb 100644 --- a/gui_fsm.cpp +++ b/gui_fsm.cpp @@ -13,7 +13,7 @@ namespace gui { FSM::FSM() : $window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Raycaster Thing"), - $main_ui($window, $levels.current(), $textures), + $main_ui($window, $levels.current()), $renderer($window), $level($levels.current()), $map_ui($level), @@ -21,8 +21,6 @@ namespace gui { $status_ui($level), $font{FONT_FILE_NAME} { - $textures.load_tiles(); - $textures.load_sprites(); } void FSM::event(Event ev) { @@ -45,8 +43,8 @@ namespace gui { $main_ui.init(); - $combat_ui.render($textures); - $status_ui.render($textures); + $combat_ui.render(); + $status_ui.render(); $status_ui.log("Welcome to the game!"); $renderer.init_terminal(); diff --git a/gui_fsm.hpp b/gui_fsm.hpp index 051d2bf..92229e0 100644 --- a/gui_fsm.hpp +++ b/gui_fsm.hpp @@ -52,7 +52,6 @@ namespace gui { CombatUI $combat_ui; StatusUI $status_ui; sf::Font $font; - TexturePack $textures; FSM(); diff --git a/main.cpp b/main.cpp index f36019c..dbbbdbc 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,8 @@ #include "gui_fsm.hpp" +#include "textures2.hpp" int main() { + textures::init(); gui::FSM main; main.event(gui::Event::STARTED); diff --git a/main_ui.cpp b/main_ui.cpp index 0a2078f..612a551 100644 --- a/main_ui.cpp +++ b/main_ui.cpp @@ -4,12 +4,11 @@ namespace gui { using namespace components; - MainUI::MainUI(sf::RenderWindow& window, GameLevel level, TexturePack& textures) : + MainUI::MainUI(sf::RenderWindow& window, GameLevel level) : $window(window), $level(level), - $textures(textures), - $overlay_ui($level, $textures), - $rayview($textures, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT) + $overlay_ui($level), + $rayview(RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT) { $window.setVerticalSyncEnabled(VSYNC); $window.setFramerateLimit(FRAME_LIMIT); diff --git a/main_ui.hpp b/main_ui.hpp index 164ec33..a571dc1 100644 --- a/main_ui.hpp +++ b/main_ui.hpp @@ -15,12 +15,11 @@ namespace gui { Stats $stats; sf::RenderWindow& $window; GameLevel $level; - TexturePack& $textures; OverlayUI $overlay_ui; Raycaster $rayview; CameraLOL $camera; - MainUI(sf::RenderWindow& window, GameLevel level, TexturePack &textures); + MainUI(sf::RenderWindow& window, GameLevel level); void mouse(int x, int y); void debug(); diff --git a/meson.build b/meson.build index 2e7d592..9016c6c 100644 --- a/meson.build +++ b/meson.build @@ -85,6 +85,7 @@ sources = [ 'status_ui.cpp', 'systems.cpp', 'texture.cpp', + 'textures2.cpp', 'tilemap.cpp', 'worldbuilder.cpp', ] @@ -105,6 +106,7 @@ executable('runtests', sources + [ 'tests/pathing.cpp', 'tests/spatialmap.cpp', 'tests/tilemap.cpp', + 'tests/textures.cpp', ], override_options: exe_defaults, dependencies: dependencies + [catch2]) diff --git a/overlay_ui.cpp b/overlay_ui.cpp index 84ef1df..4ccd1d4 100644 --- a/overlay_ui.cpp +++ b/overlay_ui.cpp @@ -8,9 +8,8 @@ namespace gui { using namespace guecs; using std::string; - OverlayUI::OverlayUI(GameLevel level, TexturePack& textures) : - $level(level), - $textures(textures) + OverlayUI::OverlayUI(GameLevel level) : + $level(level) { $gui.position(RAY_VIEW_X, RAY_VIEW_Y, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT); $gui.layout( @@ -28,7 +27,7 @@ namespace gui { auto region = $gui.entity(name); world.set(region, cell); } - $gui.init($textures); + $gui.init(); } void OverlayUI::draw(sf::RenderWindow& window) { @@ -39,7 +38,7 @@ namespace gui { auto ent = $gui.entity(region); Sprite blood{sprite_name}; auto& cell = $gui.cell_for(ent); - blood.init(cell, $textures); + blood.init(cell); $gui.set(ent, blood); } diff --git a/overlay_ui.hpp b/overlay_ui.hpp index 1d92e0f..03f0fd1 100644 --- a/overlay_ui.hpp +++ b/overlay_ui.hpp @@ -9,9 +9,8 @@ namespace gui { public: guecs::UI $gui; GameLevel $level; - TexturePack& $textures; - OverlayUI(GameLevel level, TexturePack& $textures); + OverlayUI(GameLevel level); void render(); void draw(sf::RenderWindow& window); diff --git a/raycaster.cpp b/raycaster.cpp index 1619835..53147c0 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -8,6 +8,7 @@ #include #include #include "components.hpp" +#include "textures2.hpp" using namespace fmt; using std::make_unique; @@ -33,8 +34,7 @@ inline uint32_t new_lighting(uint32_t pixel, int level) { return conv.as_int; } -Raycaster::Raycaster(TexturePack &textures, int width, int height) : - $textures(textures), +Raycaster::Raycaster(int width, int height) : $view_texture(sf::Vector2u{(unsigned int)width, (unsigned int)height}), $view_sprite($view_texture), $width(width), $height(height), @@ -44,6 +44,8 @@ Raycaster::Raycaster(TexturePack &textures, int width, int height) : $view_sprite.setPosition({0, 0}); $pixels = make_unique($width * $height); $view_texture.setSmooth(false); + $floor_texture = textures::get_floor(); + $ceiling_texture = textures::get_ceiling(); } void Raycaster::set_position(int x, int y) { @@ -226,7 +228,7 @@ void Raycaster::cast_rays() { int draw_end = line_height / 2 + $height / 2 + $pitch; if(draw_end >= $height) draw_end = $height - 1; - auto texture = $textures.get_surface($map[map_y][map_x] - 1); + auto texture = textures::get_surface($map[map_y][map_x] - 1); // calculate value of wall_x double wall_x; // where exactly the wall was hit @@ -265,9 +267,6 @@ void Raycaster::cast_rays() { void Raycaster::draw_ceiling_floor() { constexpr static const int texture_width = TEXTURE_WIDTH; constexpr static const int texture_height = TEXTURE_HEIGHT; - auto floor_texture = (const uint32_t *)$textures.floor.getPixelsPtr(); - auto ceiling_texture = (const uint32_t *)$textures.ceiling.getPixelsPtr(); - auto &lights = $level.lights->lighting(); for(int y = $height / 2 + 1; y < $height; ++y) { @@ -324,11 +323,11 @@ void Raycaster::draw_ceiling_floor() { float light_level = matrix::inbounds(lights, map_x, map_y) ? lights[map_y][map_x] : 30; // FLOOR - color = floor_texture[texture_width * ty + tx]; + color = $floor_texture[texture_width * ty + tx]; $pixels[pixcoord(x, y)] = new_lighting(color, light_level); // CEILING - color = ceiling_texture[texture_width * ty + tx]; + color = $ceiling_texture[texture_width * ty + tx]; $pixels[pixcoord(x, $height - y - 1)] = new_lighting(color, light_level); } } @@ -344,7 +343,7 @@ void Raycaster::draw(sf::RenderTarget& target) { void Raycaster::update_sprite(DinkyECS::Entity ent, components::Sprite& sprite) { fmt::println("entity UPDATE SPRITE {} will have sprite named {}", ent, sprite.name); - auto sprite_txt = $textures.get(sprite.name); + auto sprite_txt = textures::get(sprite.name); $sprites.insert_or_assign(ent, sprite_txt); } @@ -353,13 +352,13 @@ void Raycaster::set_level(GameLevel level) { auto& tiles = $level.map->tiles(); auto world = $level.world; auto& player = world->get_the(); - $map = $textures.convert_char_to_texture(tiles.$tile_ids); + $map = textures::convert_char_to_texture(tiles.$tile_ids); world->query([&](const auto ent, auto& sprite) { // player doesn't need a sprite if(player.entity == ent) return; fmt::println("entity {} will have sprite named {}", ent, sprite.name); - auto sprite_txt = $textures.get(sprite.name); + auto sprite_txt = textures::get(sprite.name); $sprites.try_emplace(ent, sprite_txt); }); } diff --git a/raycaster.hpp b/raycaster.hpp index 3442e36..e2c6bac 100644 --- a/raycaster.hpp +++ b/raycaster.hpp @@ -2,10 +2,10 @@ #include #include -#include "texture.hpp" #include "animator.hpp" #include "spatialmap.hpp" #include "levelmanager.hpp" +#include "texture.hpp" using matrix::Matrix; using RGBA = uint32_t; @@ -14,7 +14,6 @@ struct Raycaster { int $pitch=0; sf::Clock $clock; sf::Shader $brightness; - TexturePack &$textures; double $pos_x = 0; double $pos_y = 0; @@ -27,6 +26,8 @@ struct Raycaster { double $plane_y = 0.66; sf::Texture $view_texture; sf::Sprite $view_sprite; + const uint32_t *$floor_texture = nullptr; + const uint32_t *$ceiling_texture = nullptr; std::unique_ptr $pixels = nullptr; @@ -40,7 +41,7 @@ struct Raycaster { std::vector $zbuffer; // width Animator $anim; - Raycaster(TexturePack &textures, int width, int height); + Raycaster(int width, int height); void cast_rays(); void draw_ceiling_floor(); diff --git a/status_ui.cpp b/status_ui.cpp index 0f1d1b0..556de47 100644 --- a/status_ui.cpp +++ b/status_ui.cpp @@ -12,16 +12,16 @@ namespace gui { { $gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT); $gui.layout( - "[button1 | button2 | button3]" - "[button4 | button5 | button6]" - "[button7 | button8 | button9]" "[*%(100,300)log_view]" "[_]" "[_]" + "[button1 | button2 | button3]" + "[button4 | button5 | button6]" + "[button7 | button8 | button9]" ); } - void StatusUI::render(TexturePack &textures) { + void StatusUI::render() { auto& world = $gui.world(); std::vector fake_items{ @@ -48,7 +48,7 @@ namespace gui { } } - $gui.init(textures); + $gui.init(); } void StatusUI::draw(sf::RenderWindow &window) { diff --git a/status_ui.hpp b/status_ui.hpp index e2c35f0..fc494a5 100644 --- a/status_ui.hpp +++ b/status_ui.hpp @@ -2,7 +2,7 @@ #include "levelmanager.hpp" #include "constants.hpp" #include -#include "texture.hpp" +#include "textures2.hpp" #include "guecs.hpp" namespace gui { @@ -15,7 +15,7 @@ namespace gui { StatusUI(GameLevel level); void update_level(GameLevel &level) { $level = level; } void log(std::string msg); - void render(TexturePack &textures); + void render(); void draw(sf::RenderWindow &window); }; } diff --git a/tests/guecs.cpp b/tests/guecs.cpp index c01c135..d9914a1 100644 --- a/tests/guecs.cpp +++ b/tests/guecs.cpp @@ -8,9 +8,7 @@ using namespace guecs; TEST_CASE("prototype one gui", "[ecs-gui]") { guecs::UI gui; - - TexturePack textures; - textures.load_sprites(); + textures::init(); gui.position(0, 0, 1000, 500); gui.layout("[test1|test2|test3][test4|_|test5]"); @@ -24,7 +22,7 @@ TEST_CASE("prototype one gui", "[ecs-gui]") { world.set(button, {name}); } - gui.init(textures); + gui.init(); // at this point it's mostly ready but I'd need to render it to a window real quick sf::RenderWindow window; diff --git a/tests/textures.cpp b/tests/textures.cpp new file mode 100644 index 0000000..c12c66e --- /dev/null +++ b/tests/textures.cpp @@ -0,0 +1,17 @@ +#include +#include +#include +#include "textures2.hpp" + +using namespace fmt; + +TEST_CASE("test texture management", "[textures]") { + + textures::init(); + auto spider = textures::get("hairy_spider"); + + auto image = textures::load_image("assets/hairy_spider-256.png"); + + auto img_ptr = textures::get_surface(0); + REQUIRE(img_ptr != nullptr); +} diff --git a/texture.cpp b/texture.cpp index 44e7ed8..cd8c7f9 100644 --- a/texture.cpp +++ b/texture.cpp @@ -7,14 +7,14 @@ using std::shared_ptr, std::make_shared; -sf::Image TexturePack::load_image(std::string filename) { +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 TexturePack::load_sprites() { +void TextureManager::load_sprites() { Config assets("assets/config.json"); for(auto& el : assets["sprites"].items()) { @@ -28,13 +28,11 @@ void TexturePack::load_sprites() { sprite_textures[name] = {sprite, texture}; } - sword = sprite_textures["sword"]; - floor = load_image(assets["sprites"]["floor"]); ceiling = load_image(assets["sprites"]["ceiling"]); } -void TexturePack::load_tiles() { +void TextureManager::load_tiles() { Config assets("assets/tiles.json"); auto &tiles = assets.json(); @@ -50,11 +48,11 @@ void TexturePack::load_tiles() { } } -const uint32_t* TexturePack::get_surface(size_t num) { +const uint32_t* TextureManager::get_surface(size_t num) { return (const uint32_t *)surfaces[num].getPixelsPtr(); } -matrix::Matrix TexturePack::convert_char_to_texture(matrix::Matrix &tile_ids) { +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();) { @@ -65,7 +63,7 @@ matrix::Matrix TexturePack::convert_char_to_texture(matrix::Matrix &tile_ids) { return result; } -SpriteTexture TexturePack::get(std::string name) { +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 index 87ad670..6703ba9 100644 --- a/texture.hpp +++ b/texture.hpp @@ -13,13 +13,12 @@ struct SpriteTexture { std::shared_ptr texture = nullptr; }; -struct TexturePack { +struct TextureManager { std::vector surfaces; std::unordered_map sprite_textures; std::unordered_map char_to_texture; sf::Image floor; sf::Image ceiling; - SpriteTexture sword; void load_tiles(); void load_sprites(); diff --git a/textures2.cpp b/textures2.cpp new file mode 100644 index 0000000..3f07bb7 --- /dev/null +++ b/textures2.cpp @@ -0,0 +1,38 @@ +#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 new file mode 100644 index 0000000..d831c7d --- /dev/null +++ b/textures2.hpp @@ -0,0 +1,18 @@ +#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(); +}