diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-24 12:30:40 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-24 12:30:40 -0500 |
commit | 90b9831d6fb89feedeec63cb392c4535c5df60fe (patch) | |
tree | 78faf1865bc0566454254d072b85f872bbc585ea | |
parent | f264cfd7655a48f7e9a6e2fc5b1c62d6d2036025 (diff) | |
download | dispatcher-90b9831d6fb89feedeec63cb392c4535c5df60fe.tar.gz dispatcher-90b9831d6fb89feedeec63cb392c4535c5df60fe.tar.bz2 dispatcher-90b9831d6fb89feedeec63cb392c4535c5df60fe.zip |
Started state machine
The "--editor" flag can be passed to the program to start it in editor mode, which is currently nothing. The Renderer class was removed here, as each state basically needs to do its own rendering. However, refactoring to make this more elegant will probably occur in the future.
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/editor.cpp | 13 | ||||
-rw-r--r-- | src/editor.h | 17 | ||||
-rw-r--r-- | src/main.cpp | 49 | ||||
-rw-r--r-- | src/renderer.cpp | 103 | ||||
-rw-r--r-- | src/renderer.h | 15 | ||||
-rw-r--r-- | src/simulation.cpp | 43 | ||||
-rw-r--r-- | src/simulation.h | 8 | ||||
-rw-r--r-- | src/state.h | 19 |
9 files changed, 141 insertions, 128 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6fabbf3..a1456a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -21,9 +21,9 @@ include_directories( | |||
21 | 21 | ||
22 | add_executable(dispatcher | 22 | add_executable(dispatcher |
23 | src/main.cpp | 23 | src/main.cpp |
24 | src/renderer.cpp | ||
25 | src/simulation.cpp | 24 | src/simulation.cpp |
26 | src/direction.cpp | 25 | src/direction.cpp |
26 | src/editor.cpp | ||
27 | ) | 27 | ) |
28 | 28 | ||
29 | set_property(TARGET dispatcher PROPERTY CXX_STANDARD 17) | 29 | set_property(TARGET dispatcher PROPERTY CXX_STANDARD 17) |
diff --git a/src/editor.cpp b/src/editor.cpp new file mode 100644 index 0000000..6e53ac3 --- /dev/null +++ b/src/editor.cpp | |||
@@ -0,0 +1,13 @@ | |||
1 | #include "editor.h" | ||
2 | |||
3 | void Editor::tick( | ||
4 | double dt, | ||
5 | const Uint8* keystate) | ||
6 | { | ||
7 | |||
8 | } | ||
9 | |||
10 | void Editor::render(SDL_Renderer* ren) | ||
11 | { | ||
12 | |||
13 | } | ||
diff --git a/src/editor.h b/src/editor.h new file mode 100644 index 0000000..aa137ec --- /dev/null +++ b/src/editor.h | |||
@@ -0,0 +1,17 @@ | |||
1 | #ifndef EDITOR_H_8BB54FE3 | ||
2 | #define EDITOR_H_8BB54FE3 | ||
3 | |||
4 | #include "state.h" | ||
5 | |||
6 | class Editor : public State { | ||
7 | public: | ||
8 | |||
9 | void tick( | ||
10 | double dt, | ||
11 | const Uint8* keystate) override; | ||
12 | |||
13 | void render(SDL_Renderer* ren) override; | ||
14 | |||
15 | }; | ||
16 | |||
17 | #endif /* end of include guard: EDITOR_H_8BB54FE3 */ | ||
diff --git a/src/main.cpp b/src/main.cpp index c0ba15f..7c65e81 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -1,17 +1,51 @@ | |||
1 | #include "renderer.h" | 1 | #include "renderer.h" |
2 | 2 | ||
3 | #include <random> | 3 | #include <random> |
4 | #include "renderer.h" | ||
4 | #include "simulation.h" | 5 | #include "simulation.h" |
5 | #include "level.h" | 6 | #include "level.h" |
7 | #include "editor.h" | ||
6 | 8 | ||
7 | int main(int, char**) | 9 | int main(int argc, char** argv) |
8 | { | 10 | { |
9 | std::random_device randomEngine; | 11 | std::random_device randomEngine; |
10 | std::mt19937 rng(randomEngine()); | 12 | std::mt19937 rng(randomEngine()); |
11 | 13 | ||
12 | Renderer renderer; | 14 | window_ptr win( |
15 | SDL_CreateWindow( | ||
16 | "dispatcher", | ||
17 | 100, | ||
18 | 100, | ||
19 | WINDOW_SIZE.w(), | ||
20 | WINDOW_SIZE.h(), | ||
21 | SDL_WINDOW_SHOWN)); | ||
22 | |||
23 | if (!win) | ||
24 | { | ||
25 | throw sdl_error(); | ||
26 | } | ||
27 | |||
28 | renderer_ptr ren( | ||
29 | SDL_CreateRenderer( | ||
30 | win.get(), | ||
31 | -1, | ||
32 | SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)); | ||
33 | |||
34 | if (!ren) | ||
35 | { | ||
36 | throw sdl_error(); | ||
37 | } | ||
38 | |||
13 | Level level; | 39 | Level level; |
14 | Simulation sim(level); | 40 | |
41 | std::unique_ptr<State> state; | ||
42 | |||
43 | if (argc == 2 && !strcmp(argv[1], "--editor")) | ||
44 | { | ||
45 | state.reset(new Editor()); | ||
46 | } else { | ||
47 | state.reset(new Simulation(level)); | ||
48 | } | ||
15 | 49 | ||
16 | bool quit = false; | 50 | bool quit = false; |
17 | 51 | ||
@@ -43,10 +77,13 @@ int main(int, char**) | |||
43 | } | 77 | } |
44 | } | 78 | } |
45 | 79 | ||
46 | const Uint8* state = SDL_GetKeyboardState(NULL); | 80 | const Uint8* keystate = SDL_GetKeyboardState(NULL); |
47 | 81 | ||
48 | sim.tick(frameTime / 1000.0, state); | 82 | state->tick(frameTime / 1000.0, keystate); |
49 | 83 | ||
50 | renderer.render(sim); | 84 | SDL_SetRenderTarget(ren.get(), nullptr); |
85 | SDL_RenderClear(ren.get()); | ||
86 | state->render(ren.get()); | ||
87 | SDL_RenderPresent(ren.get()); | ||
51 | } | 88 | } |
52 | } | 89 | } |
diff --git a/src/renderer.cpp b/src/renderer.cpp deleted file mode 100644 index 286a530..0000000 --- a/src/renderer.cpp +++ /dev/null | |||
@@ -1,103 +0,0 @@ | |||
1 | #include "renderer.h" | ||
2 | #include "simulation.h" | ||
3 | #include "consts.h" | ||
4 | #include "level.h" | ||
5 | #include "views.h" | ||
6 | |||
7 | Renderer::Renderer() | ||
8 | { | ||
9 | win_ = window_ptr( | ||
10 | SDL_CreateWindow( | ||
11 | "dispatcher", | ||
12 | 100, | ||
13 | 100, | ||
14 | WINDOW_SIZE.w(), | ||
15 | WINDOW_SIZE.h(), | ||
16 | SDL_WINDOW_SHOWN)); | ||
17 | |||
18 | if (!win_) | ||
19 | { | ||
20 | throw sdl_error(); | ||
21 | } | ||
22 | |||
23 | ren_ = renderer_ptr( | ||
24 | SDL_CreateRenderer( | ||
25 | win_.get(), | ||
26 | -1, | ||
27 | SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)); | ||
28 | |||
29 | if (!ren_) | ||
30 | { | ||
31 | throw sdl_error(); | ||
32 | } | ||
33 | |||
34 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD); | ||
35 | SDL_SetRenderDrawColor(ren_.get(), 255, 150, 255, 255); | ||
36 | SDL_RenderFillRect(ren_.get(), nullptr); | ||
37 | } | ||
38 | |||
39 | void Renderer::render(const Simulation& sim) | ||
40 | { | ||
41 | texture_ptr canvas( | ||
42 | SDL_CreateTexture( | ||
43 | ren_.get(), | ||
44 | SDL_PIXELFORMAT_RGBA8888, | ||
45 | SDL_TEXTUREACCESS_TARGET, | ||
46 | WINDOW_SIZE.w(), | ||
47 | WINDOW_SIZE.h())); | ||
48 | |||
49 | if (!canvas) | ||
50 | { | ||
51 | throw sdl_error(); | ||
52 | } | ||
53 | |||
54 | SDL_SetRenderTarget(ren_.get(), nullptr); | ||
55 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); | ||
56 | SDL_SetRenderDrawColor(ren_.get(), 255, 150, rand() % 255, 255); | ||
57 | SDL_RenderClear(ren_.get()); | ||
58 | |||
59 | const Level& level = sim.getLevel(); | ||
60 | |||
61 | for (size_t y = 0; y < level.getSize().h(); y++) | ||
62 | { | ||
63 | for (size_t x = 0; x < level.getSize().w(); x++) | ||
64 | { | ||
65 | int val = 255 - level.at(x, y) * 10; | ||
66 | |||
67 | SDL_SetRenderDrawColor(ren_.get(), val, val, val, 255); | ||
68 | |||
69 | SDL_Rect rect { | ||
70 | static_cast<int>(x * TILE_SIZE.w()), | ||
71 | static_cast<int>(y * TILE_SIZE.h()), | ||
72 | TILE_SIZE.w(), | ||
73 | TILE_SIZE.h() | ||
74 | }; | ||
75 | |||
76 | SDL_RenderFillRect(ren_.get(), &rect); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | constexpr Layer renderOrder[] = { Layer::track, Layer::object }; | ||
81 | |||
82 | ranges::for_each( | ||
83 | ranges::view::for_each( | ||
84 | renderOrder, | ||
85 | [&] (Layer layer) { | ||
86 | return sim.entityRange() | views::isOnLayer(layer); | ||
87 | }), | ||
88 | [&] (const Entity& entity) { | ||
89 | SDL_SetRenderDrawColor(ren_.get(), entity.colorVal, entity.colorVal, 65, 255); | ||
90 | |||
91 | SDL_Rect rect { | ||
92 | static_cast<int>(entity.pos.x()), | ||
93 | static_cast<int>(entity.pos.y()), | ||
94 | static_cast<int>(entity.size.w()), | ||
95 | static_cast<int>(entity.size.h()) | ||
96 | }; | ||
97 | |||
98 | SDL_RenderFillRect(ren_.get(), &rect); | ||
99 | }); | ||
100 | |||
101 | //SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); | ||
102 | SDL_RenderPresent(ren_.get()); | ||
103 | } | ||
diff --git a/src/renderer.h b/src/renderer.h index dd3498f..ac20ec2 100644 --- a/src/renderer.h +++ b/src/renderer.h | |||
@@ -108,19 +108,4 @@ public: | |||
108 | 108 | ||
109 | using texture_ptr = std::unique_ptr<SDL_Texture, texture_deleter>; | 109 | using texture_ptr = std::unique_ptr<SDL_Texture, texture_deleter>; |
110 | 110 | ||
111 | class Renderer { | ||
112 | public: | ||
113 | |||
114 | Renderer(); | ||
115 | |||
116 | void render(const Simulation& game); | ||
117 | |||
118 | private: | ||
119 | |||
120 | sdl_wrapper sdl_; | ||
121 | img_wrapper img_; | ||
122 | window_ptr win_; | ||
123 | renderer_ptr ren_; | ||
124 | }; | ||
125 | |||
126 | #endif /* end of include guard: RENDERER_H_6A58EC30 */ | 111 | #endif /* end of include guard: RENDERER_H_6A58EC30 */ |
diff --git a/src/simulation.cpp b/src/simulation.cpp index a43a4e8..317e010 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp | |||
@@ -233,6 +233,49 @@ void Simulation::tick( | |||
233 | } | 233 | } |
234 | } | 234 | } |
235 | 235 | ||
236 | void Simulation::render(SDL_Renderer* ren) | ||
237 | { | ||
238 | for (size_t y = 0; y < level_.getSize().h(); y++) | ||
239 | { | ||
240 | for (size_t x = 0; x < level_.getSize().w(); x++) | ||
241 | { | ||
242 | int val = 255 - level_.at(x, y) * 10; | ||
243 | |||
244 | SDL_SetRenderDrawColor(ren, val, val, val, 255); | ||
245 | |||
246 | SDL_Rect rect { | ||
247 | static_cast<int>(x * TILE_SIZE.w()), | ||
248 | static_cast<int>(y * TILE_SIZE.h()), | ||
249 | TILE_SIZE.w(), | ||
250 | TILE_SIZE.h() | ||
251 | }; | ||
252 | |||
253 | SDL_RenderFillRect(ren, &rect); | ||
254 | } | ||
255 | } | ||
256 | |||
257 | constexpr Layer renderOrder[] = { Layer::track, Layer::object }; | ||
258 | |||
259 | ranges::for_each( | ||
260 | ranges::view::for_each( | ||
261 | renderOrder, | ||
262 | [&] (Layer layer) { | ||
263 | return entityRange() | views::isOnLayer(layer); | ||
264 | }), | ||
265 | [&] (const Entity& entity) { | ||
266 | SDL_SetRenderDrawColor(ren, entity.colorVal, entity.colorVal, 65, 255); | ||
267 | |||
268 | SDL_Rect rect { | ||
269 | static_cast<int>(entity.pos.x()), | ||
270 | static_cast<int>(entity.pos.y()), | ||
271 | static_cast<int>(entity.size.w()), | ||
272 | static_cast<int>(entity.size.h()) | ||
273 | }; | ||
274 | |||
275 | SDL_RenderFillRect(ren, &rect); | ||
276 | }); | ||
277 | } | ||
278 | |||
236 | id_type Simulation::emplaceEntity() | 279 | id_type Simulation::emplaceEntity() |
237 | { | 280 | { |
238 | id_type nextId; | 281 | id_type nextId; |
diff --git a/src/simulation.h b/src/simulation.h index abff3da..1502a70 100644 --- a/src/simulation.h +++ b/src/simulation.h | |||
@@ -1,8 +1,8 @@ | |||
1 | #ifndef SIMULATION_H_7BF6EEA4 | 1 | #ifndef SIMULATION_H_7BF6EEA4 |
2 | #define SIMULATION_H_7BF6EEA4 | 2 | #define SIMULATION_H_7BF6EEA4 |
3 | 3 | ||
4 | #include "state.h" | ||
4 | #include "entity.h" | 5 | #include "entity.h" |
5 | #include "renderer.h" | ||
6 | #include "schedule.h" | 6 | #include "schedule.h" |
7 | #include <range/v3/all.hpp> | 7 | #include <range/v3/all.hpp> |
8 | #include <vector> | 8 | #include <vector> |
@@ -11,7 +11,7 @@ | |||
11 | 11 | ||
12 | class Level; | 12 | class Level; |
13 | 13 | ||
14 | class Simulation { | 14 | class Simulation : public State { |
15 | public: | 15 | public: |
16 | 16 | ||
17 | // Constructor | 17 | // Constructor |
@@ -19,7 +19,9 @@ public: | |||
19 | 19 | ||
20 | void tick( | 20 | void tick( |
21 | double dt, | 21 | double dt, |
22 | const Uint8* keystate); | 22 | const Uint8* keystate) override; |
23 | |||
24 | void render(SDL_Renderer* ren) override; | ||
23 | 25 | ||
24 | id_type emplaceEntity(); | 26 | id_type emplaceEntity(); |
25 | 27 | ||
diff --git a/src/state.h b/src/state.h new file mode 100644 index 0000000..3c3cfb4 --- /dev/null +++ b/src/state.h | |||
@@ -0,0 +1,19 @@ | |||
1 | #ifndef STATE_H_86BFEF59 | ||
2 | #define STATE_H_86BFEF59 | ||
3 | |||
4 | #include "renderer.h" | ||
5 | |||
6 | class State { | ||
7 | public: | ||
8 | |||
9 | virtual ~State() = default; | ||
10 | |||
11 | virtual void tick( | ||
12 | double dt, | ||
13 | const Uint8* keystate) = 0; | ||
14 | |||
15 | virtual void render(SDL_Renderer* ren) = 0; | ||
16 | |||
17 | }; | ||
18 | |||
19 | #endif /* end of include guard: STATE_H_86BFEF59 */ | ||