diff options
Diffstat (limited to 'src/graphic.c')
-rw-r--r-- | src/graphic.c | 87 |
1 files changed, 55 insertions, 32 deletions
diff --git a/src/graphic.c b/src/graphic.c index d68c289..35d69c6 100644 --- a/src/graphic.c +++ b/src/graphic.c @@ -11,17 +11,18 @@ Uint32 getPixel(SDL_Surface *s, int x, int y) { Uint32 res=0; //FIXME : Big Endian - res &= ((Uint8 *)s->pixels)[y*s->pitch + x*(s->format->BytesPerPixel+0)] << 0; - res &= ((Uint8 *)s->pixels)[y*s->pitch + x*(s->format->BytesPerPixel+1)] << 8; - res &= ((Uint8 *)s->pixels)[y*s->pitch + x*(s->format->BytesPerPixel+2)] << 16; + res |= ((Uint8 *)s->pixels)[y*s->pitch + x*s->format->BytesPerPixel+0] << 0; + res |= ((Uint8 *)s->pixels)[y*s->pitch + x*s->format->BytesPerPixel+1] << 8; + res |= ((Uint8 *)s->pixels)[y*s->pitch + x*s->format->BytesPerPixel+2] << 16; return res; } void putPixel(SDL_Surface *s, int x, int y, Uint32 p) { + //printf("putPixel(s, %i, %i, 0x%x)\n", x, y, p); //FIXME : Big Endian - ((Uint8 *)s->pixels)[y*s->pitch + x*(s->format->BytesPerPixel+0)] = (p & 0x000000ff) >> 0; - ((Uint8 *)s->pixels)[y*s->pitch + x*(s->format->BytesPerPixel+1)] = (p & 0x0000ff00) >> 8; - ((Uint8 *)s->pixels)[y*s->pitch + x*(s->format->BytesPerPixel+2)] = (p & 0x00ff0000) >> 16; + ((Uint8 *)s->pixels)[y*s->pitch + x*s->format->BytesPerPixel+0] = (p & 0x000000ff) >> 0; + ((Uint8 *)s->pixels)[y*s->pitch + x*s->format->BytesPerPixel+1] = (p & 0x0000ff00) >> 8; + ((Uint8 *)s->pixels)[y*s->pitch + x*s->format->BytesPerPixel+2] = (p & 0x00ff0000) >> 16; } SDL_Surface * createSurface(int width, int height) { @@ -45,45 +46,67 @@ SDL_Surface * createSurface(int width, int height) { } -SDL_Surface * makeTerrain(gameIni_t *gIni, gameRess_t *gRess) { +SDL_Surface * loadGifIn24Bpp(char *filePath) { + int res; + SDL_Surface *s1, *s2; + SDL_Rect dstRect = {0,0,0,0}; + + s1=IMG_Load(filePath); + if (s1==NULL) { return NULL; } + + s2=createSurface(s1->w, s1->h); + if (s2==NULL) { return NULL; } + SDL_SetColorKey(s2, SDL_SRCCOLORKEY, s1->format->colorkey); + + res=SDL_BlitSurface(s1, NULL, s2, &dstRect); + if (res!=0) return NULL; + + return s2; +} + +int makeTerrain(gameIni_t *gIni, gameRess_t *gRess, SDL_Surface **terrain, SDL_Surface **stencil) { int res, i; int x,y,xmin,xmax,ymin,ymax,y2,xdst, ydst; Uint32 srcPixel, dstPixel, srcStencil, dstStencil; - SDL_Surface *terrain, *stencil, *tile; + SDL_Surface *tile; + //SDL_Rect dstRect; - terrain=createSurface(LEVEL_WIDTH, LEVEL_HEIGHT); - if (terrain==NULL) { + *terrain=createSurface(LEVEL_WIDTH, LEVEL_HEIGHT); + if (*terrain==NULL) { logs(LOG_ERROR, "makeTerrain(), SDL_CreateRGBSurface() returns NULL"); - return NULL; + return 1; } - stencil=createSurface(LEVEL_WIDTH, LEVEL_HEIGHT); - if (stencil==NULL) { + *stencil=createSurface(LEVEL_WIDTH, LEVEL_HEIGHT); + if (*stencil==NULL) { logs(LOG_ERROR, "makeTerrain(), SDL_CreateRGBSurface() returns NULL"); - return NULL; + return 2; } - res=SDL_FillRect(terrain, &(terrain->clip_rect), gIni->style.bgColor); + res=SDL_FillRect(*terrain, &((*terrain)->clip_rect), gIni->style.bgColor); if (res!=0) { logs(LOG_WARN, "makeTerrain(), SDL_FillRect() failed"); - return NULL; + return 3; } - res=SDL_FillRect(stencil, &(stencil->clip_rect), ccc_nothing); + res=SDL_FillRect(*stencil, &((*stencil)->clip_rect), ccc_nothing); if (res!=0) { logs(LOG_WARN, "makeTerrain(), SDL_FillRect() failed"); - return NULL; + return 4; } - SDL_LockSurface(terrain); - SDL_LockSurface(stencil); + SDL_LockSurface(*terrain); + SDL_LockSurface(*stencil); for(i=0 ; i < gIni->level.terrainCount ; i++) { //FIXME : check sanity for id value tile=gRess->style.tiles[gIni->level.terrains[i].id]; if (tile==NULL) { logs(LOG_ERROR, "makeTerrain(), tile==NULL"); - return NULL; + return 5; } +// dstRect.y=gIni->level.terrains[i].ypos; +// dstRect.x=gIni->level.terrains[i].xpos; + // 8 : NO_OVERRIDE : marquer les pixels comme indestructibles pour les prochains plaquages // 2 : REMOVE : oublier (rendre transparent) tous les pixels qu'on a déjà plaqué // 4 : Upside Down @@ -91,14 +114,14 @@ SDL_Surface * makeTerrain(gameIni_t *gIni, gameRess_t *gRess) { // For each tile pixel, without going outside of the terrain ymin=(gIni->level.terrains[i].ypos>0)?0:-gIni->level.terrains[i].ypos; - ymax=min(tile->clip_rect.h, terrain->clip_rect.h - gIni->level.terrains[i].ypos); + ymax=min(tile->clip_rect.h, (*terrain)->clip_rect.h - gIni->level.terrains[i].ypos); xmin=(gIni->level.terrains[i].xpos>0)?0:-gIni->level.terrains[i].xpos; - xmax=min(tile->clip_rect.w, terrain->clip_rect.w - gIni->level.terrains[i].xpos); + xmax=min(tile->clip_rect.w, (*terrain)->clip_rect.w - gIni->level.terrains[i].xpos); SDL_LockSurface(tile); for (y=ymin; y<ymax; y++) { for (x=0; x<xmax; x++) { // If we have Upside Down modifier, count lines in reverse order fot dest - if ( gIni->level.terrains[i].modifier % 4 ) { + if ( (gIni->level.terrains[i].modifier & 4) == 4 ) { y2=tile->clip_rect.h-1-y; } else { y2=y; @@ -108,25 +131,25 @@ SDL_Surface * makeTerrain(gameIni_t *gIni, gameRess_t *gRess) { // Grab current pixel and stencil state (from previous blits) srcPixel=getPixel(tile, x, y2); - srcStencil=getPixel(stencil, xdst, ydst); + srcStencil=getPixel(*stencil, xdst, ydst); // Act only if srcPixel is not transparent and srcStentil doesn't says NO_OVERRIDE if ( (srcPixel != tile->format->colorkey) && ( srcStencil != ccc_nooverride) ) { // If we have REMOVE modifier, dstPixel will be rolled back to bgColor, else, it will be identical to the source pixel - if ( gIni->level.terrains[i].modifier % 2 ) { + if ( (gIni->level.terrains[i].modifier & 2) == 2 ) { dstPixel=gIni->style.bgColor; } else { dstPixel=srcPixel; } - putPixel(terrain, xdst, ydst, dstPixel); + putPixel(*terrain, xdst, ydst, dstPixel); // If we have NO_OVERRIDE modifier, put that info on stencil, else, save that there is a (normal) terrain here - if ( gIni->level.terrains[i].modifier % 8 ) { + if ( (gIni->level.terrains[i].modifier & 8) == 8 ) { dstStencil=ccc_nooverride; } else { dstStencil=ccc_terrain; } - putPixel(stencil, xdst, ydst, dstStencil); + putPixel(*stencil, xdst, ydst, dstStencil); } } @@ -140,8 +163,8 @@ SDL_Surface * makeTerrain(gameIni_t *gIni, gameRess_t *gRess) { } */ } - SDL_UnlockSurface(stencil); - SDL_UnlockSurface(terrain); + SDL_UnlockSurface(*stencil); + SDL_UnlockSurface(*terrain); - return terrain; + return 0; } |