From 9f1e9717a039b36c9e85f086f37d90197c3bef53 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 18 Oct 2024 21:59:06 -0400 Subject: [PATCH] Super jank prototype of my idea but I am rendering multiple characters per cell to give the effect of a more complex character. Now to make this a data setup so it's easy to make characters. Also, probably have little add ons to show things like weapons equiped, etc. --- gui.cpp | 45 ++++++++++++++++++++++++++++++++++++--------- gui.hpp | 3 ++- map.hpp | 2 +- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/gui.cpp b/gui.cpp index c9d8e1b..d7b341f 100644 --- a/gui.cpp +++ b/gui.cpp @@ -123,6 +123,22 @@ bool GUI::handle_events() { return event_happened; } + +sf::Sprite &GUI::get_text_sprite(wchar_t tile) { + if(!$sprites.contains(tile)) { + sf::Glyph glyph = $font.getGlyph(tile, MAP_FONT_SIZE, false); + // WARNING! we actually have to do this here because SFML caches + // the glyphs on the font texture, so this gets loaded each time + // we get a new glyph from the font. + $font_texture = $font.getTexture(MAP_FONT_SIZE); + sf::Sprite sprite($font_texture); + sprite.setTextureRect(glyph.textureRect); + $sprites[tile] = sprite; + } + + return $sprites[tile]; +} + void GUI::run_systems() { auto player = $world.get(); System::enemy_pathing($world, $game_map, player); @@ -133,33 +149,39 @@ void GUI::run_systems() { void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) { if(clear) $window.clear(); std::string screenout = $screen.ToString(); - std::string map_screenout = $map_screen.ToString(); std::wstring main_screen_utf8 = $converter.from_bytes(screenout); $ui_text.setString(main_screen_utf8); $window.draw($ui_text); + std::string map_screenout = $map_screen.ToString(); std::wstring map_screen_utf8 = $converter.from_bytes(map_screenout); - sf::Texture ftext = $font.getTexture(MAP_FONT_SIZE); float y = 0.0f; float x = GAME_MAP_POS; const float line_spacing = $font.getLineSpacing(MAP_FONT_SIZE); + sf::Glyph base_glyph = $font.getGlyph(L'█', MAP_FONT_SIZE, false); + auto bg_sprite = get_text_sprite(L'█'); + bg_sprite.setColor(sf::Color(20,20,20)); + auto add_sprite = get_text_sprite(L'!'); + bool has_add = false; for(size_t i = 0; i < map_screen_utf8.size(); i++) { wchar_t tile = map_screen_utf8[i]; - sf::Glyph glyph = $font.getGlyph(tile, MAP_FONT_SIZE, false); - sf::Sprite sprite(ftext); - sprite.setTextureRect(glyph.textureRect); + sf::Sprite &sprite = get_text_sprite(tile); sprite.setPosition({x, y}); + bg_sprite.setPosition({x, y}); if(tile == L'█') { - sprite.setColor(sf::Color(100,100,100)); + sprite.setColor(sf::Color(80,80,80)); } else if(tile == L'☺') { sprite.setColor(sf::Color::Blue); } else if(tile == L'Ω') { sprite.setColor(sf::Color::Red); - } else if(tile == L'·') { - sprite.setColor(color(Value::DARK_MID)); + add_sprite.setColor(sf::Color::Red); + add_sprite.setPosition({x-3,y-3}); + has_add = true; + } else if(tile == L'#') { + sprite.setColor(sf::Color(5,5,5)); } else if(tile == L'\r') { continue; // skip these, just windows junk } else if(tile == L'\n') { @@ -171,8 +193,13 @@ void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) { sprite.setColor(color(Value::MID)); } + $window.draw(bg_sprite); $window.draw(sprite); - x += glyph.advance; + if(has_add) { + $window.draw(add_sprite); + has_add = false; + } + x += base_glyph.advance; } $window.display(); diff --git a/gui.hpp b/gui.hpp index b5d5143..6130543 100644 --- a/gui.hpp +++ b/gui.hpp @@ -50,6 +50,7 @@ class GUI { Screen $screen; Screen $map_screen; DinkyECS::World $world; + sf::Texture $font_texture; std::unordered_map $sprites; public: @@ -66,7 +67,7 @@ public: void shake(); void configure_world(); void run_systems(); - sf::Sprite get_text_sprite(wchar_t tile); + sf::Sprite &get_text_sprite(wchar_t tile); int main(); }; diff --git a/map.hpp b/map.hpp index 63c3b00..0c9fbdc 100644 --- a/map.hpp +++ b/map.hpp @@ -11,7 +11,7 @@ #define WALL_VALUE 1 #define SPACE_VALUE 0 #define WALL_TILE "█" -#define FLOOR_TILE "·" +#define FLOOR_TILE "#" #define PLAYER_TILE "☺" #define ENEMY_TILE "Ω"