From 89ba23647ce6199577a0ffe499e5e5a2098b7e3d Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Sat, 30 Jan 2010 12:35:54 +0000 Subject: [PATCH] Add support for converting 8-bit SDL_Surfaces. --- Src/Display.h | 12 ++++++++++++ Src/Display_SDL.h | 15 ++------------- Src/utils.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/Src/Display.h b/Src/Display.h index 4128936..2cd9fa8 100644 --- a/Src/Display.h +++ b/Src/Display.h @@ -34,6 +34,18 @@ extern SDL_Surface *real_screen; #endif +// Colors for speedometer/drive LEDs +enum { + black = 0, + white = 1, + fill_gray = 16, + shine_gray = 17, + shadow_gray = 18, + red = 19, + green = 20, + PALETTE_SIZE = 21, +}; + #ifdef WIN32 #include #endif diff --git a/Src/Display_SDL.h b/Src/Display_SDL.h index 398e14b..691f032 100644 --- a/Src/Display_SDL.h +++ b/Src/Display_SDL.h @@ -53,17 +53,6 @@ static itimerval pulse_tv; // SDL joysticks static SDL_Joystick *joy[2] = {NULL, NULL}; -// Colors for speedometer/drive LEDs -enum { - black = 0, - white = 1, - fill_gray = 16, - shine_gray = 17, - shadow_gray = 18, - red = 19, - green = 20, - PALETTE_SIZE = 21, -}; static Uint16 palette_16[PALETTE_SIZE]; static Uint32 palette_32[PALETTE_SIZE]; SDL_Color sdl_palette[PALETTE_SIZE]; @@ -356,7 +345,7 @@ SDL_Surface *C64Display::SurfaceFromC64Display() #endif out = SDL_CreateRGBSurface(SDL_SWSURFACE, DISPLAY_X / 2, DISPLAY_Y / 2, 8, - rmask,gmask,bmask,amask); + rmask, gmask, bmask, amask); if (!out) return NULL; @@ -375,7 +364,7 @@ SDL_Surface *C64Display::SurfaceFromC64Display() dst_pixels[ dst_off ] = v; } } - SDL_SetPalette(out, SDL_LOGPAL | SDL_PHYSPAL, sdl_palette, 0, PALETTE_SIZE); + SDL_SetColors(out, sdl_palette, 0, PALETTE_SIZE); return out; } diff --git a/Src/utils.cpp b/Src/utils.cpp index 6e49815..a143247 100644 --- a/Src/utils.cpp +++ b/Src/utils.cpp @@ -5,6 +5,9 @@ #include #include +#include +#include + #include "gui/font.hh" #include "utils.hh" @@ -185,6 +188,8 @@ static void png_user_error(png_structp ctx, png_const_charp str) fprintf(stderr, "libpng: error: %s\n", str); } +extern SDL_Color sdl_palette[PALETTE_SIZE]; + /* This is taken from http://encelo.netsons.org/programming/sdl (GPLed) */ void *sdl_surface_to_png(SDL_Surface *surf, size_t *out_sz) { @@ -192,6 +197,7 @@ void *sdl_surface_to_png(SDL_Surface *surf, size_t *out_sz) png_infop info_ptr; int i, colortype; png_bytep *row_pointers; + png_colorp palette; struct png_write_user_struct out; out.sz = 0; @@ -223,6 +229,25 @@ void *sdl_surface_to_png(SDL_Surface *surf, size_t *out_sz) png_set_IHDR(png_ptr, info_ptr, surf->w, surf->h, 8, colortype, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); + if (colortype & PNG_COLOR_MASK_PALETTE) + { + /* Set the palette if there is one. REQUIRED for indexed-color images */ + palette = (png_colorp)png_malloc(png_ptr, + PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color)); + + /* KLUDGE! For some reason, surf->format->palette doesn't work... */ + for (int i = 0; i < PALETTE_SIZE; i++) + { + SDL_Color *p = &sdl_palette[i]; + + palette[i].red = p->r; + palette[i].green = p->g; + palette[i].blue = p->b; + } + /* ... Set palette colors ... */ + png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH); + } + /* Writing the image */ png_write_info(png_ptr, info_ptr); png_set_packing(png_ptr);