summaryrefslogtreecommitdiff
path: root/gfx-iso/app/checkerboard.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2023-07-08 14:37:29 -0700
committer3gg <3gg@shellblade.net>2023-07-08 14:37:29 -0700
commit21a0d0c1c424f7db90c3282aad4bf6ad4ef809b7 (patch)
treea6ae8a8cb4108cd33713178e67d3b482fc1fd5ee /gfx-iso/app/checkerboard.c
parent303f5dc58dd8e8266df3c62fc84d9799db8047b9 (diff)
Load tile maps and tile sets from files.
Diffstat (limited to 'gfx-iso/app/checkerboard.c')
-rw-r--r--gfx-iso/app/checkerboard.c120
1 files changed, 120 insertions, 0 deletions
diff --git a/gfx-iso/app/checkerboard.c b/gfx-iso/app/checkerboard.c
new file mode 100644
index 0000000..8b394c4
--- /dev/null
+++ b/gfx-iso/app/checkerboard.c
@@ -0,0 +1,120 @@
1#include "isogfx-demo.h"
2
3#include <gfx/gfx_app.h>
4#include <isogfx/isogfx.h>
5
6#include <assert.h>
7#include <stdbool.h>
8#include <stdio.h>
9#include <stdlib.h>
10
11static const int TILE_WIDTH = 64;
12static const int TILE_HEIGHT = TILE_WIDTH / 2;
13static const int WORLD_WIDTH = 20;
14static const int WORLD_HEIGHT = 20;
15
16static const TileDesc tile_set[] = {
17 {.type = TileFromColour,
18 .width = TILE_WIDTH,
19 .height = TILE_HEIGHT,
20 .colour = (Pixel){.r = 0x38, .g = 0x3b, .b = 0x46, .a = 0xff}},
21 {.type = TileFromColour,
22 .width = TILE_WIDTH,
23 .height = TILE_HEIGHT,
24 .colour = (Pixel){.r = 0xA5, .g = 0xb3, .b = 0xc0, .a = 0xff}},
25 {.type = TileFromColour,
26 .width = TILE_WIDTH,
27 .height = TILE_HEIGHT,
28 .colour = (Pixel){.r = 0xdc, .g = 0x76, .b = 0x84, .a = 0xff}},
29};
30
31typedef enum Colour {
32 Black,
33 White,
34 Red,
35} Colour;
36
37typedef struct State {
38 Tile red;
39 int xpick;
40 int ypick;
41} State;
42
43static void make_checkerboard(IsoGfx* iso, Tile black, Tile white) {
44 assert(iso);
45 for (int y = 0; y < isogfx_world_height(iso); ++y) {
46 for (int x = 0; x < isogfx_world_width(iso); ++x) {
47 const int odd_col = x & 1;
48 const int odd_row = y & 1;
49 const Tile value = (odd_row ^ odd_col) == 0 ? black : white;
50 isogfx_set_tile(iso, x, y, value);
51 }
52 }
53}
54
55static void shutdown(IsoGfx* iso, void* app_state) {
56 assert(iso);
57 if (app_state) {
58 free(app_state);
59 }
60}
61
62static void update(IsoGfx* iso, void* app_state, double t, double dt) {
63 assert(iso);
64 assert(app_state);
65 State* state = (State*)(app_state);
66
67 double mouse_x, mouse_y;
68 gfx_app_get_mouse_position(&mouse_x, &mouse_y);
69
70 isogfx_pick_tile(iso, mouse_x, mouse_y, &state->xpick, &state->ypick);
71
72 printf("Picked tile: (%d, %d)\n", state->xpick, state->ypick);
73}
74
75static void render(IsoGfx* iso, void* app_state) {
76 assert(iso);
77 assert(app_state);
78 State* state = (State*)(app_state);
79
80 isogfx_render(iso);
81 if ((state->xpick != -1) && (state->ypick != -1)) {
82 isogfx_draw_tile(iso, state->xpick, state->ypick, state->red);
83 }
84}
85
86bool make_checkerboard_app(IsoGfx* iso, IsoGfxApp* app) {
87 assert(iso);
88 assert(app);
89
90 State* state = calloc(1, sizeof(State));
91 if (!state) {
92 return false;
93 }
94
95 if (!isogfx_make_world(
96 iso, &(WorldDesc){
97 .tile_width = TILE_WIDTH,
98 .tile_height = TILE_HEIGHT,
99 .world_width = WORLD_WIDTH,
100 .world_height = WORLD_HEIGHT})) {
101 goto cleanup;
102 }
103
104 const Tile black = isogfx_make_tile(iso, &tile_set[Black]);
105 const Tile white = isogfx_make_tile(iso, &tile_set[White]);
106 state->red = isogfx_make_tile(iso, &tile_set[Red]);
107 make_checkerboard(iso, black, white);
108 isogfx_render(iso);
109
110 app->state = state;
111 app->shutdown = shutdown;
112 app->update = update;
113 app->render = render;
114
115 return true;
116
117cleanup:
118 free(state);
119 return false;
120}