From adbd2511beec8f1caa1752bdfd755cc2f62ba425 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 9 Mar 2024 08:43:26 -0800 Subject: Make isogfx a library instead of an executable. --- gfx-iso/app/app.h | 12 --- gfx-iso/app/checkerboard.c | 120 --------------------------- gfx-iso/app/checkerboard.h | 9 -- gfx-iso/app/isogfx-demo.c | 79 ------------------ gfx-iso/app/isogfx-demo.h | 9 -- gfx-iso/app/main.c | 199 --------------------------------------------- 6 files changed, 428 deletions(-) delete mode 100644 gfx-iso/app/app.h delete mode 100644 gfx-iso/app/checkerboard.c delete mode 100644 gfx-iso/app/checkerboard.h delete mode 100644 gfx-iso/app/isogfx-demo.c delete mode 100644 gfx-iso/app/isogfx-demo.h delete mode 100644 gfx-iso/app/main.c (limited to 'gfx-iso/app') diff --git a/gfx-iso/app/app.h b/gfx-iso/app/app.h deleted file mode 100644 index 25e55eb..0000000 --- a/gfx-iso/app/app.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -typedef struct IsoGfx IsoGfx; -typedef struct IsoGfxApp IsoGfxApp; - -typedef struct IsoGfxApp { - int pixel_scale; // 0 or 1 for 1:1 scale. - void* state; - void (*shutdown)(IsoGfx*, void* state); - void (*update)(IsoGfx*, void* state, double t, double dt); - void (*render)(IsoGfx*, void* state); -} IsoGfxApp; diff --git a/gfx-iso/app/checkerboard.c b/gfx-iso/app/checkerboard.c deleted file mode 100644 index 8b394c4..0000000 --- a/gfx-iso/app/checkerboard.c +++ /dev/null @@ -1,120 +0,0 @@ -#include "isogfx-demo.h" - -#include -#include - -#include -#include -#include -#include - -static const int TILE_WIDTH = 64; -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 State { - Tile red; - int xpick; - int ypick; -} State; - -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 void shutdown(IsoGfx* iso, void* app_state) { - assert(iso); - if (app_state) { - free(app_state); - } -} - -static void update(IsoGfx* iso, void* app_state, double t, double dt) { - assert(iso); - assert(app_state); - State* state = (State*)(app_state); - - double mouse_x, mouse_y; - gfx_app_get_mouse_position(&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(IsoGfx* iso, void* app_state) { - assert(iso); - assert(app_state); - State* state = (State*)(app_state); - - isogfx_render(iso); - if ((state->xpick != -1) && (state->ypick != -1)) { - isogfx_draw_tile(iso, state->xpick, state->ypick, state->red); - } -} - -bool make_checkerboard_app(IsoGfx* iso, IsoGfxApp* app) { - assert(iso); - assert(app); - - State* state = calloc(1, sizeof(State)); - if (!state) { - return false; - } - - if (!isogfx_make_world( - iso, &(WorldDesc){ - .tile_width = TILE_WIDTH, - .tile_height = TILE_HEIGHT, - .world_width = WORLD_WIDTH, - .world_height = WORLD_HEIGHT})) { - goto cleanup; - } - - 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); - isogfx_render(iso); - - app->state = state; - app->shutdown = shutdown; - app->update = update; - app->render = render; - - return true; - -cleanup: - free(state); - return false; -} diff --git a/gfx-iso/app/checkerboard.h b/gfx-iso/app/checkerboard.h deleted file mode 100644 index 61725a5..0000000 --- a/gfx-iso/app/checkerboard.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include "app.h" - -#include - -typedef struct IsoGfxApp IsoGfxApp; - -bool make_checkerboard_app(IsoGfx*, IsoGfxApp*); diff --git a/gfx-iso/app/isogfx-demo.c b/gfx-iso/app/isogfx-demo.c deleted file mode 100644 index 9889275..0000000 --- a/gfx-iso/app/isogfx-demo.c +++ /dev/null @@ -1,79 +0,0 @@ -#include "isogfx-demo.h" - -#include -#include - -#include -#include -#include -#include - -typedef struct State { - int xpick; - int ypick; - SpriteSheet stag_sheet; - Sprite stag; -} State; - -static void shutdown(IsoGfx* iso, void* app_state) { - assert(iso); - if (app_state) { - free(app_state); - } -} - -static void update(IsoGfx* iso, void* app_state, double t, double dt) { - assert(iso); - assert(app_state); - State* state = (State*)(app_state); - - double mouse_x, mouse_y; - gfx_app_get_mouse_position(&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(IsoGfx* iso, void* app_state) { - assert(iso); - assert(app_state); - State* state = (State*)(app_state); - - isogfx_render(iso); -} - -bool make_demo_app(IsoGfx* iso, IsoGfxApp* app) { - assert(iso); - assert(app); - - State* state = calloc(1, sizeof(State)); - if (!state) { - return false; - } - - if (!isogfx_load_world(iso, "/home/jeanne/assets/tilemaps/demo1.tm")) { - goto cleanup; - } - - if (!isogfx_load_sprite_sheet( - iso, "/home/jeanne/assets/tilesets/scrabling/critters/stag/stag.ss", - &state->stag_sheet)) { - goto cleanup; - } - - state->stag = isogfx_make_sprite(iso, state->stag_sheet); - isogfx_set_sprite_position(iso, state->stag, 5, 4); - - app->pixel_scale = 2; - app->state = state; - app->shutdown = shutdown; - app->update = update; - app->render = render; - - return true; - -cleanup: - free(state); - return false; -} diff --git a/gfx-iso/app/isogfx-demo.h b/gfx-iso/app/isogfx-demo.h deleted file mode 100644 index d099824..0000000 --- a/gfx-iso/app/isogfx-demo.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include "app.h" - -#include - -typedef struct IsoGfxApp IsoGfxApp; - -bool make_demo_app(IsoGfx*, IsoGfxApp*); diff --git a/gfx-iso/app/main.c b/gfx-iso/app/main.c deleted file mode 100644 index 050a42f..0000000 --- a/gfx-iso/app/main.c +++ /dev/null @@ -1,199 +0,0 @@ -#include "app.h" -#include "checkerboard.h" -#include "isogfx-demo.h" - -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -static const int SCREEN_WIDTH = 1408; -static const int SCREEN_HEIGHT = 960; - -typedef struct State { - Gfx* gfx; - IsoGfx* iso; - IsoGfxApp app; - Texture* screen_texture; - Scene* scene; -} State; - -static bool init(const GfxAppDesc* desc, void** app_state) { - State* state = calloc(1, sizeof(State)); - if (!state) { - return false; - } - - if (!(state->iso = isogfx_new(&(IsoGfxDesc){ - .screen_width = SCREEN_WIDTH, .screen_height = SCREEN_HEIGHT}))) { - goto cleanup; - } - // if (!make_checkerboard_app(state->iso, &state->app)) { - // goto cleanup; - // } - if (!make_demo_app(state->iso, &state->app)) { - goto cleanup; - } - - // Apply pixel scaling if requested by the app. - int texture_width, texture_height; - if (state->app.pixel_scale > 1) { - texture_width = SCREEN_WIDTH / state->app.pixel_scale; - texture_height = SCREEN_HEIGHT / state->app.pixel_scale; - isogfx_resize(state->iso, texture_width, texture_height); - } else { - texture_width = SCREEN_WIDTH; - texture_height = SCREEN_HEIGHT; - } - - if (!(state->gfx = gfx_init())) { - goto cleanup; - } - RenderBackend* render_backend = gfx_get_render_backend(state->gfx); - - if (!(state->screen_texture = gfx_make_texture( - render_backend, &(TextureDesc){ - .width = texture_width, - .height = texture_height, - .dimension = Texture2D, - .format = TextureSRGBA8, - .filtering = NearestFiltering, - .wrap = ClampToEdge, - .mipmaps = false}))) { - goto cleanup; - } - - ShaderProgram* shader = gfx_make_view_texture_shader(render_backend); - if (!shader) { - goto cleanup; - } - - Geometry* geometry = gfx_make_quad_11(render_backend); - if (!geometry) { - goto cleanup; - } - - MaterialDesc material_desc = (MaterialDesc){.num_uniforms = 1}; - material_desc.uniforms[0] = (ShaderUniform){ - .type = UniformTexture, - .value.texture = state->screen_texture, - .name = sstring_make("Texture")}; - Material* material = gfx_make_material(&material_desc); - if (!material) { - return false; - } - - const MeshDesc mesh_desc = - (MeshDesc){.geometry = geometry, .material = material, .shader = shader}; - Mesh* mesh = gfx_make_mesh(&mesh_desc); - if (!mesh) { - goto cleanup; - } - - SceneObject* object = - gfx_make_object(&(ObjectDesc){.num_meshes = 1, .meshes = {mesh}}); - if (!object) { - goto cleanup; - } - - state->scene = gfx_make_scene(); - SceneNode* node = gfx_make_object_node(object); - SceneNode* root = gfx_get_scene_root(state->scene); - gfx_set_node_parent(node, root); - - *app_state = state; - return true; - -cleanup: - if (state->gfx) { - gfx_destroy(&state->gfx); - } - free(state); - return false; -} - -static void shutdown(void* app_state) { - assert(app_state); - State* state = (State*)(app_state); - - if (state->app.state) { - assert(state->iso); - (*state->app.shutdown)(state->iso, state->app.state); - } - isogfx_del(&state->iso); - gfx_destroy(&state->gfx); - free(app_state); -} - -static void update(void* app_state, double t, double dt) { - assert(app_state); - State* state = (State*)(app_state); - - isogfx_update(state->iso, t); - - assert(state->app.update); - (*state->app.update)(state->iso, state->app.state, t, dt); -} - -static void render(void* app_state) { - assert(app_state); - State* state = (State*)(app_state); - - assert(state->app.render); - (*state->app.render)(state->iso, state->app.state); - - const Pixel* screen = isogfx_get_screen_buffer(state->iso); - assert(screen); - gfx_update_texture( - state->screen_texture, &(TextureDataDesc){.pixels = screen}); - - RenderBackend* render_backend = gfx_get_render_backend(state->gfx); - Renderer* renderer = gfx_get_renderer(state->gfx); - - gfx_start_frame(render_backend); - gfx_render_scene( - renderer, &(RenderSceneParams){ - .mode = RenderDefault, .scene = state->scene, .camera = 0}); - gfx_end_frame(render_backend); -} - -static void resize(void* app_state, int width, int height) { - assert(app_state); - State* state = (State*)(app_state); - - RenderBackend* render_backend = gfx_get_render_backend(state->gfx); - gfx_set_viewport(render_backend, width, height); -} - -int main(int argc, const char** argv) { - const int initial_width = SCREEN_WIDTH; - const int initial_height = SCREEN_HEIGHT; - const int max_fps = 60; - - gfx_app_run( - &(GfxAppDesc){ - .argc = argc, - .argv = argv, - .width = initial_width, - .height = initial_height, - .max_fps = max_fps, - .update_delta_time = max_fps > 0 ? 1.0 / (double)max_fps : 0.0, - .title = "Isometric Renderer"}, - &(GfxAppCallbacks){ - .init = init, - .update = update, - .render = render, - .resize = resize, - .shutdown = shutdown}); - - return 0; -} -- cgit v1.2.3