From 1400ade977e13e3b535d3c2fddb6e15de3c9b5a5 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 8 Feb 2018 13:43:10 -0500 Subject: Moved sprite rendering into AnimatingSystem Refactored how systems work slightly. Now, rendering can be done by a number of systems working together. Since the AnimatingSystem handles the animation of sprites, it should also handle the rendering of them. Because of this, the RenderingSystem has been removed. --- src/game.cpp | 16 +++++++++------- src/system.h | 31 ++++++++++++++++++++++++++++++- src/system_manager.h | 24 ++++++++++++++++++++++++ src/systems/animating.cpp | 30 ++++++++++++++++++++++++++++++ src/systems/animating.h | 3 +++ src/systems/rendering.cpp | 37 ------------------------------------- src/systems/rendering.h | 22 ---------------------- 7 files changed, 96 insertions(+), 67 deletions(-) delete mode 100644 src/systems/rendering.cpp delete mode 100644 src/systems/rendering.h (limited to 'src') diff --git a/src/game.cpp b/src/game.cpp index 492e4d0..dfe700e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -5,11 +5,12 @@ #include "components/droppable.h" #include "components/ponderable.h" #include "components/orientable.h" -#include "systems/rendering.h" #include "systems/controlling.h" #include "systems/pondering.h" #include "systems/animating.h" #include "animation.h" +#include "renderer.h" +#include "consts.h" void key_callback(GLFWwindow* window, int key, int, int action, int) { @@ -22,14 +23,13 @@ void key_callback(GLFWwindow* window, int key, int, int action, int) return; } - game.systemManager_.getSystem().input(key, action); + game.systemManager_.input(key, action); } Game::Game(GLFWwindow* window) : window_(window) { systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); - systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); int player = entityManager_.emplaceEntity(); @@ -64,6 +64,7 @@ void Game::execute() double lastTime = glfwGetTime(); const double dt = 0.01; double accumulator = 0.0; + Texture texture(GAME_WIDTH, GAME_HEIGHT); while (!(shouldQuit_ || glfwWindowShouldClose(window_))) { @@ -76,13 +77,14 @@ void Game::execute() accumulator += frameTime; while (accumulator >= dt) { - systemManager_.getSystem().tick(dt); - systemManager_.getSystem().tick(dt); - systemManager_.getSystem().tick(dt); + systemManager_.tick(dt); accumulator -= dt; } - systemManager_.getSystem().tick(frameTime); + // Render + texture.fill(texture.entirety(), 0, 0, 0); + systemManager_.render(texture); + texture.renderScreen(); } } diff --git a/src/system.h b/src/system.h index 6a3cd14..e630c48 100644 --- a/src/system.h +++ b/src/system.h @@ -4,6 +4,7 @@ #include "entity_manager.h" class Game; +class Texture; class System { public: @@ -16,7 +17,35 @@ public: virtual ~System() = default; - virtual void tick(double dt) = 0; + /** + * Updates the state of a system. + * + * @param dt - The amount of time in seconds that have passed since the last + * update. + */ + virtual void tick(double) + { + } + + /** + * Renders to a texture. + * + * @param texture - The surface to render to. + */ + virtual void render(Texture&) + { + } + + /** + * Processes keyboard input. + * + * @param key - The relevant key. + * + * @param action - The action performed (press, released, etc). + */ + virtual void input(int, int) + { + } protected: diff --git a/src/system_manager.h b/src/system_manager.h index e2c98cb..b03c3f2 100644 --- a/src/system_manager.h +++ b/src/system_manager.h @@ -39,6 +39,30 @@ public: return *dynamic_cast(systems[systemType]); } + void tick(double dt) + { + for (std::unique_ptr& sys : loop) + { + sys->tick(dt); + } + } + + virtual void render(Texture& texture) + { + for (std::unique_ptr& sys : loop) + { + sys->render(texture); + } + } + + virtual void input(int key, int action) + { + for (std::unique_ptr& sys : loop) + { + sys->input(key, action); + } + } + }; #endif /* end of include guard: SYSTEM_MANAGER_H_544E6056 */ diff --git a/src/systems/animating.cpp b/src/systems/animating.cpp index fcbfca5..91fe925 100644 --- a/src/systems/animating.cpp +++ b/src/systems/animating.cpp @@ -1,6 +1,7 @@ #include "animating.h" #include "game.h" #include "components/animatable.h" +#include "components/transformable.h" void AnimatingSystem::tick(double) { @@ -28,6 +29,35 @@ void AnimatingSystem::tick(double) } } +void AnimatingSystem::render(Texture& texture) +{ + std::set spriteEntities = + game_.getEntityManager().getEntitiesWithComponents< + AnimatableComponent, + TransformableComponent>(); + + for (id_type entity : spriteEntities) + { + auto& sprite = game_.getEntityManager(). + getComponent(entity); + + auto& transform = game_.getEntityManager(). + getComponent(entity); + + Rectangle dstrect { + static_cast(transform.getX()), + static_cast(transform.getY()), + transform.getW(), + transform.getH()}; + + const AnimationSet& aset = sprite.getAnimationSet(); + texture.blit( + aset.getTexture(), + aset.getFrameRect(sprite.getFrame()), + dstrect); + } +} + void AnimatingSystem::startAnimation(id_type entity, std::string animation) { auto& sprite = game_.getEntityManager(). diff --git a/src/systems/animating.h b/src/systems/animating.h index 150a74a..d6a89a5 100644 --- a/src/systems/animating.h +++ b/src/systems/animating.h @@ -3,6 +3,7 @@ #include "system.h" #include +#include "renderer.h" class AnimatingSystem : public System { public: @@ -13,6 +14,8 @@ public: void tick(double dt); + void render(Texture& texture); + void startAnimation(id_type entity, std::string animation); }; diff --git a/src/systems/rendering.cpp b/src/systems/rendering.cpp deleted file mode 100644 index 8219732..0000000 --- a/src/systems/rendering.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "rendering.h" -#include "game.h" -#include "components/animatable.h" -#include "components/transformable.h" - -void RenderingSystem::tick(double) -{ - texture_.fill(texture_.entirety(), 0, 0, 0); - - std::set spriteEntities = - game_.getEntityManager().getEntitiesWithComponents< - AnimatableComponent, - TransformableComponent>(); - - for (id_type entity : spriteEntities) - { - auto& sprite = game_.getEntityManager(). - getComponent(entity); - - auto& transform = game_.getEntityManager(). - getComponent(entity); - - Rectangle dstrect { - static_cast(transform.getX()), - static_cast(transform.getY()), - transform.getW(), - transform.getH()}; - - const AnimationSet& aset = sprite.getAnimationSet(); - texture_.blit( - aset.getTexture(), - aset.getFrameRect(sprite.getFrame()), - dstrect); - } - - texture_.renderScreen(); -} diff --git a/src/systems/rendering.h b/src/systems/rendering.h deleted file mode 100644 index a53ee64..0000000 --- a/src/systems/rendering.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef RENDERING_H_76ABC02A -#define RENDERING_H_76ABC02A - -#include "system.h" -#include "renderer.h" -#include "consts.h" - -class RenderingSystem : public System { -public: - - RenderingSystem(Game& game) : System(game) - { - } - - void tick(double dt); - -private: - - Texture texture_ {GAME_WIDTH, GAME_HEIGHT}; -}; - -#endif /* end of include guard: RENDERING_H_76ABC02A */ -- cgit 1.4.1