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.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();
}
@ -135,18 +130,6 @@ void GUI::run_systems() {
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) {
if(clear) $window.clear();
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);
std::wstring map_screen_utf8 = $converter.from_bytes(map_screenout);
$map_text.setString(map_screen_utf8);
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++) {
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);
auto pos = $map_text.findCharacterPos(i);
sprite.setPosition(pos);
sprite.setPosition({x, y});
if(tile == L'') {
sprite.setColor(sf::Color(100,100,100));
} 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);
} else if(tile == L'·') {
sprite.setColor(color(Value::DARK_MID));
} else if(tile == L'\n' || tile == L'\r') {
// skip newlines
} else if(tile == L'\r') {
continue; // skip these, just windows junk
} else if(tile == L'\n') {
// newline
y += line_spacing;
x = GAME_MAP_POS;
continue;
} else {
sprite.setColor(color(Value::MID));
}
$window.draw(sprite);
x += glyph.advance;
}
$window.display();

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

Loading…
Cancel
Save