From 6e0913a3f77354eab2eeceba66db7d1750e22581 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 19 Jul 2025 12:57:36 -0700 Subject: Avoid recomputing the iso space --- src/isogfx.c | 56 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/isogfx.c b/src/isogfx.c index e182dff..0aaeb9d 100644 --- a/src/isogfx.c +++ b/src/isogfx.c @@ -32,6 +32,12 @@ typedef struct vec2 { // Renderer state. // ----------------------------------------------------------------------------- +typedef struct CoordSystem { + ivec2 o; // Origin. + ivec2 x; + ivec2 y; +} CoordSystem; + typedef struct Screen { int width; int height; @@ -48,6 +54,7 @@ typedef struct SpriteInstance { typedef struct IsoGfx { Screen screen; + CoordSystem iso_space; double last_animation_time; Tile next_tile; // For procedurally-generated tiles. Tm_Map* map; @@ -109,6 +116,20 @@ static inline Pixel* screen_xy_mut(Screen* screen, int x, int y) { return (Pixel*)screen_xy_const_ref(screen, x, y); } +/// Create the basis for the isometric coordinate system with origin and vectors +/// expressed in the Cartesian system. +static CoordSystem make_iso_coord_system( + const Tm_Map* const map, const Screen* const screen) { + assert(map); + assert(screen); + const ivec2 o = {screen->width / 2, 0}; + const ivec2 x = { + .x = map->base_tile_width / 2, .y = map->base_tile_height / 2}; + const ivec2 y = { + .x = -map->base_tile_width / 2, .y = map->base_tile_height / 2}; + return (CoordSystem){o, x, y}; +} + // ----------------------------------------------------------------------------- // Renderer, world and tile management. // ----------------------------------------------------------------------------- @@ -212,6 +233,8 @@ void isogfx_make_world(IsoGfx* iso, const WorldDesc* desc) { *iso->tileset = (Ts_TileSet){ .num_tiles = desc->num_tiles, }; + + iso->iso_space = make_iso_coord_system(iso->map, &iso->screen); } bool isogfx_load_world(IsoGfx* iso, const char* filepath) { @@ -261,6 +284,8 @@ bool isogfx_load_world(IsoGfx* iso, const char* filepath) { assert(ts_validate_tileset(tileset)); assert(tm_validate_map(map, tileset)); + iso->iso_space = make_iso_coord_system(iso->map, &iso->screen); + success = true; cleanup: @@ -462,24 +487,6 @@ void isogfx_update(IsoGfx* iso, double t) { // Rendering and picking. // ----------------------------------------------------------------------------- -typedef struct CoordSystem { - ivec2 o; /// Origin. - ivec2 x; - ivec2 y; -} CoordSystem; - -/// Create the basis for the isometric coordinate system with origin and vectors -/// expressed in the Cartesian system. -static CoordSystem make_iso_coord_system(const IsoGfx* iso) { - assert(iso); - const ivec2 o = {iso->screen.width / 2, 0}; - const ivec2 x = { - .x = iso->map->base_tile_width / 2, .y = iso->map->base_tile_height / 2}; - const ivec2 y = { - .x = -iso->map->base_tile_width / 2, .y = iso->map->base_tile_height / 2}; - return (CoordSystem){o, x, y}; -} - /// Get the screen position of the top diamond-corner of the tile at world /// (x,y). static ivec2 GetTileScreenOrigin( @@ -582,8 +589,6 @@ static void draw_world(IsoGfx* iso) { memset(iso->screen.pixels, 0, W * H * sizeof(Pixel)); - const CoordSystem iso_space = make_iso_coord_system(iso); - const Tm_Layer* layer = tm_map_get_layer(iso->map, 0); // TODO: Culling. @@ -594,7 +599,7 @@ static void draw_world(IsoGfx* iso) { for (int wy = 0; wy < iso->map->world_height; ++wy) { for (int wx = 0; wx < iso->map->world_width; ++wx) { const Tile tile = tm_layer_get_tile(iso->map, layer, wx, wy); - const ivec2 screen_origin = GetTileScreenOrigin(iso_space, wx, wy); + const ivec2 screen_origin = GetTileScreenOrigin(iso->iso_space, wx, wy); draw_tile(iso, screen_origin, tile); } } @@ -620,15 +625,13 @@ static void draw_sprite( static void draw_sprites(IsoGfx* iso) { assert(iso); - const CoordSystem iso_space = make_iso_coord_system(iso); - for (const SpriteInstance* sprite = iso->head_sprite; sprite; sprite = sprite->next) { const Ss_SpriteSheet* sheet = sprite->sheet; assert(sheet); - const ivec2 screen_origin = - GetTileScreenOrigin(iso_space, sprite->position.x, sprite->position.y); + const ivec2 screen_origin = GetTileScreenOrigin( + iso->iso_space, sprite->position.x, sprite->position.y); draw_sprite(iso, screen_origin, sprite, sheet); } } @@ -646,8 +649,7 @@ void isogfx_draw_tile(IsoGfx* iso, int x, int y, Tile tile) { assert(x < iso->map->world_width); assert(y < iso->map->world_height); - const CoordSystem iso_space = make_iso_coord_system(iso); - const ivec2 screen_origin = GetTileScreenOrigin(iso_space, x, y); + const ivec2 screen_origin = GetTileScreenOrigin(iso->iso_space, x, y); draw_tile(iso, screen_origin, tile); } -- cgit v1.2.3