From 4cfe1361fff6223d11a25cefd74af7a51b7d57bd Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Sat, 21 Apr 2018 18:20:49 +0200 Subject: Hide game and interface behind a pimpl. --- src/game/game.cpp | 2 +- src/game/game.hpp | 130 +++++++++++++++++++++++++++++++++++++++++++++++ src/game/interface.cpp | 6 --- src/game/pseudostate.cpp | 64 +++++++++++++++++++++++ src/game/units.cpp | 6 +-- 5 files changed, 198 insertions(+), 10 deletions(-) create mode 100644 src/game/pseudostate.cpp (limited to 'src/game') diff --git a/src/game/game.cpp b/src/game/game.cpp index 084ac38..5f89a60 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -400,7 +400,7 @@ void state_t::tick(ntime_t time_) frames_since_t0 = 0; frames_behind++; - interface->print("(Lag: " + std::to_string(left) + ")"); + interface.print("(Lag: " + std::to_string(left) + ")"); break; } diff --git a/src/game/game.hpp b/src/game/game.hpp index e35f156..e4fdba2 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -18,6 +18,136 @@ along with Minitrem. If not, see . #include "../common.hpp" namespace game { + class state_t; + class entity_t; + class unit_t; + class effect_t; +} + +namespace interface { + class state_t; + + typedef struct { + std::string label; + int action; + + float r0, r1; + float t0, t1; + } pie_item_t; + + class pie_menu_t { + v2f_t x, x_world; + float radius; + pie_item_t *selected = nullptr; + + protected: + friend state_t; + std::vector items; + + public: + bool is_open = false; + + void open(v2f_t wmouse, v2f_t mouse); + void close(game::state_t *game); + void update(v2f_t mouse); + void render_to(render::state_t *render); + }; + + class state_t { + protected: + friend game::pseudostate_t; + + sf::RenderWindow *window; + game::state_t *game; + + float em; + + struct { + v2f_t zero_point_s = v2f_t(0, 0); + v2f_t center = v2f_t(0, 0); + + int zoom = 3; + float zoom_s = 3.0f; + + bool panning = false; + sf::Vector2f pan_ref; + + bool following = true; + } camera; + + struct { + bool selecting = false; + int type; + rectf_t rect; + } select; + + pie_menu_t pie_menu; + + typedef struct { + double time; + std::string text; + } log_entry_t; + + std::list log; + + void start_following(void); + void stop_following(void); + + public: + v3f_t camera_3d; // for audio + + void tick(double dt); + void render_to(render::state_t *render); + + void print(std::string str); + }; +} + +namespace game { + enum { + SELECT_NEW, + SELECT_OR, + SELECT_XOR + }; + + class state_t { + void group_say(std::string text); + bool select_unit(unit_t *unit, int type); + + public: + world::world_t world; + interface::state_t interface; + procgen::prng_t prng; + std::unordered_set awake_entities; + std::unordered_set selected_units; + // Some deletes have to be deferred to the next frame. + std::unordered_set deletion_list; + + ntime_t time = 1; // game time + double now, dt; // FIXME: refactor the code, use ntime_t everywhere + + // frame timing data (game runs at a different frequency than the renderer) + bool paused; + ntime_t t0; + size_t frames = 0, frames_since_t0, frames_behind = 0; + + void start(void); + void stop(void); + void tick(ntime_t time); + void compute_ambience(render::state_t *render); + void pause(void); + void resume(void); + + void wake_area(v2f_t x); + void explosion(v2f_t x); + void hivemind_alert(v2f_t x, float r, bool do_move, v2f_t move_to); + + // These are called by the interface. + void select(rectf_t rect, int type); + bool populate_pie_menu(std::vector &items); + void command(v2f_t x, int number); + }; + enum { ET_NONE, ET_UNIT, diff --git a/src/game/interface.cpp b/src/game/interface.cpp index 8cc2a85..c40023d 100644 --- a/src/game/interface.cpp +++ b/src/game/interface.cpp @@ -22,12 +22,6 @@ namespace interface { using namespace game; // FIXME -state_t::state_t(sf::RenderWindow *window_, game::state_t *game_) -{ - window = window_; - game = game_; -} - static sf::Vector2f compute_pan(sf::RenderWindow *window, sf::Vector2f pan_ref) { sf::Vector2i mouse = sf::Mouse::getPosition(*window); diff --git a/src/game/pseudostate.cpp b/src/game/pseudostate.cpp new file mode 100644 index 0000000..0f2d0c5 --- /dev/null +++ b/src/game/pseudostate.cpp @@ -0,0 +1,64 @@ +/* +This file is part of Minitrem. + +Minitrem is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Minitrem is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Minitrem. If not, see . +*/ + +#include "game.hpp" + +namespace game { + +pseudostate_t::pseudostate_t(sf::RenderWindow *window_) +{ + pimpl = new state_t(); + + pimpl->interface.window = window_; + pimpl->interface.game = pimpl; +} + +pseudostate_t::~pseudostate_t(void) +{ + delete pimpl; +} + +void pseudostate_t::start(void) +{ + pimpl->start(); +} + +void pseudostate_t::stop(void) +{ + pimpl->stop(); +} + +void pseudostate_t::tick(ntime_t time, double dt) +{ + pimpl->tick(time); + pimpl->interface.tick(dt); +} + +world::world_t *pseudostate_t::get_world(void) +{ + return &pimpl->world; +} + +void pseudostate_t::render_interface_to(render::state_t *render) +{ + pimpl->interface.render_to(render); + + pimpl->compute_ambience(render); + audio::update(pimpl->interface.camera_3d, pimpl->paused); +} + +} diff --git a/src/game/units.cpp b/src/game/units.cpp index 2b3b081..828ca6b 100644 --- a/src/game/units.cpp +++ b/src/game/units.cpp @@ -102,7 +102,7 @@ void unit_t::say(std::string str, bool on_interface) say_time = game->now; if (on_interface) - game->interface->print(name + ": " + str); + game->interface.print(name + ": " + str); } bool unit_t::keep_moving(double speed) @@ -220,11 +220,11 @@ void unit_t::die(unit_t *killer) case UNIT_SOLDIER: case UNIT_SCIENTIST: case UNIT_SPIDER: - game->interface->print(name + " " + text::get(text::UNIT_DEATH) + "."); + game->interface.print(name + " " + text::get(text::UNIT_DEATH) + "."); break; default: - game->interface->print(name + " " + text::get(text::UNIT_DESTRUCTION) + "."); + game->interface.print(name + " " + text::get(text::UNIT_DESTRUCTION) + "."); break; } -- cgit