summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Shaw <jshaw@netsurf-browser.org>2007-11-26 22:58:38 (GMT)
committer James Shaw <jshaw@netsurf-browser.org>2007-11-26 22:58:38 (GMT)
commit77ea66727da396aca31721713accb6927327c8f2 (patch)
tree6cfcbe526b18415fdc95d39a5a3ccac9cf3e73b6
parent8c6e6d78d38544ec7922fea54478255d03018f1e (diff)
downloadlibrosprite-77ea66727da396aca31721713accb6927327c8f2.tar.gz
librosprite-77ea66727da396aca31721713accb6927327c8f2.tar.bz2
Begin mask support
svn path=/import/jshaw/libsprite/; revision=9994
-rw-r--r--trunk/example.c17
-rw-r--r--trunk/libsprite.c51
-rw-r--r--trunk/libsprite.h2
3 files changed, 47 insertions, 23 deletions
diff --git a/trunk/example.c b/trunk/example.c
index d1d8853..bdd12b4 100644
--- a/trunk/example.c
+++ b/trunk/example.c
@@ -7,8 +7,14 @@
/* color is 0xrrggbbaa */
void sdl_draw_pixel(SDL_Surface* surface, uint32_t x, uint32_t y, uint32_t color)
{
- uint32_t mapped_color = SDL_MapRGB(surface->format, (color & 0xff000000) >> 24, (color & 0x00ff0000) >> 16, (color & 0x0000ff00) >> 8);
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 / 256.0);
+ uint32_t g = ((color & 0x00ff0000) >> 16) * (alpha / 256.0);
+ uint32_t b = ((color & 0x0000ff00) >> 8) * (alpha / 256.0);
+ uint32_t mapped_color = SDL_MapRGBA(surface->format, r, g, b, alpha);
+
*pixel = mapped_color;
}
@@ -50,7 +56,8 @@ int main(int argc, char *argv[])
printf("extension_size %u\n", sprite_area->extension_size);
SDL_Surface *screen;
- screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
+ screen = SDL_SetVideoMode(800, 600, 32, SDL_ANYFORMAT);
+ SDL_SetAlpha(screen, SDL_SRCALPHA, 0);
for (uint32_t i = 0; i < sprite_area->sprite_count; i++) {
struct sprite* sprite = sprite_area->sprites[i];
@@ -65,8 +72,8 @@ int main(int argc, char *argv[])
printf("hasPalette %s\n", sprite->has_palette ? "YES" : "NO");
if (sprite->has_palette) printf("paletteSize %u\n", sprite->palettesize);
- printf("hasMask %s\n", sprite->hasmask ? "YES" : "NO");
- if (sprite->hasmask) printf("maskbpp %u\n", sprite->mode->maskbpp);
+ printf("hasMask %s\n", sprite->has_mask ? "YES" : "NO");
+ if (sprite->has_mask) printf("maskbpp %u\n", sprite->mode->maskbpp);
sdl_blank(screen);
@@ -76,7 +83,7 @@ int main(int argc, char *argv[])
}
}
- SDL_UpdateRect(screen, 0, 0, 799, 599);
+ SDL_UpdateRect(screen, 0, 0, 0, 0);
fgetc(stdin);
}
diff --git a/trunk/libsprite.c b/trunk/libsprite.c
index f52e200..2120f94 100644
--- a/trunk/libsprite.c
+++ b/trunk/libsprite.c
@@ -18,6 +18,12 @@ struct sprite_header {
uint32_t first_used_bit; /* old format only (spriteType = 0) */
uint32_t last_used_bit;
uint32_t image_size; /* bytes */
+ uint32_t mask_size;
+};
+
+struct sprite_mask_state {
+ uint32_t x;
+ uint32_t y;
};
static struct sprite_mode oldmodes[256];
@@ -331,6 +337,16 @@ uint32_t sprite_upscale_color(uint32_t pixel, struct sprite_mode* mode)
}
}
+/* Get the next mask byte.
+ * Mask of 0xff denotes 100% opaque, 0x00 denotes 100% transparent
+ */
+uint8_t sprite_get_mask(struct sprite* sprite, struct sprite_header* header, struct sprite_mask_state* mask_state)
+{
+ /* a 1bpp mask (for new mode sprites), each row is word aligned (therefore potential righthand wastage */
+ sprite = sprite; header = header; mask_state = mask_state;
+ return 0xff;
+}
+
void sprite_load_high_color(uint8_t* image_in, uint8_t* mask, struct sprite* sprite, struct sprite_header* header)
{
mask = mask;
@@ -440,22 +456,20 @@ struct sprite* sprite_load_sprite(FILE* spritefile)
sprite->palettesize = imageOffset - 44;
sprite->has_palette = (sprite->palettesize > 0);
- uint32_t image_size;
- uint32_t maskSize;
+ /* sprite has no mask if imageOffset == maskOffset */
if (imageOffset == maskOffset) {
- sprite->hasmask = false;
- image_size = nextSpriteOffset - 44 - sprite->palettesize;
- maskSize = 0;
+ sprite->has_mask = false;
+ header->image_size = nextSpriteOffset - 44 - sprite->palettesize;
+ header->mask_size = 0;
} else {
- sprite->hasmask = true;
- image_size = maskOffset - imageOffset;
- maskSize = nextSpriteOffset - 44 - sprite->palettesize - image_size;
+ sprite->has_mask = true;
+ header->image_size = maskOffset - imageOffset;
+ header->mask_size = nextSpriteOffset - 44 - sprite->palettesize - header->image_size;
}
- header->image_size = image_size;
-
- if (sprite->hasmask) LOGDBG("maskSize %u\n", maskSize);
+ if (sprite->has_mask) LOGDBG("mask_size (bits) %u\n", header->mask_size * 8);
+ if (sprite->has_mask) LOGDBG(" w*h %u\n", sprite->width * sprite->height);
if (sprite->has_palette) {
assert(sprite->palettesize % 8 == 0);
@@ -478,15 +492,18 @@ struct sprite* sprite_load_sprite(FILE* spritefile)
}
}
- uint8_t* image = malloc(image_size);
- sprite_read_bytes(spritefile, image, image_size);
+ uint8_t* image = malloc(header->image_size);
+ sprite_read_bytes(spritefile, image, header->image_size);
- uint8_t* mask = malloc(maskSize);
- sprite_read_bytes(spritefile, mask, maskSize);
+ uint8_t* mask = NULL;
+ if (sprite->has_mask) {
+ mask = malloc(header->mask_size);
+ sprite_read_bytes(spritefile, mask, header->mask_size);
+ }
- /* sanity check imageSize */
+ /* sanity check image_size */
assert((header->width_words) * 4 * (sprite->height) == header->image_size);
- /* TODO: sanity check maskSize */
+ /* TODO: sanity check mask_size */
if (sprite->mode->colorbpp > 8) {
sprite_load_high_color(image, mask, sprite, header);
} else {
diff --git a/trunk/libsprite.h b/trunk/libsprite.h
index 3fdc6c8..8d89631 100644
--- a/trunk/libsprite.h
+++ b/trunk/libsprite.h
@@ -30,7 +30,7 @@ struct sprite_palette {
struct sprite {
unsigned char name[13]; /* last byte for 0 terminator */
struct sprite_mode* mode;
- bool hasmask;
+ bool has_mask;
bool has_palette;
uint32_t palettesize; /* in number of entries (each entry is a word) */
uint32_t* palette;