Now able to render the map at a different size from the rest of the UI and also only shake the map.

main
Zed A. Shaw 2 days ago
parent 77945be4d7
commit b8a0d9bbd1
  1. 63
      gui.cpp
  2. 18
      gui.hpp
  3. 2
      status.txt

@ -48,16 +48,25 @@ sf::Color GUI::color(Value val) {
GUI::GUI() : $game_map(GAME_MAP_X, GAME_MAP_Y), GUI::GUI() : $game_map(GAME_MAP_X, GAME_MAP_Y),
$canvas(GAME_MAP_X * 2, GAME_MAP_Y * 4), $canvas(GAME_MAP_X * 2, GAME_MAP_Y * 4),
$window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"), $window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"),
$screen(SCREEN_X, SCREEN_Y) $screen(SCREEN_X, SCREEN_Y),
$map_screen(GAME_MAP_X, GAME_MAP_Y)
{ {
int res = $hit_buf.loadFromFile("./assets/hit.wav"); int res = $hit_buf.loadFromFile("./assets/hit.wav");
dbc::check(res, "failed to load hit.wav"); dbc::check(res, "failed to load hit.wav");
$hit_sound.setBuffer($hit_buf); $hit_sound.setBuffer($hit_buf);
$font.loadFromFile("./assets/text.otf"); $font.loadFromFile("./assets/text.otf");
$text.setFont($font);
$text.setCharacterSize(30); $ui_text.setFont($font);
$text.setFillColor(color(Value::LIGHT_DARK)); $ui_text.setPosition(0,0);
$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::LIGHT_DARK));
$game_map.generate(); $game_map.generate();
$player.location = $game_map.place_entity(0); $player.location = $game_map.place_entity(0);
$enemy.location = $game_map.place_entity(1); $enemy.location = $game_map.place_entity(1);
@ -77,20 +86,20 @@ void GUI::create_renderer() {
for(size_t x = 0; x < walls[0].size(); ++x) { for(size_t x = 0; x < walls[0].size(); ++x) {
for(size_t y = 0; y < walls.size(); ++y) { for(size_t y = 0; y < walls.size(); ++y) {
string tile = walls[y][x] == 1 ? "#" : format("{}", paths[y][x]); string tile = walls[y][x] == 1 ? WALL_TILE : format("{}", paths[y][x]);
if(tile == "#") { if(tile == WALL_TILE) {
$canvas.DrawText(x*2, y*4, tile); $canvas.DrawText(x*2, y*4, tile);
} else if($show_paths) { } else if($show_paths) {
//int pnum = paths[y][x]; //int pnum = paths[y][x];
$canvas.DrawText(x*2, y*4, tile); $canvas.DrawText(x*2, y*4, tile);
} else { } else {
$canvas.DrawText(x*2, y*4, "."); $canvas.DrawText(x*2, y*4, FLOOR_TILE);
} }
} }
} }
$canvas.DrawText($enemy.location.x*2, $enemy.location.y*4, "!"); $canvas.DrawText($enemy.location.x*2, $enemy.location.y*4, ENEMY_TILE);
$canvas.DrawText($player.location.x*2, $player.location.y*4, "@"); $canvas.DrawText($player.location.x*2, $player.location.y*4, PLAYER_TILE);
$canvas.DrawText($goal.x*2, $goal.y*4, "$"); $canvas.DrawText($goal.x*2, $goal.y*4, "$");
return canvas($canvas); return canvas($canvas);
@ -105,7 +114,7 @@ void GUI::create_renderer() {
) | xflex_grow ) | xflex_grow
), ),
separator(), separator(),
hbox($map_view->Render()), hbox(),
}); });
}); });
} }
@ -158,39 +167,45 @@ void GUI::handle_events() {
void GUI::burn() { void GUI::burn() {
for(int i = 0; i < 20; ++i) { for(int i = 0; i < 20; ++i) {
$text.setFillColor(color(i % VALUES.size())); $map_text.setFillColor(color(i % VALUES.size()));
int x = Random::rand_int(-10,10); int x = Random::rand_int(-10,10);
int y = Random::rand_int(-10,10); int y = Random::rand_int(-10,10);
$text.setPosition({(float)x,(float)y}); draw_screen(false, x, y);
$window.draw($text);
$window.display();
std::this_thread::sleep_for(2ms); std::this_thread::sleep_for(2ms);
} }
$text.setFillColor(color(Value::LIGHT_DARK)); $map_text.setFillColor(color(Value::LIGHT_DARK));
} }
inline void draw_screen(sf::RenderWindow &window, sf::Text &text, float x=0, float y=0) { void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) {
text.setPosition({x,y}); if(clear) $window.clear();
window.clear(); $window.draw($ui_text);
window.draw(text); $map_text.setPosition(GAME_MAP_POS+map_off_x, map_off_y);
window.display(); $window.draw($map_text);
$window.display();
} }
void GUI::shake() { void GUI::shake() {
for(int i = 0; i < 10; ++i) { for(int i = 0; i < 10; ++i) {
int x = Random::rand_int(-10,10); int x = Random::rand_int(-10,10);
int y = Random::rand_int(-10,10); int y = Random::rand_int(-10,10);
draw_screen($window, $text, (float)x, (float)y); // add x/y back to draw screen
draw_screen(true, x, y);
std::this_thread::sleep_for(1ms); std::this_thread::sleep_for(1ms);
} }
} }
void GUI::render_scene() { void GUI::render_scene() {
Render($map_screen, $map_view->Render());
Render($screen, $document->Render()); Render($screen, $document->Render());
std::string $screenout = $screen.ToString(); std::string $screenout = $screen.ToString();
std::wstring utf8 = $converter.from_bytes($screenout); std::wstring main_screen_utf8 = $converter.from_bytes($screenout);
$text.setString(utf8); $ui_text.setString(main_screen_utf8);
std::string $map_screenout = $map_screen.ToString();
std::wstring map_screen_utf8 = $converter.from_bytes($map_screenout);
$map_text.setString(map_screen_utf8);
if($shake_it) { if($shake_it) {
shake(); shake();
@ -202,7 +217,7 @@ void GUI::render_scene() {
$burn_baby_burn = false; $burn_baby_burn = false;
} }
draw_screen($window, $text, 0, 0); draw_screen();
} }
int GUI::main() { int GUI::main() {

@ -16,12 +16,19 @@
using std::string; using std::string;
using ftxui::Canvas, ftxui::Component, ftxui::Screen; using ftxui::Canvas, ftxui::Component, ftxui::Screen;
constexpr int GAME_MAP_X = 60; constexpr int GAME_MAP_X = 30;
constexpr int GAME_MAP_Y = 30; constexpr int GAME_MAP_Y = 15;
constexpr int SCREEN_X = 106; constexpr int GAME_MAP_POS = 600;
constexpr int SCREEN_X = 40;
constexpr int SCREEN_Y = 30; constexpr int SCREEN_Y = 30;
constexpr int VIDEO_X = 1600; constexpr int VIDEO_X = 1600;
constexpr int VIDEO_Y = 900; constexpr int VIDEO_Y = 900;
constexpr int MAP_FONT_SIZE=60;
constexpr int UI_FONT_SIZE=30;
#define WALL_TILE "█"
#define FLOOR_TILE "·"
#define PLAYER_TILE "☺"
#define ENEMY_TILE "Ω"
enum class Value { enum class Value {
BLACK=0, DARK_DARK, DARK_MID, BLACK=0, DARK_DARK, DARK_MID,
@ -42,12 +49,14 @@ class GUI {
Component $map_view; Component $map_view;
Canvas $canvas; Canvas $canvas;
sf::Font $font; sf::Font $font;
sf::Text $text; sf::Text $ui_text;
sf::Text $map_text;
bool $shake_it = false; bool $shake_it = false;
bool $burn_baby_burn = false; bool $burn_baby_burn = false;
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;
public: public:
GUI(); GUI();
@ -59,6 +68,7 @@ public:
void create_renderer(); void create_renderer();
void render_scene(); void render_scene();
void handle_events(); void handle_events();
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 burn();

@ -4,4 +4,6 @@ TODO:
* Dynamically determine the font vs. screensize to get an exact FTXUI screen size. * Dynamically determine the font vs. screensize to get an exact FTXUI screen size.
* Write a test that generates a ton of maps then confirms there's a path from one room to every other room? * Write a test that generates a ton of maps then confirms there's a path from one room to every other room?
* If the player is trapped in a room the enemy just travles through walls. * If the player is trapped in a room the enemy just travles through walls.
* Add FLECS.
* Render a different screen for the map to use a different font size.
* Lua integration? * Lua integration?

Loading…
Cancel
Save