aboutsummaryrefslogtreecommitdiff
path: root/src/sdl/draw.c
blob: ab4803ec4c6d6807d3bf2e7799fc97fba9f6d531 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <stdbool.h> // bool
#include "util.h" // warn()/die()
#include "color.h" // set_color()
#include "draw.h"
#include "sprites.h"

#define DRAW_SPRITES

static size_t
get_cell_size(
  const draw_ctx_t * const draw_ctx
) {
  return 32 + 8 * *draw_ctx->zoom;
}

static SDL_Rect
get_cell_rect(
  const draw_ctx_t * const draw_ctx,
  const sok_pos_t pos
) {
  const size_t cell_size = get_cell_size(draw_ctx);

  return (SDL_Rect) {
    draw_ctx->render_ofs_x + pos.x * cell_size,
    draw_ctx->render_ofs_y + pos.y * cell_size,
    cell_size,
    cell_size
  };
}

static void
draw_cell(
  draw_ctx_t * const draw_ctx,
  const sok_pos_t pos,
  const sprite_t sprite
) {
  const SDL_Rect rect = get_cell_rect(draw_ctx, pos);

#ifdef DRAW_SPRITES
  SDL_Texture *tex = draw_ctx->sprites[sprite];
  if (SDL_RenderCopy(draw_ctx->renderer, tex, NULL, &rect)) {
    die("SDL_RenderCopy(): %s", SDL_GetError());
  }
#else
  if (SDL_RenderFillRect(draw_ctx->renderer, &rect)) {
    die("SDL_RenderFillRect(): %s", SDL_GetError());
  }
#endif /* DRAW_SPRITES */
}

static bool
draw_on_size(
  const sok_ctx_t * const ctx,
  const sok_pos_t level_size,
  void * const data
) {
  draw_ctx_t * const draw_ctx = data;
  const size_t cell_size = get_cell_size(draw_ctx);

  // get renderer size
  int renderer_x, renderer_y;
  if (SDL_GetRendererOutputSize(draw_ctx->renderer, &renderer_x, &renderer_y)) {
    die("SDL_GetRendererOutputSize(): %s", SDL_GetError());
  }

  // calculate renderer offset
  draw_ctx->render_ofs_x = (renderer_x - level_size.x * cell_size) / 2;
  draw_ctx->render_ofs_y = (renderer_y - level_size.y * cell_size) / 2;

#ifdef DRAW_SPRITES
  for (size_t y = 0; y < level_size.y; y++) {
    for (size_t x = 0; x < level_size.x; x++) {
      const sok_pos_t pos = { x, y };
      draw_cell(draw_ctx, pos, SPRITE_FLOOR);
    }
  }
#endif /* DRAW_SPRITES */

  return true;
}

static bool
draw_on_walls_start(
  const sok_ctx_t * const ctx,
  void * const data
) {
  draw_ctx_t * const draw_ctx = data;
  set_color(draw_ctx->renderer, COLOR_WALL);

  return true;
}

static bool
draw_on_wall(
  const sok_ctx_t * const ctx,
  const sok_pos_t pos,
  void * const data
) {
  draw_ctx_t * const draw_ctx = data;
  draw_cell(draw_ctx, pos, SPRITE_WALL);
  return true;
}

static bool
draw_on_goals_start(
  const sok_ctx_t * const ctx,
  void * const data
) {
  draw_ctx_t * const draw_ctx = data;
  set_color(draw_ctx->renderer, COLOR_GOAL);

  return true;
}

static bool
draw_on_goal(
  const sok_ctx_t * const ctx,
  const sok_pos_t pos,
  const bool has_player,
  const bool has_box,
  void * const data
) {
  draw_ctx_t * const draw_ctx = data;

  draw_cell(draw_ctx, pos, SPRITE_GOAL);

  return true;
}

static bool
draw_on_home(
  const sok_ctx_t * const ctx,
  const sok_pos_t pos,
  const bool has_goal,
  void * const data
) {
  draw_ctx_t * const draw_ctx = data;

  set_color(draw_ctx->renderer, has_goal ? COLOR_HOME_GOAL : COLOR_HOME);
  draw_cell(draw_ctx, pos, SPRITE_HOME);

  return true;
}

static bool
draw_on_box(
  const sok_ctx_t * const ctx,
  const sok_pos_t pos,
  const bool has_goal,
  void * const data
) {
  draw_ctx_t * const draw_ctx = data;

  set_color(draw_ctx->renderer, has_goal ? COLOR_BOX_GOAL : COLOR_BOX);
  draw_cell(draw_ctx, pos, SPRITE_BOX);

  return true;
}

static const sok_ctx_walk_cbs_t
DRAW_CBS = {
  .on_size        = draw_on_size,
  .on_walls_start = draw_on_walls_start,
  .on_wall        = draw_on_wall,
  .on_goals_start = draw_on_goals_start,
  .on_goal        = draw_on_goal,
  .on_home        = draw_on_home,
  .on_box         = draw_on_box,
};

void
draw(
  draw_ctx_t * const draw_ctx
) {
  // clear background
  set_color(draw_ctx->renderer, sok_ctx_is_done(draw_ctx->ctx) ? COLOR_BG_WON : COLOR_BG);
  SDL_RenderClear(draw_ctx->renderer);

  // render
  sok_ctx_walk(draw_ctx->ctx, &DRAW_CBS, draw_ctx);

    // flip
  SDL_RenderPresent(draw_ctx->renderer);
}