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