SDLGameEngine

src/sgefadefx.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 static SGEFADEFX *sgeFadeFXInit(SGEFADEFX *ret, Uint32 type, Uint32 runTime) {
00012         ret->type=type;
00013         ret->runTime=runTime;
00014         ret->startTime=SDL_GetTicks();
00015         ret->realStartTime=ret->startTime;
00016         ret->finished=0;
00017         ret->deleteSurfaces=NO;
00018         ret->preDelay=0;
00019         ret->postDelay=0;
00020         ret->surface=screen;
00021 
00022         switch (type) {
00023                 case SGEFADEFX_FADE:
00024                         ret->updateFunction=sgeFadeFXFadeUpdate;
00025                         break;
00026                 case SGEFADEFX_WIPE_LEFT:
00027                 case SGEFADEFX_WIPE_RIGHT:
00028                 case SGEFADEFX_WIPE_TOP:
00029                 case SGEFADEFX_WIPE_BOTTOM:
00030                 case SGEFADEFX_SCROLL_LEFT:
00031                 case SGEFADEFX_SCROLL_RIGHT:
00032                 case SGEFADEFX_SCROLL_TOP:
00033                 case SGEFADEFX_SCROLL_BOTTOM:
00034                         ret->updateFunction=sgeFadeFXWipeScrollUpdate;
00035                         break;
00036                 default:
00037                         sgeBailOut("Unknown fade effect: %d\n", type);
00038         }
00039 
00040         return ret;
00041 }
00042 
00043 SGEFADEFX *sgeFadeFXNew(Uint32 type, Uint32 runTime, SGESPRITE *source, SGESPRITE *destination) {
00044         SGEFADEFX *ret;
00045         sgeNew(ret, SGEFADEFX);
00046         ret->srcSprite=source;
00047         ret->dstSprite=destination;
00048         ret->src=sgeSpriteGetSDLSurface(source);
00049         ret->dst=sgeSpriteGetSDLSurface(destination);
00050         return sgeFadeFXInit(ret, type, runTime);
00051 }
00052 
00053 SGEFADEFX *sgeFadeFXNewSDLSurface(Uint32 type, Uint32 runTime, SDL_Surface *source, SDL_Surface *destination) {
00054         SGEFADEFX *ret;
00055         sgeNew(ret, SGEFADEFX);
00056         ret->srcSprite=NULL;
00057         ret->dstSprite=NULL;
00058         ret->src=source;
00059         ret->dst=destination;
00060         return sgeFadeFXInit(ret, type, runTime);
00061 }
00062 
00063 void sgeFadeFXDestroy(SGEFADEFX *fx) {
00064         if (fx->deleteSurfaces) {
00065                 if (fx->srcSprite==NULL) {
00066                         SDL_FreeSurface(fx->src);
00067                         SDL_FreeSurface(fx->dst);
00068                 } else {
00069                         sgeSpriteDestroy(fx->srcSprite);
00070                         sgeSpriteDestroy(fx->dstSprite);
00071                 }
00072         }
00073         sgeFree(fx);
00074 }
00075 
00076 void sgeFadeFXDraw(SGEFADEFX *fx) {
00077         if (fx->preDelay>0) {
00078                 if (SDL_GetTicks()-fx->realStartTime>fx->preDelay) {
00079                         fx->preDelay=0;
00080                 }
00081                 fx->startTime=SDL_GetTicks();
00082                 fx->current=0;
00083         } else {
00084                 fx->current=SDL_GetTicks()-fx->startTime;
00085         }
00086         (fx->updateFunction)((void *)fx);
00087         if (fx->postDelay>0) {
00088                 if (SDL_GetTicks()>(fx->startTime+fx->runTime))
00089                         if (SDL_GetTicks()-(fx->startTime+fx->runTime)>fx->postDelay) {
00090                                 fx->finished=1;
00091                         }
00092         } else {
00093                 if (SDL_GetTicks()-fx->startTime>fx->runTime) {
00094                         fx->finished=1;
00095                 }
00096         }
00097 }
00098 
00099 void sgeFadeFXDeleteSurfaces(SGEFADEFX *fx, Uint8 yesno) {
00100         fx->deleteSurfaces=yesno;
00101 }
00102 
00103 int sgeFadeFXFinished(SGEFADEFX *fx) {
00104         return fx->finished;
00105 }
00106 
00107 void sgeFadeFXPreDelay(SGEFADEFX *fx, Uint32 delay) {
00108         fx->preDelay=delay;
00109 }
00110 
00111 void sgeFadeFXPostDelay(SGEFADEFX *fx, Uint32 delay) {
00112         fx->postDelay=delay;
00113 }
00114 
00115 void sgeFadeFXSetTarget(SGEFADEFX *fx, SDL_Surface *surface) {
00116         fx->surface=surface;
00117 }
00118 
00119 void sgeFadeFXFadeUpdate(void *fxdata) {
00120         SGEFADEFX *fx=(SGEFADEFX *)fxdata;
00121         Uint8 alpha;
00122         SDL_Surface *tmp;
00123 
00124         if (fx->current<fx->runTime) {
00125                 alpha=255*fx->current/fx->runTime;
00126         } else {
00127                 alpha=255;
00128         }
00129 
00130         if (alpha<255) {
00131                 SDL_BlitSurface(fx->src,NULL,fx->surface,NULL);
00132                 tmp=sgeChangeSDLSurfaceAlpha(fx->dst,alpha);
00133                 SDL_BlitSurface(tmp,NULL,fx->surface,NULL);
00134                 SDL_FreeSurface(tmp);
00135         } else {
00136                 SDL_BlitSurface(fx->dst,NULL,fx->surface,NULL);
00137         }
00138 }
00139 
00140 void sgeFadeFXWipeScrollUpdate(void *fxdata) {
00141         SGEFADEFX *fx=(SGEFADEFX *)fxdata;
00142         SDL_Rect srcrectsrc, dstrectsrc, srcrectdst, dstrectdst;
00143         Uint32 srcwidth, dstwidth;
00144         Uint32 srcheight, dstheight;
00145 
00146         if (fx->current>fx->runTime) {
00147                 SDL_BlitSurface(fx->dst, NULL, fx->surface, NULL);
00148                 return;
00149         }
00150 
00151         srcrectsrc.x=0;
00152         srcrectsrc.y=0;
00153         srcrectdst.x=0;
00154         srcrectdst.y=0;
00155         dstrectsrc.x=0;
00156         dstrectsrc.y=0;
00157         dstrectdst.x=0;
00158         dstrectdst.y=0;
00159 
00160 
00161         switch (fx->type) {
00162                 case SGEFADEFX_SCROLL_RIGHT:
00163                         dstwidth=fx->surface->w*fx->current/fx->runTime;
00164                         srcwidth=fx->surface->w-dstwidth;
00165 
00166                         srcrectsrc.x=fx->src->w-srcwidth;
00167                         srcrectsrc.w=srcwidth;
00168                         dstrectsrc.w=dstwidth;
00169                         dstrectdst.x=srcwidth+1;
00170 
00171                         srcrectsrc.h=fx->src->h;
00172                         dstrectsrc.h=fx->dst->h;
00173                         break;
00174                 case SGEFADEFX_SCROLL_LEFT:
00175                         dstwidth=fx->surface->w*fx->current/fx->runTime;
00176                         srcwidth=fx->surface->w-dstwidth;
00177 
00178                         dstrectsrc.x=fx->dst->w-dstwidth;
00179                         dstrectsrc.w=dstwidth;
00180                         srcrectdst.x=dstwidth+1;
00181                         srcrectsrc.w=srcwidth;
00182 
00183                         srcrectsrc.h=fx->src->h;
00184                         dstrectsrc.h=fx->dst->h;
00185                         break;
00186                 case SGEFADEFX_SCROLL_TOP:
00187                         dstheight=fx->surface->h*fx->current/fx->runTime;
00188                         srcheight=fx->surface->h-dstheight;
00189 
00190                         dstrectsrc.y=fx->dst->h-dstheight;
00191                         dstrectsrc.h=dstheight;
00192                         srcrectdst.y=dstheight+1;
00193                         srcrectsrc.h=srcheight;
00194 
00195                         srcrectsrc.w=fx->src->w;
00196                         dstrectsrc.w=fx->dst->w;
00197                         break;
00198                 case SGEFADEFX_SCROLL_BOTTOM:
00199                         dstheight=fx->surface->h*fx->current/fx->runTime;
00200                         srcheight=fx->surface->h-dstheight;
00201 
00202                         srcrectsrc.y=fx->src->h-srcheight;
00203                         srcrectsrc.h=srcheight;
00204                         dstrectsrc.h=dstheight;
00205                         dstrectdst.y=srcheight+1;
00206 
00207                         srcrectsrc.w=fx->src->w;
00208                         dstrectsrc.w=fx->dst->w;
00209                         break;
00210                 case SGEFADEFX_WIPE_RIGHT:
00211                         dstwidth=fx->surface->w*fx->current/fx->runTime;
00212                         srcwidth=fx->surface->w-dstwidth;
00213 
00214                         srcrectsrc.w=srcwidth;
00215 
00216                         dstrectsrc.x=srcwidth+1;
00217                         dstrectsrc.w=dstwidth;
00218                         dstrectdst.x=srcwidth+1;
00219 
00220                         srcrectsrc.h=fx->src->h;
00221                         dstrectsrc.h=fx->dst->h;
00222                         break;
00223                 case SGEFADEFX_WIPE_TOP:
00224                         dstheight=fx->surface->h*fx->current/fx->runTime;
00225                         srcheight=fx->surface->h-dstheight;
00226 
00227                         srcrectsrc.y=dstheight+1;
00228                         srcrectsrc.h=srcheight;
00229                         srcrectdst.y=dstheight+1;
00230 
00231                         dstrectsrc.h=dstheight;
00232 
00233                         srcrectsrc.w=fx->src->w;
00234                         dstrectsrc.w=fx->dst->w;
00235                         break;
00236                 case SGEFADEFX_WIPE_BOTTOM:
00237                         dstheight=fx->surface->h*fx->current/fx->runTime;
00238                         srcheight=fx->surface->h-dstheight;
00239 
00240                         srcrectsrc.h=srcheight;
00241 
00242                         dstrectsrc.y=srcheight+1;
00243                         dstrectsrc.h=dstheight;
00244                         dstrectdst.y=srcheight+1;
00245 
00246                         srcrectsrc.w=fx->src->w;
00247                         dstrectsrc.w=fx->dst->w;
00248                         break;
00249                 default:
00250                         dstwidth=fx->surface->w*fx->current/fx->runTime;
00251                         srcwidth=fx->surface->w-dstwidth;
00252 
00253                         srcrectsrc.x=dstwidth+1;
00254                         srcrectsrc.w=srcwidth;
00255                         srcrectdst.x=dstwidth+1;
00256 
00257                         dstrectsrc.w=dstwidth;
00258 
00259                         srcrectsrc.h=fx->src->h;
00260                         dstrectsrc.h=fx->dst->h;
00261                         break;
00262         }
00263 
00264         SDL_BlitSurface(fx->src, &srcrectsrc, fx->surface, &srcrectdst);
00265         SDL_BlitSurface(fx->dst, &dstrectsrc, fx->surface, &dstrectdst);
00266 }
00267 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines