00001 /* 00002 libwt - Vassilis Virvilis Toolkit - a widget library 00003 Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the 00017 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, SA. 00019 */ 00020 00021 #ifndef WT_SDLRECT_H 00022 #define WT_SDLRECT_H 00023 00024 //#include <iosfwd> 00025 00026 #include <SDL/SDL_video.h> 00027 00028 #include <cassert> 00029 00030 namespace Wt { 00031 00032 /*! 00033 \note don't change object size to keep SDL_Color compatibility. 00034 th means: 00035 - no new data members 00036 - no virtual functions 00037 */ 00038 class SDLRect { 00039 public: 00040 /// Create an empty rectangle at (0, 0) 00041 SDLRect() { 00042 assert(sizeof(SDLRect) == sizeof(SDL_Rect)); 00043 setRect(0, 0, 0, 0); 00044 } 00045 00046 /// Create a rectangle 00047 SDLRect(int x, int y, int w, int h) { 00048 assert(sizeof(SDLRect) == sizeof(SDL_Rect)); 00049 setRect(x, y, w, h); 00050 } 00051 00052 /// Create a rectangle from a SDL_Rect 00053 SDLRect(const SDL_Rect& sdl_rect) 00054 : sdl_rect(sdl_rect) { 00055 assert(sizeof(SDLRect) == sizeof(SDL_Rect)); 00056 } 00057 00058 /// set's the rect's. 00059 void setRect(int x, int y, int w, int h) { 00060 sdl_rect.x = x; 00061 sdl_rect.y = y; 00062 sdl_rect.w = (w> 0) ? w: 0; 00063 sdl_rect.h = (h> 0) ? h: 0; 00064 } 00065 00066 /// assign operator 00067 SDLRect& operator=(const SDL_Rect &r) { 00068 sdl_rect = r; 00069 return *this; 00070 } 00071 00072 int width() const { 00073 return sdl_rect.w; 00074 } 00075 00076 /// Return the height of the rectangle 00077 int height() const { 00078 return sdl_rect.h; 00079 } 00080 00081 int x() const { 00082 return sdl_rect.x; 00083 } 00084 00085 int y() const { 00086 return sdl_rect.y; 00087 } 00088 00089 int left() const { 00090 return sdl_rect.x; 00091 } 00092 int top() const { 00093 return sdl_rect.y; 00094 } 00095 int right() const { 00096 return sdl_rect.x + sdl_rect.w - 1; 00097 } 00098 int bottom() const { 00099 return sdl_rect.y + sdl_rect.h - 1; 00100 } 00101 00102 void setX(int pos) { 00103 sdl_rect.x = pos; 00104 } 00105 00106 void setY(int pos) { 00107 sdl_rect.y = pos; 00108 } 00109 00110 void setLeft(int pos) { 00111 sdl_rect.x = pos; 00112 } 00113 00114 void setTop(int pos) { 00115 sdl_rect.y = pos; 00116 } 00117 00118 void setWidth(int width) { 00119 sdl_rect.w = width; 00120 } 00121 00122 void setHeight(int height) { 00123 sdl_rect.h = height; 00124 } 00125 00126 ///move the rectangle, specifying new upperleft corner 00127 void moveTopLeft(int x, int y) { 00128 sdl_rect.x = x; 00129 sdl_rect.y = y; 00130 } 00131 00132 ///move the rectangle by (dx, dy) 00133 void moveBy(int dx, int dy) { 00134 sdl_rect.x += dx; 00135 sdl_rect.y += dy; 00136 } 00137 00138 ///this keeps the upper left corner and moves the lower right one 00139 void resize(int w, int h) { 00140 if(w >= 0 && h >= 0) { 00141 sdl_rect.w = w; 00142 sdl_rect.h = h; 00143 } 00144 } 00145 00146 /// Compare two rectangles for equality 00147 bool operator==(const SDLRect &other) const { 00148 return sdl_rect.x == other.sdl_rect.x && sdl_rect.y == other.sdl_rect.y 00149 && sdl_rect.w == other.sdl_rect.w && sdl_rect.h == other.sdl_rect.h; 00150 } 00151 00152 /// Compare two rectangles 00153 bool operator!=(const SDLRect &other) const { 00154 return !(*this == other); 00155 } 00156 00157 /// Returns true if the rectangle contains point (x, y) 00158 bool contains(int x, int y) const { 00159 return y >= sdl_rect.y && y < sdl_rect.y + sdl_rect.h 00160 && x >= sdl_rect.x && x < sdl_rect.x + sdl_rect.w; 00161 } 00162 00163 /// Returns true if the rectangle contains r 00164 bool contains(const SDLRect &r) const { 00165 return left() <= r.left() && right() >= r.right() && 00166 top() <= r.top() && bottom() >= r.bottom(); 00167 } 00168 00169 /// Returns true if the rectangle overlaps with r 00170 bool intersects(const SDLRect &r) const { 00171 return (right() >= r.left() && 00172 left() <= r.right() && 00173 bottom() >= r.top() && 00174 top() <= r.bottom()); 00175 } 00176 00177 operator const SDL_Rect& () const { 00178 return sdl_rect; 00179 } 00180 00181 operator SDL_Rect& () { 00182 return sdl_rect; 00183 } 00184 00185 operator const SDL_Rect *() const { 00186 return &sdl_rect; 00187 } 00188 00189 operator SDL_Rect *() { 00190 return &sdl_rect; 00191 } 00192 00193 private: 00194 SDL_Rect sdl_rect; 00195 }; 00196 00197 } // namespace 00198 00199 #endif //WT_SDLRECT_H
This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.