From a1b3da48c17182746372031d0789410e9d8457de Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 9 Mar 2012 16:08:31 +0100 Subject: [PATCH] Fix crash happening on 8 and 16 bit color displays. Commit f51dbd0669453290fb167caa7de8578cfcb7f7fd changed the way of generating images of ships taking damage, and assumed that the SDL_Surfaces containing the images were always 24 or 32 bpp. However, SDL will already have converted them to the display format, so we need to properly handle 8 and 16 bpp as well. --- code/resources.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/code/resources.cpp b/code/resources.cpp index 60299c0..47a7e5a 100644 --- a/code/resources.cpp +++ b/code/resources.cpp @@ -89,10 +89,33 @@ void loadGameGraphics() continue; shipShape[i] = createSurface(shipShape[i - SHIP_HIT_INDEX]->w, shipShape[i- SHIP_HIT_INDEX]->h); blit(shipShape[i - SHIP_HIT_INDEX], 0, 0, shipShape[i]); - uint32_t *p = (uint32_t *)shipShape[i]->pixels; - for(int j = 0; j < shipShape[i]->w * shipShape[i]->h; j++) - if(p[j]) - p[j] |= shipShape[i]->format->Rmask; + + switch(shipShape[i]->format->BytesPerPixel) { + case 4: { + uint32_t *p = (uint32_t *)shipShape[i]->pixels; + for (int j = 0; j < shipShape[i]->w * shipShape[i]->h; j++) + if (p[j]) + p[j] |= shipShape[i]->format->Rmask; + break; + } + + case 2: { + uint16_t *p = (uint16_t *)shipShape[i]->pixels; + for (int j = 0; j < shipShape[i]->w * shipShape[i]->h; j++) + if (p[j]) + p[j] |= shipShape[i]->format->Rmask; + break; + } + + case 1: { + uint8_t *p = (uint8_t *)shipShape[i]->pixels; + for (int j = 0; j < shipShape[i]->w * shipShape[i]->h; j++) + if (p[j]) + p[j] = SDL_MapRGB(shipShape[i]->format, 255, 0, 0); + break; + } + } + SDL_SetColorKey(shipShape[i], (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(shipShape[i]->format, 0, 0, 0)); }