Cleaned up the gui display some more for future GUI elements.

main
Zed A. Shaw 3 weeks ago
parent 2ced72a475
commit 7b5c84b5f7
  1. 31
      gui.cpp
  2. 5
      gui.hpp
  3. 41
      render.cpp
  4. 13
      render.hpp
  5. 2
      status.txt

@ -33,11 +33,11 @@ GUI::GUI(DinkyECS::World &world, Map& game_map) :
$game_map(game_map),
$log({{"Welcome to the game!"}}),
$view_port{0,0},
$screen(SCREEN_X, SCREEN_Y),
$status_screen(SCREEN_X, SCREEN_Y),
$map_screen(0,0),
$world(world),
$sounds("./assets"),
$renderer($canvas, $map_screen, $screen)
$renderer()
{
// this needs a config file soon
// $sounds.load("ambient", "ambient_sound.mp3");
@ -70,7 +70,12 @@ void GUI::create_renderer() {
return canvas($canvas);
});
$document = Renderer([&, player]{
$prompt = Renderer([&] {
return hbox({hflow(vbox(text("HELLO!")))}) | border;
});
$status_ui = Renderer([&, player]{
const auto& player_combat = $world.get<Combat>(player.entity);
const auto& inventory = $world.get<Inventory>(player.entity);
$status_text = player_combat.hp > 0 ? "NOT DEAD" : "DEAD!!!!!!";
@ -192,17 +197,29 @@ void GUI::shake() {
int x = Random::uniform<int>(-10,10);
int y = Random::uniform<int>(-10,10);
// add x/y back to draw screen
$renderer.draw_screen(true, x, y);
$renderer.draw_screen($map_screen, x, y);
std::this_thread::sleep_for(1ms);
}
}
void GUI::render_scene() {
$screen.Clear();
$renderer.clear();
$status_screen.Clear();
Render($status_screen, $status_ui->Render());
$renderer.draw_text_ui($status_screen, 0, 0);
$map_screen.Clear();
Render($map_screen, $map_view->Render());
Render($screen, $document->Render());
$renderer.draw_screen();
$renderer.draw_screen($map_screen);
/*
Screen prompt_screen(30,10);
Render(prompt_screen, $prompt->Render());
$renderer.draw_text_ui(prompt_screen, 700, 300);
*/
$renderer.display();
}
int GUI::main() {

@ -35,12 +35,13 @@ struct ActionLog {
class GUI {
string $status_text = "NOT DEAD";
Canvas $canvas;
Component $document;
Component $status_ui;
Component $prompt;
Component $map_view;
Map& $game_map;
ActionLog $log;
Point $view_port;
Screen $screen;
Screen $status_screen;
Screen $map_screen;
DinkyECS::World& $world;
SoundManager $sounds;

@ -28,13 +28,10 @@ sf::Color SFMLRender::color(Value val) {
return VALUES[size_t(val)];
}
SFMLRender::SFMLRender(Canvas &canvas, Screen &map_screen, Screen &screen) :
SFMLRender::SFMLRender() :
$window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"),
$map_font_size(0),
$line_spacing(0),
$canvas(canvas),
$map_screen(map_screen),
$screen(screen),
$default_fg(color(Value::LIGHT_MID)),
$default_bg(color(Value::BLACK)),
$ansi($default_fg, $default_bg)
@ -44,7 +41,9 @@ SFMLRender::SFMLRender(Canvas &canvas, Screen &map_screen, Screen &screen) :
$ui_text.setFont($font);
$ui_text.setPosition(0,0);
$ui_text.setCharacterSize(UI_FONT_SIZE);
$ui_text.setFillColor(color(Value::LIGHT_LIGHT));
$ui_text.setFillColor(color(Value::LIGHT_MID));
sf::Glyph glyph = $font.getGlyph(L'H', UI_FONT_SIZE, false);
$ui_bounds = glyph.bounds;
}
sf::Sprite &SFMLRender::get_text_sprite(wchar_t tile) {
@ -96,13 +95,6 @@ bool SFMLRender::resize_map(int new_size, Point &view_port) {
}
}
void SFMLRender::draw_main_ui() {
std::string screenout = $screen.ToString();
std::wstring main_screen_utf8 = $converter.from_bytes(screenout);
$ui_text.setString(main_screen_utf8);
$window.draw($ui_text);
}
inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds, sf::FloatRect bg_bounds, float &width_delta, float &height_delta) {
// should look into caching all this instead of calcing it each time
sp_bounds = sprite.getLocalBounds();
@ -149,11 +141,24 @@ void SFMLRender::render_text(std::string &text, float x, float y) {
});
}
void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) {
if(clear) $window.clear();
draw_main_ui();
void SFMLRender::draw_text_ui(Screen &screen, float x, float y) {
sf::RectangleShape backing(
sf::Vector2f($ui_bounds.width * screen.dimx() * 1.4,
$ui_bounds.height * screen.dimy() * 1.6));
backing.setFillColor(sf::Color(0, 0, 0));
backing.setPosition(x, y);
$window.draw(backing);
std::string screenout = screen.ToString();
std::wstring main_screen_utf8 = $converter.from_bytes(screenout);
$ui_text.setPosition(x, y);
$ui_text.setString(main_screen_utf8);
$window.draw($ui_text);
}
std::string map_screenout = $map_screen.ToString();
render_text(map_screenout, GAME_MAP_POS+map_off_x, map_off_y);
$window.display();
void SFMLRender::draw_screen(Screen &screen, float x, float y) {
std::string screenout = screen.ToString();
render_text(screenout, GAME_MAP_POS+x, y);
}

@ -38,16 +38,14 @@ struct SFMLRender {
sf::Glyph $base_glyph;
sf::Sprite $bg_sprite;
sf::FloatRect $bg_bounds;
Canvas& $canvas;
Screen& $map_screen;
Screen& $screen;
sf::Text $ui_text;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
sf::Color $default_fg;
sf::Color $default_bg;
ANSIParser $ansi;
sf::FloatRect $ui_bounds;
SFMLRender(Canvas &canvas, Screen &map_screen, Screen &screen);
SFMLRender();
// disable copy
SFMLRender(SFMLRender &other) = delete;
@ -57,8 +55,8 @@ struct SFMLRender {
sf::Sprite &get_text_sprite(wchar_t tile);
bool resize_map(int new_size, Point &view_port);
void render_text(std::string &text, float x, float y);
void draw_main_ui();
void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f);
void draw_text_ui(Screen &screen, float x, float y);
void draw_screen(Screen &screen, float map_off_x=0.0f, float map_off_y=0.0f);
bool poll_event(sf::Event &event) {
return $window.pollEvent(event);
@ -69,5 +67,6 @@ struct SFMLRender {
bool is_open() { return $window.isOpen(); }
int font_size() { return $map_font_size; }
void clear() { $window.clear(); }
void display() { $window.display(); }
};

@ -16,3 +16,5 @@ TODO:
* Lua integration?
* check out SoLoud.
* BUG: When enemies die they can be overlapping another enemy.

Loading…
Cancel
Save