From 4ffa1895390066f2bbd347c0689a1b667a21fd24 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 27 Jun 2025 10:17:41 -0700 Subject: Initial commit --- demos/CMakeLists.txt | 2 + demos/checkerboard/CMakeLists.txt | 16 ++++ demos/checkerboard/checkerboard.c | 166 ++++++++++++++++++++++++++++++++++++++ demos/isomap/CMakeLists.txt | 16 ++++ demos/isomap/isomap.c | 105 ++++++++++++++++++++++++ 5 files changed, 305 insertions(+) create mode 100644 demos/CMakeLists.txt create mode 100644 demos/checkerboard/CMakeLists.txt create mode 100644 demos/checkerboard/checkerboard.c create mode 100644 demos/isomap/CMakeLists.txt create mode 100644 demos/isomap/isomap.c (limited to 'demos') diff --git a/demos/CMakeLists.txt b/demos/CMakeLists.txt new file mode 100644 index 0000000..c0a4101 --- /dev/null +++ b/demos/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(checkerboard) +add_subdirectory(isomap) diff --git a/demos/checkerboard/CMakeLists.txt b/demos/checkerboard/CMakeLists.txt new file mode 100644 index 0000000..d1691c6 --- /dev/null +++ b/demos/checkerboard/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.0) + +project(checkerboard) + +set(CMAKE_C_STANDARD 17) +set(CMAKE_C_STANDARD_REQUIRED On) +set(CMAKE_C_EXTENSIONS Off) + +add_executable(checkerboard + checkerboard.c) + +target_link_libraries(checkerboard PRIVATE + gfx-app + isogfx-backend) + +target_compile_options(checkerboard PRIVATE -Wall -Wextra -Wpedantic) diff --git a/demos/checkerboard/checkerboard.c b/demos/checkerboard/checkerboard.c new file mode 100644 index 0000000..dbc817c --- /dev/null +++ b/demos/checkerboard/checkerboard.c @@ -0,0 +1,166 @@ +#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 = 32; +static const int TILE_HEIGHT = TILE_WIDTH / 2; +static const int WORLD_WIDTH = 20; +static const int WORLD_HEIGHT = 20; + +static const TileDesc tile_set[] = { + {.type = TileFromColour, + .width = TILE_WIDTH, + .height = TILE_HEIGHT, + .colour = (Pixel){.r = 0x38, .g = 0x3b, .b = 0x46, .a = 0xff}}, + {.type = TileFromColour, + .width = TILE_WIDTH, + .height = TILE_HEIGHT, + .colour = (Pixel){.r = 0xA5, .g = 0xb3, .b = 0xc0, .a = 0xff}}, + {.type = TileFromColour, + .width = TILE_WIDTH, + .height = TILE_HEIGHT, + .colour = (Pixel){.r = 0xdc, .g = 0x76, .b = 0x84, .a = 0xff}}, +}; + +typedef enum Colour { + Black, + White, + Red, +} Colour; + +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); + for (int y = 0; y < isogfx_world_height(iso); ++y) { + for (int x = 0; x < isogfx_world_width(iso); ++x) { + const int odd_col = x & 1; + const int odd_row = y & 1; + const Tile value = (odd_row ^ odd_col) == 0 ? black : white; + isogfx_set_tile(iso, x, y, value); + } + } +} + +static bool init(GfxAppState* state, int argc, const char** argv) { + assert(state); + + (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, + .tile_height = TILE_HEIGHT, + .world_width = WORLD_WIDTH, + .world_height = WORLD_HEIGHT})) { + return false; + } + + const Tile black = isogfx_make_tile(iso, &tile_set[Black]); + const Tile white = isogfx_make_tile(iso, &tile_set[White]); + 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(GfxAppState* state) { + assert(state); + + IsoBackendShutdown(&state->backend); + isogfx_del(&state->iso); +} + +static void update(GfxAppState* state, double t, double dt) { + assert(state); + (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(GfxAppState* state) { + assert(state); + + 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) { + 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; +} diff --git a/demos/isomap/CMakeLists.txt b/demos/isomap/CMakeLists.txt new file mode 100644 index 0000000..2dbfd32 --- /dev/null +++ b/demos/isomap/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.0) + +project(isomap) + +set(CMAKE_C_STANDARD 17) +set(CMAKE_C_STANDARD_REQUIRED On) +set(CMAKE_C_EXTENSIONS Off) + +add_executable(isomap + isomap.c) + +target_link_libraries(isomap PRIVATE + gfx-app + isogfx-backend) + +target_compile_options(isomap PRIVATE -Wall -Wextra -Wpedantic) diff --git a/demos/isomap/isomap.c b/demos/isomap/isomap.c new file mode 100644 index 0000000..a233659 --- /dev/null +++ b/demos/isomap/isomap.c @@ -0,0 +1,105 @@ +#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; + +typedef struct GfxAppState { + IsoBackend* backend; + IsoGfx* iso; + int xpick; + int ypick; + SpriteSheet stag_sheet; + Sprite stag; +} GfxAppState; + +static bool init(GfxAppState* state, int argc, const char** argv) { + assert(state); + (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; + } + + if (!isogfx_load_sprite_sheet( + iso, "/home/jeanne/assets/tilesets/scrabling/critters/stag/stag.ss", + &state->stag_sheet)) { + return false; + } + + 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(GfxAppState* state) { + assert(state); + // +} + +static void update(GfxAppState* state, double t, double dt) { + assert(state); + (void)dt; + + IsoGfx* iso = state->iso; + isogfx_update(iso, t); +} + +static void render(GfxAppState* state) { + assert(state); + + IsoGfx* iso = state->iso; + isogfx_render(iso); + 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) { + 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