summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Shaw <jshaw@netsurf-browser.org>2007-11-25 20:08:07 +0000
committerJames Shaw <jshaw@netsurf-browser.org>2007-11-25 20:08:07 +0000
commitf7c9c74640eedd25cad4da3720d8ba5cbb0d8863 (patch)
tree3acb559c9c0c3632695a80eadd9ff0d3d1219417
parent2da03295d0d851c06cc3e9cd84489eeb8843650a (diff)
downloadlibrosprite-f7c9c74640eedd25cad4da3720d8ba5cbb0d8863.tar.gz
librosprite-f7c9c74640eedd25cad4da3720d8ba5cbb0d8863.tar.bz2
Add SDL drawing in example, fix RGB handling in high_color parser
svn path=/import/jshaw/libsprite/; revision=9988
-rw-r--r--trunk/Makefile2
-rw-r--r--trunk/example.c28
-rw-r--r--trunk/libsprite.c11
3 files changed, 35 insertions, 6 deletions
diff --git a/trunk/Makefile b/trunk/Makefile
index 62702de..e6e712e 100644
--- a/trunk/Makefile
+++ b/trunk/Makefile
@@ -9,7 +9,7 @@ LDFLAGS = -L./
#-Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes
example: libsprite.a example.o
- ${LD} -g -o $@ example.o ${LDFLAGS} -lsprite
+ ${LD} -g -o $@ example.o ${LDFLAGS} -lsprite -lSDL
libsprite.a: libsprite.o
${AR} ${ARFLAGS} libsprite.a libsprite.o
diff --git a/trunk/example.c b/trunk/example.c
index 15fd2de..ff2f72c 100644
--- a/trunk/example.c
+++ b/trunk/example.c
@@ -1,8 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
+#include <SDL/SDL.h>
#include "libsprite.h"
+/* 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;
+ *pixel = mapped_color;
+}
+
int main(int argc, char *argv[])
{
if (argc < 2) {
@@ -10,6 +19,12 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
+ if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
+ fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
+ exit(1);
+ }
+ atexit(SDL_Quit);
+
char* filename = argv[1];
FILE* spritefile = fopen(filename, "rb");
@@ -25,8 +40,12 @@ int main(int argc, char *argv[])
printf("sprite_count %u\n", sprite_area->sprite_count);
printf("extension_size %u\n", sprite_area->extension_size);
+ SDL_Surface *screen;
+ screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
+
for (uint32_t i = 0; i < sprite_area->sprite_count; i++) {
struct sprite* sprite = sprite_area->sprites[i];
+ printf("%x\n", sprite->image[0]);
printf("\nname %s\n", sprite->name);
printf("colorbpp %u\n", sprite->mode->colorbpp);
printf("xdpi %u\n", sprite->mode->xdpi);
@@ -39,6 +58,15 @@ int main(int argc, char *argv[])
printf("hasMask %s\n", sprite->hasmask ? "YES" : "NO");
if (sprite->hasmask) printf("maskbpp %u\n", sprite->mode->maskbpp);
+
+ 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]);
+ }
+ }
+
+ SDL_UpdateRect(screen, 0, 0, 799, 599);
+ fgetc(stdin);
}
fclose(spritefile);
diff --git a/trunk/libsprite.c b/trunk/libsprite.c
index 1b8bf16..67f7bad 100644
--- a/trunk/libsprite.c
+++ b/trunk/libsprite.c
@@ -165,7 +165,7 @@ uint32_t sprite_upscale_color(uint32_t pixel, uint32_t bpp)
case 32:
return pixel;
case 24:
- return pixel & (23 << 8); /* TODO: mask out alpha -- any point? */
+ return pixel & 0x00001700; /* TODO: mask out alpha -- any point? */
case 16:
/* TODO */
return pixel;
@@ -195,16 +195,17 @@ void sprite_load_high_color(uint8_t* image_in, uint8_t* mask, struct sprite* spr
assert(header->first_used_bit == 0);
for (uint32_t y = 0; y < sprite->height; y++) {
+ uint32_t x_pixels = 0;
for (uint32_t x = 0; x < row_max_bit; x += bpp) {
uint32_t pixel = 0;
for (uint32_t j = 0; j < bytesPerPixel; j++) {
uint8_t b = image_in[currentByteIndex++];
- pixel = pixel | (b << (j * 8));
+ pixel = pixel | (b << ((bytesPerPixel - j - 1) * 8));
}
pixel = sprite_upscale_color(pixel, bpp);
- sprite->image[y*sprite->width + x] = pixel;
- /* TODO: put pixels in sprite->image */
+ sprite->image[y*sprite->width + x_pixels] = pixel;
+ x_pixels++;
}
/* Ensure byte index is pointing at start of next row */
@@ -277,7 +278,7 @@ struct sprite* sprite_load_sprite(FILE* spritefile)
assert(sprite->mode->xdpi > 0);
assert(sprite->mode->ydpi > 0);
- /* TODO l/r wastage */
+ /* TODO left-hand wastage */
assert((header->last_used_bit + 1) % sprite->mode->colorbpp == 0);
/*assert(header->width_words % sprite->mode->colorbpp == 0);*/