--- oshw-sdl/_sdltile.c 2004-08-15 21:17:06.000000000 +0100 +++ oshw-sdl/sdltile.c 2004-09-03 11:10:53.756598054 +0100 @@ -9,6 +9,7 @@ #include #include #include "SDL.h" +#include "SDL_image.h" #include "sdlgen.h" #include "../err.h" #include "../state.h" @@ -452,6 +453,7 @@ /* Create a new surface containing a single tile with transparent * pixels, as indicated by the given color key. */ +/* Chris E.: Alpha is used instead if available */ static SDL_Surface *extractkeyedtile(SDL_Surface *src, int ximg, int yimg, int wimg, int himg, Uint32 transpclr) @@ -459,17 +461,28 @@ SDL_Surface *dest; SDL_Surface *temp; SDL_Rect rect; + int hasalpha; dest = newsurface(wimg, himg, TRUE); - SDL_FillRect(dest, NULL, SDL_MapRGBA(dest->format, - 0, 0, 0, SDL_ALPHA_TRANSPARENT)); - SDL_SetColorKey(src, SDL_SRCCOLORKEY, transpclr); + hasalpha = (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA; + if(hasalpha) { + if(SDL_SetAlpha(dest, SDL_SRCALPHA, src->format->alpha) == -1 || /* Copy per-surface alpha value */ + SDL_SetAlpha(src, 0, 0) == -1) /* Copy alpha data rather than merging */ + die("SDL_SetAlpha", SDL_GetError()); + } else { + SDL_FillRect(dest, NULL, SDL_MapRGBA(dest->format, + 0, 0, 0, SDL_ALPHA_TRANSPARENT)); + SDL_SetColorKey(src, SDL_SRCCOLORKEY, transpclr); + } rect.x = ximg; rect.y = yimg; rect.w = dest->w; rect.h = dest->h; SDL_BlitSurface(src, &rect, dest, NULL); - SDL_SetColorKey(src, 0, 0); + if(hasalpha) + SDL_SetAlpha(src, SDL_SRCALPHA, dest->format->alpha); /* Restore alpha settings */ + else + SDL_SetColorKey(src, 0, 0); /* Remove color key */ temp = dest; dest = SDL_DisplayFormatAlpha(temp); @@ -522,7 +535,7 @@ SDL_Surface *temp; SDL_Rect rect; unsigned char *s, *d; - Uint32 transp, black; + Uint8 r, g, b, a; int x, y; rect.x = ximg; @@ -532,9 +545,6 @@ dest = newsurface(rect.w, rect.h, TRUE); SDL_BlitSurface(src, &rect, dest, NULL); - black = SDL_MapRGB(src->format, 0, 0, 0); - transp = SDL_MapRGBA(dest->format, 0, 0, 0, SDL_ALPHA_TRANSPARENT); - if (SDL_MUSTLOCK(src)) SDL_LockSurface(src); if (SDL_MUSTLOCK(dest)) @@ -544,8 +554,10 @@ + xmask * src->format->BytesPerPixel; for (y = 0 ; y < dest->h ; ++y) { for (x = 0 ; x < dest->w ; ++x) { - if (pixelat(src, xmask + x, ymask + y) == black) - ((Uint32*)d)[x] = transp; + SDL_GetRGB(pixelat(src, xmask + x, ymask + y), src->format, &r, &g, &b); + a = (r + g + b) / 3; + SDL_GetRGB(((Uint32*)d)[x], dest->format, &r, &g, &b); + ((Uint32*)d)[x] = SDL_MapRGBA(dest->format, r, g, b, a); } s += src->pitch; d += dest->pitch; @@ -639,9 +651,8 @@ if (!s) return FALSE; remembersurface(s); - tileptr[id].celcount = 1; + ++tileptr[id].celcount; tileptr[id].opaque[0] = s; - tileptr[id].transp[0] = NULL; } if (tileidmap[n].xtransp >= 0) { s = extractmaskedtile(tiles, @@ -654,8 +665,7 @@ if (!s) return FALSE; remembersurface(s); - tileptr[id].celcount = 1; - tileptr[id].opaque[0] = NULL; + ++tileptr[id].celcount; tileptr[id].transp[0] = s; } } @@ -1070,10 +1080,10 @@ SDL_Surface *tiles = NULL; int f, w, h; - tiles = SDL_LoadBMP(filename); + tiles = IMG_Load(filename); if (!tiles) { if (complain) - errmsg(filename, "cannot read bitmap: %s", SDL_GetError()); + errmsg(filename, "cannot read tile images: %s", IMG_GetError()); return FALSE; } if (tiles->format->palette && sdlg.screen->format->palette)