summaryrefslogtreecommitdiff
path: root/gfx-iso/demos
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
parent0a78a9d9c1ac2090da56f395d6f2394a8a210010 (diff)
Handle resizing.
Diffstat (limited to 'gfx-iso/demos')
-rw-r--r--gfx-iso/demos/checkerboard/CMakeLists.txt3
-rw-r--r--gfx-iso/demos/checkerboard/checkerboard.c94
-rw-r--r--gfx-iso/demos/isomap/CMakeLists.txt3
-rw-r--r--gfx-iso/demos/isomap/isomap.c88
4 files changed, 132 insertions, 56 deletions
diff --git a/gfx-iso/demos/checkerboard/CMakeLists.txt b/gfx-iso/demos/checkerboard/CMakeLists.txt
index f178262..d1691c6 100644
--- a/gfx-iso/demos/checkerboard/CMakeLists.txt
+++ b/gfx-iso/demos/checkerboard/CMakeLists.txt
@@ -10,6 +10,7 @@ add_executable(checkerboard
10 checkerboard.c) 10 checkerboard.c)
11 11
12target_link_libraries(checkerboard PRIVATE 12target_link_libraries(checkerboard PRIVATE
13 isogfx-app) 13 gfx-app
14 isogfx-backend)
14 15
15target_compile_options(checkerboard PRIVATE -Wall -Wextra -Wpedantic) 16target_compile_options(checkerboard PRIVATE -Wall -Wextra -Wpedantic)
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}
diff --git a/gfx-iso/demos/isomap/CMakeLists.txt b/gfx-iso/demos/isomap/CMakeLists.txt
index 13edcc7..2dbfd32 100644
--- a/gfx-iso/demos/isomap/CMakeLists.txt
+++ b/gfx-iso/demos/isomap/CMakeLists.txt
@@ -10,6 +10,7 @@ add_executable(isomap
10 isomap.c) 10 isomap.c)
11 11
12target_link_libraries(isomap PRIVATE 12target_link_libraries(isomap PRIVATE
13 isogfx-app) 13 gfx-app
14 isogfx-backend)
14 15
15target_compile_options(isomap PRIVATE -Wall -Wextra -Wpedantic) 16target_compile_options(isomap PRIVATE -Wall -Wextra -Wpedantic)
diff --git a/gfx-iso/demos/isomap/isomap.c b/gfx-iso/demos/isomap/isomap.c
index 5ab926c..a233659 100644
--- a/gfx-iso/demos/isomap/isomap.c
+++ b/gfx-iso/demos/isomap/isomap.c
@@ -1,23 +1,41 @@
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 8
7typedef struct IsoGfxAppState { 9static const int WINDOW_WIDTH = 1408;
10static const int WINDOW_HEIGHT = 960;
11static const int MAX_FPS = 60;
12
13// Virtual screen dimensions.
14static const int SCREEN_WIDTH = 704;
15static const int SCREEN_HEIGHT = 480;
16
17typedef struct GfxAppState {
18 IsoBackend* backend;
19 IsoGfx* iso;
8 int xpick; 20 int xpick;
9 int ypick; 21 int ypick;
10 SpriteSheet stag_sheet; 22 SpriteSheet stag_sheet;
11 Sprite stag; 23 Sprite stag;
12} IsoGfxAppState; 24} GfxAppState;
13 25
14static bool init( 26static bool init(GfxAppState* state, int argc, const char** argv) {
15 IsoGfxAppState* state, IsoGfx* iso, int argc, const char** argv) {
16 assert(state); 27 assert(state);
17 assert(iso);
18 (void)argc; 28 (void)argc;
19 (void)argv; 29 (void)argv;
20 30
31 if (!(state->iso = isogfx_new(&(IsoGfxDesc){
32 .screen_width = SCREEN_WIDTH, .screen_height = SCREEN_HEIGHT}))) {
33 return false;
34 }
35 IsoGfx* iso = state->iso;
36
37 isogfx_resize(iso, SCREEN_WIDTH, SCREEN_HEIGHT);
38
21 if (!isogfx_load_world(iso, "/home/jeanne/assets/tilemaps/demo1.tm")) { 39 if (!isogfx_load_world(iso, "/home/jeanne/assets/tilemaps/demo1.tm")) {
22 return false; 40 return false;
23 } 41 }
@@ -31,47 +49,57 @@ static bool init(
31 state->stag = isogfx_make_sprite(iso, state->stag_sheet); 49 state->stag = isogfx_make_sprite(iso, state->stag_sheet);
32 isogfx_set_sprite_position(iso, state->stag, 5, 4); 50 isogfx_set_sprite_position(iso, state->stag, 5, 4);
33 51
52 if (!(state->backend = IsoBackendInit(iso))) {
53 return false;
54 }
55
34 return true; 56 return true;
35} 57}
36 58
37static void shutdown(IsoGfxAppState* state, IsoGfx* iso) { 59static void shutdown(GfxAppState* state) {
38 assert(state); 60 assert(state);
39 assert(iso); 61 //
40} 62}
41 63
42static void update(IsoGfxAppState* state, IsoGfx* iso, double t, double dt) { 64static void update(GfxAppState* state, double t, double dt) {
43 assert(state); 65 assert(state);
44 assert(iso);
45
46 (void)t;
47 (void)dt; 66 (void)dt;
48 67
49 double mouse_x, mouse_y; 68 IsoGfx* iso = state->iso;
50 gfx_app_get_mouse_position(&mouse_x, &mouse_y); 69 isogfx_update(iso, t);
70}
51 71
52 isogfx_pick_tile(iso, mouse_x, mouse_y, &state->xpick, &state->ypick); 72static void render(GfxAppState* state) {
73 assert(state);
53 74
54 // printf("Picked tile: (%d, %d)\n", state->xpick, state->ypick); 75 IsoGfx* iso = state->iso;
76 isogfx_render(iso);
77 IsoBackendRender(state->backend, iso);
55} 78}
56 79
57static void render(IsoGfxAppState* state, IsoGfx* iso) { 80static void resize(GfxAppState* state, int width, int height) {
58 assert(state); 81 assert(state);
59 assert(iso);
60 82
61 isogfx_render(iso); 83 IsoBackendResizeWindow(state->backend, state->iso, width, height);
62} 84}
63 85
64int main(int argc, const char** argv) { 86int main(int argc, const char** argv) {
65 IsoGfxAppState state = {0}; 87 GfxAppState state = {0};
66 iso_run( 88 gfx_app_run(
67 argc, argv, 89 &(GfxAppDesc){
68 &(IsoGfxApp){ 90 .argc = argc,
69 .pixel_scale = 2, 91 .argv = argv,
70 .state = &state, 92 .width = WINDOW_WIDTH,
71 .init = init, 93 .height = WINDOW_HEIGHT,
72 .shutdown = shutdown, 94 .max_fps = MAX_FPS,
73 .update = update, 95 .update_delta_time = MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0,
74 .render = render, 96 .title = "Isometric Renderer",
75 }); 97 .app_state = &state},
98 &(GfxAppCallbacks){
99 .init = init,
100 .update = update,
101 .render = render,
102 .resize = resize,
103 .shutdown = shutdown});
76 return 0; 104 return 0;
77} 105}