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 /src/renderer.cpp | |
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.
Diffstat (limited to 'src/renderer.cpp')
-rw-r--r-- | src/renderer.cpp | 103 |
1 files changed, 0 insertions, 103 deletions
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 | } | ||