From: Adam G. <ra...@us...> - 2001-08-30 05:59:48
|
Update of /cvsroot/sdlmm/SDLmm/src In directory usw-pr-cvs1:/tmp/cvs-serv4030 Modified Files: Makefile.am sdlmm.h sdlmm_srect.cpp sdlmm_srect.h sdlmm_timer.h Added Files: sdlmm_misc.h Log Message: Added a few fucntions to SRect. ie Move, Contains, Union, Intersect --- NEW FILE --- /* * SDLmm - a C++ wrapper for SDL and related libraries * Copyright © 2001 David Hedbor <da...@he...> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ #ifndef SDLMM_MISC_H #define SDLMM_MISC_H namespace SDLmm { //! Find the maximum of two values. template <class T> inline const T& Max(const T& t1, const T& t2) { if (t1 < t2) return t2; else return t1; } //! Find the minimum of two values. template <class T> inline const T& Min(const T& t1, const T& t2) { if (t1 < t2) return t1; else return t2; } } #endif // SDLMM_MISC_H Index: Makefile.am =================================================================== RCS file: /cvsroot/sdlmm/SDLmm/src/Makefile.am,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- Makefile.am 2001/07/06 20:41:18 1.22 +++ Makefile.am 2001/08/30 05:59:45 1.23 @@ -3,7 +3,7 @@ # The SDL library target lib_LTLIBRARIES = libSDLmm.la -HDRS=sdlmm_global.h sdlmm.h sdlmm_spoint.h sdlmm_srect.h sdlmm_color.h sdlmm_surface.h sdlmm_videoinfo.h \ +HDRS=sdlmm_global.h sdlmm_misc.h sdlmm.h sdlmm_spoint.h sdlmm_srect.h sdlmm_color.h sdlmm_surface.h sdlmm_videoinfo.h \ sdlmm_display.h sdlmm_basesurface.h sdlmm_pixelformat.h sdlmm_event.h sdlmm_eventhandler.h \ sdlmm_audio.h sdlmm_timer.h sdlmm_joystick.h sdlmm_cd.h sdlmm_config.h Index: sdlmm.h =================================================================== RCS file: /cvsroot/sdlmm/SDLmm/src/sdlmm.h,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- sdlmm.h 2001/07/26 20:10:26 1.17 +++ sdlmm.h 2001/08/30 05:59:45 1.18 @@ -123,6 +123,7 @@ #undef PACKAGE #undef VERSION #include "sdlmm_global.h" +#include "sdlmm_misc.h" #include "sdlmm_spoint.h" #include "sdlmm_srect.h" #include "sdlmm_color.h" Index: sdlmm_srect.cpp =================================================================== RCS file: /cvsroot/sdlmm/SDLmm/src/sdlmm_srect.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- sdlmm_srect.cpp 2001/07/06 20:30:38 1.7 +++ sdlmm_srect.cpp 2001/08/30 05:59:45 1.8 @@ -66,5 +66,27 @@ w = bottom_right_point.x - upper_left_point.x; h = bottom_right_point.y - upper_left_point.y; } + + SRect Intersect(const SRect& r1, const SRect& r2) { + SRect r; + r.x = Max(r1.x, r2.x); + r.y = Max(r1.y, r2.y); + r.w = Min(r1.x + r1.w, r2.x + r2.w) - r.x; + r.h = Min(r1.y + r1.h, r2.y + r2.h) - r.y; + if (r.w < 0) r.w = 0; + if (r.h < 0) r.h = 0; + return r; + } + + SRect Union(const SRect& r1, const SRect& r2) { + SRect r; + r.x = Min(r1.x, r2.x); + r.y = Min(r1.y, r2.y); + r.w = Max(r1.x + r1.w, r2.x + r2.w) - r.x; + r.h = Max(r1.y + r1.h, r2.y + r2.h) - r.y; + ASSERT(r.w >= 0); + ASSERT(r.h >= 0); + return r; + } } Index: sdlmm_srect.h =================================================================== RCS file: /cvsroot/sdlmm/SDLmm/src/sdlmm_srect.h,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- sdlmm_srect.h 2001/08/07 00:36:38 1.17 +++ sdlmm_srect.h 2001/08/30 05:59:45 1.18 @@ -69,7 +69,7 @@ \param bottom_right_point SPoint for the bottom right corner of the rectangle */ SRect(const SPoint &upper_left_point, - const SPoint &bottom_right_point); + const SPoint &bottom_right_point); //! Constructor which initializes the class from an SPoint object //! and integer values. @@ -110,9 +110,23 @@ */ bool operator==(const SDL_Rect& rect) const { return ((x == rect.x) && (y == rect.y) && - (w == rect.w) && (h == rect.h)); + (w == rect.w) && (h == rect.h)); } + //! Move the position of the rectangle. + /*! + \param point difference to move by. + */ + void Move(const SPoint& point) { + x += point.x; y += point.y; + } + + //! Is the point in the rectangle? + bool Contains(const SPoint& point) const { + return (x <= point.x) && (y <= point.y) && + ((x+w) > point.x) && ((y+h) > point.y); + } + //! Get the coordinates for the upper left corner of the SRect /*! \return SPoint object */ SPoint GetUpperLeft() const { return SPoint(x, y); } @@ -127,6 +141,16 @@ SPoint GetBottomRight() const { return SPoint(x+w, y+h); } }; + + //! Get intersection rectangle of two rectangles + /*! The intersection rectangle is the largest rectangle that is + contained by both r1 and r2. */ + SRect Intersect(const SRect& r1, const SRect& r2); + + //! Get union rectangle of two rectangles + /*! The union rectangle is the smallest rectangle that is + contains both r1 and r2. */ + SRect Union(const SRect& r1, const SRect& r2); } #endif // SDLMM_SRECT_H Index: sdlmm_timer.h =================================================================== RCS file: /cvsroot/sdlmm/SDLmm/src/sdlmm_timer.h,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- sdlmm_timer.h 2001/08/13 01:56:26 1.7 +++ sdlmm_timer.h 2001/08/30 05:59:45 1.8 @@ -38,9 +38,6 @@ void Mark(); unsigned int GetElapsed() const { - // What is the purpose of this assertion? - // it will always be true since Ticks is an unsigned int/grendel - // ASSERT(m_Elapsed >= 0); return m_Elapsed; } |