diff options
Diffstat (limited to 'src/renderer.cpp')
| -rw-r--r-- | src/renderer.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
| diff --git a/src/renderer.cpp b/src/renderer.cpp new file mode 100644 index 0000000..2916dd6 --- /dev/null +++ b/src/renderer.cpp | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | #include "renderer.h" | ||
| 2 | #include "simulation.h" | ||
| 3 | #include "consts.h" | ||
| 4 | #include "level.h" | ||
| 5 | |||
| 6 | Renderer::Renderer() | ||
| 7 | { | ||
| 8 | win_ = window_ptr( | ||
| 9 | SDL_CreateWindow( | ||
| 10 | "dispatcher", | ||
| 11 | 100, | ||
| 12 | 100, | ||
| 13 | WINDOW_SIZE.w(), | ||
| 14 | WINDOW_SIZE.h(), | ||
| 15 | SDL_WINDOW_SHOWN)); | ||
| 16 | |||
| 17 | if (!win_) | ||
| 18 | { | ||
| 19 | throw sdl_error(); | ||
| 20 | } | ||
| 21 | |||
| 22 | ren_ = renderer_ptr( | ||
| 23 | SDL_CreateRenderer( | ||
| 24 | win_.get(), | ||
| 25 | -1, | ||
| 26 | SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)); | ||
| 27 | |||
| 28 | if (!ren_) | ||
| 29 | { | ||
| 30 | throw sdl_error(); | ||
| 31 | } | ||
| 32 | |||
| 33 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD); | ||
| 34 | SDL_SetRenderDrawColor(ren_.get(), 255, 150, 255, 255); | ||
| 35 | SDL_RenderFillRect(ren_.get(), nullptr); | ||
| 36 | } | ||
| 37 | |||
| 38 | void Renderer::render(const Simulation& sim) | ||
| 39 | { | ||
| 40 | texture_ptr canvas( | ||
| 41 | SDL_CreateTexture( | ||
| 42 | ren_.get(), | ||
| 43 | SDL_PIXELFORMAT_RGBA8888, | ||
| 44 | SDL_TEXTUREACCESS_TARGET, | ||
| 45 | WINDOW_SIZE.w(), | ||
| 46 | WINDOW_SIZE.h())); | ||
| 47 | |||
| 48 | if (!canvas) | ||
| 49 | { | ||
| 50 | throw sdl_error(); | ||
| 51 | } | ||
| 52 | |||
| 53 | SDL_SetRenderTarget(ren_.get(), nullptr); | ||
| 54 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); | ||
| 55 | SDL_SetRenderDrawColor(ren_.get(), 255, 150, rand() % 255, 255); | ||
| 56 | SDL_RenderClear(ren_.get()); | ||
| 57 | |||
| 58 | const Level& level = sim.getLevel(); | ||
| 59 | |||
| 60 | for (size_t y = 0; y < level.getSize().h(); y++) | ||
| 61 | { | ||
| 62 | for (size_t x = 0; x < level.getSize().w(); x++) | ||
| 63 | { | ||
| 64 | int val = level.at(x, y) * 10; | ||
| 65 | |||
| 66 | SDL_SetRenderDrawColor(ren_.get(), val, val, 0, 255); | ||
| 67 | |||
| 68 | SDL_Rect rect { | ||
| 69 | static_cast<int>(x * TILE_SIZE.w()), | ||
| 70 | static_cast<int>(y * TILE_SIZE.h()), | ||
| 71 | TILE_SIZE.w(), | ||
| 72 | TILE_SIZE.h() | ||
| 73 | }; | ||
| 74 | |||
| 75 | SDL_RenderFillRect(ren_.get(), &rect); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | for (Simulation::id_type id : sim.getActive()) | ||
| 80 | { | ||
| 81 | const Entity& entity = sim.getEntity(id); | ||
| 82 | |||
| 83 | SDL_SetRenderDrawColor(ren_.get(), 100, 100, 100, 255); | ||
| 84 | |||
| 85 | SDL_Rect rect { | ||
| 86 | static_cast<int>(entity.pos.x()), | ||
| 87 | static_cast<int>(entity.pos.y()), | ||
| 88 | static_cast<int>(entity.size.w()), | ||
| 89 | static_cast<int>(entity.size.h()) | ||
| 90 | }; | ||
| 91 | |||
| 92 | SDL_RenderFillRect(ren_.get(), &rect); | ||
| 93 | } | ||
| 94 | |||
| 95 | //SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); | ||
| 96 | SDL_RenderPresent(ren_.get()); | ||
| 97 | } | ||
