diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-24 12:50:55 -0500 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-24 12:50:55 -0500 |
| commit | 41593f09b47537ccae1b19dc390f7d7dbb532937 (patch) | |
| tree | d46962995230a1ad97e28af7606782da81d724a1 | |
| parent | 90b9831d6fb89feedeec63cb392c4535c5df60fe (diff) | |
| download | dispatcher-41593f09b47537ccae1b19dc390f7d7dbb532937.tar.gz dispatcher-41593f09b47537ccae1b19dc390f7d7dbb532937.tar.bz2 dispatcher-41593f09b47537ccae1b19dc390f7d7dbb532937.zip | |
Rendering of level map moved to Level class
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/enums.h | 1 | ||||
| -rw-r--r-- | src/level.cpp | 40 | ||||
| -rw-r--r-- | src/level.h | 3 | ||||
| -rw-r--r-- | src/simulation.cpp | 20 | ||||
| -rw-r--r-- | src/simulation.h | 2 |
6 files changed, 51 insertions, 16 deletions
| diff --git a/CMakeLists.txt b/CMakeLists.txt index a1456a6..884bb43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
| @@ -24,6 +24,7 @@ add_executable(dispatcher | |||
| 24 | src/simulation.cpp | 24 | src/simulation.cpp |
| 25 | src/direction.cpp | 25 | src/direction.cpp |
| 26 | src/editor.cpp | 26 | src/editor.cpp |
| 27 | src/level.cpp | ||
| 27 | ) | 28 | ) |
| 28 | 29 | ||
| 29 | set_property(TARGET dispatcher PROPERTY CXX_STANDARD 17) | 30 | set_property(TARGET dispatcher PROPERTY CXX_STANDARD 17) |
| diff --git a/src/enums.h b/src/enums.h index e2056f5..69a50bc 100644 --- a/src/enums.h +++ b/src/enums.h | |||
| @@ -9,6 +9,7 @@ enum class ColliderType { | |||
| 9 | }; | 9 | }; |
| 10 | 10 | ||
| 11 | enum class Layer { | 11 | enum class Layer { |
| 12 | map, | ||
| 12 | track, | 13 | track, |
| 13 | object | 14 | object |
| 14 | }; | 15 | }; |
| diff --git a/src/level.cpp b/src/level.cpp new file mode 100644 index 0000000..b97eb70 --- /dev/null +++ b/src/level.cpp | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | #include "level.h" | ||
| 2 | |||
| 3 | texture_ptr Level::render(SDL_Renderer* ren, Layer layer) const | ||
| 4 | { | ||
| 5 | texture_ptr canvas( | ||
| 6 | SDL_CreateTexture( | ||
| 7 | ren, | ||
| 8 | SDL_PIXELFORMAT_RGBA8888, | ||
| 9 | SDL_TEXTUREACCESS_TARGET, | ||
| 10 | WINDOW_SIZE.w(), | ||
| 11 | WINDOW_SIZE.h())); | ||
| 12 | |||
| 13 | SDL_SetRenderTarget(ren, canvas.get()); | ||
| 14 | |||
| 15 | for (size_t y = 0; y < size_.h(); y++) | ||
| 16 | { | ||
| 17 | for (size_t x = 0; x < size_.w(); x++) | ||
| 18 | { | ||
| 19 | if (layer == Layer::map) | ||
| 20 | { | ||
| 21 | int val = 255 - at(x, y) * 10; | ||
| 22 | |||
| 23 | SDL_SetRenderDrawColor(ren, val, val, val, 255); | ||
| 24 | |||
| 25 | SDL_Rect rect { | ||
| 26 | static_cast<int>(x * TILE_SIZE.w()), | ||
| 27 | static_cast<int>(y * TILE_SIZE.h()), | ||
| 28 | TILE_SIZE.w(), | ||
| 29 | TILE_SIZE.h() | ||
| 30 | }; | ||
| 31 | |||
| 32 | SDL_RenderFillRect(ren, &rect); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | SDL_SetRenderTarget(ren, nullptr); | ||
| 38 | |||
| 39 | return canvas; | ||
| 40 | } | ||
| diff --git a/src/level.h b/src/level.h index 1ca4970..bc05837 100644 --- a/src/level.h +++ b/src/level.h | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | #define LEVEL_H_678CFCCF | 2 | #define LEVEL_H_678CFCCF |
| 3 | 3 | ||
| 4 | #include <vector> | 4 | #include <vector> |
| 5 | #include "renderer.h" | ||
| 5 | #include "vector.h" | 6 | #include "vector.h" |
| 6 | #include "consts.h" | 7 | #include "consts.h" |
| 7 | #include "tileset.h" | 8 | #include "tileset.h" |
| @@ -36,6 +37,8 @@ public: | |||
| 36 | return tileset_; | 37 | return tileset_; |
| 37 | } | 38 | } |
| 38 | 39 | ||
| 40 | texture_ptr render(SDL_Renderer* ren, Layer layer) const; | ||
| 41 | |||
| 39 | private: | 42 | private: |
| 40 | 43 | ||
| 41 | vec2s size_; | 44 | vec2s size_; |
| diff --git a/src/simulation.cpp b/src/simulation.cpp index 317e010..4d8ec02 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp | |||
| @@ -235,25 +235,13 @@ void Simulation::tick( | |||
| 235 | 235 | ||
| 236 | void Simulation::render(SDL_Renderer* ren) | 236 | void Simulation::render(SDL_Renderer* ren) |
| 237 | { | 237 | { |
| 238 | for (size_t y = 0; y < level_.getSize().h(); y++) | 238 | if (!renderedMap_) |
| 239 | { | 239 | { |
| 240 | for (size_t x = 0; x < level_.getSize().w(); x++) | 240 | renderedMap_ = level_.render(ren, Layer::map); |
| 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 | } | 241 | } |
| 256 | 242 | ||
| 243 | SDL_RenderCopy(ren, renderedMap_.get(), nullptr, nullptr); | ||
| 244 | |||
| 257 | constexpr Layer renderOrder[] = { Layer::track, Layer::object }; | 245 | constexpr Layer renderOrder[] = { Layer::track, Layer::object }; |
| 258 | 246 | ||
| 259 | ranges::for_each( | 247 | ranges::for_each( |
| diff --git a/src/simulation.h b/src/simulation.h index 1502a70..bb77806 100644 --- a/src/simulation.h +++ b/src/simulation.h | |||
| @@ -87,6 +87,8 @@ private: | |||
| 87 | std::vector<Entity> entities_; | 87 | std::vector<Entity> entities_; |
| 88 | std::deque<id_type> available_; | 88 | std::deque<id_type> available_; |
| 89 | std::set<id_type> active_; | 89 | std::set<id_type> active_; |
| 90 | |||
| 91 | texture_ptr renderedMap_; | ||
| 90 | }; | 92 | }; |
| 91 | 93 | ||
| 92 | #endif /* end of include guard: SIMULATION_H_7BF6EEA4 */ | 94 | #endif /* end of include guard: SIMULATION_H_7BF6EEA4 */ |
