Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

RotateImage.cpp

Go to the documentation of this file.
00001 #define __VCS_CPP__
00002 
00009 
00010 #ifdef _WIN32_WCE
00011 #       define ROTATE_KEYS
00012 #       define ROTATE_NUMPAD
00013 #       define ROTATION CLOCKWISE
00014 #else
00015 #       define ROTATION OFF
00016 #endif
00017 
00018 #include "RotateImage.h"
00019 #include <stdlib.h>
00020 
00021 #include <SDL/begin_code.h>
00022 
00023 
00024 #define RotateY_CCW(z, x,y)   (((z)->h)-(x)-1)
00025 #define RotateY_CW(z,x,y)     (x)
00026 #define RotateY_NULL(s, x, y) (y)
00027 
00028 #define RotateX_CCW(z, x, y)  (y)
00029 #define RotateX_CW(z,x,y)     (((z)->w)-(y)-1)
00030 #define RotateX_NULL(z, x, y) (x)
00031 
00032 #define RotateW_R(w,h)        (h)
00033 #define RotateW_F(w,h)        (w)
00034 
00035 #define RotateH_R(w,h)        (w)
00036 #define RotateH_F(w,h)        (h)
00037 
00038 #define UnRotateW             RotateW
00039 #define UnRotateH             RotateH
00040 
00041 #define RotateWH(v) { int w,h; w=(v)->w; h=(v)->h; (v)->w=RotateW(w,h); (v)->h=RotateH(w,h); }
00042 
00043 
00044 VCS_Surface *__VideoSurface=NULL;
00045 
00046 static int VCS_TransCoord_C(Sint16 *x, Sint16 *y)
00047 {
00048   Sint16 tx;
00049   VCS_Surface *Ref=VCS_GetVideoSurface();
00050   if((x==NULL)||(y==NULL)||(Ref==NULL)) return(-1);
00051 
00052   tx=(*x);
00053   (*x)=Ref->OldSurface->w-(*y);
00054   (*y)=tx;
00055   
00056   return(0);                          
00057 }
00058 
00059 static SDL_Rect *VCS_TransRect_C(const VCS_Surface *Ref, const SDL_Rect *Rect, SDL_Rect *NewRect)
00060 {
00061         if((Ref==NULL)||(Rect==NULL)||(NewRect==NULL)) return NULL;
00062 
00063         SDL_Rect Out={0};
00064         
00065         Out.w=Rect->h;
00066         Out.h=Rect->w;
00067         Out.y=Rect->x;
00068         Out.x=Ref->OldSurface->w-(Rect->y+Rect->h);
00069 
00070         memcpy(NewRect,&Out,sizeof(SDL_Rect));
00071         return(NewRect);
00072 }
00073 
00074 static int VCS_TransCoord_CC(Sint16 *x, Sint16 *y)
00075 {
00076   Sint16 ty;
00077   VCS_Surface *Ref=VCS_GetVideoSurface();
00078   if((x==NULL)||(y==NULL)||(Ref==NULL)) return(-1);
00079 
00080   ty=(*y);
00081   (*y)=Ref->OldSurface->h-(*x);
00082   (*x)=ty;
00083   return(0);  
00084 }
00085 
00086 static  SDL_Rect *VCS_TransRect_CC(const VCS_Surface *Ref, const SDL_Rect *Rect, SDL_Rect *NewRect)
00087 {       
00088         if((Ref==NULL)||(Rect==NULL)||(NewRect==NULL)) return NULL;
00089 
00090         SDL_Rect Out={0};
00091 
00092         Out.w=Rect->h;
00093         Out.h=Rect->w;
00094         Out.x=Rect->y;
00095         Out.y=Ref->OldSurface->h-(Rect->x+Rect->w);
00096 
00097         memcpy(NewRect,&Out,sizeof(SDL_Rect));
00098         return NewRect;
00099 }
00100 
00101 int VCS_TransCoord_NULL(Sint16 *x, Sint16 *y)
00102 {
00103   if((x==NULL)||(y==NULL))  return(-1);
00104   else                      return(0);
00105 }
00106 
00107 static  SDL_Rect *VCS_TransRect_NULL(const VCS_Surface *Ref, const SDL_Rect *Rect, SDL_Rect *NewRect)
00108 {
00109         if((Ref==NULL)||(Rect==NULL)||(NewRect==NULL)) return NULL;
00110 
00111         memcpy(NewRect,Rect,sizeof(SDL_Rect));
00112         return NewRect;
00113 }
00114 
00115 
00116 #if( ROTATION == CLOCKWISE)
00117 
00118 #define VCS_TransRect     VCS_TransRect_C
00119 #define VCS_UnTransRect   VCS_TransRect_CC
00120 #define VCS_TransCoord    VCS_TransCoord_C
00121 #define VCS_UnTransCoord  VCS_TransCoord_CC
00122 
00123 #define RotateW RotateW_R
00124 #define RotateH RotateH_R
00125 
00126 #define RotateX RotateX_CW
00127 #define RotateY RotateY_CW
00128 #define UnRotateX RotateX_CCW
00129 #define UnRotateY RotateY_CCW
00130 
00131 
00132 SDLKey VCLK_LEFT        = SDLK_DOWN;
00133 SDLKey VCLK_RIGHT       = SDLK_UP;
00134 SDLKey VCLK_UP          = SDLK_RIGHT;
00135 SDLKey VCLK_DOWN        = SDLK_LEFT;
00136 SDLKey VCLK_KP9         = SDLK_KP7;
00137 SDLKey VCLK_KP8         = SDLK_KP4;
00138 SDLKey VCLK_KP7         = SDLK_KP1;
00139 SDLKey VCLK_KP6         = SDLK_KP8;
00140 SDLKey VCLK_KP5         = SDLK_KP5;     //Not really needed, just there for continuity
00141 SDLKey VCLK_KP4         = SDLK_KP2;
00142 SDLKey VCLK_KP3         = SDLK_KP9;
00143 SDLKey VCLK_KP2         = SDLK_KP6;
00144 SDLKey VCLK_KP1         = SDLK_KP3;
00145 #elif ( ROTATION == CCLOCKWISE ) 
00146 
00147 #define VCS_TransRect     VCS_TransRect_CC
00148 #define VCS_UnTransRect   VCS_TransRect_C
00149 #define VCS_TransCoord    VCS_TransCoord_CC
00150 #define VCS_UnTransCoord  VCS_TransCoord_C
00151 
00152 
00153 #define RotateW RotateW_R
00154 #define RotateH RotateH_R
00155 
00156 #define RotateX RotateX_CCW
00157 #define RotateY RotateY_CCW
00158 #define UnRotateX RotateX_CW
00159 #define UnRotateY RotateY_CW
00160 
00161 SDLKey VCLK_KP9         = SDLK_KP3;
00162 SDLKey VCLK_KP8         = SDLK_KP6;
00163 SDLKey VCLK_KP7         = SDLK_KP9;
00164 SDLKey VCLK_KP6         = SDLK_KP2;
00165 SDLKey VCLK_KP5         = SDLK_KP5;     //Not really needed, just there for continuity
00166 SDLKey VCLK_KP4         = SDLK_KP4;
00167 SDLKey VCLK_KP3         = SDLK_KP1;
00168 SDLKey VCLK_KP2         = SDLK_KP8;
00169 SDLKey VCLK_KP1         = SDLK_KP7;
00170 SDLKey VCLK_LEFT        = SDLK_UP;
00171 SDLKey VCLK_RIGHT       = SDLK_DOWN;
00172 SDLKey VCLK_UP          = SDLK_RIGHT;
00173 SDLKey VCLK_DOWN        = SDLK_LEFT;
00174 #elif (ROTATION==NONE)
00175 
00176 #define VCS_TransRect     VCS_TransRect_NULL
00177 #define VCS_UnTransRect   VCS_TransRect_NULL
00178 #define VCS_TransCoord    VCS_TransCoord_NULL
00179 #define VCS_UnTransCoord  VCS_TransCoord_NULL
00180 
00181 #define RotateW RotateW_F
00182 #define RotateH RotateH_F
00183 
00184 #define RotateX RotateX_NULL
00185 #define RotateY RotateY_NULL
00186 #define UnRotateX RotateX_NULL
00187 #define UnRotateY RotateY_NULL
00188 
00189 
00190 SDLKey VCLK_KP9         = SDLK_KP9;
00191 SDLKey VCLK_KP8         = SDLK_KP8;
00192 SDLKey VCLK_KP7         = SDLK_KP8;
00193 
00194 
00195 SDLKey VCLK_KP6         = SDLK_KP6;
00196 SDLKey VCLK_KP5         = SDLK_KP5;     //Not really needed, just there for continuity
00197 SDLKey VCLK_KP4         = SDLK_KP4;
00198 SDLKey VCLK_KP3         = SDLK_KP3;
00199 SDLKey VCLK_KP2         = SDLK_KP2;
00200 SDLKey VCLK_KP1         = SDLK_KP1;
00201 SDLKey VCLK_LEFT        = SDLK_LEFT;
00202 SDLKey VCLK_RIGHT       = SDLK_RIGHT;
00203 SDLKey VCLK_UP          = SDLK_UP;
00204 SDLKey VCLK_DOWN        = SDLK_DOWN;
00205 #else
00206 #error "No default rotation selected."
00207 #endif
00208 
00209 int VCS_TranslateCoords(Sint16 *x, Sint16 *y)
00210 {
00211   return(VCS_TransCoord(x,y));
00212 }
00213 
00214 VCS_Surface *SDL_VCS(SDL_Surface *surface)
00215 {
00216         if(surface==NULL) return NULL;
00217         VCS_Surface *es=new VCS_Surface();
00218         memset(es,0,sizeof(VCS_Surface));
00219         memcpy(es,surface,sizeof(SDL_Surface));
00220         es->OldSurface=surface;
00221 
00222         RotateWH(es);
00223         es->RotationMode=ROTATION;
00224 
00225         return es;
00226 }
00227 
00228 /*
00229 int VCS_TransCoords(Sint16 *x, Sint16 *y)
00230 {
00231   SDL_Rect r={0,0,0,0};
00232   if((x==NULL)||(y==NULL))  return(-1);      
00233   r.x=*x;
00234   r.y=*y;
00235   
00236   VCS_TranslateRect(VCS_GetVideoSurface,&r,&r);
00237   (*x)=r.x;
00238   (*y)=r.y;
00239   
00240   return(0);  
00241 }*/
00242 
00243 void VCS_WM_SetIcon(VCS_Surface *icon, Uint8 *mask)
00244 {
00245         SDL_WM_SetIcon(icon->OldSurface,mask);
00246 }
00247 
00248 void VCS_ReinitSurface(VCS_Surface *surfOut, SDL_Surface *surfIn)
00249 {
00250   surfOut->OldSurface=surfIn;
00251   memcpy(surfOut,surfIn,sizeof(SDL_Surface));
00252 
00253   RotateWH(surfOut);
00254   surfOut->RotationMode=ROTATION;
00255 }
00256 
00257 int VCS_SetColors(VCS_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
00258 {
00259         int retVal=SDL_SetColors(surface->OldSurface,colors,firstcolor,ncolors);
00260 
00261         surface->format=surface->OldSurface->format;
00262         return retVal;
00263 }
00264 
00265 void VCS_UnlockSurface(VCS_Surface *surface)
00266 {
00267         if(surface==NULL) return;       
00268         
00269         //Don't attempt to unlock non-locked surfaces
00270         if(surface->VCS_Locked>0)
00271         {
00272                 //Decrement the lock-count
00273                 surface->VCS_Locked--;
00274 
00275                 //If it's 0, unlock it.
00276 
00277                 if(surface->VCS_Locked==0)
00278                 {
00279                         //Get the virtual surface they've been screwing with
00280                         SDL_Surface *surf=surface->LockedSurface;
00281 
00282                         //They're not gonna use it anymore.
00283                         surface->LockedSurface=NULL;
00284                 
00285                         //Unlock it so we can blit it
00286                         SDL_UnlockSurface(surf);
00287 
00288                         //Rotate it - return it to physical coordinates
00289                         SDL_Surface *newSurf=VCS_RotateSurface(surf,true);
00290 
00291 
00292                         //Copy it over our real surface
00293                         SDL_BlitSurface(newSurf,NULL,surface->OldSurface,NULL);
00294 
00295                         //Free the new surface
00296                         SDL_FreeSurface(newSurf);
00297 
00298                         //Make the virtual surface look real again
00299                         memcpy(surface,surface->OldSurface,sizeof(SDL_Surface));
00300 
00301                         int w=surface->OldSurface->w;
00302                         int h=surface->OldSurface->h;
00303                         
00304                         //Restore virtual height and witdh
00305                         surface->h=RotateH(w,h);
00306                         surface->w=RotateW(w,h);
00307                 }
00308         }
00309 }
00310 
00311 int VCS_LockSurface(VCS_Surface *surface)
00312 {
00313         if(surface==NULL) return -1;
00314         
00315         //If this is the first lock:
00316         if(surface->VCS_Locked==0)
00317         {
00318                 //Take the rotated image, and create an unrotated copy. 
00319                 SDL_Surface *surf=VCS_UnRotateSurface(surface,false);
00320 
00321                 //If can't be locked, free it and return error.
00322                 if(SDL_LockSurface(surf)==-1)
00323                 {
00324                         SDL_FreeSurface(surf);
00325                         return -1;
00326                 }
00327                 
00328                 surface->LockedSurface=surf;
00329                 memcpy(surface,surf,sizeof(SDL_Surface));               
00330         }
00331 
00332         surface->VCS_Locked++;
00333 
00334         return(0);
00335 }
00336 
00337 
00338 int FindColor(VCS_Surface *surface, SDL_Color *color)
00339 {       
00340     if(surface->OldSurface->format->BitsPerPixel!=8)
00341         {
00342                 fprintf(stderr,"FindColor> Surface not 8bpp\n");
00343                 return -1;
00344         }
00345 
00346         for(int i=0; i<surface->OldSurface->format->palette->ncolors; i++)
00347         if(memcmp(surface->OldSurface->format->palette->colors+i,color,sizeof(SDL_Color))==0)
00348         {
00349                 //Color found
00350                 return i;
00351         }
00352 
00353         //color not found
00354         return -1;
00355 }
00356 
00357 VCS_Surface *VCS_DisplayFormat(VCS_Surface *surface)
00358 {
00359   SDL_Surface *newsurf=SDL_DisplayFormat(surface->OldSurface);
00360 
00361   if(newsurf==NULL) return(NULL);
00362   else              return(SDL_VCS(newsurf));
00363 }
00364 
00365 void VCS_SetColorKey(VCS_Surface *surface, Uint32 flag, Uint32 color)
00366 {
00367         SDL_SetColorKey(surface->OldSurface,flag,color);
00368   surface->flags=surface->OldSurface->flags;  
00369 }
00370 
00371  void VCS_GetClipRect(VCS_Surface *surface, SDL_Rect *rect)
00372 {
00373         if(rect==NULL) return;
00374 
00375         SDL_GetClipRect(surface->OldSurface,rect);
00376 
00377         VCS_UnTransRect(surface,rect,rect);
00378 }
00379 
00380  void VCS_SetClipRect(VCS_Surface *surface, SDL_Rect *rect)
00381 {
00382         SDL_Rect rectOut;
00383         if(VCS_TransRect(surface,rect,&rectOut)==NULL)
00384         {
00385                 SDL_SetClipRect(surface->OldSurface,NULL);
00386         }
00387         else
00388         {
00389                 SDL_SetClipRect(surface->OldSurface,&rectOut);
00390 
00391         }
00392 }
00393 
00394 void VCS_TranslateEvent(SDL_Event *event)
00395 {
00396         if(event==NULL) return;
00397 
00398         int x=0,y=0,xrel=0,yrel=0;
00399 
00400         SDL_Surface *surf=SDL_GetVideoSurface();
00401         if(surf==NULL) return;
00402 
00403         VCS_Surface *surface=SDL_VCS(surf);
00404 
00405         switch (event->type)
00406         {
00407 #ifdef ROTATE_KEYS
00408         case SDL_KEYDOWN:
00409         case SDL_KEYUP:
00410 
00411                 switch(event->key.keysym.sym)
00412                 {
00413                 case SDLK_UP:
00414                         event->key.keysym.sym=VCLK_UP;
00415                         break;
00416                 case SDLK_DOWN:
00417 
00418 
00419                         event->key.keysym.sym=VCLK_DOWN;
00420                         break;
00421 
00422 
00423                 case SDLK_LEFT:
00424                         event->key.keysym.sym=VCLK_LEFT;
00425                         break;
00426                 case SDLK_RIGHT:
00427                         event->key.keysym.sym=VCLK_RIGHT;
00428                         break;
00429 
00430 #ifdef ROTATE_NUMPAD
00431                 case SDLK_KP1:
00432                         event->key.keysym.sym=VCLK_KP1;
00433                         break;
00434                 case SDLK_KP2:
00435                         event->key.keysym.sym=VCLK_KP2;
00436                         break;
00437                 case SDLK_KP3:
00438                         event->key.keysym.sym=VCLK_KP3;
00439                         break;
00440                 case SDLK_KP4:
00441                         event->key.keysym.sym=VCLK_KP4;
00442                         break;
00443                 case SDLK_KP5:
00444                         event->key.keysym.sym=VCLK_KP5;
00445                         break;
00446                 case SDLK_KP6:
00447                         event->key.keysym.sym=VCLK_KP6;
00448                         break;
00449                 case SDLK_KP7:
00450                         event->key.keysym.sym=VCLK_KP7;
00451                         break;
00452                 case SDLK_KP8:
00453                         event->key.keysym.sym=VCLK_KP8;
00454                         break;
00455                 case SDLK_KP9:
00456                         event->key.keysym.sym=VCLK_KP9;
00457                         break;
00458 
00459                 default:
00460                         break;
00461 #endif
00462                 }
00463                 break;
00464 #endif
00465         case SDL_MOUSEMOTION:           
00466                 x=event->motion.x;
00467                 y=event->motion.y;
00468                 xrel=event->motion.xrel;
00469                 yrel=event->motion.yrel;
00470 
00471                 event->motion.x=UnRotateX(surface,x,y);
00472 
00473                 event->motion.y=UnRotateY(surface,x,y);
00474 
00475                 switch(ROTATION)
00476                 {
00477                 case OFF:
00478                         break;
00479                 case CLOCKWISE:
00480                         event->motion.yrel=-xrel;
00481                         event->motion.xrel=yrel;
00482                         break;
00483                 case CCLOCKWISE:
00484                         event->motion.yrel=xrel;
00485                         event->motion.xrel=-yrel;
00486                         break;
00487 
00488                 default:
00489                         break;
00490                 }
00491 
00492                 break;
00493 
00494         case SDL_MOUSEBUTTONDOWN:
00495         case SDL_MOUSEBUTTONUP:
00496 
00497                 x=event->button.x;
00498                 y=event->button.y;
00499                 event->button.x=UnRotateX(surface,x,y);
00500                 event->button.y=UnRotateY(surface,x,y);
00501 
00502                 break;
00503 
00504         default:
00505                 break;
00506         }
00507 }
00508 
00509 
00510 int VCS_PollEvent(SDL_Event *event)
00511 {
00512         static SDL_Event oldEvent;
00513 
00514         int retval=SDL_PollEvent(&oldEvent);
00515 
00516         memcpy(event,&oldEvent,sizeof(SDL_Event));
00517         
00518         VCS_TranslateEvent(event);
00519 
00520         return(retval);
00521 }
00522 
00523 int VCS_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action, Uint32 mask)
00524 {
00525         int n;
00526 
00527         int retval=SDL_PeepEvents(events,numevents,action,mask);
00528 
00529         for(n=0; n<retval; n++)
00530         {
00531                 VCS_TranslateEvent(events+n);
00532         }
00533 
00534         return retval;
00535 }
00536 
00537 
00538 VCS_Surface *VCS_GetVideoSurface()
00539 {
00540 
00541   return(__VideoSurface);
00542 }
00543 
00544 
00545 int VCS_SetAlpha(VCS_Surface *surface, Uint32 flag, Uint8 alpha)
00546 {
00547         return(SDL_SetAlpha(surface->OldSurface,flag,alpha));
00548 }
00549 
00550 Uint8 VCS_GetMouseState(int *x, int *y)
00551 {
00552         VCS_Surface *vid;
00553         int x2,y2;
00554         int retval;
00555 
00556         vid=VCS_GetVideoSurface();
00557         if(vid==NULL) return 0;
00558 
00559         retval=SDL_GetMouseState(&x2,&y2);
00560 
00561         if(x!=NULL)
00562         {
00563                 (*x)=UnRotateX(vid,x2,y2);
00564         }
00565 
00566         if(y!=NULL)
00567         {
00568                 (*y)=UnRotateY(vid,x2,y2);
00569         }
00570         return retval;
00571 }
00572 
00573 
00574 void VCS_FreeSurface(VCS_Surface *surface)
00575 {
00576 
00577         SDL_FreeSurface(surface->OldSurface);
00578 
00579         delete surface;
00580 }
00581 
00582  int VCS_Flip(VCS_Surface *surface)
00583 {
00584         return(SDL_Flip(surface->OldSurface));
00585 }
00586 
00587 void PrintRect(SDL_Rect *rect)
00588 {
00589         if(rect)        fprintf(stderr,"PrintRect> xy:(%d,%d) wh:(%d,%d)\n",rect->x,rect->y,rect->w,rect->h);
00590         else            fprintf(stderr,"PrintRect> NULL rect\n");
00591 }
00592 
00593  void PrintSurface(SDL_Surface *s)
00594 {
00595         if(!s)  fprintf(stderr,"PrintSurface> Blank surface.\n");
00596         else
00597         {
00598                 SDL_PixelFormat *f=s->format;
00599                 fprintf(stderr,"<PrintSurface> wh:(%d,%d) P:%u PixelData:%p\n",s->w,s->h,s->pitch,s->pixels);
00600                 fprintf(stderr,"Flag:%d BPP: %d\n",s->flags,f->BitsPerPixel);
00601                 fprintf(stderr,"RM:%08x GM:%08x BM:%08x AM: %08x\n",f->Rmask,f->Gmask,f->Bmask,f->Amask);
00602                 PrintRect(&s->clip_rect);
00603                 fprintf(stderr,"</PrintSurface>\n");
00604         }
00605 }
00606 
00607  VCS_Surface *VCS_ConvertSurface(VCS_Surface *ref,SDL_PixelFormat *format, Uint32 flags)
00608 {
00609         SDL_Surface *oldSurface=ref->OldSurface;
00610         return(SDL_VCS(SDL_ConvertSurface(oldSurface,format,flags)));
00611 } 
00612 
00613 
00614 VCS_Surface *VCS_CreateRGBSurface(Uint32 flags,Sint16 w,Sint16 h,Uint8 bpp,Uint32 rmask, Uint32 gmask, Uint32 bmask, Uint32 amask)
00615 {       
00616         SDL_Surface *s=SDL_CreateRGBSurface(flags,RotateW(w,h),RotateH(w,h),bpp,rmask,gmask,bmask,amask);
00617 
00618         return(SDL_VCS(s));
00619 }
00620 
00621 void VCS_FillRect(VCS_Surface *surface, SDL_Rect *rect, Uint32 color)
00622 {
00623         if(!surface) return;
00624 
00625         SDL_Rect dstrect;
00626         SDL_Rect *outrect=VCS_TransRect(surface,rect,&dstrect);
00627 
00628         SDL_FillRect(surface->OldSurface,outrect,color);
00629 }
00630 
00631 VCS_Surface *VCS_SetVideoMode(Sint32 w, Sint32 h, Uint8 bpp,Uint32 flags)
00632 {       
00633         SDL_Surface *s=SDL_SetVideoMode(RotateW(w,h),RotateH(w,h),bpp,flags);
00634 
00635   if(s==NULL)
00636     return(NULL);
00637         else if(__VideoSurface==NULL)
00638     __VideoSurface=SDL_VCS(s);
00639         else
00640                 VCS_ReinitSurface(__VideoSurface,s);
00641         
00642         return(__VideoSurface);
00643 }
00644 
00645 void VCS_Quit(void)
00646 {
00647         if(__VideoSurface!=NULL)
00648         {
00649                 delete __VideoSurface;
00650         }
00651 
00652         SDL_Quit();
00653 }
00654 
00655 int VCS_BlitSurface(VCS_Surface *source, SDL_Rect *srcrect, VCS_Surface *dest, SDL_Rect *dstrect)
00656 {
00657         if((!source)||(!dest)) return -1;
00658 
00659         SDL_Rect src_rect={0};
00660         SDL_Rect dst_rect={0};
00661 
00662         memset(&src_rect,0,sizeof(SDL_Rect));
00663         memset(&dst_rect,0,sizeof(SDL_Rect));
00664 
00665         if(srcrect==NULL)
00666         {
00667                 src_rect.x=0;
00668                 src_rect.y=0;
00669                 src_rect.w=source->w;
00670                 src_rect.h=source->h;           
00671         }
00672         else
00673         {
00674                 src_rect=*srcrect;
00675         }
00676 
00677         if(dstrect==NULL)
00678         {
00679                 dst_rect.x=0;
00680                 dst_rect.y=0;
00681         }
00682         else
00683         {
00684                 dst_rect=*dstrect;
00685         }
00686 
00687   dst_rect.w=src_rect.w;
00688   dst_rect.h=src_rect.h;
00689   
00690 
00691 
00692         VCS_TransRect(source,&src_rect,&src_rect);
00693 
00694 
00695         VCS_TransRect(dest,&dst_rect,&dst_rect);
00696 
00697 
00698         return(SDL_BlitSurface(source->OldSurface,&src_rect,dest->OldSurface,&dst_rect));
00699 }
00700 
00701 void VCS_UpdateRect(VCS_Surface *screen,Sint32 x, Sint32 y, Sint32 w, Sint32 h)
00702 {
00703         if(!screen) return;
00704         SDL_Rect rect={x,y,w,h};
00705 
00706         if((x==0)&&(y==0)&&(w==0)&&(h==0))
00707         {
00708                 SDL_Flip(screen->OldSurface);
00709                 return;
00710         }
00711 
00712         VCS_UpdateRects(screen,1,&rect);
00713 }
00714 
00715  void VCS_UpdateRects(VCS_Surface *screen, int numrects, SDL_Rect *rects)
00716 {
00717         if(!screen) return;
00718         if(!rects) return;
00719 
00720         SDL_Rect *newrects=new SDL_Rect[numrects];
00721         for(int n=0; n<numrects; n++)
00722         {               VCS_TransRect(screen,&(rects[n]),&(newrects[n]));
00723         }
00724 
00725         SDL_UpdateRects(screen->OldSurface,numrects,newrects);
00726 
00727         delete[](newrects);
00728 }
00729 
00730 //Create an empty surface with the same format as a pre-existing one.
00731 SDL_Surface *CloneSurface(SDL_Surface *surface, Uint32 w, Uint32 h)
00732 {
00733         if(surface==NULL) return NULL;
00734 
00735 
00736         SDL_PixelFormat *format=surface->format;
00737 
00738         SDL_Surface *out=SDL_CreateRGBSurface(  SDL_SWSURFACE,w,h,
00739                                                                         format->BitsPerPixel,
00740                                                                         format->Rmask, format->Gmask,
00741                                                                         format->Bmask, format->Amask    );
00742 
00743         SDL_Surface *out2=SDL_ConvertSurface(out,format,SDL_SWSURFACE);
00744         SDL_FreeSurface(out);
00745         return(out2);
00746 }
00747 
00748 VCS_Surface *VCS_CloneSurface(VCS_Surface *surface, Uint32 w, Uint32 h)
00749 {
00750         if(surface==NULL) return NULL;
00751 
00752         SDL_PixelFormat *format=surface->format;
00753 
00754         VCS_Surface *out=VCS_CreateRGBSurface(  SDL_SWSURFACE,w,h,
00755                                                                         format->BitsPerPixel,
00756                                                                         format->Rmask, format->Gmask,
00757                                                                         format->Bmask, format->Amask    );
00758 
00759         VCS_Surface *out2=VCS_ConvertSurface(out,format,SDL_SWSURFACE);
00760         VCS_FreeSurface(out);
00761         return(out2);
00762 }
00763 
00764 
00765 //Take a surface, rotate it to specs, and return it.
00766 //Note, that the actual w and h may be reversed compared
00767 //to the virtual ones.
00768 //Expects an actual SDL_Surface, not one 'tampered' with by
00769 //VCS.  Internal use only.
00770 SDL_Surface *VCS_UnRotateSurface(VCS_Surface *surface, bool autofree)
00771 {       
00772         if(surface==NULL) return NULL;
00773 
00774         int x, y;
00775         SDL_Surface *rSurf=surface->OldSurface;
00776         SDL_Surface *buffer;
00777 
00778         buffer=CloneSurface(rSurf,UnRotateW(rSurf->w,rSurf->h),UnRotateH(rSurf->w,rSurf->h));
00779 
00780         if(buffer==NULL) return NULL;
00781 
00782         SDL_LockSurface(buffer);
00783         SDL_LockSurface(rSurf);
00784 
00785         for(y=0; y<rSurf->h; y++)
00786         for(x=0; x<rSurf->w; x++)
00787         {
00788                 Uint32 d=getpixel(rSurf,x,y);           
00789                 putpixel(buffer,UnRotateX(buffer,x,y),UnRotateY(buffer,x,y),d);
00790         }
00791 
00792 
00793         SDL_UnlockSurface(rSurf);
00794         SDL_UnlockSurface(buffer);
00795 
00796         if(autofree)
00797         {
00798                 VCS_FreeSurface(surface);
00799         }
00800 
00801         return(buffer);
00802 }
00803 
00804 
00805 
00806 
00807 //Take a surface, rotate it to specs, and return it.
00808 //Note, that the actual w and h may be reversed compared
00809 //to the virtual ones.
00810 //Expects an actual SDL_Surface, not one 'tampered' with by
00811 //VCS.  Internal use only.
00812 SDL_Surface *VCS_RotateSurface(SDL_Surface *surface, bool autofree)
00813 {
00814         SDL_Surface *buffer;
00815         Sint32 x,y;     
00816 
00817         buffer=CloneSurface(surface,RotateW(surface->w,surface->h),RotateH(surface->w,surface->h));     
00818 
00819         if(buffer==NULL)
00820         {
00821                 if(autofree)
00822                 {
00823                         SDL_FreeSurface(surface);
00824                 }
00825                 return NULL;
00826         }
00827 
00828         SDL_LockSurface(surface);
00829         SDL_LockSurface(buffer);
00830                 
00831                 for(y=0; y<surface->h; y++)
00832                 for(x=0; x<surface->w; x++)
00833                 {
00834                         Uint32 d=getpixel(surface,x,y);         
00835                         putpixel(buffer,RotateX(buffer,x,y),RotateY(buffer,x,y),d);
00836                 }               
00837 
00838         SDL_UnlockSurface(buffer);
00839         SDL_UnlockSurface(surface);
00840 
00841         if(autofree)
00842         {
00843                 SDL_FreeSurface(surface);
00844         }
00845 
00846         return(buffer);
00847 }
00848 
00849 
00850 void VCS_SaveBMP(VCS_Surface *surface, const char *file)
00851 {
00852         SDL_SaveBMP(surface->OldSurface,file);
00853 }
00854 
00855 VCS_Surface *VCS_Load(const char *file)
00856 {
00857   SDL_Surface *Surface=IMG_Load(file);
00858         if(Surface!=NULL)
00859         {
00860                 Surface=VCS_RotateSurface(Surface);
00861                 return(SDL_VCS(Surface));
00862         }
00863         else
00864         {
00865                 return NULL;
00866         }  
00867 }
00868 
00869 //Load a bmp surface into a RotSurface pointer.
00870 VCS_Surface *VCS_LoadBMP(const char *file)
00871 {
00872         SDL_Surface *Surface=SDL_LoadBMP(file);
00873         if(Surface!=NULL)
00874         {
00875                 Surface=VCS_RotateSurface(Surface);
00876                 return(SDL_VCS(Surface));
00877 
00878         }
00879         else
00880         {
00881                 return NULL;
00882         }       
00883 }
00884 
00889 Uint32 getpixel(SDL_Surface *surface, int x, int y)
00890 {
00891     int bpp = surface->format->BytesPerPixel;
00892     /* Here p is the address to the pixel we want to retrieve */
00893     Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
00894 
00895     switch(bpp) {
00896     case 1:
00897         return *p;
00898 
00899     case 2:
00900 
00901         return *(Uint16 *)p;
00902 
00903     case 3:
00904         if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
00905             return p[0] << 16 | p[1] << 8 | p[2];
00906         else
00907             return p[0] | p[1] << 8 | p[2] << 16;
00908 
00909     case 4:
00910         return *(Uint32 *)p;
00911 
00912     default:
00913         return 0;       /* shouldn't happen, but avoids warnings */
00914     }
00915 }
00916 
00917 //Gee, I dunno...
00918  void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
00919 {
00920     int bpp = surface->format->BytesPerPixel;
00921     /* Here p is the address to the pixel we want to set */
00922 
00923 
00924     Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
00925 
00926     switch(bpp) {
00927     case 1:
00928         *p = pixel;
00929         break;
00930 
00931     case 2:
00932         *(Uint16 *)p = pixel;
00933         break;
00934 
00935 
00936     case 3:
00937         if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
00938             p[0] = (pixel >> 16) & 0xff;
00939             p[1] = (pixel >> 8) & 0xff;
00940             p[2] = pixel & 0xff;
00941         } else {
00942             p[0] = pixel & 0xff;
00943             p[1] = (pixel >> 8) & 0xff;
00944             p[2] = (pixel >> 16) & 0xff;
00945 
00946         }
00947         break;
00948 
00949     case 4:
00950         *(Uint32 *)p = pixel;
00951         break;
00952         default:
00953         break;       /* shouldn't happen, but avoids warnings */
00954 
00955     }
00956 }
00957 
00958 #include <SDL/close_code.h>

Generated on Sat Oct 11 13:19:25 2003 for Spritelib by doxygen 1.3.4