SDLGameEngine

src/sgespriteimage.c

00001 /*
00002  * Copyright (c) 2007 Heiko Irrgang
00003  *
00004  * The license and distribution terms for this file may be
00005  * found in the file COPYING in this distribution or at
00006  * http://93-interactive.com/cms/products/software/sdl-game-engine/license/
00007  */
00008 
00009 #include <sge.h>
00010 
00011 SGESPRITEIMAGE *sgeSpriteImageNew() {
00012         SGESPRITEIMAGE *ret;
00013         sgeNew(ret, SGESPRITEIMAGE);
00014         ret->image=NULL;
00015         ret->useAlpha=1;
00016         ret->x=0;
00017         ret->y=0;
00018         ret->w=0;
00019         ret->h=0;
00020         return ret;
00021 }
00022 
00023 SGESPRITEIMAGE *sgeSpriteImageNewFile(SGEFILE *f, const char *name) {
00024         SGESPRITEIMAGE *ret;
00025         SDL_Surface *img=sgeReadImage(f,name);
00026         sgeNew(ret, SGESPRITEIMAGE);
00027         sgeSpriteImageSetImage(ret, img);
00028         return ret;
00029 }
00030 
00031 void sgeSpriteImageDestroy(SGESPRITEIMAGE *s) {
00032         if (s->image!=NULL) SDL_FreeSurface(s->image);
00033         sgeFree(s);
00034 }
00035 
00036 SGESPRITEIMAGE *sgeSpriteImageDuplicate(SGESPRITEIMAGE *s) {
00037         SGESPRITEIMAGE *newimage;
00038         sgeNew(newimage,SGESPRITEIMAGE);
00039         newimage->useAlpha=s->useAlpha;
00040         newimage->collisionColor=s->collisionColor;
00041         newimage->x=s->x;
00042         newimage->y=s->y;
00043         newimage->w=s->w;
00044         newimage->h=s->h;
00045         newimage->image=sgeDuplicateSDLSurface(s->image);
00046         return newimage;
00047 }
00048 
00049 void sgeSpriteImageSetImage(SGESPRITEIMAGE *s, SDL_Surface *image) {
00050         if (s->image!=NULL) SDL_FreeSurface(s->image);
00051         s->image=image;
00052         s->w=image->w;
00053         s->h=image->h;
00054         if (image->format->BitsPerPixel==32) {
00055                 s->useAlpha=1;
00056         } else {
00057                 sgeSpriteImageSetCollisionColor(s, 0, 0, 0, 0xff);
00058         }
00059 }
00060 
00061 void sgeSpriteImageDraw(SGESPRITEIMAGE *s, Uint8 alpha, SDL_Surface *dest) {
00062         SDL_Rect r;
00063         SDL_Surface *alphasurface;
00064         r.x=s->x;
00065         r.y=s->y;
00066         if (alpha==0xff) {
00067                 SDL_BlitSurface(s->image,NULL,dest,&r);
00068         } else {
00069                 alphasurface=sgeChangeSDLSurfaceAlpha(s->image, alpha);
00070                 SDL_BlitSurface(alphasurface,NULL,dest,&r);
00071                 SDL_FreeSurface(alphasurface);
00072         }
00073 }
00074 
00075 void sgeSpriteImageDrawXY(SGESPRITEIMAGE *s, int x, int y, Uint8 alpha, SDL_Surface *dest) {
00076         SDL_Rect r;
00077         SDL_Surface *alphasurface;
00078         r.x=x;
00079         r.y=y;
00080 
00081         if (alpha==0xff) {
00082                 SDL_BlitSurface(s->image,NULL,dest,&r);
00083         } else {
00084                 alphasurface=sgeChangeSDLSurfaceAlpha(s->image, alpha);
00085                 SDL_BlitSurface(alphasurface,NULL,dest,&r);
00086                 SDL_FreeSurface(alphasurface);
00087         }
00088 }
00089 
00090 int sgeSpriteImageBoxCollide(SGESPRITEIMAGE *a, SGESPRITEIMAGE *b) {
00091         Sint32 axaw, bxbw;
00092         Sint32 ayah, bybh;
00093 
00094         axaw=a->x+a->w;
00095         bxbw=b->x+b->w;
00096 
00097         if (
00098                         ( (axaw>=b->x) && (axaw<=bxbw) ) ||
00099                         ( (a->x<=b->x) && (axaw>=b->x) ) ||
00100                         ( (b->x<=a->x) && (bxbw>=a->x) )
00101         ) {
00102                 ayah=a->y+a->h;
00103                 bybh=b->y+b->h;
00104                 if ( (ayah>=b->y) && (ayah<=bybh) ) {
00105                         return 1;
00106                 }
00107                 if ( (a->y>=b->y) && (a->y<=bybh) ) {
00108                         return 1;
00109                 }
00110                 if ( (b->y<=a->y) && (bybh>=a->y) ) {
00111                         return 1;
00112                 }
00113                 if ( (a->y<=b->y) && (ayah>=b->y) ) {
00114                         return 1;
00115                 }
00116                 return 0;
00117         }
00118         if ( (a->x>=b->x) && (a->x<=bxbw) ) {
00119                 ayah=a->y+a->h;
00120                 bybh=b->y+b->h;
00121                 if ( (ayah>=b->y) && (ayah<=bybh) ) {
00122                         return 1;
00123                 }
00124                 if ( (a->y>=b->y) && (a->y<=bybh) ) {
00125                         return 1;
00126                 }
00127                 if ( (b->y<=a->y) && (bybh>=a->y) ) {
00128                         return 1;
00129                 }
00130                 if ( (a->y<=b->y) && (ayah=b->y) ) {
00131                         return 1;
00132                 }
00133                 return 0;
00134         }
00135         return 0;
00136 }
00137 
00138 int sgeSpriteImageCollide(SGESPRITEIMAGE *a, SGESPRITEIMAGE *b) {
00139         int ax, ay;
00140         int bx, by;
00141         int cw, ch;
00142         int x,y;
00143         Uint32 *a32, *b32;
00144         Uint16 *a16, *b16;
00145         Uint8 *a8, *b8;
00146         Uint32 pa, pb, tmpcola, tmpcolb;
00147         Uint8 ra,ga,ba,aa,rb,gb,bb,ab;
00148         if (sgeSpriteImageBoxCollide(a,b)) {
00149                 if (a->x>b->x) {
00150                         ax=0;
00151                         bx=a->x-b->x;
00152                         cw=MIN(MIN(b->w-(a->x-b->x),a->w),b->w);
00153                 } else {
00154                         ax=b->x-a->x;
00155                         bx=0;
00156                         cw=MIN(MIN(a->w-(b->x-a->x),a->w),b->w);
00157                 }
00158                 if (a->y>b->y) {
00159                         ay=0;
00160                         by=a->y-b->y;
00161                         ch=MIN(MIN(b->h-(a->y-b->y),a->h),b->h);
00162                 } else {
00163                         ay=b->y-a->y;
00164                         by=0;
00165                         ch=MIN(MIN(a->h-(b->y-a->y),a->h),b->h);
00166                 }
00167                 if (ch<1 || cw<1) {
00168                         return 0;
00169                 }
00170                 a32=(Uint32 *)a->image->pixels;
00171                 b32=(Uint32 *)b->image->pixels;
00172                 a16=(Uint16 *)a->image->pixels;
00173                 b16=(Uint16 *)b->image->pixels;
00174                 a8=(Uint8 *)a->image->pixels;
00175                 b8=(Uint8 *)b->image->pixels;
00176                 for (x=0;x<cw;x++) {
00177                         for (y=0;y<ch;y++) {
00178                                 if (a->image->format->BitsPerPixel==32||a->image->format->BitsPerPixel==24) {
00179                                         pa=a32[(y+ay)*a->w+x+ax];
00180                                         pb=b32[(y+by)*b->w+x+bx];
00181                                 } else if (a->image->format->BitsPerPixel==16) {
00182                                         pa=a16[(y+ay)*a->w+x+ax];
00183                                         pb=b16[(y+by)*b->w+x+bx];
00184                                 } else {
00185                                         pa=a8[(y+ay)*a->w+x+ax];
00186                                         pb=b8[(y+by)*b->w+x+bx];
00187                                 }
00188                                 SDL_GetRGBA(pa, a->image->format, &ra, &ga, &ba, &aa);
00189                                 SDL_GetRGBA(pb, b->image->format, &rb, &gb, &bb, &ab);
00190                                 if (a->useAlpha==1 && b->useAlpha==1 && aa!=0 && ab!=0) return 1;
00191                                 if (a->useAlpha==0 && b->useAlpha==0) {
00192                                         tmpcola=SDL_MapRGB(a->image->format,ra,ga,ba);
00193                                         tmpcolb=SDL_MapRGB(b->image->format,rb,gb,bb);
00194                                         if (tmpcola!=a->collisionColor && tmpcolb!=b->collisionColor) return 1;
00195                                 }
00196                         }
00197                 }
00198         }
00199         return 0;
00200 }
00201 
00202 void sgeSpriteImageUseAlpha(SGESPRITEIMAGE *s) {
00203         sgeUseAlpha(s->image);
00204         s->useAlpha=1;
00205 }
00206 
00207 void sgeSpriteImageIgnoreAlpha(SGESPRITEIMAGE *s) {
00208         sgeIgnoreAlpha(s->image);
00209         s->useAlpha=0;
00210 }
00211 
00212 void sgeSpriteImageSetCollisionColor(SGESPRITEIMAGE *s, int r, int g, int b, int a) {
00213         Uint32 col;
00214         if (a<0) {
00215                 col=SDL_MapRGB(s->image->format, r, g, b);
00216         } else {
00217                 col=SDL_MapRGBA(s->image->format, r, g, b, a);
00218         }
00219         s->collisionColor=col;
00220 }
00221 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines