00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef RECT_H
00019 #define RECT_H
00020
00021 #include "RotateImage.h"
00022 #include "Fxp.h"
00023
00028 #include <SDL/begin_code.h>
00029
00038
00044 class DECLSPEC Rect:public SDL_Rect
00045 {
00046 public:
00048 inline Rect()
00049 {
00050 Set(0,0,0,0);
00051 }
00052
00054 inline Rect(SDL_Rect rect)
00055 {
00056 Set(rect);
00057 }
00058
00060 inline Rect(Sint16 xin, Sint16 yin, Uint16 win, Uint16 hin)
00061 {
00062 Set(xin,yin,win,hin);
00063 }
00064
00066 Rect(SfxpCoord coord, Uint16 win, Uint16 hin)
00067 {
00068 x=coord.X();
00069 y=coord.Y();
00070 w=win;
00071 h=hin;
00072 }
00073
00075 inline Rect Set(SDL_Rect r)
00076 {
00077 x=r.x;
00078 y=r.y;
00079 w=r.w;
00080 h=r.h;
00081 return(*this);
00082 }
00083
00085 inline Rect operator =(SDL_Rect r)
00086 {
00087 Set(r);
00088 return(*this);
00089 }
00090
00092 inline Rect Set(Sint16 xin, Sint16 yin, Uint16 win, Uint16 hin)
00093 {
00094 x=xin;
00095 y=yin;
00096 w=win;
00097 h=hin;
00098 return(*this);
00099 }
00100
00102 inline Rect MoveTo(Sint16 xin, Sint16 yin)
00103 {
00104 x=xin;
00105 y=yin;
00106 return(*this);
00107 }
00108
00110 inline Rect Resize(Uint16 win, Uint16 hin)
00111 {
00112 w=win;
00113 h=hin;
00114 return(*this);
00115 }
00116
00118 inline Rect operator +=(const SfxpCoord coord)
00119 {
00120 x+=coord.X();
00121 y+=coord.Y();
00122 return(*this);
00123 }
00124
00126 inline Rect operator -=(const SfxpCoord coord)
00127 {
00128 x-=coord.X();
00129 y-=coord.Y();
00130 return(*this);
00131 }
00132
00134 inline bool operator ==(const Rect &rect)
00135 {
00136 if((x==rect.x)&&(y==rect.y)&&(w=rect.w)&&(h==rect.h))
00137 {
00138 return(true);
00139 }
00140 else
00141 {
00142 return(false);
00143 }
00144 }
00145
00147 inline bool operator !=(const Rect &rect)
00148 {
00149 return(!((*this)==rect));
00150 }
00151
00153 inline int LastX()
00154 {
00155 if(w==0) return(-1);
00156 else return(x+(w-1));
00157 }
00158
00160 inline int LastY()
00161 {
00162 if(h==0) return(-1);
00163 else return(y+(h-1));
00164 }
00165
00167 inline bool Intersect(SDL_Rect &r, SDL_Rect rect) const
00168 {
00169 if(Intersect1D(r.x,r.w,x,w,rect.x,rect.w))
00170 {
00171 return(Intersect1D(r.y,r.h,y,h,rect.y,rect.h));
00172 }
00173 else
00174 {
00175 return(false);
00176 }
00177 }
00178
00180 inline bool Overlaps(SDL_Rect rect) const
00181 {
00182 if(Collide1D(x,w,rect.x,rect.w))
00183 {
00184 return(Collide1D(y,h,rect.y,rect.h));
00185 }
00186 else
00187 {
00188 return(false);
00189 }
00190 }
00191
00193 inline void Draw(SDL_Surface *surf, Uint8 r, Uint8 g, Uint8 b)
00194 {
00195 if(surf==NULL) return;
00196
00197 SDL_FillRect(surf,this,SDL_MapRGB(surf->format,r,g,b));
00198 }
00199
00201 inline void Update(SDL_Surface *screen)
00202 {
00203 SDL_UpdateRect(screen,x,y,w,h);
00204 }
00205
00207 inline int Area() const
00208 {
00209 return(((Sint16)w)*((Sint16)h));
00210 }
00211
00213 inline Rect &Normalize()
00214 {
00215 if(x<0)
00216 {
00217 w+=x;
00218 x=0;
00219 }
00220
00221 if(y<0)
00222 {
00223 h+=y;
00224 y=0;
00225 }
00226
00227 return(*this);
00228 }
00229
00231 inline bool Merge(SDL_Rect r)
00232 {
00233 int minx=0,maxx=0,miny=0,maxy=0;
00234 int prevarea=-1;
00235 Rect newrect;
00236
00237 prevarea=Rect(r).Area()+Area();
00238
00239 minx=x;
00240 maxx=x+w;
00241 miny=y;
00242 maxy=y+h;
00243
00244 if(r.x<minx) minx=r.x;
00245 if(r.x+r.w>maxx) maxx=r.x+r.w;
00246 if(r.y<miny) miny=r.y;
00247 if(r.y+r.h>maxy) maxy=r.y+r.h;
00248
00249 newrect.Set(minx,miny,maxx-minx,maxy-miny);
00250
00251 if(newrect.Area()<=prevarea)
00252 {
00253 Set(newrect);
00254 return(true);
00255 }
00256 else
00257 {
00258 return(false);
00259 }
00260 }
00261
00263 int Print(FILE *fp=stderr) const
00264 {
00265 return(fprintf(fp,"Rect(%p) (%d,%d:%d,%d)\n",(void *)this,x,y,w,h));
00266 }
00267
00268
00269
00270
00272 inline Rect Shrink(int border=1);
00273 inline Rect ShrinkBottom(int amount);
00274 inline Rect ShrinkTop(int amount);
00275 inline Rect ExpandDown(int amount, int max=-1);
00276 inline Rect ExpandUp(int amount);
00277 inline Rect ShrinkLeft(int amount);
00278 inline Rect ExpandLeft(int amount);
00279 inline Rect ShrinkRight(int amount);
00280 inline Rect ExpandRight(int amount, int max=-1);
00281 inline bool AreaIsTransparent(SDL_Surface *surf, Rect r);
00282
00283 private:
00284
00290 inline bool Intersect1D(Sint16 &pout, Uint16 &lout,
00291 Sint16 pos1, Uint16 len1,
00292 Sint16 pos2, Uint16 len2 ) const
00293 {
00294 int dp=pos2-pos1;
00295
00296 if(dp<0)
00297 {
00298 Swap(pos1,pos2);
00299 Swap(len1,len2);
00300 dp=-dp;
00301 }
00302
00303 if(Collide1D(pos1,len1,pos2,len2))
00304 {
00305 if(Contains(pos1,len1,pos2,len2))
00306 {
00307 pout=pos2;
00308 lout=len2;
00309 return(true);
00310 }
00311 else
00312 {
00313 if(len1==dp) return(false);
00314
00315 pout=pos2;
00316 lout=len1-dp;
00317 return(true);
00318 }
00319 }
00320
00321 return(false);
00322 }
00323
00325 inline void Swap(Sint16 &i1, Sint16 &i2) const
00326 {
00327 Sint16 i3=i1;
00328 i1=i2;
00329 i2=i3;
00330 }
00331
00333 inline void Swap(Uint16 &i1, Uint16 &i2) const
00334 {
00335 Uint16 i3=i1;
00336 i1=i2;
00337 i2=i3;
00338 }
00339
00341 inline bool Contains(int pos1, int len1, int pos2, int len2) const
00342 {
00343 if( (pos2>=pos1) && ((pos2+len2)<=(pos1+len1)) )
00344 {
00345 return(true);
00346 }
00347 else
00348 {
00349 return(false);
00350 }
00351 }
00352
00354 inline bool Collide1D(int pos1, int len1, int pos2, int len2) const
00355 {
00356 int dp=pos2-pos1;
00357 if(dp>=0)
00358 {
00359 if(dp<len1) return(true);
00360 else return(false);
00361 }
00362 else
00363 {
00364 dp=-dp;
00365 if(dp<len2) return(true);
00366 else return(false);
00367 }
00368 }
00369 };
00370
00372
00373 #include <SDL/close_code.h>
00374
00375 #endif
00376