From 41593f09b47537ccae1b19dc390f7d7dbb532937 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 24 Feb 2019 12:50:55 -0500 Subject: Rendering of level map moved to Level class --- src/enums.h | 1 + src/level.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/level.h | 3 +++ src/simulation.cpp | 20 ++++---------------- src/simulation.h | 2 ++ 5 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 src/level.cpp (limited to 'src') 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 { }; enum class Layer { + map, track, object }; 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 @@ +#include "level.h" + +texture_ptr Level::render(SDL_Renderer* ren, Layer layer) const +{ + texture_ptr canvas( + SDL_CreateTexture( + ren, + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, + WINDOW_SIZE.w(), + WINDOW_SIZE.h())); + + SDL_SetRenderTarget(ren, canvas.get()); + + for (size_t y = 0; y < size_.h(); y++) + { + for (size_t x = 0; x < size_.w(); x++) + { + if (layer == Layer::map) + { + int val = 255 - at(x, y) * 10; + + SDL_SetRenderDrawColor(ren, val, val, val, 255); + + SDL_Rect rect { + static_cast(x * TILE_SIZE.w()), + static_cast(y * TILE_SIZE.h()), + TILE_SIZE.w(), + TILE_SIZE.h() + }; + + SDL_RenderFillRect(ren, &rect); + } + } + } + + SDL_SetRenderTarget(ren, nullptr); + + return canvas; +} 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 @@ #define LEVEL_H_678CFCCF #include +#include "renderer.h" #include "vector.h" #include "consts.h" #include "tileset.h" @@ -36,6 +37,8 @@ public: return tileset_; } + texture_ptr render(SDL_Renderer* ren, Layer layer) const; + private: 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( void Simulation::render(SDL_Renderer* ren) { - for (size_t y = 0; y < level_.getSize().h(); y++) + if (!renderedMap_) { - for (size_t x = 0; x < level_.getSize().w(); x++) - { - int val = 255 - level_.at(x, y) * 10; - - SDL_SetRenderDrawColor(ren, val, val, val, 255); - - SDL_Rect rect { - static_cast(x * TILE_SIZE.w()), - static_cast(y * TILE_SIZE.h()), - TILE_SIZE.w(), - TILE_SIZE.h() - }; - - SDL_RenderFillRect(ren, &rect); - } + renderedMap_ = level_.render(ren, Layer::map); } + SDL_RenderCopy(ren, renderedMap_.get(), nullptr, nullptr); + constexpr Layer renderOrder[] = { Layer::track, Layer::object }; 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: std::vector entities_; std::deque available_; std::set active_; + + texture_ptr renderedMap_; }; #endif /* end of include guard: SIMULATION_H_7BF6EEA4 */ -- cgit 1.4.1