From daf6262c029892212f6b9b4014887c2c37e9ef75 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 31 Aug 2024 18:58:39 -0700 Subject: Handle resizing. --- gfx-iso/demos/checkerboard/checkerboard.c | 94 +++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 24 deletions(-) (limited to 'gfx-iso/demos/checkerboard/checkerboard.c') 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 @@ -#include +#include #include +#include + #include #include #include +static const int WINDOW_WIDTH = 1408; +static const int WINDOW_HEIGHT = 960; +static const int MAX_FPS = 60; + +// Virtual screen dimensions. +static const int SCREEN_WIDTH = 704; +static const int SCREEN_HEIGHT = 480; + static const int TILE_WIDTH = 64; static const int TILE_HEIGHT = TILE_WIDTH / 2; static const int WORLD_WIDTH = 20; @@ -31,11 +41,13 @@ typedef enum Colour { Red, } Colour; -typedef struct IsoGfxAppState { - Tile red; - int xpick; - int ypick; -} IsoGfxAppState; +typedef struct GfxAppState { + IsoBackend* backend; + IsoGfx* iso; + Tile red; + int xpick; + int ypick; +} GfxAppState; static void make_checkerboard(IsoGfx* iso, Tile black, Tile white) { assert(iso); @@ -49,14 +61,20 @@ static void make_checkerboard(IsoGfx* iso, Tile black, Tile white) { } } -static bool init( - IsoGfxAppState* state, IsoGfx* iso, int argc, const char** argv) { +static bool init(GfxAppState* state, int argc, const char** argv) { assert(state); - assert(iso); (void)argc; (void)argv; + if (!(state->iso = isogfx_new(&(IsoGfxDesc){ + .screen_width = SCREEN_WIDTH, .screen_height = SCREEN_HEIGHT}))) { + return false; + } + IsoGfx* iso = state->iso; + + isogfx_resize(iso, SCREEN_WIDTH, SCREEN_HEIGHT); + if (!isogfx_make_world( iso, &(WorldDesc){ .tile_width = TILE_WIDTH, @@ -71,50 +89,78 @@ static bool init( state->red = isogfx_make_tile(iso, &tile_set[Red]); make_checkerboard(iso, black, white); + if (!(state->backend = IsoBackendInit(iso))) { + return false; + } + return true; } -static void shutdown(IsoGfxAppState* state, IsoGfx* iso) { +static void shutdown(GfxAppState* state) { assert(state); - assert(iso); + + IsoBackendShutdown(&state->backend); + isogfx_del(&state->iso); } -static void update(IsoGfxAppState* state, IsoGfx* iso, double t, double dt) { +static void update(GfxAppState* state, double t, double dt) { assert(state); - assert(iso); - - (void)t; (void)dt; + IsoGfx* iso = state->iso; + + isogfx_update(iso, t); + + // Get mouse position in window coordinates. double mouse_x, mouse_y; gfx_app_get_mouse_position(&mouse_x, &mouse_y); + // Map from window coordinates to virtual screen coordinates. + IsoBackendGetMousePosition( + state->backend, mouse_x, mouse_y, &mouse_x, &mouse_y); + isogfx_pick_tile(iso, mouse_x, mouse_y, &state->xpick, &state->ypick); printf("Picked tile: (%d, %d)\n", state->xpick, state->ypick); } -static void render(IsoGfxAppState* state, IsoGfx* iso) { +static void render(GfxAppState* state) { assert(state); - assert(iso); + + IsoGfx* iso = state->iso; isogfx_render(iso); if ((state->xpick != -1) && (state->ypick != -1)) { isogfx_draw_tile(iso, state->xpick, state->ypick, state->red); } + + IsoBackendRender(state->backend, iso); +} + +static void resize(GfxAppState* state, int width, int height) { + assert(state); + + IsoBackendResizeWindow(state->backend, state->iso, width, height); } int main(int argc, const char** argv) { - IsoGfxAppState state = {0}; - iso_run( - argc, argv, - &(IsoGfxApp){ - .state = &state, + GfxAppState state = {0}; + gfx_app_run( + &(GfxAppDesc){ + .argc = argc, + .argv = argv, + .width = WINDOW_WIDTH, + .height = WINDOW_HEIGHT, + .max_fps = MAX_FPS, + .update_delta_time = MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0, + .title = "Isometric Renderer", + .app_state = &state}, + &(GfxAppCallbacks){ .init = init, - .shutdown = shutdown, .update = update, .render = render, - }); + .resize = resize, + .shutdown = shutdown}); return 0; } -- cgit v1.2.3