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

rectlist.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           rectlist.cpp  -  description
00003                              -------------------
00004     begin                : Fri Mar 7 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 #include "rectlist.h"
00018 
00025 
00026 /* True means OK, false means rect out of range */
00027 bool Normalize(SDL_Rect &r,SDL_Surface *screen)
00028 {
00029   if(r.x<0)
00030   {
00031     if(-r.x>r.w)
00032     {
00033       return(false);
00034     }
00035 
00036     r.w+=r.x;
00037     r.x=0;
00038   }
00039 
00040   if(r.y<0)
00041   {
00042     if(-r.y>r.h)
00043     {
00044       return(false);
00045     }
00046 
00047     r.h+=r.y;
00048     r.y=0;
00049   }
00050 
00051   if(r.x+r.w>screen->w)
00052   {
00053     if(r.x>=screen->w)
00054     {
00055       return(false);
00056     }
00057     r.w=screen->w-r.x;    
00058   }
00059 
00060   if(r.y+r.h>screen->h)
00061   {
00062     if(r.y>=screen->h)
00063     {
00064       return(false);
00065     }
00066     r.h=screen->h-r.y;
00067   }
00068 
00069   return(true);  
00070 }
00071 
00072 RectList::RectList(int numrects)
00073 {
00074   size=numrects;
00075   rects = new Rect[numrects];
00076 }
00077 
00078 void RectList::Clear()
00079 {
00080   pos=0;
00081 }
00082 
00083 bool RectList::Reduce(SDL_Surface *screen)
00084 {
00085   bool reduced=false;
00086   int n=-1;
00087   
00088   for(n=0; n<pos; n++)
00089   {
00090     if(!Normalize(rects[n],screen))
00091     {
00092       Remove(n);
00093       reduced=true;
00094       n--;
00095     }
00096   }
00097   
00098   return(reduced);
00099 }
00100 
00101 void RectList::Update(SDL_Surface *screen)
00102 {
00103   Reduce(screen);
00104 
00105   SDL_UpdateRects(screen,pos,rects);
00106 }
00107 
00108 bool RectList::Remove(int index)
00109 {
00110   if(index>=pos)
00111   {
00112     return(false);
00113   }
00114   /* If it's the end one, just shrink stack */
00115   else if(index==(pos-1))    
00116   {
00117     pos--;
00118   }
00119   else
00120   { /* Else, move in one from the end */
00121     rects[index]=rects[pos-1];
00122     pos--;
00123   }
00124     
00125   return(true);
00126 }
00127 
00128 Rect RectList::operator[](int index)
00129 {
00130   if(index>=pos)
00131   {
00132     return(Rect(0,0,0,0));
00133   }
00134   else
00135   {
00136     return(rects[index]);
00137   }
00138 }
00139 
00140 Rect RectList::Pop()
00141 {
00142   if(pos==0)
00143   {
00144     return(Rect(0,0,0,0));
00145   }
00146   else
00147   {
00148     return(rects[--pos]);
00149   }
00150 }
00151 
00152 int RectList::Pos()
00153 {
00154   return(pos);
00155 }
00156 
00157 int RectList::Size()
00158 {
00159   return(size);
00160 }
00161 
00162 bool RectList::Push(Rect r)
00163 {
00164   if(pos>=(size-1))
00165   {
00166     return(false);
00167   }
00168   else
00169   {    
00170     rects[pos++]=r;
00171     return(true);
00172   }
00173 }
00174 
00175 RectList::~RectList()
00176 {
00177   delete []rects;
00178 }

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