diff options
author | 3gg <3gg@shellblade.net> | 2024-03-09 08:43:26 -0800 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2024-03-09 08:43:26 -0800 |
commit | adbd2511beec8f1caa1752bdfd755cc2f62ba425 (patch) | |
tree | 8fde167e9d9951b43e571a2417ae55f9572bea28 /gfx-iso/demos/checkerboard | |
parent | 4bc4ca2796bd434880b77d3c4bcbb56107456777 (diff) |
Make isogfx a library instead of an executable.
Diffstat (limited to 'gfx-iso/demos/checkerboard')
-rw-r--r-- | gfx-iso/demos/checkerboard/CMakeLists.txt | 15 | ||||
-rw-r--r-- | gfx-iso/demos/checkerboard/checkerboard.c | 114 |
2 files changed, 129 insertions, 0 deletions
diff --git a/gfx-iso/demos/checkerboard/CMakeLists.txt b/gfx-iso/demos/checkerboard/CMakeLists.txt new file mode 100644 index 0000000..f178262 --- /dev/null +++ b/gfx-iso/demos/checkerboard/CMakeLists.txt | |||
@@ -0,0 +1,15 @@ | |||
1 | cmake_minimum_required(VERSION 3.0) | ||
2 | |||
3 | project(checkerboard) | ||
4 | |||
5 | set(CMAKE_C_STANDARD 17) | ||
6 | set(CMAKE_C_STANDARD_REQUIRED On) | ||
7 | set(CMAKE_C_EXTENSIONS Off) | ||
8 | |||
9 | add_executable(checkerboard | ||
10 | checkerboard.c) | ||
11 | |||
12 | target_link_libraries(checkerboard PRIVATE | ||
13 | isogfx-app) | ||
14 | |||
15 | target_compile_options(checkerboard PRIVATE -Wall -Wextra -Wpedantic) | ||
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 | |||
8 | static const int TILE_WIDTH = 64; | ||
9 | static const int TILE_HEIGHT = TILE_WIDTH / 2; | ||
10 | static const int WORLD_WIDTH = 20; | ||
11 | static const int WORLD_HEIGHT = 20; | ||
12 | |||
13 | static 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 | |||
28 | typedef enum Colour { | ||
29 | Black, | ||
30 | White, | ||
31 | Red, | ||
32 | } Colour; | ||
33 | |||
34 | typedef struct IsoGfxAppState { | ||
35 | Tile red; | ||
36 | int xpick; | ||
37 | int ypick; | ||
38 | } IsoGfxAppState; | ||
39 | |||
40 | static 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 | |||
52 | static 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 | |||
74 | static void shutdown(IsoGfxAppState* state, IsoGfx* iso) { | ||
75 | assert(state); | ||
76 | assert(iso); | ||
77 | } | ||
78 | |||
79 | static 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 | |||
91 | static 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 | |||
102 | int 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 | } | ||