summaryrefslogtreecommitdiff
path: root/gfx-iso/demos/checkerboard/checkerboard.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-08-31 18:58:39 -0700
committer3gg <3gg@shellblade.net>2024-08-31 18:58:39 -0700
commitdaf6262c029892212f6b9b4014887c2c37e9ef75 (patch)
tree40b24f4f23cc48e55c3511c28c7bef29834d61b3 /gfx-iso/demos/checkerboard/checkerboard.c
parent0a78a9d9c1ac2090da56f395d6f2394a8a210010 (diff)
Handle resizing.
Diffstat (limited to 'gfx-iso/demos/checkerboard/checkerboard.c')
-rw-r--r--gfx-iso/demos/checkerboard/checkerboard.c94
1 files changed, 70 insertions, 24 deletions
diff --git a/gfx-iso/demos/checkerboard/checkerboard.c b/gfx-iso/demos/checkerboard/checkerboard.c
index e684bc3..0f01b72 100644
--- a/gfx-iso/demos/checkerboard/checkerboard.c
+++ b/gfx-iso/demos/checkerboard/checkerboard.c
@@ -1,10 +1,20 @@
1#include <isogfx/app.h> 1#include <isogfx/backend.h>
2#include <isogfx/isogfx.h> 2#include <isogfx/isogfx.h>
3 3
4#include <gfx/app.h>
5
4#include <assert.h> 6#include <assert.h>
5#include <stdbool.h> 7#include <stdbool.h>
6#include <stdio.h> 8#include <stdio.h>
7 9
10static const int WINDOW_WIDTH = 1408;
11static const int WINDOW_HEIGHT = 960;
12static const int MAX_FPS = 60;
13
14// Virtual screen dimensions.
15static const int SCREEN_WIDTH = 704;
16static const int SCREEN_HEIGHT = 480;
17
8static const int TILE_WIDTH = 64; 18static const int TILE_WIDTH = 64;
9static const int TILE_HEIGHT = TILE_WIDTH / 2; 19static const int TILE_HEIGHT = TILE_WIDTH / 2;
10static const int WORLD_WIDTH = 20; 20static const int WORLD_WIDTH = 20;
@@ -31,11 +41,13 @@ typedef enum Colour {
31 Red, 41 Red,
32} Colour; 42} Colour;
33 43
34typedef struct IsoGfxAppState { 44typedef struct GfxAppState {
35 Tile red; 45 IsoBackend* backend;
36 int xpick; 46 IsoGfx* iso;
37 int ypick; 47 Tile red;
38} IsoGfxAppState; 48 int xpick;
49 int ypick;
50} GfxAppState;
39 51
40static void make_checkerboard(IsoGfx* iso, Tile black, Tile white) { 52static void make_checkerboard(IsoGfx* iso, Tile black, Tile white) {
41 assert(iso); 53 assert(iso);
@@ -49,14 +61,20 @@ static void make_checkerboard(IsoGfx* iso, Tile black, Tile white) {
49 } 61 }
50} 62}
51 63
52static bool init( 64static bool init(GfxAppState* state, int argc, const char** argv) {
53 IsoGfxAppState* state, IsoGfx* iso, int argc, const char** argv) {
54 assert(state); 65 assert(state);
55 assert(iso);
56 66
57 (void)argc; 67 (void)argc;
58 (void)argv; 68 (void)argv;
59 69
70 if (!(state->iso = isogfx_new(&(IsoGfxDesc){
71 .screen_width = SCREEN_WIDTH, .screen_height = SCREEN_HEIGHT}))) {
72 return false;
73 }
74 IsoGfx* iso = state->iso;
75
76 isogfx_resize(iso, SCREEN_WIDTH, SCREEN_HEIGHT);
77
60 if (!isogfx_make_world( 78 if (!isogfx_make_world(
61 iso, &(WorldDesc){ 79 iso, &(WorldDesc){
62 .tile_width = TILE_WIDTH, 80 .tile_width = TILE_WIDTH,
@@ -71,50 +89,78 @@ static bool init(
71 state->red = isogfx_make_tile(iso, &tile_set[Red]); 89 state->red = isogfx_make_tile(iso, &tile_set[Red]);
72 make_checkerboard(iso, black, white); 90 make_checkerboard(iso, black, white);
73 91
92 if (!(state->backend = IsoBackendInit(iso))) {
93 return false;
94 }
95
74 return true; 96 return true;
75} 97}
76 98
77static void shutdown(IsoGfxAppState* state, IsoGfx* iso) { 99static void shutdown(GfxAppState* state) {
78 assert(state); 100 assert(state);
79 assert(iso); 101
102 IsoBackendShutdown(&state->backend);
103 isogfx_del(&state->iso);
80} 104}
81 105
82static void update(IsoGfxAppState* state, IsoGfx* iso, double t, double dt) { 106static void update(GfxAppState* state, double t, double dt) {
83 assert(state); 107 assert(state);
84 assert(iso);
85
86 (void)t;
87 (void)dt; 108 (void)dt;
88 109
110 IsoGfx* iso = state->iso;
111
112 isogfx_update(iso, t);
113
114 // Get mouse position in window coordinates.
89 double mouse_x, mouse_y; 115 double mouse_x, mouse_y;
90 gfx_app_get_mouse_position(&mouse_x, &mouse_y); 116 gfx_app_get_mouse_position(&mouse_x, &mouse_y);
91 117
118 // Map from window coordinates to virtual screen coordinates.
119 IsoBackendGetMousePosition(
120 state->backend, mouse_x, mouse_y, &mouse_x, &mouse_y);
121
92 isogfx_pick_tile(iso, mouse_x, mouse_y, &state->xpick, &state->ypick); 122 isogfx_pick_tile(iso, mouse_x, mouse_y, &state->xpick, &state->ypick);
93 123
94 printf("Picked tile: (%d, %d)\n", state->xpick, state->ypick); 124 printf("Picked tile: (%d, %d)\n", state->xpick, state->ypick);
95} 125}
96 126
97static void render(IsoGfxAppState* state, IsoGfx* iso) { 127static void render(GfxAppState* state) {
98 assert(state); 128 assert(state);
99 assert(iso); 129
130 IsoGfx* iso = state->iso;
100 131
101 isogfx_render(iso); 132 isogfx_render(iso);
102 133
103 if ((state->xpick != -1) && (state->ypick != -1)) { 134 if ((state->xpick != -1) && (state->ypick != -1)) {
104 isogfx_draw_tile(iso, state->xpick, state->ypick, state->red); 135 isogfx_draw_tile(iso, state->xpick, state->ypick, state->red);
105 } 136 }
137
138 IsoBackendRender(state->backend, iso);
139}
140
141static void resize(GfxAppState* state, int width, int height) {
142 assert(state);
143
144 IsoBackendResizeWindow(state->backend, state->iso, width, height);
106} 145}
107 146
108int main(int argc, const char** argv) { 147int main(int argc, const char** argv) {
109 IsoGfxAppState state = {0}; 148 GfxAppState state = {0};
110 iso_run( 149 gfx_app_run(
111 argc, argv, 150 &(GfxAppDesc){
112 &(IsoGfxApp){ 151 .argc = argc,
113 .state = &state, 152 .argv = argv,
153 .width = WINDOW_WIDTH,
154 .height = WINDOW_HEIGHT,
155 .max_fps = MAX_FPS,
156 .update_delta_time = MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0,
157 .title = "Isometric Renderer",
158 .app_state = &state},
159 &(GfxAppCallbacks){
114 .init = init, 160 .init = init,
115 .shutdown = shutdown,
116 .update = update, 161 .update = update,
117 .render = render, 162 .render = render,
118 }); 163 .resize = resize,
164 .shutdown = shutdown});
119 return 0; 165 return 0;
120} 166}