Testing is almost complete, but now I'm in the area where it's a lot of interaction and probably need mocks or a way to inject keys to the gui.

main
Zed A. Shaw 2 days ago
parent 2576b16869
commit 48df9248b2
  1. 3
      gui.cpp
  2. 1
      meson.build
  3. 3
      render.cpp
  4. 2
      render.hpp
  5. 51
      tests/render.cpp

@ -52,10 +52,9 @@ GUI::GUI(DinkyECS::World &world, Map& game_map) :
} }
void GUI::resize_map(int new_size) { void GUI::resize_map(int new_size) {
if($renderer.resize_grid(new_size, $map_view)) { $renderer.resize_grid(new_size, $map_view);
// set canvas to best size // set canvas to best size
$canvas = Canvas($map_view.width * 2, $map_view.height * 4); $canvas = Canvas($map_view.width * 2, $map_view.height * 4);
}
} }
void GUI::save_world() { void GUI::save_world() {

@ -32,6 +32,7 @@ runtests = executable('runtests', [
'render.cpp', 'render.cpp',
'pathing.cpp', 'pathing.cpp',
'lights.cpp', 'lights.cpp',
'systems.cpp',
'worldbuilder.cpp', 'worldbuilder.cpp',
'tests/fsm.cpp', 'tests/fsm.cpp',
'tests/dbc.cpp', 'tests/dbc.cpp',

@ -44,7 +44,7 @@ sf::Sprite &SFMLRender::get_text_sprite(wchar_t tile) {
} }
bool SFMLRender::resize_grid(int new_size, Panel &panel_out) { void SFMLRender::resize_grid(int new_size, Panel &panel_out) {
auto glyph = $font.getGlyph($config.bg_tile, new_size, false); auto glyph = $font.getGlyph($config.bg_tile, new_size, false);
int view_x = std::ceil(($config.video_x - panel_out.x) / glyph.bounds.width); int view_x = std::ceil(($config.video_x - panel_out.x) / glyph.bounds.width);
int view_y = std::ceil(($config.video_y - panel_out.y) / glyph.bounds.height); int view_y = std::ceil(($config.video_y - panel_out.y) / glyph.bounds.height);
@ -57,7 +57,6 @@ bool SFMLRender::resize_grid(int new_size, Panel &panel_out) {
$bg_sprite = get_text_sprite($config.bg_tile); $bg_sprite = get_text_sprite($config.bg_tile);
$bg_bounds = $bg_sprite.getLocalBounds(); $bg_bounds = $bg_sprite.getLocalBounds();
panel_out.resize(view_x, view_y); panel_out.resize(view_x, view_y);
return true;
} }
inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds, sf::FloatRect bg_bounds, float &width_delta, float &height_delta) { inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds, sf::FloatRect bg_bounds, float &width_delta, float &height_delta) {

@ -50,7 +50,7 @@ struct SFMLRender {
SFMLRender(SFMLRender &other) = delete; SFMLRender(SFMLRender &other) = delete;
sf::Sprite &get_text_sprite(wchar_t tile); sf::Sprite &get_text_sprite(wchar_t tile);
bool resize_grid(int new_size, Panel &panel_out); void resize_grid(int new_size, Panel &panel_out);
void render_grid(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y); void render_grid(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y);
void render_text(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y); void render_text(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y);

@ -2,9 +2,17 @@
#include <fmt/core.h> #include <fmt/core.h>
#include "render.hpp" #include "render.hpp"
#include "panel.hpp" #include "panel.hpp"
#include "map.hpp"
#include "worldbuilder.hpp"
#include "config.hpp"
#include "components.hpp"
#include "systems.hpp"
#include "save.hpp"
using namespace ftxui; using namespace ftxui;
using namespace fmt; using namespace fmt;
using namespace components;
using namespace lighting;
using std::string; using std::string;
void run_renderer(SFMLRender &renderer, Panel &panel) { void run_renderer(SFMLRender &renderer, Panel &panel) {
@ -15,11 +23,10 @@ void run_renderer(SFMLRender &renderer, Panel &panel) {
renderer.draw(panel); renderer.draw(panel);
} }
TEST_CASE("can render a text or grid panel", "[render]") { TEST_CASE("can render a text panel", "[render]") {
SFMLRender renderer; SFMLRender renderer;
Panel panel(0, 0, 20, 5); Panel panel(0, 0, 20, 5);
Panel grid(200, 200, 20, 5, true);
auto text_box = Renderer([]{ auto text_box = Renderer([]{
return hbox({ return hbox({
@ -29,10 +36,44 @@ TEST_CASE("can render a text or grid panel", "[render]") {
}); });
panel.set_renderer(text_box); panel.set_renderer(text_box);
grid.set_renderer(text_box);
run_renderer(renderer, panel); run_renderer(renderer, panel);
run_renderer(renderer, grid); renderer.close();
}
TEST_CASE("can render a text", "[render]") {
SFMLRender renderer;
DinkyECS::World world;
save::load_configs(world);
const auto& config = world.get_the<MapConfig>();
Panel map_view(0, 0, 20, 5, true);
Map map(20,20);
WorldBuilder builder(map);
builder.generate();
Player player{world.entity()};
world.set_the<Player>(player);
world.set<Tile>(player.entity, {config.PLAYER_TILE});
world.set<LightSource>(player.entity, {6,1});
world.set<Position>(player.entity, {map.place_entity(0)});
LightRender lights(map.width(), map.height(), map.limit());
Canvas map_canvas(map_view.width * 2, map_view.height * 4);
map_view.set_renderer(Renderer([&] {
System::draw_map(world, map, lights.lighting(), map_canvas, map_view.width, map_view.height);
return canvas(map_canvas);
}));
run_renderer(renderer, map_view);
for(int i = 2; i < 14; i++) {
renderer.resize_grid(i * 10, map_view);
run_renderer(renderer, map_view);
}
renderer.close(); renderer.close();
} }

Loading…
Cancel
Save