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/isomap/CMakeLists.txt | 3 +- gfx-iso/demos/isomap/isomap.c | 88 ++++++++++++++++++++++++------------- 2 files changed, 60 insertions(+), 31 deletions(-) (limited to 'gfx-iso/demos/isomap') 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 isomap.c) target_link_libraries(isomap PRIVATE - isogfx-app) + gfx-app + isogfx-backend) target_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 @@ -#include +#include #include +#include + #include #include -typedef struct IsoGfxAppState { +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; + +typedef struct GfxAppState { + IsoBackend* backend; + IsoGfx* iso; int xpick; int ypick; SpriteSheet stag_sheet; Sprite stag; -} IsoGfxAppState; +} GfxAppState; -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_load_world(iso, "/home/jeanne/assets/tilemaps/demo1.tm")) { return false; } @@ -31,47 +49,57 @@ static bool init( state->stag = isogfx_make_sprite(iso, state->stag_sheet); isogfx_set_sprite_position(iso, state->stag, 5, 4); + 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); + // } -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; - double mouse_x, mouse_y; - gfx_app_get_mouse_position(&mouse_x, &mouse_y); + IsoGfx* iso = state->iso; + isogfx_update(iso, t); +} - isogfx_pick_tile(iso, mouse_x, mouse_y, &state->xpick, &state->ypick); +static void render(GfxAppState* state) { + assert(state); - // printf("Picked tile: (%d, %d)\n", state->xpick, state->ypick); + IsoGfx* iso = state->iso; + isogfx_render(iso); + IsoBackendRender(state->backend, iso); } -static void render(IsoGfxAppState* state, IsoGfx* iso) { +static void resize(GfxAppState* state, int width, int height) { assert(state); - assert(iso); - isogfx_render(iso); + IsoBackendResizeWindow(state->backend, state->iso, width, height); } int main(int argc, const char** argv) { - IsoGfxAppState state = {0}; - iso_run( - argc, argv, - &(IsoGfxApp){ - .pixel_scale = 2, - .state = &state, - .init = init, - .shutdown = shutdown, - .update = update, - .render = render, - }); + 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, + .update = update, + .render = render, + .resize = resize, + .shutdown = shutdown}); return 0; } -- cgit v1.2.3