From e28a62894f368cccc64932fe83c91f32d7cb7288 Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Tue, 7 Nov 2017 12:41:23 +0100 Subject: Hide debug draws by default, start working on selecting. --- src/common.hpp | 22 ++++++++++------ src/game.cpp | 11 +++++--- src/interface.cpp | 79 ++++++++++++++++++++++++++++++++++++++----------------- src/main.cpp | 4 +++ src/render.cpp | 33 +++++++++++++---------- src/world.cpp | 21 +-------------- 6 files changed, 101 insertions(+), 69 deletions(-) diff --git a/src/common.hpp b/src/common.hpp index a301881..ecdb549 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -9,6 +9,10 @@ #include #include "math.hpp" +extern bool debug_draw_cmodels; +extern bool debug_draw_paths; +extern bool debug_draw_tile_coords; + namespace procgen { class prng_t { uint32_t state = 0; @@ -95,11 +99,6 @@ namespace world { protected: friend render::state_t; - typedef struct { - v2f_t x; - std::string text; - } debug_t; - std::list debug; public: world_t(void); @@ -177,7 +176,9 @@ namespace game { void start(void); void tick(double now); - void debug_click(v2f_t x); + // These are called by the interface. + void select(rectf_t rect); + void command(v2f_t x); }; } @@ -190,10 +191,15 @@ namespace interface { sf::Vector2f center; int target_zoom = 3; float zoom = 3.0f; - bool dragging = false; - sf::Vector2f drag_ref; + bool panning = false; + sf::Vector2f pan_ref; } camera; + struct { + bool selecting = false; + rectf_t rect; + } select; + public: state_t(sf::RenderWindow *window_, game::state_t *game); void tick(void); diff --git a/src/game.cpp b/src/game.cpp index e8c179a..f5341d1 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -125,7 +125,7 @@ public: render->render(&assets::human.body_idle, render_bounds, move.angle); render->render(&assets::human.head_idle, render_bounds, move.angle); - if (move.moving) + if (move.moving && debug_draw_paths) render->debug_path(&move.path); } }; @@ -148,9 +148,14 @@ void state_t::start(void) } -void state_t::debug_click(v2f_t x) +void state_t::select(rectf_t x) { - human.start_moving(x); + +} + +void state_t::command(v2f_t x) +{ + } void state_t::tick(double now) diff --git a/src/interface.cpp b/src/interface.cpp index 0650889..d6137fd 100644 --- a/src/interface.cpp +++ b/src/interface.cpp @@ -8,11 +8,11 @@ state_t::state_t(sf::RenderWindow *window_, game::state_t *game_) game = game_; } -static sf::Vector2f compute_drag(sf::RenderWindow *window, sf::Vector2f drag_ref) +static sf::Vector2f compute_pan(sf::RenderWindow *window, sf::Vector2f pan_ref) { sf::Vector2i mouse = sf::Mouse::getPosition(*window); sf::Vector2f vmouse = window->mapPixelToCoords(mouse); - return -(vmouse - drag_ref); + return -(vmouse - pan_ref); } void state_t::tick(void) @@ -20,6 +20,7 @@ void state_t::tick(void) sf::Vector2u size; sf::Event event; sf::Vector2f view_size; + v2f_t wmouse; // Mouse position in world space; size = window->getSize(); @@ -42,30 +43,46 @@ void state_t::tick(void) while (window->pollEvent(event)) { + // FIXME: refactor this nested switch clusterfuck switch (event.type) { case sf::Event::Closed: window->close(); return; case sf::Event::MouseButtonPressed: - if (event.mouseButton.button == 0) { - v2f_t point = window->mapPixelToCoords( - sf::Vector2i(event.mouseButton.x, - event.mouseButton.y)); - game->debug_click(point); - } else if (event.mouseButton.button == 1) { - camera.dragging = true; - camera.drag_ref = window->mapPixelToCoords( - sf::Vector2i(event.mouseButton.x, - event.mouseButton.y)); + wmouse = window->mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)); + + switch (event.mouseButton.button) { + case sf::Mouse::Button::Left: + select.selecting = true; + select.rect[0] = wmouse; + select.rect[1] = wmouse; + break; + + case sf::Mouse::Button::Middle: + camera.panning = true; + camera.pan_ref = wmouse; + break; + + default:; } break; case sf::Event::MouseButtonReleased: - if (event.mouseButton.button == 1) { - if (camera.dragging) - camera.center += compute_drag(window, camera.drag_ref); - camera.dragging = false; + switch (event.mouseButton.button) { + case sf::Mouse::Button::Left: + if (select.selecting) + game->select(select.rect); + select.selecting = false; + break; + + case sf::Mouse::Button::Middle: + if (camera.panning) + camera.center += compute_pan(window, camera.pan_ref); + camera.panning = false; + break; + + default:; } break; @@ -78,22 +95,36 @@ void state_t::tick(void) break; case sf::Event::KeyPressed: - if (event.key.code == sf::Keyboard::Key::I) { - sf::Vector2i mouse = sf::Mouse::getPosition(*window); - sf::Vector2f vmouse = window->mapPixelToCoords(mouse); - game->world.debug_point(vmouse); - } - break; + switch (event.key.code) { + case sf::Keyboard::Key::F1: + debug_draw_cmodels ^= 1; + break; + + case sf::Keyboard::Key::F2: + debug_draw_paths ^= 1; + break; + + case sf::Keyboard::Key::F3: + debug_draw_tile_coords ^= 1; + break; + default:; + } default:; } } - if (camera.dragging) { - sf::Vector2f delta = compute_drag(window, camera.drag_ref); + if (camera.panning) { + sf::Vector2f delta = compute_pan(window, camera.pan_ref); window->setView(sf::View(camera.center + delta, view_size)); } else window->setView(sf::View(camera.center, view_size)); + + // Compute this _after_ the setView above. + wmouse = window->mapPixelToCoords(sf::Mouse::getPosition(*window)); + + if (select.selecting) + select.rect[1] = wmouse; } } // namespace interface diff --git a/src/main.cpp b/src/main.cpp index 352981c..01c1df4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,10 @@ #include "common.hpp" #include +bool debug_draw_cmodels = false; +bool debug_draw_paths = false; +bool debug_draw_tile_coords = false; + uint64_t nano_clock(void) { struct timespec ts; diff --git a/src/render.cpp b/src/render.cpp index 313bc4d..41bcd4b 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -29,13 +29,16 @@ static void draw_tile(sf::RenderWindow *window, v2f_t x, world::tile_t *tile, wot_rect.setFillColor(sf::Color::White); wot_rect.setOutlineColor(sf::Color::Transparent); window->draw(wot_rect); + wot_rect.setTexture(NULL); - std::stringstream ss; - ss << "L=" << local; - sf::Text text(ss.str(), font, 20); - text.setPosition(x); - text.setScale(0.005, 0.005); - window->draw(text); + if (debug_draw_tile_coords) { + std::stringstream ss; + ss << "L=" << local; + sf::Text text(ss.str(), font, 20); + text.setPosition(x); + text.setScale(0.005, 0.005); + window->draw(text); + } } static void draw_sector(sf::RenderWindow *window, world::sector_t *sector) @@ -49,6 +52,14 @@ static void draw_sector(sf::RenderWindow *window, world::sector_t *sector) void interface::state_t::render() { + if (select.selecting) { + wot_rect.setSize(select.rect.dims()); + wot_rect.setPosition(select.rect[0]); + wot_rect.setFillColor(sf::Color(200, 100, 50, 100)); + wot_rect.setOutlineThickness(0.02f); + wot_rect.setOutlineColor(sf::Color(200, 100, 50, 255)); + window->draw(wot_rect); + } } namespace render { @@ -147,15 +158,9 @@ void state_t::render(game::state_t *game) for (world::entity_t *ent : ents) { ent->render_to(this); - drender_entity(ent); - } - for (world::world_t::debug_t &debug : game->world.debug) { - sf::Text text(debug.text, font, 20); - text.setPosition(debug.x); - text.setScale(0.006, 0.006); - text.setFillColor(sf::Color::Red); - window->draw(text); + if (debug_draw_cmodels) + drender_entity(ent); } } diff --git a/src/world.cpp b/src/world.cpp index 2339dbf..004f03f 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -86,7 +86,6 @@ bool world_t::find_path(v2f_t src, v2f_t dst, cmodel_t *cmodel, entity_t *ignore path_finder_t finder; rectf_t bounds; v2f_t cmodel_dims; - bool found; finder.setup_nodes(src, dst, cmodel->cflags); @@ -114,25 +113,7 @@ bool world_t::find_path(v2f_t src, v2f_t dst, cmodel_t *cmodel, entity_t *ignore if (ent != ignore) finder.eliminate_nodes(ent->cmodel.bounds); - found = finder.find(); - - debug.clear(); - - for (size_t y = 0; y < finder.height; y++) - for (size_t x = 0; x < finder.width; x++) { - path_node_t *node = finder.nodes + y * finder.width + x; - std::stringstream ss; - - ss << finder.base + tile_index_t(x, y) << "\n"; - if (node->accessible) - ss << node->dist; - else - ss << "inaccessible"; - - debug.push_back((debug_t){finder.base + tile_index_t(x, y), ss.str()}); - } - - if (!found) + if (finder.find()) return false; finder.export_path(path); -- cgit