Cleanup before trying to make the tile rendering faster by pre-loading the sprites needed, or caching as they're requested.

main
Zed A. Shaw 1 month ago
parent 08d71f9bdc
commit 31c86fa2b3
  1. 36
      gui.cpp
  2. 5
      gui.hpp

@ -67,11 +67,6 @@ GUI::GUI() : $game_map(GAME_MAP_X, GAME_MAP_Y),
$ui_text.setCharacterSize(UI_FONT_SIZE); $ui_text.setCharacterSize(UI_FONT_SIZE);
$ui_text.setFillColor(color(Value::LIGHT_LIGHT)); $ui_text.setFillColor(color(Value::LIGHT_LIGHT));
$map_text.setFont($font);
$map_text.setPosition(GAME_MAP_POS,0);
$map_text.setCharacterSize(MAP_FONT_SIZE);
$map_text.setFillColor(color(Value::MID));
$game_map.generate(); $game_map.generate();
} }
@ -135,18 +130,6 @@ void GUI::run_systems() {
System::combat($world, player); System::combat($world, player);
} }
void GUI::burn() {
for(int i = 0; i < 20; ++i) {
$map_text.setFillColor(color(i % VALUES.size()));
int x = Random::uniform<int>(-10,10);
int y = Random::uniform<int>(-10,10);
draw_screen(false, x, y);
std::this_thread::sleep_for(2ms);
}
$map_text.setFillColor(color(Value::MID));
}
void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) { void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) {
if(clear) $window.clear(); if(clear) $window.clear();
std::string screenout = $screen.ToString(); std::string screenout = $screen.ToString();
@ -156,17 +139,19 @@ void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) {
$window.draw($ui_text); $window.draw($ui_text);
std::wstring map_screen_utf8 = $converter.from_bytes(map_screenout); std::wstring map_screen_utf8 = $converter.from_bytes(map_screenout);
$map_text.setString(map_screen_utf8);
sf::Texture ftext = $font.getTexture(MAP_FONT_SIZE); 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);
for(size_t i = 0; i < map_screen_utf8.size(); i++) { for(size_t i = 0; i < map_screen_utf8.size(); i++) {
wchar_t tile = map_screen_utf8[i]; wchar_t tile = map_screen_utf8[i];
sf::Glyph glyph = $font.getGlyph(tile, MAP_FONT_SIZE, false); sf::Glyph glyph = $font.getGlyph(tile, MAP_FONT_SIZE, false);
sf::Sprite sprite(ftext); sf::Sprite sprite(ftext);
sprite.setTextureRect(glyph.textureRect); sprite.setTextureRect(glyph.textureRect);
auto pos = $map_text.findCharacterPos(i); sprite.setPosition({x, y});
sprite.setPosition(pos);
if(tile == L'') { if(tile == L'') {
sprite.setColor(sf::Color(100,100,100)); sprite.setColor(sf::Color(100,100,100));
} else if(tile == L'') { } else if(tile == L'') {
@ -175,14 +160,19 @@ void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) {
sprite.setColor(sf::Color::Red); sprite.setColor(sf::Color::Red);
} else if(tile == L'·') { } else if(tile == L'·') {
sprite.setColor(color(Value::DARK_MID)); sprite.setColor(color(Value::DARK_MID));
} else if(tile == L'\n' || tile == L'\r') { } else if(tile == L'\r') {
// skip newlines continue; // skip these, just windows junk
} else if(tile == L'\n') {
// newline
y += line_spacing;
x = GAME_MAP_POS;
continue; continue;
} else { } else {
sprite.setColor(color(Value::MID)); sprite.setColor(color(Value::MID));
} }
$window.draw(sprite); $window.draw(sprite);
x += glyph.advance;
} }
$window.display(); $window.display();

@ -4,6 +4,7 @@
#include <SFML/Graphics/Font.hpp> #include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Text.hpp> #include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <codecvt> #include <codecvt>
#include <ftxui/component/component.hpp> #include <ftxui/component/component.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
@ -44,12 +45,12 @@ class GUI {
Canvas $canvas; Canvas $canvas;
sf::Font $font; sf::Font $font;
sf::Text $ui_text; sf::Text $ui_text;
sf::Text $map_text;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter; std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
sf::RenderWindow $window; sf::RenderWindow $window;
Screen $screen; Screen $screen;
Screen $map_screen; Screen $map_screen;
DinkyECS::World $world; DinkyECS::World $world;
std::unordered_map<wchar_t, sf::Sprite> $sprites;
public: public:
GUI(); GUI();
@ -63,9 +64,9 @@ public:
bool handle_events(); bool handle_events();
void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f); void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f);
void shake(); void shake();
void burn();
void configure_world(); void configure_world();
void run_systems(); void run_systems();
sf::Sprite get_text_sprite(wchar_t tile);
int main(); int main();
}; };

Loading…
Cancel
Save