summary refs log tree commit diff stats
path: root/src/level.cpp
blob: e22a43f029bc234998fd51bb46521338c63d3873 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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,
      LEVEL_SIZE.w(),
      LEVEL_SIZE.h()));

  SDL_SetRenderTarget(ren, canvas.get());
  SDL_SetTextureBlendMode(canvas.get(), SDL_BLENDMODE_BLEND);
  SDL_SetRenderDrawColor(ren, 0, 0, 0, 0);
  SDL_RenderFillRect(ren, nullptr);

  for (size_t y = 0; y < size_.h(); y++)
  {
    for (size_t x = 0; x < size_.w(); x++)
    {
      SDL_Rect src {
        0,
        static_cast<int>(at(x, y, layer) * TILE_SIZE.h()),
        TILE_SIZE.w(),
        TILE_SIZE.h()
      };

      SDL_Rect dest {
        static_cast<int>(x * TILE_SIZE.w()),
        static_cast<int>(y * TILE_SIZE.h()),
        TILE_SIZE.w(),
        TILE_SIZE.h()
      };

      SDL_RenderCopy(
        ren,
        getTileset(layer).getImage().get(),
        &src,
        &dest);
    }
  }

  SDL_SetRenderTarget(ren, nullptr);

  return canvas;
}