summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Shaw <jshaw@netsurf-browser.org>2007-11-26 14:28:15 +0000
committerJames Shaw <jshaw@netsurf-browser.org>2007-11-26 14:28:15 +0000
commit1881d513165e082155b4fa3472713521070b7ada (patch)
tree88b3415eebfaa87fb2bdaed5468ae3d87227d462
parentf7c9c74640eedd25cad4da3720d8ba5cbb0d8863 (diff)
downloadlibrosprite-1881d513165e082155b4fa3472713521070b7ada.tar.gz
librosprite-1881d513165e082155b4fa3472713521070b7ada.tar.bz2
Fix 16bpp handling, clear SDL screen between sprites
svn path=/import/jshaw/libsprite/; revision=9989
-rw-r--r--trunk/Makefile5
-rw-r--r--trunk/example.c13
-rw-r--r--trunk/libsprite.c37
3 files changed, 48 insertions, 7 deletions
diff --git a/trunk/Makefile b/trunk/Makefile
index e6e712e..9aca95c 100644
--- a/trunk/Makefile
+++ b/trunk/Makefile
@@ -15,4 +15,7 @@ libsprite.a: libsprite.o
${AR} ${ARFLAGS} libsprite.a libsprite.o
%.o: %.c
- ${CC} -c -g ${CFLAGS} -o $@ $< \ No newline at end of file
+ ${CC} -c -g ${CFLAGS} -o $@ $<
+
+clean:
+ rm -f $(wildcard *.o) $(wildcard *.a) example \ No newline at end of file
diff --git a/trunk/example.c b/trunk/example.c
index ff2f72c..9bd5627 100644
--- a/trunk/example.c
+++ b/trunk/example.c
@@ -12,6 +12,15 @@ void sdl_draw_pixel(SDL_Surface* surface, uint32_t x, uint32_t y, uint32_t color
*pixel = mapped_color;
}
+void sdl_blank(SDL_Surface* surface)
+{
+ for (uint32_t y = 0; y < (uint32_t) surface->h; y++) {
+ for (uint32_t x = 0; x < (uint32_t) surface->w; x++) {
+ sdl_draw_pixel(surface, x, y, (uint32_t) 0);
+ }
+ }
+}
+
int main(int argc, char *argv[])
{
if (argc < 2) {
@@ -19,7 +28,7 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
+ if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
@@ -59,6 +68,8 @@ int main(int argc, char *argv[])
printf("hasMask %s\n", sprite->hasmask ? "YES" : "NO");
if (sprite->hasmask) printf("maskbpp %u\n", sprite->mode->maskbpp);
+ sdl_blank(screen);
+
for (uint32_t y = 0; y < sprite->height; y++) {
for (uint32_t x = 0; x < sprite->width; x++) {
sdl_draw_pixel(screen, x, y, sprite->image[y*sprite->width + x]);
diff --git a/trunk/libsprite.c b/trunk/libsprite.c
index 67f7bad..31c2db2 100644
--- a/trunk/libsprite.c
+++ b/trunk/libsprite.c
@@ -21,6 +21,13 @@ struct sprite_header {
static struct sprite_mode oldmodes[256];
+static uint8_t sprite_16bpp_translate[] = {
+ 0x00, 0x08, 0x10, 0x18, 0x20, 0x29, 0x31, 0x39,
+ 0x41, 0x4a, 0x52, 0x5a, 0x62, 0x6a, 0x73, 0x7b,
+ 0x83, 0x8b, 0x94, 0x9c, 0xa4, 0xac, 0xb4, 0xbd,
+ 0xc5, 0xcd, 0xd5, 0xde, 0xe6, 0xee, 0xf6, 0xff
+};
+
void sprite_init()
{
for (uint32_t i = 0; i < 256; i++) {
@@ -163,12 +170,28 @@ uint32_t sprite_upscale_color(uint32_t pixel, uint32_t bpp)
{
switch (bpp) {
case 32:
- return pixel;
+ /* reverse byte order */
+ return ((pixel & (0x000000ff)) << 24) | ((pixel & 0x0000ff00) << 8) | ((pixel & 0x00ff0000) >> 8) | ((pixel & 0xff000000) >> 24);
case 24:
return pixel & 0x00001700; /* TODO: mask out alpha -- any point? */
case 16:
- /* TODO */
+ /* assume incoming format of b_00000000000000000bbbbbgggggrrrrr */
+ {
+ uint8_t red = pixel & 31;
+ uint8_t green = (pixel & (31 << 5)) >> 5;
+ uint8_t blue = (pixel & (31 << 10)) >> 10;
+
+ /* sanity check */
+ assert(red < 32);
+ assert(green < 32);
+ assert(blue < 32);
+
+ pixel = (sprite_16bpp_translate[red] << 24)
+ | (sprite_16bpp_translate[green] << 16)
+ | (sprite_16bpp_translate[blue] << 8);
+ printf("%x %x %x ", red, green, blue);
return pixel;
+ }
case 8:
case 4:
case 2:
@@ -200,7 +223,7 @@ void sprite_load_high_color(uint8_t* image_in, uint8_t* mask, struct sprite* spr
uint32_t pixel = 0;
for (uint32_t j = 0; j < bytesPerPixel; j++) {
uint8_t b = image_in[currentByteIndex++];
- pixel = pixel | (b << ((bytesPerPixel - j - 1) * 8));
+ pixel = pixel | (b << (j * 8));
}
pixel = sprite_upscale_color(pixel, bpp);
@@ -230,12 +253,14 @@ void sprite_load_low_color(uint8_t* image_in, uint8_t* mask, struct sprite* spri
current_byte_index += 4;
for (uint32_t y = 0; y < sprite->height; y++) {
+ uint32_t x_pixels = 0;
for (uint32_t x = header->first_used_bit; x < row_max_bit ; x += bpp) {
const uint32_t offset_into_word = x % 32;
uint32_t pixel = (currentword & (bitmask << offset_into_word)) >> offset_into_word;
pixel = sprite_palette_lookup(sprite, pixel); /* lookup returns 32bpp */
- sprite->image[y*sprite->width + x] = pixel;
+ sprite->image[y*sprite->width + x_pixels] = pixel;
+ x_pixels++;
/* If we're not at the end of the row and we've processed all of this word, fetch the next one */
if (x + bpp < row_max_bit && offset_into_word + bpp == 32) {
@@ -319,7 +344,9 @@ struct sprite* sprite_load_sprite(FILE* spritefile)
uint32_t word2 = sprite_read_word(spritefile);
assert(word1 == word2); /* TODO: if they aren't, START FLASHING */
- sprite->palette[j] = word1;
+ /* swap rr and bb parts (seems to give the right result, but where is it documented? */
+ uint32_t entry = ((word1 & 0xff000000) >> 16) | (word1 & 0x00ff0000) | ((word1 & 0x0000ff00) << 16);
+ sprite->palette[j] = entry;
}
}