diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/game.cpp | 16 | ||||
-rw-r--r-- | src/system.h | 31 | ||||
-rw-r--r-- | src/system_manager.h | 24 | ||||
-rw-r--r-- | src/systems/animating.cpp | 30 | ||||
-rw-r--r-- | src/systems/animating.h | 3 | ||||
-rw-r--r-- | src/systems/rendering.cpp | 37 | ||||
-rw-r--r-- | src/systems/rendering.h | 22 |
7 files changed, 96 insertions, 67 deletions
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 @@ | |||
5 | #include "components/droppable.h" | 5 | #include "components/droppable.h" |
6 | #include "components/ponderable.h" | 6 | #include "components/ponderable.h" |
7 | #include "components/orientable.h" | 7 | #include "components/orientable.h" |
8 | #include "systems/rendering.h" | ||
9 | #include "systems/controlling.h" | 8 | #include "systems/controlling.h" |
10 | #include "systems/pondering.h" | 9 | #include "systems/pondering.h" |
11 | #include "systems/animating.h" | 10 | #include "systems/animating.h" |
12 | #include "animation.h" | 11 | #include "animation.h" |
12 | #include "renderer.h" | ||
13 | #include "consts.h" | ||
13 | 14 | ||
14 | void key_callback(GLFWwindow* window, int key, int, int action, int) | 15 | void key_callback(GLFWwindow* window, int key, int, int action, int) |
15 | { | 16 | { |
@@ -22,14 +23,13 @@ void key_callback(GLFWwindow* window, int key, int, int action, int) | |||
22 | return; | 23 | return; |
23 | } | 24 | } |
24 | 25 | ||
25 | game.systemManager_.getSystem<ControllingSystem>().input(key, action); | 26 | game.systemManager_.input(key, action); |
26 | } | 27 | } |
27 | 28 | ||
28 | Game::Game(GLFWwindow* window) : window_(window) | 29 | Game::Game(GLFWwindow* window) : window_(window) |
29 | { | 30 | { |
30 | systemManager_.emplaceSystem<ControllingSystem>(*this); | 31 | systemManager_.emplaceSystem<ControllingSystem>(*this); |
31 | systemManager_.emplaceSystem<PonderingSystem>(*this); | 32 | systemManager_.emplaceSystem<PonderingSystem>(*this); |
32 | systemManager_.emplaceSystem<RenderingSystem>(*this); | ||
33 | systemManager_.emplaceSystem<AnimatingSystem>(*this); | 33 | systemManager_.emplaceSystem<AnimatingSystem>(*this); |
34 | 34 | ||
35 | int player = entityManager_.emplaceEntity(); | 35 | int player = entityManager_.emplaceEntity(); |
@@ -64,6 +64,7 @@ void Game::execute() | |||
64 | double lastTime = glfwGetTime(); | 64 | double lastTime = glfwGetTime(); |
65 | const double dt = 0.01; | 65 | const double dt = 0.01; |
66 | double accumulator = 0.0; | 66 | double accumulator = 0.0; |
67 | Texture texture(GAME_WIDTH, GAME_HEIGHT); | ||
67 | 68 | ||
68 | while (!(shouldQuit_ || glfwWindowShouldClose(window_))) | 69 | while (!(shouldQuit_ || glfwWindowShouldClose(window_))) |
69 | { | 70 | { |
@@ -76,13 +77,14 @@ void Game::execute() | |||
76 | accumulator += frameTime; | 77 | accumulator += frameTime; |
77 | while (accumulator >= dt) | 78 | while (accumulator >= dt) |
78 | { | 79 | { |
79 | systemManager_.getSystem<ControllingSystem>().tick(dt); | 80 | systemManager_.tick(dt); |
80 | systemManager_.getSystem<PonderingSystem>().tick(dt); | ||
81 | systemManager_.getSystem<AnimatingSystem>().tick(dt); | ||
82 | 81 | ||
83 | accumulator -= dt; | 82 | accumulator -= dt; |
84 | } | 83 | } |
85 | 84 | ||
86 | systemManager_.getSystem<RenderingSystem>().tick(frameTime); | 85 | // Render |
86 | texture.fill(texture.entirety(), 0, 0, 0); | ||
87 | systemManager_.render(texture); | ||
88 | texture.renderScreen(); | ||
87 | } | 89 | } |
88 | } | 90 | } |
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 @@ | |||
4 | #include "entity_manager.h" | 4 | #include "entity_manager.h" |
5 | 5 | ||
6 | class Game; | 6 | class Game; |
7 | class Texture; | ||
7 | 8 | ||
8 | class System { | 9 | class System { |
9 | public: | 10 | public: |
@@ -16,7 +17,35 @@ public: | |||
16 | 17 | ||
17 | virtual ~System() = default; | 18 | virtual ~System() = default; |
18 | 19 | ||
19 | virtual void tick(double dt) = 0; | 20 | /** |
21 | * Updates the state of a system. | ||
22 | * | ||
23 | * @param dt - The amount of time in seconds that have passed since the last | ||
24 | * update. | ||
25 | */ | ||
26 | virtual void tick(double) | ||
27 | { | ||
28 | } | ||
29 | |||
30 | /** | ||
31 | * Renders to a texture. | ||
32 | * | ||
33 | * @param texture - The surface to render to. | ||
34 | */ | ||
35 | virtual void render(Texture&) | ||
36 | { | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * Processes keyboard input. | ||
41 | * | ||
42 | * @param key - The relevant key. | ||
43 | * | ||
44 | * @param action - The action performed (press, released, etc). | ||
45 | */ | ||
46 | virtual void input(int, int) | ||
47 | { | ||
48 | } | ||
20 | 49 | ||
21 | protected: | 50 | protected: |
22 | 51 | ||
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: | |||
39 | return *dynamic_cast<T*>(systems[systemType]); | 39 | return *dynamic_cast<T*>(systems[systemType]); |
40 | } | 40 | } |
41 | 41 | ||
42 | void tick(double dt) | ||
43 | { | ||
44 | for (std::unique_ptr<System>& sys : loop) | ||
45 | { | ||
46 | sys->tick(dt); | ||
47 | } | ||
48 | } | ||
49 | |||
50 | virtual void render(Texture& texture) | ||
51 | { | ||
52 | for (std::unique_ptr<System>& sys : loop) | ||
53 | { | ||
54 | sys->render(texture); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | virtual void input(int key, int action) | ||
59 | { | ||
60 | for (std::unique_ptr<System>& sys : loop) | ||
61 | { | ||
62 | sys->input(key, action); | ||
63 | } | ||
64 | } | ||
65 | |||
42 | }; | 66 | }; |
43 | 67 | ||
44 | #endif /* end of include guard: SYSTEM_MANAGER_H_544E6056 */ | 68 | #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 @@ | |||
1 | #include "animating.h" | 1 | #include "animating.h" |
2 | #include "game.h" | 2 | #include "game.h" |
3 | #include "components/animatable.h" | 3 | #include "components/animatable.h" |
4 | #include "components/transformable.h" | ||
4 | 5 | ||
5 | void AnimatingSystem::tick(double) | 6 | void AnimatingSystem::tick(double) |
6 | { | 7 | { |
@@ -28,6 +29,35 @@ void AnimatingSystem::tick(double) | |||
28 | } | 29 | } |
29 | } | 30 | } |
30 | 31 | ||
32 | void AnimatingSystem::render(Texture& texture) | ||
33 | { | ||
34 | std::set<id_type> spriteEntities = | ||
35 | game_.getEntityManager().getEntitiesWithComponents< | ||
36 | AnimatableComponent, | ||
37 | TransformableComponent>(); | ||
38 | |||
39 | for (id_type entity : spriteEntities) | ||
40 | { | ||
41 | auto& sprite = game_.getEntityManager(). | ||
42 | getComponent<AnimatableComponent>(entity); | ||
43 | |||
44 | auto& transform = game_.getEntityManager(). | ||
45 | getComponent<TransformableComponent>(entity); | ||
46 | |||
47 | Rectangle dstrect { | ||
48 | static_cast<int>(transform.getX()), | ||
49 | static_cast<int>(transform.getY()), | ||
50 | transform.getW(), | ||
51 | transform.getH()}; | ||
52 | |||
53 | const AnimationSet& aset = sprite.getAnimationSet(); | ||
54 | texture.blit( | ||
55 | aset.getTexture(), | ||
56 | aset.getFrameRect(sprite.getFrame()), | ||
57 | dstrect); | ||
58 | } | ||
59 | } | ||
60 | |||
31 | void AnimatingSystem::startAnimation(id_type entity, std::string animation) | 61 | void AnimatingSystem::startAnimation(id_type entity, std::string animation) |
32 | { | 62 | { |
33 | auto& sprite = game_.getEntityManager(). | 63 | 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 @@ | |||
3 | 3 | ||
4 | #include "system.h" | 4 | #include "system.h" |
5 | #include <string> | 5 | #include <string> |
6 | #include "renderer.h" | ||
6 | 7 | ||
7 | class AnimatingSystem : public System { | 8 | class AnimatingSystem : public System { |
8 | public: | 9 | public: |
@@ -13,6 +14,8 @@ public: | |||
13 | 14 | ||
14 | void tick(double dt); | 15 | void tick(double dt); |
15 | 16 | ||
17 | void render(Texture& texture); | ||
18 | |||
16 | void startAnimation(id_type entity, std::string animation); | 19 | void startAnimation(id_type entity, std::string animation); |
17 | 20 | ||
18 | }; | 21 | }; |
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 @@ | |||
1 | #include "rendering.h" | ||
2 | #include "game.h" | ||
3 | #include "components/animatable.h" | ||
4 | #include "components/transformable.h" | ||
5 | |||
6 | void RenderingSystem::tick(double) | ||
7 | { | ||
8 | texture_.fill(texture_.entirety(), 0, 0, 0); | ||
9 | |||
10 | std::set<id_type> spriteEntities = | ||
11 | game_.getEntityManager().getEntitiesWithComponents< | ||
12 | AnimatableComponent, | ||
13 | TransformableComponent>(); | ||
14 | |||
15 | for (id_type entity : spriteEntities) | ||
16 | { | ||
17 | auto& sprite = game_.getEntityManager(). | ||
18 | getComponent<AnimatableComponent>(entity); | ||
19 | |||
20 | auto& transform = game_.getEntityManager(). | ||
21 | getComponent<TransformableComponent>(entity); | ||
22 | |||
23 | Rectangle dstrect { | ||
24 | static_cast<int>(transform.getX()), | ||
25 | static_cast<int>(transform.getY()), | ||
26 | transform.getW(), | ||
27 | transform.getH()}; | ||
28 | |||
29 | const AnimationSet& aset = sprite.getAnimationSet(); | ||
30 | texture_.blit( | ||
31 | aset.getTexture(), | ||
32 | aset.getFrameRect(sprite.getFrame()), | ||
33 | dstrect); | ||
34 | } | ||
35 | |||
36 | texture_.renderScreen(); | ||
37 | } | ||
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 @@ | |||
1 | #ifndef RENDERING_H_76ABC02A | ||
2 | #define RENDERING_H_76ABC02A | ||
3 | |||
4 | #include "system.h" | ||
5 | #include "renderer.h" | ||
6 | #include "consts.h" | ||
7 | |||
8 | class RenderingSystem : public System { | ||
9 | public: | ||
10 | |||
11 | RenderingSystem(Game& game) : System(game) | ||
12 | { | ||
13 | } | ||
14 | |||
15 | void tick(double dt); | ||
16 | |||
17 | private: | ||
18 | |||
19 | Texture texture_ {GAME_WIDTH, GAME_HEIGHT}; | ||
20 | }; | ||
21 | |||
22 | #endif /* end of include guard: RENDERING_H_76ABC02A */ | ||