00001 #ifndef __RECT_H
00002 #define __RECT_H
00003
00004 #include <SDL/SDL.h>
00005
00006 class Rect
00007 {
00008 public:
00009 Rect() { r.x = r.y = r.w = r.h = 0; }
00010 Rect( int x, int y, int w, int h ) { setRect( x, y, w, h ); }
00011 ~Rect() {}
00012
00013 void setRect( int x, int y, int w, int h ) { r.x = x; r.y = y; r.w = w; r.h = h; }
00014 SDL_Rect getRect() { return r; }
00015 int x() { return r.x; }
00016 int y() { return r.y; }
00017 int w() { return r.w; }
00018 int h() { return r.h; }
00019
00020 void x( int x) { r.x = x; }
00021 void y( int y) { r.y = y; }
00022 void w( int w) { r.w = w; }
00023 void h( int h) { r.h = h; }
00024
00025 void moveBy( int x, int y )
00026 {
00027 r.x += x;
00028 r.y += y;
00029 }
00030
00031 bool intersects( Rect r2 )
00032 {
00033 int tw = r.w;
00034 int th = r.h;
00035 int rw = r2.w();
00036 int rh = r2.h();
00037 if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
00038 return false;
00039 }
00040 int tx = r.x;
00041 int ty = r.y;
00042 int rx = r2.x();
00043 int ry = r2.y();
00044 rw += rx;
00045 rh += ry;
00046 tw += tx;
00047 th += ty;
00048
00049
00050 return ((rw < rx || rw > tx) &&
00051 (rh < ry || rh > ty) &&
00052 (tw < tx || tw > rx) &&
00053 (th < ty || th > ry));
00054 }
00055
00056 private:
00057 SDL_Rect r;
00058 };
00059
00060 #endif
00061