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

rect_ops.h

00001 /***************************************************************************
00002                           rect_ops.h  -  description
00003                              -------------------
00004     begin                : Wed Jun 11 2003
00005     copyright            : (C) 2003 by Tyler Montbriand
00006     email                : tsm@accesscomm.ca
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 /***************************************************************************
00018                           rect_ops.h  -  description
00019                              -------------------
00020     begin                : Wed Jun 11 2003
00021     copyright            : (C) 2003 by Tyler Montbriand
00022     email                : tsm@accesscomm.ca
00023  ***************************************************************************/
00024 
00025 /***************************************************************************
00026  *                                                                         *
00027  *   This program is free software; you can redistribute it and/or modify  *
00028  *   it under the terms of the GNU General Public License as published by  *
00029  *   the Free Softw+are Foundation; either version 2 of the License, or     *
00030  *   (at your option) any later version.                                   *
00031  *                                                                         *
00032  ***************************************************************************/
00033 #ifndef __RECT_OPS_H__
00034 #define __RECT_OPS_H__
00035 
00036 #ifndef __cplusplus
00037 #error "Requires C++ compilation"
00038 #endif
00039 
00040 #include "rect.h"
00041 
00042 inline bool AreaIsTransparent(SDL_Surface *surf, Rect r)
00043 {
00044   int x,y;
00045   Uint32 transcolor=0;
00046 
00047   if(surf==NULL)  return(true);
00048   else if(!(surf->flags&SDL_SRCCOLORKEY))
00049   {
00050     return(false);
00051   }
00052 
00053   transcolor=surf->format->colorkey;
00054   
00055   SDL_LockSurface(surf);
00056 
00057   for(y=r.y; y<(r.y+r.h); y++)  
00058   for(x=r.x; x<(r.x+r.w); x++)
00059   {
00060     Uint32 c=getpixel(surf,x,y);
00061     if(c!=transcolor)
00062     {
00063       SDL_UnlockSurface(surf);
00064       return(false);
00065     }
00066   }
00067 
00068   SDL_UnlockSurface(surf);
00069 
00070   return(true);
00071 }
00072 
00073 inline Rect Rect::ShrinkBottom(int amount)
00074 {
00075   if(amount<0)
00076     return(ExpandDown(-amount));
00077   else if(amount>h)
00078     Set(0,0,0,0);
00079   else
00080     h-=amount;
00081 
00082   return(*this);
00083 }
00084 
00085 inline Rect Rect::ShrinkTop(int amount)
00086 {
00087   if(amount<0)  return(ExpandUp(-amount));
00088   if(amount>h)
00089     Set(0,0,0,0);
00090   else
00091   {
00092     y+=amount;
00093     h-=amount;
00094   }
00095 
00096   return(*this);
00097 }
00098 
00099 inline Rect Rect::ExpandDown(int amount, int max)
00100 {
00101   if(amount<0)  return(ShrinkBottom(-amount));
00102   if(max>=0)
00103   {
00104     if(y+h+amount>max)
00105       h=(max-y);
00106     else
00107       h+=amount;
00108   }
00109   else
00110   {
00111     h+=amount;
00112   }
00113 
00114   return(*this);
00115 }
00116 
00117 inline Rect Rect::ExpandUp(int amount)
00118 {
00119   if(amount<0)  return(ShrinkTop(-amount));
00120   else if(amount>=y)
00121   {
00122     h+=y;
00123     y=0;
00124   }
00125   else
00126   {
00127     h+=amount;
00128     y-=amount;
00129   }
00130 
00131   return(*this);
00132 }
00133 
00134 
00135 inline Rect Rect::Shrink(int border)
00136 {
00137   if((w==0)||(h==0))
00138   {
00139     /* Do nothing */
00140   }
00141   if( ((border*2)>=w)|| ((border*2)>=h) )
00142   {
00143     Set(0,0,0,0); /* Zero out rect  */
00144   }
00145   else    /* Shrink the rectangle */
00146   {
00147     w-=(border*2);  /* Reduce width               */
00148     h-=(border*2);  /* Reduce height              */
00149     x+=border;      /* Offset X into new position */
00150     y+=border;      /* Offset Y into new position */
00151   }
00152 
00153   return(*this);
00154 }
00155 
00156 inline Rect Rect::ShrinkLeft(int amount)
00157 {
00158   if(amount<0)
00159     return(ExpandLeft(-amount));
00160   else if(amount>w)
00161   {
00162     Set(0,0,0,0);
00163   }
00164   else
00165   {
00166     x+=amount;
00167     w-=amount;
00168   }
00169 
00170   return(*this);
00171 }
00172 
00173 inline Rect Rect::ExpandLeft(int amount)
00174 {
00175   if(amount<0)  return(ShrinkLeft(-amount));
00176   
00177   x-=amount;
00178   w+=amount;
00179 
00180   if(x<0)
00181   {
00182      amount+=x;
00183      w+=amount;
00184      x=0;
00185   }
00186 
00187   return(*this);
00188 }
00189 
00190 inline Rect Rect::ShrinkRight(int amount)
00191 {
00192   if(amount<0)
00193     return(ExpandRight(amount));
00194   else if(amount>=w)
00195     Set(0,0,0,0);
00196   else
00197     w-=amount;
00198     
00199   return(*this);
00200 }
00201 
00202 inline Rect Rect::ExpandRight(int amount, int max)
00203 {
00204   if(amount<0)  return(ShrinkRight(-amount));
00205   else
00206   {
00207     if(max>=0)
00208     {
00209       if((x+w+amount)>max)
00210         w=max-x;
00211       else
00212         w+=amount;
00213     }
00214     else
00215     {
00216       w+=amount;
00217     }
00218   }
00219 }
00220 
00221 #endif/*__RECT_OPS_H__*/

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