summaryrefslogtreecommitdiff
path: root/gfx-iso/src/isogfx.c
blob: b38efe75b300097e6cebc70b0109968de5766abc (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <isogfx/isogfx.h>

#include <mempool.h>

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

/// Maximum number of tiles unless the user chooses a non-zero value.
#define DEFAULT_MAX_NUM_TILES 1024

typedef struct TileData {
  Pixel pixels[1]; // Dynamically allocated.
} TileData;

DEF_MEMPOOL_DYN(TilePool, TileData)

typedef struct IsoGfx {
  Tile*    world;
  Pixel*   screen;
  uint8_t* tile_mask;
  TilePool tiles;
  int      screen_width;
  int      screen_height;
  int      tile_width;
  int      tile_height;
  int      world_width;
  int      world_height;
  int      max_num_tiles;
} IsoGfx;

typedef struct ivec2 {
  int x, y;
} ivec2;

typedef struct vec2 {
  double x, y;
} vec2;

static inline ivec2 ivec2_add(ivec2 a, ivec2 b) {
  return (ivec2){.x = a.x + b.x, .y = a.y + b.y};
}

static inline ivec2 ivec2_scale(ivec2 a, int s) {
  return (ivec2){.x = a.x * s, .y = a.y * s};
}

static inline ivec2 iso2cart(ivec2 iso, int s, int t, int w) {
  return (ivec2){
      .x = (iso.x - iso.y) * (s / 2) + (w / 2), .y = (iso.x + iso.y) * (t / 2)};
}

// Method 1.
// static inline vec2 cart2iso(vec2 cart, int s, int t, int w) {
//  const double x    = cart.x - (double)(w / 2);
//  const double xiso = (x * t + cart.y * s) / (double)(s * t);
//  return (vec2){
//      .x = (int)(xiso), .y = (int)((2.0 / (double)t) * cart.y - xiso)};
//}

// Method 2.
static inline vec2 cart2iso(vec2 cart, int s, int t, int w) {
  const double one_over_s = 1. / (double)s;
  const double one_over_t = 1. / (double)t;
  const double x          = cart.x - (double)(w / 2);
  return (vec2){
      .x = (one_over_s * x + one_over_t * cart.y),
      .y = (-one_over_s * x + one_over_t * cart.y)};
}

Pixel* tile_xy_mut(const IsoGfx* iso, TileData* tile, int x, int y) {
  assert(iso);
  assert(tile);
  assert(tile->pixels);
  assert(x >= 0);
  assert(y >= 0);
  assert(x < iso->tile_width);
  assert(y < iso->tile_height);
  return &tile->pixels[y * iso->tile_width + x];
}

Pixel tile_xy(const IsoGfx* iso, const TileData* tile, int x, int y) {
  assert(iso);
  assert(tile);
  assert(tile->pixels);
  assert(x >= 0);
  assert(y >= 0);
  assert(x < iso->tile_width);
  assert(y < iso->tile_height);
  return tile->pixels[y * iso->tile_width + x];
}

static inline Tile world_xy(IsoGfx* iso, int x, int y) {
  assert(iso);
  assert(x >= 0);
  assert(y >= 0);
  assert(x < iso->world_width);
  assert(y < iso->world_height);
  return iso->world[y * iso->world_width + x];
}

static inline Tile* world_xy_mut(IsoGfx* iso, int x, int y) {
  assert(iso);
  assert(x >= 0);
  assert(y >= 0);
  assert(x < iso->world_width);
  assert(y < iso->world_height);
  return &iso->world[y * iso->world_width + x];
}

static inline Pixel screen_xy(IsoGfx* iso, int x, int y) {
  assert(iso);
  assert(x >= 0);
  assert(y >= 0);
  assert(x < iso->screen_width);
  assert(y < iso->screen_height);
  return iso->screen[y * iso->screen_width + x];
}

static inline Pixel* screen_xy_mut(IsoGfx* iso, int x, int y) {
  assert(iso);
  assert(x >= 0);
  assert(y >= 0);
  assert(x < iso->screen_width);
  assert(y < iso->screen_height);
  return &iso->screen[y * iso->screen_width + x];
}

static void draw_tile(IsoGfx* iso, ivec2 origin, Tile tile) {
  assert(iso);

  const TileData* data = mempool_get_block(&iso->tiles, tile);
  assert(data);

  for (int py = 0; py < iso->tile_height; ++py) {
    for (int px = 0; px < iso->tile_width; ++px) {
      const Pixel colour = tile_xy(iso, data, px, py);
      const int   sx     = origin.x + px;
      const int   sy     = origin.y + py;
      if ((sx >= 0) && (sy >= 0) && (sx < iso->screen_width) &&
          (sy < iso->screen_height)) {
        const uint8_t mask = iso->tile_mask[py * iso->tile_width + px];
        if (mask == 1) {
          *screen_xy_mut(iso, sx, sy) = colour;
        }
      }
    }
  }
}

static void draw(IsoGfx* iso) {
  assert(iso);

  const int W = iso->screen_width;
  const int H = iso->screen_height;

  memset(iso->screen, 0, W * H * sizeof(Pixel));

  const ivec2 o = {(iso->screen_width / 2) - (iso->tile_width / 2), 0};
  const ivec2 x = {.x = iso->tile_width / 2, .y = iso->tile_height / 2};
  const ivec2 y = {.x = -iso->tile_width / 2, .y = iso->tile_height / 2};

  // TODO: Culling.
  // Ex: map the screen corners to tile space to cull.
  // Ex: walk in screen space and fetch the tile.
  // The tile-centric approach might be more cache-friendly, however, since the
  // screen-centric approach would juggle multiple tiles throughout the scan.
  for (int ty = 0; ty < iso->world_height; ++ty) {
    for (int tx = 0; tx < iso->world_width; ++tx) {
      const Tile  tile = world_xy(iso, tx, ty);
      const ivec2 so =
          ivec2_add(o, ivec2_add(ivec2_scale(x, tx), ivec2_scale(y, ty)));
      draw_tile(iso, so, tile);
    }
  }
}

/// Creates a tile mask procedurally.
static void make_tile_mask(IsoGfx* iso) {
  assert(iso);
  assert(iso->tile_mask);

  for (int y = 0; y < iso->tile_height / 2; ++y) {
    const int mask_start = iso->tile_width / 2 - 2 * y - 1;
    const int mask_end   = iso->tile_width / 2 + 2 * y + 1;
    for (int x = 0; x < iso->tile_width; ++x) {
      const bool    masked = (mask_start <= x) && (x <= mask_end);
      const uint8_t val    = masked ? 1 : 0;

      // Top half.
      iso->tile_mask[y * iso->tile_width + x] = val;

      // Bottom half reflects the top half.
      const int y_reflected = iso->tile_height - y - 1;
      iso->tile_mask[y_reflected * iso->tile_width + x] = val;
    }
  }
}

/// Creates a tile with a constant colour.
static void make_tile_from_colour(
    const IsoGfx* iso, Pixel colour, TileData* tile) {
  assert(iso);
  assert(tile);

  for (int y = 0; y < iso->tile_height; ++y) {
    for (int x = 0; x < iso->tile_width; ++x) {
      *tile_xy_mut(iso, tile, x, y) = colour;
    }
  }
}

IsoGfx* isogfx_new(const IsoGfxDesc* desc) {
  assert(desc->screen_width > 0);
  assert(desc->screen_height > 0);
  assert(desc->tile_width > 0);
  assert(desc->tile_height > 0);
  // Part of our implementation assumes even widths and heights for greater
  // precision.
  assert((desc->screen_width & 1) == 0);
  assert((desc->screen_height & 1) == 0);
  assert((desc->tile_width & 1) == 0);
  assert((desc->tile_height & 1) == 0);

  IsoGfx* iso = calloc(1, sizeof(IsoGfx));
  if (!iso) {
    return 0;
  }

  iso->screen_width  = desc->screen_width;
  iso->screen_height = desc->screen_height;
  iso->tile_width    = desc->tile_width;
  iso->tile_height   = desc->tile_height;
  iso->world_width   = desc->world_width;
  iso->world_height  = desc->world_height;
  iso->max_num_tiles =
      desc->max_num_tiles > 0 ? desc->max_num_tiles : DEFAULT_MAX_NUM_TILES;

  const int world_size  = desc->world_width * desc->world_height;
  const int screen_size = desc->screen_width * desc->screen_height;
  const int tile_size   = desc->tile_width * desc->tile_height;

  const int tile_size_bytes = tile_size * (int)sizeof(Pixel);

  if (!(iso->world = calloc(world_size, sizeof(Tile)))) {
    goto cleanup;
  }
  if (!(iso->screen = calloc(screen_size, sizeof(Pixel)))) {
    goto cleanup;
  }
  if (!(iso->tile_mask = calloc(tile_size, sizeof(uint8_t)))) {
    goto cleanup;
  }
  if (!mempool_make_dyn(&iso->tiles, iso->max_num_tiles, tile_size_bytes)) {
    goto cleanup;
  }

  make_tile_mask(iso);

  return iso;

cleanup:
  isogfx_del(&iso);
  return 0;
}

void isogfx_del(IsoGfx** pIso) {
  assert(pIso);
  IsoGfx* iso = *pIso;
  if (iso) {
    if (iso->world) {
      free(iso->world);
    }
    if (iso->screen) {
      free(iso->screen);
    }
    if (iso->tile_mask) {
      free(iso->tile_mask);
    }
    mempool_del(&iso->tiles);
    free(iso);
  }
}

Tile isogfx_make_tile(IsoGfx* iso, const TileDesc* desc) {
  assert(iso);
  assert(desc);

  TileData* tile = mempool_alloc(&iso->tiles);
  assert(tile); // TODO: Make this a hard assert.

  switch (desc->type) {
  case TileFromColour:
    make_tile_from_colour(iso, desc->colour, tile);
    break;
  case TileFromFile:
    assert(false); // TODO
    break;
  case TileFromMemory:
    assert(false); // TODO
    break;
  }

  return (Tile)mempool_get_block_index(&iso->tiles, tile);
}

void isogfx_set_tile(IsoGfx* iso, int x, int y, Tile tile) {
  assert(iso);
  *world_xy_mut(iso, x, y) = tile;
}

void isogfx_pick_tile(
    const IsoGfx* iso, double xcart, double ycart, int* xiso, int* yiso) {
  assert(iso);
  assert(xiso);
  assert(yiso);

  const vec2 xy_iso = cart2iso(
      (vec2){.x = xcart, .y = ycart}, iso->tile_width, iso->tile_height,
      iso->screen_width);

  if ((0 <= xy_iso.x) && (xy_iso.x < iso->world_width) && (0 <= xy_iso.y) &&
      (xy_iso.y < iso->world_height)) {
    *xiso = (int)xy_iso.x;
    *yiso = (int)xy_iso.y;
  } else {
    *xiso = -1;
    *yiso = -1;
  }
}

void isogfx_render(IsoGfx* iso) {
  assert(iso);
  draw(iso);
}

void isogfx_draw_tile(IsoGfx* iso, int x, int y, Tile tile) {
  assert(iso);
  assert(x >= 0);
  assert(y >= 0);
  assert(x < iso->world_width);
  assert(y < iso->world_height);

  const ivec2 o  = {(iso->screen_width / 2) - (iso->tile_width / 2), 0};
  const ivec2 vx = {.x = iso->tile_width / 2, .y = iso->tile_height / 2};
  const ivec2 vy = {.x = -iso->tile_width / 2, .y = iso->tile_height / 2};
  const ivec2 so =
      ivec2_add(o, ivec2_add(ivec2_scale(vx, x), ivec2_scale(vy, y)));

  draw_tile(iso, so, tile);
}

const Pixel* isogfx_get_screen_buffer(const IsoGfx* iso) {
  assert(iso);
  return iso->screen;
}

int isogfx_world_width(const IsoGfx* iso) {
  assert(iso);
  return iso->world_width;
}

int isogfx_world_height(const IsoGfx* iso) {
  assert(iso);
  return iso->world_height;
}