From 5294ea7acb86de460e2426a6dac1d281979d0c3b Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 4 Sep 2025 19:16:32 -0700 Subject: Rename isogfx -> gfx2d --- src/gfx2d.c | 381 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 191 insertions(+), 190 deletions(-) (limited to 'src/gfx2d.c') diff --git a/src/gfx2d.c b/src/gfx2d.c index 698e32c..266a5f7 100644 --- a/src/gfx2d.c +++ b/src/gfx2d.c @@ -32,11 +32,11 @@ typedef struct vec2 { // Renderer state. // ----------------------------------------------------------------------------- -typedef struct CoordSystem { +typedef struct IsoCoordSystem { ivec2 o; // Origin. ivec2 x; ivec2 y; -} CoordSystem; +} IsoCoordSystem; typedef struct Screen { int width; @@ -52,9 +52,9 @@ typedef struct SpriteInstance { int frame; // Current frame of animation. } SpriteInstance; -typedef struct IsoGfx { +typedef struct Gfx2d { Screen screen; - CoordSystem iso_space; + IsoCoordSystem iso_space; ivec2 camera; double last_animation_time; Tile next_tile; // For procedurally-generated tiles. @@ -63,7 +63,7 @@ typedef struct IsoGfx { SpriteInstance* head_sprite; // Head of sprites list. memstack stack; size_t watermark; -} IsoGfx; +} Gfx2d; // ----------------------------------------------------------------------------- // Math and world / tile / screen access. @@ -102,7 +102,7 @@ static ivec2 map2screen( /// Create the basis for the isometric coordinate system with origin and vectors /// expressed in the Cartesian system. -static CoordSystem make_iso_coord_system( +static IsoCoordSystem make_iso_coord_system( const Tm_Map* const map, const Screen* const screen) { assert(map); assert(screen); @@ -111,7 +111,7 @@ static CoordSystem make_iso_coord_system( .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}; + return (IsoCoordSystem){o, x, y}; } /// Map isometric coordinates to Cartesian coordinates. @@ -121,7 +121,7 @@ static CoordSystem make_iso_coord_system( /// /// Takes the camera displacement into account. static ivec2 iso2cart( - const CoordSystem iso_space, ivec2 camera, int iso_x, int iso_y) { + const IsoCoordSystem iso_space, ivec2 camera, int iso_x, int iso_y) { const ivec2 vx_offset = ivec2_scale(iso_space.x, iso_x); const ivec2 vy_offset = ivec2_scale(iso_space.y, iso_y); const ivec2 origin_world_space = @@ -170,63 +170,63 @@ static inline Pixel* screen_xy_mut(Screen* screen, int x, int y) { // Renderer, world and tile management. // ----------------------------------------------------------------------------- -IsoGfx* isogfx_new(const IsoGfxDesc* desc) { +Gfx2d* gfx2d_new(const Gfx2dDesc* desc) { assert(desc->screen_width > 0); assert(desc->screen_height > 0); // Part of our implementation assumes even widths and heights for precision. assert((desc->screen_width & 1) == 0); assert((desc->screen_height & 1) == 0); - IsoGfx tmp = {0}; + Gfx2d tmp = {0}; if (!memstack_make(&tmp.stack, desc->memory_size, desc->memory)) { goto cleanup; } - IsoGfx* iso = - memstack_alloc_aligned(&tmp.stack, sizeof(IsoGfx), alignof(IsoGfx)); - *iso = tmp; + Gfx2d* gfx = + memstack_alloc_aligned(&tmp.stack, sizeof(Gfx2d), alignof(Gfx2d)); + *gfx = tmp; const size_t screen_size_bytes = desc->screen_width * desc->screen_height * sizeof(Pixel); Pixel* screen = - memstack_alloc_aligned(&iso->stack, screen_size_bytes, alignof(Pixel)); + memstack_alloc_aligned(&gfx->stack, screen_size_bytes, alignof(Pixel)); - iso->screen = (Screen){.width = desc->screen_width, + gfx->screen = (Screen){.width = desc->screen_width, .height = desc->screen_height, .pixels = screen}; - iso->last_animation_time = 0.0; - iso->watermark = memstack_watermark(&iso->stack); + gfx->last_animation_time = 0.0; + gfx->watermark = memstack_watermark(&gfx->stack); - return iso; + return gfx; cleanup: - isogfx_del(&iso); + gfx2d_del(&gfx); return nullptr; } -void isogfx_clear(IsoGfx* iso) { - assert(iso); - iso->last_animation_time = 0.0; - iso->next_tile = 0; - iso->map = nullptr; - iso->tileset = nullptr; - iso->head_sprite = nullptr; - // The base of the stack contains the IsoGfx and the screen buffer. Make sure +void gfx2d_clear(Gfx2d* gfx) { + assert(gfx); + gfx->last_animation_time = 0.0; + gfx->next_tile = 0; + gfx->map = nullptr; + gfx->tileset = nullptr; + gfx->head_sprite = nullptr; + // The base of the stack contains the Gfx2d and the screen buffer. Make sure // we don't clear them. - memstack_set_watermark(&iso->stack, iso->watermark); + memstack_set_watermark(&gfx->stack, gfx->watermark); } -void isogfx_del(IsoGfx** ppIso) { - assert(ppIso); - IsoGfx* iso = *ppIso; - if (iso) { - memstack_del(&iso->stack); - *ppIso = nullptr; +void gfx2d_del(Gfx2d** ppGfx) { + assert(ppGfx); + Gfx2d* gfx = *ppGfx; + if (gfx) { + memstack_del(&gfx->stack); + *ppGfx = nullptr; } } -void isogfx_make_map(IsoGfx* iso, const MapDesc* desc) { - assert(iso); +void gfx2d_make_map(Gfx2d* gfx, const MapDesc* desc) { + assert(gfx); assert(desc); assert(desc->tile_width > 0); assert(desc->tile_height > 0); @@ -241,7 +241,7 @@ void isogfx_make_map(IsoGfx* iso, const MapDesc* desc) { assert(desc->num_tiles > 0); // Handle recreation by destroying the previous world and sprites. - isogfx_clear(iso); + gfx2d_clear(gfx); const int world_size = desc->world_width * desc->world_height; const size_t map_size_bytes = sizeof(Tm_Map) + (world_size * sizeof(Tile)); @@ -256,8 +256,8 @@ void isogfx_make_map(IsoGfx* iso, const MapDesc* desc) { (desc->num_tiles * sizeof(Ts_Tile)) + tile_data_size_bytes; - iso->map = memstack_alloc_aligned(&iso->stack, map_size_bytes, 4); - *iso->map = (Tm_Map){ + gfx->map = memstack_alloc_aligned(&gfx->stack, map_size_bytes, 4); + *gfx->map = (Tm_Map){ .world_width = desc->world_width, .world_height = desc->world_height, .base_tile_width = desc->tile_width, @@ -265,34 +265,34 @@ void isogfx_make_map(IsoGfx* iso, const MapDesc* desc) { .num_layers = 1, }; - iso->tileset = memstack_alloc_aligned(&iso->stack, tileset_size_bytes, 4); - *iso->tileset = (Ts_TileSet){ + gfx->tileset = memstack_alloc_aligned(&gfx->stack, tileset_size_bytes, 4); + *gfx->tileset = (Ts_TileSet){ .num_tiles = desc->num_tiles, }; - iso->iso_space = make_iso_coord_system(iso->map, &iso->screen); + gfx->iso_space = make_iso_coord_system(gfx->map, &gfx->screen); } -bool isogfx_load_map(IsoGfx* iso, const char* filepath) { - assert(iso); +bool gfx2d_load_map(Gfx2d* gfx, const char* filepath) { + assert(gfx); assert(filepath); bool success = false; // Handle recreation by destroying the previous world and sprites. - isogfx_clear(iso); + gfx2d_clear(gfx); // Load the map. printf("Load tile map: %s\n", filepath); WITH_FILE(filepath, { const size_t map_size = get_file_size_f(file); - iso->map = memstack_alloc_aligned(&iso->stack, map_size, 4); - success = read_file_f(file, iso->map); + gfx->map = memstack_alloc_aligned(&gfx->stack, map_size, 4); + success = read_file_f(file, gfx->map); }); if (!success) { goto cleanup; } - Tm_Map* const map = iso->map; + Tm_Map* const map = gfx->map; printf("Map orientation: %d\n", ((Tm_Flags*)&map->flags)->orientation); @@ -308,39 +308,39 @@ bool isogfx_load_map(IsoGfx* iso, const char* filepath) { printf("Load tile set: %s\n", ts_path_cwd); WITH_FILE(ts_path_cwd, { const size_t file_size = get_file_size_f(file); - iso->tileset = memstack_alloc_aligned(&iso->stack, file_size, 4); - success = read_file_f(file, iso->tileset); + gfx->tileset = memstack_alloc_aligned(&gfx->stack, file_size, 4); + success = read_file_f(file, gfx->tileset); }); if (!success) { // TODO: Log errors using the log library. goto cleanup; } - const Ts_TileSet* const tileset = iso->tileset; + const Ts_TileSet* const tileset = gfx->tileset; printf("Loaded tile set (%u tiles): %s\n", tileset->num_tiles, ts_path_cwd); // TODO: These assertions on input data should be library runtime errors. assert(ts_validate_tileset(tileset)); assert(tm_validate_map(map, tileset)); - iso->iso_space = make_iso_coord_system(iso->map, &iso->screen); + gfx->iso_space = make_iso_coord_system(gfx->map, &gfx->screen); success = true; cleanup: if (!success) { - isogfx_clear(iso); + gfx2d_clear(gfx); } return success; } -int isogfx_world_width(const IsoGfx* iso) { - assert(iso); - return iso->map->world_width; +int gfx2d_world_width(const Gfx2d* gfx) { + assert(gfx); + return gfx->map->world_width; } -int isogfx_world_height(const IsoGfx* iso) { - assert(iso); - return iso->map->world_height; +int gfx2d_world_height(const Gfx2d* gfx) { + assert(gfx); + return gfx->map->world_height; } static void make_tile_from_colour( @@ -368,19 +368,19 @@ static void make_tile_from_colour( } } -Tile isogfx_make_tile(IsoGfx* iso, const TileDesc* desc) { - assert(iso); +Tile gfx2d_make_tile(Gfx2d* gfx, const TileDesc* desc) { + assert(gfx); assert(desc); // Client must create a world first. - assert(iso->map); - assert(iso->tileset); + assert(gfx->map); + assert(gfx->tileset); // Currently, procedural tiles must match the base tile size. - assert(desc->width == iso->map->base_tile_width); - assert(desc->height == iso->map->base_tile_height); + assert(desc->width == gfx->map->base_tile_width); + assert(desc->height == gfx->map->base_tile_height); // Cannot exceed max tiles. - assert(iso->next_tile < iso->tileset->num_tiles); + assert(gfx->next_tile < gfx->tileset->num_tiles); - const Tile tile = iso->next_tile++; + const Tile tile = gfx->next_tile++; const size_t tile_size_bytes = desc->width * desc->height * sizeof(Pixel); @@ -389,16 +389,16 @@ Tile isogfx_make_tile(IsoGfx* iso, const TileDesc* desc) { assert(desc->width > 0); assert(desc->height > 0); - Ts_Tile* const ts_tile = ts_tileset_get_tile_mut(iso->tileset, tile); + Ts_Tile* const ts_tile = ts_tileset_get_tile_mut(gfx->tileset, tile); *ts_tile = (Ts_Tile){ - .width = iso->map->base_tile_width, - .height = iso->map->base_tile_height, + .width = gfx->map->base_tile_width, + .height = gfx->map->base_tile_height, .pixels = tile * tile_size_bytes, }; Pixel* const tile_pixels = - ts_tileset_get_tile_pixels_mut(iso->tileset, tile); + ts_tileset_get_tile_pixels_mut(gfx->tileset, tile); make_tile_from_colour(desc->colour, ts_tile, tile_pixels); break; } @@ -416,31 +416,31 @@ Tile isogfx_make_tile(IsoGfx* iso, const TileDesc* desc) { return tile; } -void isogfx_set_tile(IsoGfx* iso, int x, int y, Tile tile) { - assert(iso); +void gfx2d_set_tile(Gfx2d* gfx, int x, int y, Tile tile) { + assert(gfx); - Tm_Layer* const layer = tm_map_get_layer_mut(iso->map, 0); - Tile* map_tile = tm_layer_get_tile_mut(iso->map, layer, x, y); + Tm_Layer* const layer = tm_map_get_layer_mut(gfx->map, 0); + Tile* map_tile = tm_layer_get_tile_mut(gfx->map, layer, x, y); *map_tile = tile; } -void isogfx_set_tiles(IsoGfx* iso, int x0, int y0, int x1, int y1, Tile tile) { - assert(iso); +void gfx2d_set_tiles(Gfx2d* gfx, int x0, int y0, int x1, int y1, Tile tile) { + assert(gfx); for (int y = y0; y < y1; ++y) { for (int x = x0; x < x1; ++x) { - isogfx_set_tile(iso, x, y, tile); + gfx2d_set_tile(gfx, x, y, tile); } } } -SpriteSheet isogfx_load_sprite_sheet(IsoGfx* iso, const char* filepath) { - assert(iso); +SpriteSheet gfx2d_load_sprite_sheet(Gfx2d* gfx, const char* filepath) { + assert(gfx); assert(filepath); bool success = false; SpriteSheet spriteSheet = 0; - const size_t watermark = memstack_watermark(&iso->stack); + const size_t watermark = memstack_watermark(&gfx->stack); // Load sprite sheet file. printf("Load sprite sheet: %s\n", filepath); @@ -448,7 +448,7 @@ SpriteSheet isogfx_load_sprite_sheet(IsoGfx* iso, const char* filepath) { WITH_FILE(filepath, { const size_t file_size = get_file_size_f(file); ss_sheet = - memstack_alloc_aligned(&iso->stack, file_size, alignof(Ss_SpriteSheet)); + memstack_alloc_aligned(&gfx->stack, file_size, alignof(Ss_SpriteSheet)); success = read_file_f(file, ss_sheet); }); if (!success) { @@ -461,63 +461,63 @@ SpriteSheet isogfx_load_sprite_sheet(IsoGfx* iso, const char* filepath) { cleanup: if (!success) { if (ss_sheet) { - memstack_set_watermark(&iso->stack, watermark); + memstack_set_watermark(&gfx->stack, watermark); } } return spriteSheet; } -Sprite isogfx_make_sprite(IsoGfx* iso, SpriteSheet sheet) { - assert(iso); +Sprite gfx2d_make_sprite(Gfx2d* gfx, SpriteSheet sheet) { + assert(gfx); assert(sheet); // TODO: Remove memstack_alloc() and replace it with a same-name macro that // calls memstack_alloc_aligned() with sizeof/alignof. No real point in // having unaligned allocations. SpriteInstance* sprite = memstack_alloc_aligned( - &iso->stack, sizeof(SpriteInstance), alignof(SpriteInstance)); + &gfx->stack, sizeof(SpriteInstance), alignof(SpriteInstance)); sprite->sheet = (const Ss_SpriteSheet*)sheet; - sprite->next = iso->head_sprite; - iso->head_sprite = sprite; + sprite->next = gfx->head_sprite; + gfx->head_sprite = sprite; return (Sprite)sprite; } -void isogfx_set_sprite_position(IsoGfx* iso, Sprite hSprite, int x, int y) { - assert(iso); +void gfx2d_set_sprite_position(Gfx2d* gfx, Sprite hSprite, int x, int y) { + assert(gfx); SpriteInstance* sprite = (SpriteInstance*)hSprite; sprite->position.x = x; sprite->position.y = y; } -void isogfx_set_sprite_animation(IsoGfx* iso, Sprite hSprite, int animation) { - assert(iso); +void gfx2d_set_sprite_animation(Gfx2d* gfx, Sprite hSprite, int animation) { + assert(gfx); SpriteInstance* sprite = (SpriteInstance*)hSprite; sprite->animation = animation; } -void isogfx_update(IsoGfx* iso, double t) { - assert(iso); +void gfx2d_update(Gfx2d* gfx, double t) { + assert(gfx); // If this is the first time update() is called after initialization, just // record the starting animation time. - if (iso->last_animation_time == 0.0) { - iso->last_animation_time = t; + if (gfx->last_animation_time == 0.0) { + gfx->last_animation_time = t; return; } - if ((t - iso->last_animation_time) >= ANIMATION_UPDATE_DELTA) { + if ((t - gfx->last_animation_time) >= ANIMATION_UPDATE_DELTA) { // TODO: Consider linking animated sprites in a separate list so that we // only walk over those here and not also the static sprites. - for (SpriteInstance* sprite = iso->head_sprite; sprite; + for (SpriteInstance* sprite = gfx->head_sprite; sprite; sprite = sprite->next) { const Ss_SpriteSheet* sheet = sprite->sheet; const Ss_Row* row = ss_get_sprite_sheet_row(sheet, sprite->animation); sprite->frame = (sprite->frame + 1) % row->num_cols; } - iso->last_animation_time = t; + gfx->last_animation_time = t; } } @@ -556,6 +556,7 @@ static void draw_rect( Screen* screen, ivec2 top_left, int rect_width, int rect_height, const Pixel* pixels, const uint8_t* indices) { assert(screen); + assert(pixels); #define rect_pixel(X, Y) \ (indices ? pixels[indices[Y * rect_width + X]] : pixels[Y * rect_width + X]) @@ -584,116 +585,116 @@ static void draw_rect( } /// Draw a tile in an orthogonal map. -static void draw_tile_ortho(IsoGfx* iso, Tile tile, int x, int y) { - assert(iso); - assert(iso->tileset); +static void draw_tile_ortho(Gfx2d* gfx, Tile tile, int x, int y) { + assert(gfx); + assert(gfx->tileset); assert(x >= 0); assert(y >= 0); - assert(x < iso->map->world_width); - assert(y < iso->map->world_height); + assert(x < gfx->map->world_width); + assert(y < gfx->map->world_height); - const Ts_Tile* pTile = ts_tileset_get_tile(iso->tileset, tile); - const Pixel* pixels = ts_tileset_get_tile_pixels(iso->tileset, tile); + const Ts_Tile* pTile = ts_tileset_get_tile(gfx->tileset, tile); + const Pixel* pixels = ts_tileset_get_tile_pixels(gfx->tileset, tile); const ivec2 screen_origin = map2screen( - iso->camera, iso->map->base_tile_width, iso->map->base_tile_height, x, y); + gfx->camera, gfx->map->base_tile_width, gfx->map->base_tile_height, x, y); draw_rect( - &iso->screen, screen_origin, pTile->width, pTile->height, pixels, + &gfx->screen, screen_origin, pTile->width, pTile->height, pixels, nullptr); } /// Draw a tile in an isometric map. -static void draw_tile_iso(IsoGfx* iso, Tile tile, int iso_x, int iso_y) { - assert(iso); - assert(iso->tileset); +static void draw_tile_iso(Gfx2d* gfx, Tile tile, int iso_x, int iso_y) { + assert(gfx); + assert(gfx->tileset); assert(iso_x >= 0); assert(iso_y >= 0); - assert(iso_x < iso->map->world_width); - assert(iso_y < iso->map->world_height); + assert(iso_x < gfx->map->world_width); + assert(iso_y < gfx->map->world_height); - const Ts_Tile* pTile = ts_tileset_get_tile(iso->tileset, tile); - const Pixel* pixels = ts_tileset_get_tile_pixels(iso->tileset, tile); + const Ts_Tile* pTile = ts_tileset_get_tile(gfx->tileset, tile); + const Pixel* pixels = ts_tileset_get_tile_pixels(gfx->tileset, tile); // Compute the screen coordinates of the top diamond-corner of the tile (the // base tile for super tiles). // World (0, 0) -> (screen_width / 2, 0). const ivec2 screen_origin = - iso2cart(iso->iso_space, iso->camera, iso_x, iso_y); + iso2cart(gfx->iso_space, gfx->camera, iso_x, iso_y); // Move from the top diamond-corner to the top-left corner of the tile image. // For regular tiles, tile height == base tile height, so the y offset is 0. // For super tiles, move as high up as the height of the tile. const ivec2 offset = { - -(iso->map->base_tile_width / 2), - pTile->height - iso->map->base_tile_height}; + -(gfx->map->base_tile_width / 2), + pTile->height - gfx->map->base_tile_height}; const ivec2 top_left = ivec2_add(screen_origin, offset); draw_rect( - &iso->screen, top_left, pTile->width, pTile->height, pixels, nullptr); + &gfx->screen, top_left, pTile->width, pTile->height, pixels, nullptr); } -static void draw_map_ortho(IsoGfx* iso) { - assert(iso); - assert(iso->map); +static void draw_map_ortho(Gfx2d* gfx) { + assert(gfx); + assert(gfx->map); // TODO: Same TODOs as in draw_map_iso(). - const Tm_Layer* layer = tm_map_get_layer(iso->map, 0); + const Tm_Layer* layer = tm_map_get_layer(gfx->map, 0); - 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); - draw_tile_ortho(iso, tile, wx, wy); + for (int wy = 0; wy < gfx->map->world_height; ++wy) { + for (int wx = 0; wx < gfx->map->world_width; ++wx) { + const Tile tile = tm_layer_get_tile(gfx->map, layer, wx, wy); + draw_tile_ortho(gfx, tile, wx, wy); } } } -static void draw_map_iso(IsoGfx* iso) { - assert(iso); - assert(iso->map); +static void draw_map_iso(Gfx2d* gfx) { + assert(gfx); + assert(gfx->map); // TODO: Support for multiple layers. - const Tm_Layer* layer = tm_map_get_layer(iso->map, 0); + const Tm_Layer* layer = tm_map_get_layer(gfx->map, 0); // TODO: Culling. // Ex: map the screen corners to tile space to cull. // Ex: walk in screen space and fetch the tile. // The tile-centric approach might be more cache-friendly since the // screen-centric approach would juggle multiple tiles throughout the scan. - 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); - draw_tile_iso(iso, tile, wx, wy); + for (int wy = 0; wy < gfx->map->world_height; ++wy) { + for (int wx = 0; wx < gfx->map->world_width; ++wx) { + const Tile tile = tm_layer_get_tile(gfx->map, layer, wx, wy); + draw_tile_iso(gfx, tile, wx, wy); } } } -static void draw_map(IsoGfx* iso) { - assert(iso); - assert(iso->map); - assert(iso->screen.pixels); +static void draw_map(Gfx2d* gfx) { + assert(gfx); + assert(gfx->map); + assert(gfx->screen.pixels); - const int W = iso->screen.width; - const int H = iso->screen.height; + const int W = gfx->screen.width; + const int H = gfx->screen.height; - memset(iso->screen.pixels, 0, W * H * sizeof(Pixel)); + memset(gfx->screen.pixels, 0, W * H * sizeof(Pixel)); - const Tm_Flags* flags = (const Tm_Flags*)&iso->map->flags; + const Tm_Flags* flags = (const Tm_Flags*)&gfx->map->flags; switch (flags->orientation) { case Tm_Orthogonal: - draw_map_ortho(iso); + draw_map_ortho(gfx); break; case Tm_Isometric: - draw_map_iso(iso); + draw_map_iso(gfx); break; } } /// Draw a sprite in an orthogonal/Cartesian coordinate system. static void draw_sprite_ortho( - IsoGfx* iso, const SpriteInstance* sprite, const Ss_SpriteSheet* sheet) { - assert(iso); + Gfx2d* gfx, const SpriteInstance* sprite, const Ss_SpriteSheet* sheet) { + assert(gfx); assert(sprite); assert(sheet); assert(sprite->animation >= 0); @@ -704,20 +705,20 @@ static void draw_sprite_ortho( // -base_tile_width/2 along the x-axis to align the sprite with the leftmost // edge of the tile it is on. const ivec2 screen_origin = map2screen( - iso->camera, iso->map->base_tile_width, iso->map->base_tile_height, + gfx->camera, gfx->map->base_tile_width, gfx->map->base_tile_height, sprite->position.x, sprite->position.y); const Ss_Row* row = ss_get_sprite_sheet_row(sheet, sprite->animation); const uint8_t* frame = ss_get_sprite_sheet_sprite(sheet, row, sprite->frame); draw_rect( - &iso->screen, screen_origin, sheet->sprite_width, sheet->sprite_height, + &gfx->screen, screen_origin, sheet->sprite_width, sheet->sprite_height, sheet->palette.colours, frame); } /// Draw a sprite in an isometric coordinate system. static void draw_sprite_iso( - IsoGfx* iso, const SpriteInstance* sprite, const Ss_SpriteSheet* sheet) { - assert(iso); + Gfx2d* gfx, const SpriteInstance* sprite, const Ss_SpriteSheet* sheet) { + assert(gfx); assert(sprite); assert(sheet); assert(sprite->animation >= 0); @@ -728,92 +729,92 @@ static void draw_sprite_iso( // -base_tile_width/2 along the x-axis to align the sprite with the leftmost // edge of the tile it is on. const ivec2 screen_origin = iso2cart( - iso->iso_space, iso->camera, sprite->position.x, sprite->position.y); - const ivec2 offset = {-(iso->map->base_tile_width / 2), 0}; + gfx->iso_space, gfx->camera, sprite->position.x, sprite->position.y); + const ivec2 offset = {-(gfx->map->base_tile_width / 2), 0}; const ivec2 top_left = ivec2_add(screen_origin, offset); const Ss_Row* row = ss_get_sprite_sheet_row(sheet, sprite->animation); const uint8_t* frame = ss_get_sprite_sheet_sprite(sheet, row, sprite->frame); draw_rect( - &iso->screen, top_left, sheet->sprite_width, sheet->sprite_height, + &gfx->screen, top_left, sheet->sprite_width, sheet->sprite_height, sheet->palette.colours, frame); } -static void draw_sprites(IsoGfx* iso) { - assert(iso); - assert(iso->map); +static void draw_sprites(Gfx2d* gfx) { + assert(gfx); + assert(gfx->map); - const Tm_Flags* flags = (const Tm_Flags*)&iso->map->flags; + const Tm_Flags* flags = (const Tm_Flags*)&gfx->map->flags; switch (flags->orientation) { case Tm_Orthogonal: - for (const SpriteInstance* sprite = iso->head_sprite; sprite; + for (const SpriteInstance* sprite = gfx->head_sprite; sprite; sprite = sprite->next) { - draw_sprite_ortho(iso, sprite, sprite->sheet); + draw_sprite_ortho(gfx, sprite, sprite->sheet); } break; case Tm_Isometric: - for (const SpriteInstance* sprite = iso->head_sprite; sprite; + for (const SpriteInstance* sprite = gfx->head_sprite; sprite; sprite = sprite->next) { - draw_sprite_iso(iso, sprite, sprite->sheet); + draw_sprite_iso(gfx, sprite, sprite->sheet); } break; } } -void isogfx_set_camera(IsoGfx* iso, int x, int y) { - assert(iso); - iso->camera = (ivec2){x, y}; +void gfx2d_set_camera(Gfx2d* gfx, int x, int y) { + assert(gfx); + gfx->camera = (ivec2){x, y}; } -void isogfx_render(IsoGfx* iso) { - assert(iso); - draw_map(iso); - draw_sprites(iso); +void gfx2d_render(Gfx2d* gfx) { + assert(gfx); + draw_map(gfx); + draw_sprites(gfx); } -void isogfx_draw_tile(IsoGfx* iso, int x, int y, Tile tile) { - assert(iso); - assert(iso->map); +void gfx2d_draw_tile(Gfx2d* gfx, int x, int y, Tile tile) { + assert(gfx); + assert(gfx->map); - const Tm_Flags* flags = (const Tm_Flags*)&iso->map->flags; + const Tm_Flags* flags = (const Tm_Flags*)&gfx->map->flags; switch (flags->orientation) { case Tm_Orthogonal: - draw_tile_ortho(iso, tile, x, y); + draw_tile_ortho(gfx, tile, x, y); break; case Tm_Isometric: - draw_tile_iso(iso, tile, x, y); + draw_tile_iso(gfx, tile, x, y); break; } } -void isogfx_get_screen_size(const IsoGfx* iso, int* width, int* height) { - assert(iso); +void gfx2d_get_screen_size(const Gfx2d* gfx, int* width, int* height) { + assert(gfx); assert(width); assert(height); - *width = iso->screen.width; - *height = iso->screen.height; + *width = gfx->screen.width; + *height = gfx->screen.height; } -const Pixel* isogfx_get_screen_buffer(const IsoGfx* iso) { - assert(iso); - return iso->screen.pixels; +const Pixel* gfx2d_get_screen_buffer(const Gfx2d* gfx) { + assert(gfx); + return gfx->screen.pixels; } -void isogfx_pick_tile( - const IsoGfx* iso, double xcart, double ycart, int* xiso, int* yiso) { - assert(iso); +void gfx2d_pick_tile( + const Gfx2d* gfx, double xcart, double ycart, int* xiso, int* yiso) { + assert(gfx); assert(xiso); assert(yiso); - const vec2 camera = ivec2_to_vec2(iso->camera); + const vec2 camera = ivec2_to_vec2(gfx->camera); const vec2 xy_cart = vec2_add(camera, (vec2){xcart, ycart}); const vec2 xy_iso = cart2iso( - xy_cart, iso->map->base_tile_width, iso->map->base_tile_height, - iso->screen.width); + xy_cart, gfx->map->base_tile_width, gfx->map->base_tile_height, + gfx->screen.width); - if ((0 <= xy_iso.x) && (xy_iso.x < iso->map->world_width) && - (0 <= xy_iso.y) && (xy_iso.y < iso->map->world_height)) { + if ((0 <= xy_iso.x) && (xy_iso.x < gfx->map->world_width) && + (0 <= xy_iso.y) && (xy_iso.y < gfx->map->world_height)) { *xiso = (int)xy_iso.x; *yiso = (int)xy_iso.y; } else { -- cgit v1.2.3