From fd6e983aff4bbeb2e9e7e5115fafa1aca7e409a8 Mon Sep 17 00:00:00 2001 From: James Shaw Date: Wed, 28 Nov 2007 16:44:17 +0000 Subject: Broken implementation of mask for low colour sprites svn path=/import/jshaw/libsprite/; revision=9998 --- trunk/example.c | 11 ++-- trunk/libsprite.c | 169 +++++++++++++++++++++++++++++------------------------- 2 files changed, 99 insertions(+), 81 deletions(-) diff --git a/trunk/example.c b/trunk/example.c index 8424c11..f2a7a86 100644 --- a/trunk/example.c +++ b/trunk/example.c @@ -9,11 +9,14 @@ void sdl_draw_pixel(SDL_Surface* surface, uint32_t x, uint32_t y, uint32_t color { uint32_t* pixel = ((uint32_t*) (surface->pixels)) + (y * surface->pitch/4) + x; /* pretty sure SDL can do this, but can't figure out how */ - uint32_t alpha = 0xff;/*color & 0x000000ff;*/ - uint32_t r = ((color & 0xff000000) >> 24) * (alpha / 255.0); + uint32_t alpha = color & 0x000000ff; + uint32_t r = alpha; + uint32_t g = alpha; + uint32_t b = alpha; + /*uint32_t r = ((color & 0xff000000) >> 24) * (alpha / 255.0); uint32_t g = ((color & 0x00ff0000) >> 16) * (alpha / 255.0); - uint32_t b = ((color & 0x0000ff00) >> 8) * (alpha / 255.0); - uint32_t mapped_color = SDL_MapRGBA(surface->format, r, g, b, alpha); + uint32_t b = ((color & 0x0000ff00) >> 8) * (alpha / 255.0);*/ + uint32_t mapped_color = SDL_MapRGB(surface->format, r, g, b); *pixel = mapped_color; } diff --git a/trunk/libsprite.c b/trunk/libsprite.c index c3806ac..aef9f96 100644 --- a/trunk/libsprite.c +++ b/trunk/libsprite.c @@ -45,82 +45,82 @@ static const uint8_t sprite_16bpp_translate[] = { * which in turn requires sprite_load_palette(FILE* f) * defined in this file */ -static const uint32_t sprite_1bpp_palette[] = { 0xffffff00, 0x0 }; +static const uint32_t sprite_1bpp_palette[] = { 0xffffffff, 0xff }; -static const uint32_t sprite_2bpp_palette[] = { 0xffffff00, 0xbbbbbb00, 0x77777700, 0x0 }; +static const uint32_t sprite_2bpp_palette[] = { 0xffffffff, 0xbbbbbbff, 0x777777ff, 0xff }; static const uint32_t sprite_4bpp_palette[] = { -0xffffff00, 0xdddddd00, 0xbbbbbb00, 0x99999900, -0x77777700, 0x55555500, 0x33333300, 0x0, -0x449900, 0xeeee0000, 0xcc0000, 0xdd000000, -0xeeeebb00, 0x55880000, 0xffbb0000, 0xbbff00 +0xffffffff, 0xddddddff, 0xbbbbbbff, 0x999999ff, +0x777777ff, 0x555555ff, 0x333333ff, 0xff, +0x4499ff, 0xeeee00ff, 0xcc00ff, 0xdd0000ff, +0xeeeebbff, 0x558800ff, 0xffbb00ff, 0xbbffff }; static const uint32_t sprite_8bpp_palette[] = { -0x0, 0x11111100, 0x22222200, 0x33333300, -0x44000000, 0x55111100, 0x66222200, 0x77333300, -0x4400, 0x11115500, 0x22226600, 0x33337700, -0x44004400, 0x55115500, 0x66226600, 0x77337700, -0x88000000, 0x99111100, 0xaa222200, 0xbb333300, -0xcc000000, 0xdd111100, 0xee222200, 0xff333300, -0x88004400, 0x99115500, 0xaa226600, 0xbb337700, -0xcc004400, 0xdd115500, 0xee226600, 0xff337700, -0x440000, 0x11551100, 0x22662200, 0x33773300, -0x44440000, 0x55551100, 0x66662200, 0x77773300, -0x444400, 0x11555500, 0x22666600, 0x33777700, -0x44444400, 0x55555500, 0x66666600, 0x77777700, -0x88440000, 0x99551100,0xaa662200, 0xbb773300, -0xcc440000, 0xdd551100, 0xee662200, 0xff773300, -0x88444400, 0x99555500, 0xaa666600, 0xbb777700, -0xcc444400, 0xdd555500, 0xee666600, 0xff777700, -0x880000, 0x11991100, 0x22aa2200, 0x33bb3300, -0x44880000, 0x55991100, 0x66aa2200, 0x77bb3300, -0x884400, 0x11995500, 0x22aa6600, 0x33bb7700, -0x44884400, 0x55995500, 0x66aa6600, 0x77bb7700, -0x88880000, 0x99991100, 0xaaaa2200, 0xbbbb3300, -0xcc880000, 0xdd991100, 0xeeaa2200, 0xffbb3300, -0x88884400, 0x99995500, 0xaaaa6600, 0xbbbb7700, -0xcc884400, 0xdd995500, 0xeeaa6600, 0xffbb7700, -0xcc0000, 0x11dd1100, 0x22ee2200, 0x33ff3300, -0x44cc0000, 0x55dd1100, 0x66ee2200, 0x77ff3300, -0xcc4400, 0x11dd5500, 0x22ee6600, 0x33ff7700, -0x44cc4400, 0x55dd5500, 0x66ee6600, 0x77ff7700, -0x88cc0000, 0x99dd1100, 0xaaee2200, 0xbbff3300, -0xcccc0000, 0xdddd1100, 0xeeee2200, 0xffff3300, -0x88cc4400, 0x99dd5500, 0xaaee6600, 0xbbff7700, -0xcccc4400, 0xdddd5500, 0xeeee6600, 0xffff7700, -0x8800, 0x11119900, 0x2222aa00, 0x3333bb00, -0x44008800, 0x55119900, 0x6622aa00, 0x7733bb00, -0xcc00, 0x1111dd00, 0x2222ee00, 0x3333ff00, -0x4400cc00, 0x5511dd00, 0x6622ee00, 0x7733ff00, -0x88008800, 0x99119900, 0xaa22aa00, 0xbb33bb00, -0xcc008800, 0xdd119900, 0xee22aa00, 0xff33bb00, -0x8800cc00, 0x9911dd00, 0xaa22ee00, 0xbb33ff00, -0xcc00cc00, 0xdd11dd00, 0xee22ee00, 0xff33ff00, -0x448800, 0x11559900, 0x2266aa00, 0x3377bb00, -0x44448800, 0x55559900, 0x6666aa00, 0x7777bb00, -0x44cc00, 0x1155dd00, 0x2266ee00, 0x3377ff00, -0x4444cc00, 0x5555dd00, 0x6666ee00, 0x7777ff00, -0x88448800, 0x99559900, 0xaa66aa00, 0xbb77bb00, -0xcc448800, 0xdd559900, 0xee66aa00, 0xff77bb00, -0x8844cc00, 0x9955dd00, 0xaa66ee00, 0xbb77ff00, -0xcc44cc00, 0xdd55dd00, 0xee66ee00, 0xff77ff00, -0x888800, 0x11999900, 0x22aaaa00, 0x33bbbb00, -0x44888800, 0x55999900, 0x66aaaa00, 0x77bbbb00, -0x88cc00, 0x1199dd00, 0x22aaee00, 0x33bbff00, -0x4488cc00, 0x5599dd00, 0x66aaee00, 0x77bbff00, -0x88888800, 0x99999900, 0xaaaaaa00, 0xbbbbbb00, -0xcc888800, 0xdd999900, 0xeeaaaa00, 0xffbbbb00, -0x8888cc00, 0x9999dd00, 0xaaaaee00, 0xbbbbff00, -0xcc88cc00, 0xdd99dd00, 0xeeaaee00, 0xffbbff00, -0xcc8800, 0x11dd9900, 0x22eeaa00, 0x33ffbb00, -0x44cc8800, 0x55dd9900, 0x66eeaa00, 0x77ffbb00, -0xcccc00, 0x11dddd00, 0x22eeee00, 0x33ffff00, -0x44cccc00, 0x55dddd00, 0x66eeee00, 0x77ffff00, -0x88cc8800, 0x99dd9900, 0xaaeeaa00, 0xbbffbb00, -0xcccc8800, 0xdddd9900, 0xeeeeaa00, 0xffffbb00, -0x88cccc00, 0x99dddd00, 0xaaeeee00, 0xbbffff00, -0xcccccc00, 0xdddddd00, 0xeeeeee00, 0xffffff00 +0xff, 0x111111ff, 0x222222ff, 0x333333ff, +0x440000ff, 0x551111ff, 0x662222ff, 0x773333ff, +0x44ff, 0x111155ff, 0x222266ff, 0x333377ff, +0x440044ff, 0x551155ff, 0x662266ff, 0x773377ff, +0x880000ff, 0x991111ff, 0xaa2222ff, 0xbb3333ff, +0xcc0000ff, 0xdd1111ff, 0xee2222ff, 0xff3333ff, +0x880044ff, 0x991155ff, 0xaa2266ff, 0xbb3377ff, +0xcc0044ff, 0xdd1155ff, 0xee2266ff, 0xff3377ff, +0x4400ff, 0x115511ff, 0x226622ff, 0x337733ff, +0x444400ff, 0x555511ff, 0x666622ff, 0x777733ff, +0x4444ff, 0x115555ff, 0x226666ff, 0x337777ff, +0x444444ff, 0x555555ff, 0x666666ff, 0x777777ff, +0x884400ff, 0x995511ff, 0xaa6622ff, 0xbb7733ff, +0xcc4400ff, 0xdd5511ff, 0xee6622ff, 0xff7733ff, +0x884444ff, 0x995555ff, 0xaa6666ff, 0xbb7777ff, +0xcc4444ff, 0xdd5555ff, 0xee6666ff, 0xff7777ff, +0x8800ff, 0x119911ff, 0x22aa22ff, 0x33bb33ff, +0x448800ff, 0x559911ff, 0x66aa22ff, 0x77bb33ff, +0x8844ff, 0x119955ff, 0x22aa66ff, 0x33bb77ff, +0x448844ff, 0x559955ff, 0x66aa66ff, 0x77bb77ff, +0x888800ff, 0x999911ff, 0xaaaa22ff, 0xbbbb33ff, +0xcc8800ff, 0xdd9911ff, 0xeeaa22ff, 0xffbb33ff, +0x888844ff, 0x999955ff, 0xaaaa66ff, 0xbbbb77ff, +0xcc8844ff, 0xdd9955ff, 0xeeaa66ff, 0xffbb77ff, +0xcc00ff, 0x11dd11ff, 0x22ee22ff, 0x33ff33ff, +0x44cc00ff, 0x55dd11ff, 0x66ee22ff, 0x77ff33ff, +0xcc44ff, 0x11dd55ff, 0x22ee66ff, 0x33ff77ff, +0x44cc44ff, 0x55dd55ff, 0x66ee66ff, 0x77ff77ff, +0x88cc00ff, 0x99dd11ff, 0xaaee22ff, 0xbbff33ff, +0xcccc00ff, 0xdddd11ff, 0xeeee22ff, 0xffff33ff, +0x88cc44ff, 0x99dd55ff, 0xaaee66ff, 0xbbff77ff, +0xcccc44ff, 0xdddd55ff, 0xeeee66ff, 0xffff77ff, +0x88ff, 0x111199ff, 0x2222aaff, 0x3333bbff, +0x440088ff, 0x551199ff, 0x6622aaff, 0x7733bbff, +0xccff, 0x1111ddff, 0x2222eeff, 0x3333ffff, +0x4400ccff, 0x5511ddff, 0x6622eeff, 0x7733ffff, +0x880088ff, 0x991199ff, 0xaa22aaff, 0xbb33bbff, +0xcc0088ff, 0xdd1199ff, 0xee22aaff, 0xff33bbff, +0x8800ccff, 0x9911ddff, 0xaa22eeff, 0xbb33ffff, +0xcc00ccff, 0xdd11ddff, 0xee22eeff, 0xff33ffff, +0x4488ff, 0x115599ff, 0x2266aaff, 0x3377bbff, +0x444488ff, 0x555599ff, 0x6666aaff, 0x7777bbff, +0x44ccff, 0x1155ddff, 0x2266eeff, 0x3377ffff, +0x4444ccff, 0x5555ddff, 0x6666eeff, 0x7777ffff, +0x884488ff, 0x995599ff, 0xaa66aaff, 0xbb77bbff, +0xcc4488ff, 0xdd5599ff, 0xee66aaff, 0xff77bbff, +0x8844ccff, 0x9955ddff, 0xaa66eeff, 0xbb77ffff, +0xcc44ccff, 0xdd55ddff, 0xee66eeff, 0xff77ffff, +0x8888ff, 0x119999ff, 0x22aaaaff, 0x33bbbbff, +0x448888ff, 0x559999ff, 0x66aaaaff, 0x77bbbbff, +0x88ccff, 0x1199ddff, 0x22aaeeff, 0x33bbffff, +0x4488ccff, 0x5599ddff, 0x66aaeeff, 0x77bbffff, +0x888888ff, 0x999999ff, 0xaaaaaaff, 0xbbbbbbff, +0xcc8888ff, 0xdd9999ff, 0xeeaaaaff, 0xffbbbbff, +0x8888ccff, 0x9999ddff, 0xaaaaeeff, 0xbbbbffff, +0xcc88ccff, 0xdd99ddff, 0xeeaaeeff, 0xffbbffff, +0xcc88ff, 0x11dd99ff, 0x22eeaaff, 0x33ffbbff, +0x44cc88ff, 0x55dd99ff, 0x66eeaaff, 0x77ffbbff, +0xccccff, 0x11ddddff, 0x22eeeeff, 0x33ffffff, +0x44ccccff, 0x55ddddff, 0x66eeeeff, 0x77ffffff, +0x88cc88ff, 0x99dd99ff, 0xaaeeaaff, 0xbbffbbff, +0xcccc88ff, 0xdddd99ff, 0xeeeeaaff, 0xffffbbff, +0x88ccccff, 0x99ddddff, 0xaaeeeeff, 0xbbffffff, +0xccccccff, 0xddddddff, 0xeeeeeeff, 0xffffffff }; void sprite_init(void) @@ -360,9 +360,9 @@ struct sprite_mask_state* sprite_init_mask_state(struct sprite* sprite, struct s mask_state->x = header->first_used_bit; mask_state->y = 0; mask_state->first_used_bit = header->first_used_bit; - mask_state->row_max_bit = sprite->width * sprite->mode->maskbpp; + mask_state->row_max_bit = sprite->width * sprite->mode->mask_width; mask_state->height = sprite->height; - mask_state->bpp = sprite->mode->maskbpp; + mask_state->bpp = sprite->mode->mask_width; mask_state->current_word = BTUINT(mask); mask_state->current_byte_index = 4; @@ -372,12 +372,12 @@ struct sprite_mask_state* sprite_init_mask_state(struct sprite* sprite, struct s /* Get the next mask byte. * Mask of 0xff denotes 100% opaque, 0x00 denotes 100% transparent */ -uint8_t sprite_next_mask_pixel(uint8_t* mask, struct sprite_mask_state* mask_state) +uint32_t sprite_next_mask_pixel(uint8_t* mask, struct sprite_mask_state* mask_state) { /* a 1bpp mask (for new mode sprites), each row is word aligned (therefore potential righthand wastage */ const uint32_t bitmask = (1 << mask_state->bpp) - 1; const uint32_t offset_into_word = mask_state->x % 32; - const uint8_t pixel = (mask_state->current_word & (bitmask << offset_into_word)) >> offset_into_word; + uint32_t pixel = (mask_state->current_word & (bitmask << offset_into_word)) >> offset_into_word; if (mask_state->x + mask_state->bpp < mask_state->row_max_bit && offset_into_word + mask_state->bpp == 32) { mask_state->current_word = BTUINT((mask + mask_state->current_byte_index)); @@ -396,6 +396,13 @@ uint8_t sprite_next_mask_pixel(uint8_t* mask, struct sprite_mask_state* mask_sta mask_state->y++; } + /* + * if the mask is 1bpp, make sure the whole byte is 0x00 or 0xff + */ + if (mask_state->bpp < 8) { + pixel = (pixel & 1) ? 0xff : 0x00; + } + return pixel; } @@ -436,7 +443,9 @@ void sprite_load_high_color(uint8_t* image_in, uint8_t* mask, struct sprite* spr void sprite_load_low_color(uint8_t* image_in, uint8_t* mask, struct sprite* sprite, struct sprite_header* header) { - mask = mask; + struct sprite_mask_state* mask_state = NULL; + if (sprite->has_mask) mask_state = sprite_init_mask_state(sprite, header, mask); + sprite->image = malloc(sprite->width * sprite->height * 4); /* all image data is 32bpp going out */ const uint32_t bpp = sprite->mode->colorbpp; @@ -454,6 +463,10 @@ void sprite_load_low_color(uint8_t* image_in, uint8_t* mask, struct sprite* spri uint32_t pixel = (currentword & (bitmask << offset_into_word)) >> offset_into_word; pixel = sprite_palette_lookup(sprite, pixel); /* lookup returns 32bpp */ + if (sprite->has_mask) { + uint8_t mask_pixel = sprite_next_mask_pixel(mask, mask_state); + pixel = pixel | mask_pixel; + } sprite->image[y*sprite->width + x_pixels] = pixel; x_pixels++; @@ -470,6 +483,8 @@ void sprite_load_low_color(uint8_t* image_in, uint8_t* mask, struct sprite* spri current_byte_index += 4; } } + + if (sprite->has_mask) free(mask_state); } struct sprite* sprite_load_sprite(FILE* spritefile) @@ -538,7 +553,7 @@ struct sprite* sprite_load_sprite(FILE* spritefile) assert(word1 == word2); /* TODO: if they aren't, START FLASHING */ /* swap rr and bb parts (seems to give the right result, but where is it documented? PRM1-731 */ - uint32_t entry = ((word1 & 0xff000000) >> 16) | (word1 & 0x00ff0000) | ((word1 & 0x0000ff00) << 16); + uint32_t entry = ((word1 & 0xff000000) >> 16) | (word1 & 0x00ff0000) | ((word1 & 0x0000ff00) << 16) | 0xff; sprite->palette[j] = entry; } } @@ -615,7 +630,7 @@ struct sprite_palette* sprite_load_palette(FILE* f) if (b[2] == 16) { /*assert(c == b[1]);*/ - uint32_t entry = (b[3] << 24) | (b[4] << 16) | (b[5] << 8); + uint32_t entry = (b[3] << 24) | (b[4] << 16) | (b[5] << 8) | 0xff; /* last byte is alpha */ palette->palette[c] = entry; c++; -- cgit v1.2.3