From cef3385c2bee0b098a7795548345a9281ace008e Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Wed, 26 Jul 2023 08:39:37 -0700 Subject: Add support for paletted sprites. --- gfx-iso/src/isogfx.c | 65 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 19 deletions(-) (limited to 'gfx-iso/src/isogfx.c') diff --git a/gfx-iso/src/isogfx.c b/gfx-iso/src/isogfx.c index 9ba1bec..4568375 100644 --- a/gfx-iso/src/isogfx.c +++ b/gfx-iso/src/isogfx.c @@ -100,26 +100,49 @@ static inline const Ts_Tile* ts_tileset_get_next_tile( /// The pixels of the row follow a "sprite-major" order. It contains the /// 'sprite_width * sprite_height' pixels for the first column/sprite, then the /// second column/sprite, etc. +/// +/// Pixels are 8-bit indices into the sprite sheet's colour palette. typedef struct Ss_Row { uint16_t num_cols; /// Number of columns in this row. - Pixel pixels[1]; /// Count: num_cols * sprite_width * sprite_height. + uint8_t pixels[1]; /// Count: num_cols * sprite_width * sprite_height. } Ss_Row; +typedef struct Ss_Palette { + uint16_t num_colours; + Pixel colours[1]; /// Count: num_colors. +} Ss_Palette; + /// Sprite sheet top-level data definition. /// /// Sprite width and height are assumed constant throughout the sprite sheet. typedef struct Ss_SpriteSheet { - uint16_t sprite_width; /// Sprite width in pixels. - uint16_t sprite_height; /// Sprite height in pixels. - uint16_t num_rows; - Ss_Row rows[1]; /// Count: num_rows. + uint16_t sprite_width; /// Sprite width in pixels. + uint16_t sprite_height; /// Sprite height in pixels. + uint16_t num_rows; + Ss_Palette palette; /// Variable size. + Ss_Row rows[1]; /// Count: num_rows. Variable offset. } Ss_SpriteSheet; -const Ss_Row* get_sprite_sheet_row(const Ss_SpriteSheet* sheet, int row) { +static inline const Ss_Row* get_sprite_sheet_row( + const Ss_SpriteSheet* sheet, int row) { assert(sheet); assert(row >= 0); assert(row < sheet->num_rows); - return &sheet->rows[row]; + // Skip over the palette. + const Ss_Row* rows = + (const Ss_Row*)(&sheet->palette.colours[0] + sheet->palette.num_colours); + return &rows[row]; +} + +static inline const uint8_t* get_sprite_sheet_sprite( + const Ss_SpriteSheet* sheet, const Ss_Row* row, int col) { + assert(sheet); + assert(row); + assert(col >= 0); + assert(col < row->num_cols); + const int sprite_offset = col * sheet->sprite_width * sheet->sprite_height; + const uint8_t* sprite = &row->pixels[sprite_offset]; + return sprite; } // ----------------------------------------------------------------------------- @@ -732,11 +755,19 @@ static Pixel alpha_blend(Pixel src, Pixel dst) { /// /// The rectangle's pixels are assumed to be arranged in a linear, row-major /// fashion. +/// +/// If indices are given, then the image is assumed to be colour-paletted, where +/// 'pixels' is the palette and 'indices' the pixel indices. Otherwise, the +/// image is assumed to be in plain RGBA format. static void draw_rect( IsoGfx* iso, ivec2 origin, int rect_width, int rect_height, - const Pixel* pixels) { + const Pixel* pixels, const uint8_t* indices) { assert(iso); +#define rect_pixel(x, y) \ + (indices ? pixels[indices[py * rect_width + px]] \ + : pixels[py * rect_width + px]) + // Rect can exceed screen bounds, so we must clip it. #define max(a, b) (a > b ? a : b) const int py_offset = max(0, rect_height - origin.y); @@ -748,7 +779,7 @@ static void draw_rect( const int sy = origin.y + py - py_offset; for (int px = 0; (px < rect_width) && (origin.x + px < iso->screen_width); ++px) { - const Pixel colour = pixels[py * rect_width + px]; + const Pixel colour = rect_pixel(px, py); if (colour.a > 0) { const int sx = origin.x + px; const Pixel dst = screen_xy(iso, sx, sy); @@ -767,7 +798,7 @@ static void draw_tile(IsoGfx* iso, ivec2 origin, Tile tile) { const Pixel* pixels = tile_xy_const_ref(iso, tile_data, 0, 0); - draw_rect(iso, origin, tile_data->width, tile_data->height, pixels); + draw_rect(iso, origin, tile_data->width, tile_data->height, pixels, 0); } static void draw(IsoGfx* iso) { @@ -807,15 +838,11 @@ static void draw_sprite( assert(sprite->animation < sheet->num_rows); assert(sprite->frame >= 0); - const SpriteSheetRow* ss_row = &sheet->rows[sprite->animation]; - assert(sprite->frame < ss_row->num_cols); - - const int sprite_offset = - sprite->frame * sheet->sprite_width * sheet->sprite_height; - - const Pixel* frame = &ss_row->pixels[sprite_offset]; - - draw_rect(iso, origin, sheet->sprite_width, sheet->sprite_height, frame); + const SpriteSheetRow* row = get_sprite_sheet_row(sheet, sprite->animation); + const uint8_t* frame = get_sprite_sheet_sprite(sheet, row, sprite->frame); + draw_rect( + iso, origin, sheet->sprite_width, sheet->sprite_height, + sheet->palette.colours, frame); } static void draw_sprites(IsoGfx* iso) { -- cgit v1.2.3