mx::mxSurface Class Reference

List of all members.

Public Member Functions

 mxSurface (SDL_Surface *surface=0)
 mxSurface (const mxSurface &c)
virtual ~mxSurface ()
mxSurfaceoperator= (const mxSurface &c)
mxSurfaceoperator= (SDL_Surface *surface)
SDL_Surface * getSurface () const
SDL_Surface * surface () const
void setCopySurface (SDL_Surface *surface)
const int width () const
const int height () const
const int pitch () const
 operator SDL_Surface * ()
 operator SDL_PixelFormat * ()
mxPoint ** buildResizeTable (int nw, int nh, int w, int h, mxPoint **buffer)
unsigned int & operator[] (unsigned int pos)
void lockSurface ()
const bool isLocked () const
void unlockSurface ()
void * rawData ()
bool createSurface (int w, int h)
bool createSurface (Size &s)
bool copyResizeSurface (const mxSurface &c)
bool copyResizeSurface (const mxSurface &c, SDL_Rect *src, SDL_Rect *dst)
bool copySurface (const mxSurface &c, SDL_Rect *source, SDL_Rect *dest)
bool copySurface (const mxSurface &c, mx::Rect source, mx::Rect dest)
bool copySurfaceColorKey (const mxSurface &c, SDL_Rect *source, SDL_Rect *dest, SDL_Color color_key)
void Flip () const
virtual void noZero (bool noremove)
void cleanSurface ()
void updateRect (unsigned int x, unsigned int y, unsigned int w, unsigned int h) const
void Clear () const
const bool loadBitmapFromMemory (void *memory)
const int linepos (const int x, const int y) const
Size size ()
mxSurfacecopySurface ()

Protected Member Functions

void clean_up ()

Protected Attributes

SDL_Surface * object_surface


Detailed Description

class

Definition at line 32 of file mxsurface.h.


Constructor & Destructor Documentation

mx::mxSurface::mxSurface ( SDL_Surface *  surface = 0  ) 

default constructor,

Parameters:
surface has default argument of 0

Definition at line 52 of file mxsurface.cpp.

References noZero(), and object_surface.

Referenced by copySurface().

00052                                                  : is_locked(false)
00053         {
00054                 p_buffer = 0;
00055 
00056                 object_surface = surface;
00057                 noZero(false);
00058         }

mx::mxSurface::mxSurface ( const mxSurface c  ) 

copy constructor

Parameters:
c of type mxSurface

Definition at line 60 of file mxsurface.cpp.

References clean_up(), noZero(), and object_surface.

00060                                                : is_locked(false)
00061         {
00062 
00063                 p_buffer = 0;
00064                 clean_up();
00065                 object_surface = c.object_surface;
00066                 noZero(false);
00067         }

mx::mxSurface::~mxSurface (  )  [virtual]

virtual Destructor

Definition at line 70 of file mxsurface.cpp.

References clean_up().

00071         {
00072                 clean_up();
00073         }


Member Function Documentation

mxPoint ** mx::mxSurface::buildResizeTable ( int  nw,
int  nh,
int  w,
int  h,
mxPoint **  buffer 
)

buildResizeTable - builds a table for a stretch reallocation to increase speed of already slow stretch function

Parameters:
nw new width
nh new height
w old width
h old height
buffer array of array of mxPoint pointers
Returns:
newly allocated table

Definition at line 237 of file mxsurface.cpp.

References mx::_mxPoint::x, and mx::_mxPoint::y.

Referenced by copyResizeSurface().

00238         {
00239 
00240                 int i,z;
00241 
00242                 std::cout << "building resize table\n";
00243 
00244                 buffer = new mxPoint*[w];
00245 
00246                 for(i =  0; i < w; i++)
00247                 {
00248 
00249                         buffer[i] = new mxPoint[ h ];
00250 
00251                 }
00252 
00253 
00254                 for(i = 0; i < w; i++)
00255                 {
00256 
00257                         for(z = 0; z < h; z++)
00258                         {
00259                                 mxPoint *ptr = &buffer[i][z];
00260                                 ptr->x = stretchX(nw, i, w);
00261                                 ptr->y = stretchY(nh, z, h);
00262 
00263                         }
00264 
00265                 }
00266 
00267                 return buffer;
00268 
00269         }

void mx::mxSurface::clean_up (  )  [protected]

internal cleanup

Definition at line 178 of file mxsurface.cpp.

References getSurface(), and object_surface.

Referenced by cleanSurface(), createSurface(), mxSurface(), operator=(), setCopySurface(), and ~mxSurface().

00179         {
00180 
00181                 if(p_buffer != 0)
00182                 {
00183 
00184                         int i;
00185                         for(i = 0; i < getSurface()->w; i++)
00186                         {
00187 
00188                                 delete [] p_buffer[i];
00189                         }
00190 
00191                         delete [] p_buffer;
00192                         p_buffer = 0;
00193                 }
00194 
00195 
00196 
00197                 if(object_surface && protected_noremove == false)
00198                 {
00199 
00200                         cout << "libmx: removing surface: " << (unsigned long) object_surface <<  " ";
00201                         SDL_FreeSurface(object_surface);
00202                         object_surface = 0;
00203                         cout << "removed\n";
00204                 }
00205 
00206 
00207         }

void mx::mxSurface::cleanSurface (  ) 

Use this function to manually clear the surface (free) it

Definition at line 173 of file mxsurface.cpp.

References clean_up().

00174         {
00175                 clean_up();
00176         }

void mx::mxSurface::Clear (  )  const

Clear this surface

Definition at line 328 of file mxsurface.cpp.

References object_surface.

00329         {
00330                 SDL_FillRect(object_surface, 0, SDL_MapRGB(object_surface->format, 0, 0, 0));
00331         }

bool mx::mxSurface::copyResizeSurface ( const mxSurface c,
SDL_Rect *  src,
SDL_Rect *  dst 
)

copy surface from this object to size of surface referenced by param c stretch from src rect to dest rect

Parameters:
c dest surface
src source rectangle
dst destination rectangle
Returns:
true for success false for failure

Definition at line 310 of file mxsurface.cpp.

References getSurface().

00310                                                                                           {
00311 
00312 #if (HAVE_SDL_STRETCH == 0)
00313 #warning "you should have sdl-stretch compiled for this function to work\n"
00314 
00315                 return false;
00316 #else
00317                 SDL_StretchSurfaceRect( c.getSurface(), src, getSurface(), dst );
00318 #endif
00319 
00320                 return true;
00321         }

bool mx::mxSurface::copyResizeSurface ( const mxSurface c  ) 

copy surface to new surface by stretching it with resizeTable or with SDL_Stretch if enabled

Parameters:
c surface to copy to
Returns:
true for success false for failure

Definition at line 272 of file mxsurface.cpp.

References buildResizeTable(), getSurface(), object_surface, mx::_mxPoint::x, and mx::_mxPoint::y.

Referenced by mx::mxWnd::sizedFlip().

00273         {
00274 
00275 #if (HAVE_SDL_STRETCH == 0)
00276 
00277                 if(p_buffer == 0)
00278                 {
00279                         p_buffer = buildResizeTable(c.getSurface()->w, c.getSurface()->h, getSurface()->w, getSurface()->h, p_buffer);
00280                 }
00281 
00282                 mx::mxPainter pobj1(c), pobj2(*this);
00283 
00284                 register int i,z;
00285 
00286                 pobj1.lock(); pobj2.lock();
00287 
00288                 for(i = 0; i < object_surface->w; i++)
00289                 {
00290 
00291                         for(z = 0; z < object_surface->h; z++)
00292                         {
00293 
00294                                 static Color color;
00295                                 mxPoint *cur_pixel = &p_buffer[i][z];
00296                                 color =  pobj1.getpixel(cur_pixel->x, cur_pixel->y);
00297                                 pobj2.setpixel(i,z, color);
00298                         }
00299                 }
00300 
00301                 pobj1.unlock(); pobj2.unlock();
00302 
00303 #else
00304                 SDL_StretchSurfaceRect( c.getSurface(), 0, getSurface(), 0);
00305 #endif
00306 
00307                 return true;
00308         }

mxSurface * mx::mxSurface::copySurface (  ) 

creates a copy of this surface

Returns:
mxSurface pointer for newly allocated surface

Definition at line 418 of file mxsurface.cpp.

References copySurface(), createSurface(), height(), mxSurface(), and width().

Referenced by copySurfaceColorKey(), and setCopySurface().

00419         {
00420 
00421                 mxSurface *surf = 0;
00422 
00423                 try
00424                 {
00425 
00426                         surf = new mxSurface();
00427                         surf->createSurface(width(), height());
00428                         surf->copySurface(*this, 0, 0);
00429                         return surf;
00430 
00431                 }
00432                 catch(...)
00433                 {
00434                         delete surf;
00435                 }
00436 
00437                 return 0;
00438         }

bool mx::mxSurface::copySurface ( const mxSurface c,
mx::Rect  source,
mx::Rect  dest 
) [inline]

copySurface - Copy a surface to this surface

Parameters:
c for surface
source for Rect object to contain source rectangle
dest for Rect object to contain destination rectangle
Returns:
boolean value for true on success false on failure

Definition at line 161 of file mxsurface.h.

References copySurface(), mx::Rect::height(), mx::Rect::width(), mx::Rect::x(), and mx::Rect::y().

Referenced by copySurface().

00161 { SDL_Rect src = { source.x(), source.y(), source.width(), source.height() }; SDL_Rect dst = { dest.x(), dest.y(), dest.width(), dest.height() }; return copySurface(c, &src, &dst); }

bool mx::mxSurface::copySurface ( const mxSurface c,
SDL_Rect *  source,
SDL_Rect *  dest 
)

copySurface - Copy a surface to this surface

Parameters:
c surface to copy from
source source rectangle
dest destination rectangle in this surface
Returns:
boolean value for true on success false for failure

Definition at line 146 of file mxsurface.cpp.

References object_surface.

Referenced by copySurface().

00147         {
00148 
00149                 if(object_surface == 0) {
00150 
00151                         throw mx::mxException<std::string>("libmx: Error cannot copy null surface\n");
00152 
00153                         return false;
00154                 }
00155 
00156                 SDL_BlitSurface(c.object_surface, source, object_surface, dest);
00157                 return true;
00158 
00159         }

bool mx::mxSurface::copySurfaceColorKey ( const mxSurface c,
SDL_Rect *  source,
SDL_Rect *  dest,
SDL_Color  color_key 
)

copySurfaceColorKey - Copy a surface to this surface with a color key

Parameters:
c surface to copy
source SDL_Rect for source
dest SDL_Rect for dest
color_key SDL_Color for color key
Returns:
boolean value to stand for true on success and false on failure

Definition at line 161 of file mxsurface.cpp.

References copySurface(), and object_surface.

00162         {
00163 
00164                 if(object_surface == 0) return false;
00165 
00166                 SDL_SetColorKey(c.object_surface, SDL_SRCCOLORKEY, SDL_MapRGB(c.object_surface->format, color_key.r, color_key.g, color_key.b));
00167                 copySurface(c, source, dest);
00168 
00169                 return true;
00170 
00171         }

bool mx::mxSurface::createSurface ( Size s  ) 

createSurface - Create a New Surface

Parameters:
s Size object Size(w,h) in pixels
Returns:
surface creation successful? true yes false no

Definition at line 109 of file mxsurface.cpp.

References createSurface(), mx::Size::height, and mx::Size::width.

00110         {
00111                 return createSurface(s.width, s.height);
00112         }

bool mx::mxSurface::createSurface ( int  w,
int  h 
)

createSurface - Create a New Surface

Parameters:
w width in pixels
h height in pixels
Returns:
surface creation successful? true yes false no

Definition at line 114 of file mxsurface.cpp.

References clean_up(), and object_surface.

Referenced by copySurface(), mx::mxWnd::createSizedBackground(), createSurface(), and setCopySurface().

00115         {
00116 
00117                 static int rmask, gmask, bmask, amask;
00118 
00119                 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
00120                 rmask = 0xff000000;
00121                 gmask = 0x00ff0000;
00122                 bmask = 0x0000ff00;
00123                 amask = 0x000000ff;
00124                 #else
00125                 rmask = 0x000000ff;
00126                 gmask = 0x0000ff00;
00127                 bmask = 0x00ff0000;
00128                 amask = 0xff000000;
00129                 #endif
00130 
00131 
00132                 clean_up();
00133 
00134                 object_surface =  SDL_CreateRGBSurface(SDL_SWSURFACE,w,h,32, rmask, gmask, bmask, amask);
00135 
00136                 std::cout << "libmx: created surface  @" << w << "/" << h << "\n";
00137 
00138                 if(object_surface)
00139                 return true;
00140 
00141                 throw mx::mxException<std::string>("libmx: Error surface creation failed\n");
00142 
00143                 return false;
00144         }

void mx::mxSurface::Flip (  )  const

Flip this surface if surface is front surface

Definition at line 209 of file mxsurface.cpp.

References object_surface.

Referenced by mx::mxWnd::renderScreen(), and mx::mxWnd::sizedFlip().

00210         {
00211                 SDL_Flip(object_surface);
00212         }

SDL_Surface * mx::mxSurface::getSurface (  )  const

const int mx::mxSurface::height (  )  const

height of this surface

Returns:
height of this surface

Definition at line 400 of file mxsurface.cpp.

References object_surface.

Referenced by copySurface(), mx::mxWnd::initWnd(), and size().

00401         {
00402                 if(object_surface == 0) return 0;
00403                 return object_surface->h;
00404         }

const bool mx::mxSurface::isLocked (  )  const

isLocked - is this surface locked ?

Returns:
boolean value for locked or unlocked

Definition at line 348 of file mxsurface.cpp.

00349         {
00350                 return (is_locked);
00351         }

const int mx::mxSurface::linepos ( const int  x,
const int  y 
) const [inline]

line pos get the position in the 4byte alinged stream for X,Y coordinate pair , used for pixel plotting

Parameters:
x x coordinate
y y coordinate
Returns:
returns the offset for setting the data

Definition at line 203 of file mxsurface.h.

References width().

00203 { return (x+(y*width())); }

const bool mx::mxSurface::loadBitmapFromMemory ( void *  memory  ) 

Load a bitmap from Memory versus from file

Parameters:
memory pointer to stream of bytes for bitmap
Returns:
true/false value for success

Definition at line 372 of file mxsurface.cpp.

References object_surface.

00373         {
00374                 SDL_RWops *opz;
00375                 opz = SDL_RWFromMem(memory, sizeof(memory));
00376                 if(opz == 0) throw mx::mxException<std::string>(" Error loading memory chunk to bitmap ");
00377                 object_surface = SDL_LoadBMP_RW(opz, 1);
00378                 SDL_FreeRW(opz);
00379                 return true;
00380         }

void mx::mxSurface::lockSurface (  ) 

lockSurface - locks the surface for plotting pixels or manipulating the surface's raw data

Definition at line 333 of file mxsurface.cpp.

References object_surface.

00334         {
00335 
00336                 if(object_surface == 0) throw mx::mxException<std::string>(" null surface passed to lock operation\n ");
00337 
00338 
00339                 if(SDL_MUSTLOCK(object_surface))
00340                 {
00341                         SDL_LockSurface(object_surface);
00342                 }
00343 
00344                 is_locked = true;
00345 
00346         }

void mx::mxSurface::noZero ( bool  noremove  )  [virtual]

Use this function if you do not want this object's destructor to release the surface on destruction

Parameters:
noremove true for removal, false for no free

Definition at line 228 of file mxsurface.cpp.

Referenced by mx::mxWnd::initWnd(), and mxSurface().

00229         {
00230                 protected_noremove = zero;
00231         }

mx::mxSurface::operator SDL_PixelFormat * (  ) 

operator of SDL_PixelFormat for use in expressions

Returns:
SDL_PixelFormat *

Definition at line 219 of file mxsurface.cpp.

References object_surface.

00220         {
00221                 if(object_surface == 0) throw mx::mxException<std::string>( " error null surface trying to use pixel format for implicit conversion");
00222 
00223                 return object_surface->format;
00224 
00225         }

mx::mxSurface::operator SDL_Surface * (  ) 

operator SDL_Surface for use in expressions

Returns:
SDL_Surface *

Definition at line 214 of file mxsurface.cpp.

References object_surface.

00215         {
00216                 return object_surface;
00217         }

mxSurface & mx::mxSurface::operator= ( SDL_Surface *  surface  ) 

overloaded assignment operator =

Parameters:
surface of type SDL_Surface * assign a surface to this object
Returns:
of type mxSurface Reference of this object

Definition at line 93 of file mxsurface.cpp.

References clean_up(), and object_surface.

00094         {
00095 
00096                 if(surface == 0) throw mx::mxException<std::string>("libmx: error operator= on mxsurface: surface is null");
00097 
00098 
00099                 clean_up();
00100                 object_surface = surface;
00101                 return *this;
00102         }

mxSurface & mx::mxSurface::operator= ( const mxSurface c  ) 

overloaded assignment operator =

Parameters:
c of type mxSurface
Returns:
of type mxSurface Reference of this object

Definition at line 76 of file mxsurface.cpp.

References clean_up(), and object_surface.

00077         {
00078                 clean_up();
00079                 object_surface = c.object_surface;
00080                 return *this;
00081         }

unsigned int & mx::mxSurface::operator[] ( unsigned int  pos  ) 

overloaded operator[] for use in getting and plotting pixels for a surface be sure to lockSurface before

Parameters:
pos the position of the data to be put or get aligned by 4 bytes
Returns:
data at position pos

Definition at line 384 of file mxsurface.cpp.

References rawData().

00385         {
00386                         if(is_locked == false) throw mx::mxException<std::string>( " Error trying to manipulate pixels on surface which is not locked ");
00387                         void  *r = rawData();
00388                         unsigned int *ptr = (unsigned int *)r;
00389                         return ptr[pos];
00390         }

const int mx::mxSurface::pitch (  )  const

returns pitch

Returns:
pitch of image

Definition at line 406 of file mxsurface.cpp.

References object_surface.

00407         {
00408                 if(object_surface == 0) return 0;
00409                 return object_surface->pitch;
00410         }

void * mx::mxSurface::rawData (  ) 

rawData return the surface's raw pixel data, be sure to lockSurface first

Returns:
void * pointing to the surface's raw data

Definition at line 365 of file mxsurface.cpp.

References object_surface.

Referenced by operator[]().

00366         {
00367                 if(is_locked == true) return object_surface->pixels;
00368                 return 0;
00369         }

void mx::mxSurface::setCopySurface ( SDL_Surface *  surface  ) 

setCopySurface sets the surface by copying it to a newly created surface of 32bpp

Parameters:
surface a raw SDL_Surface

Definition at line 84 of file mxsurface.cpp.

References clean_up(), copySurface(), and createSurface().

00085         {
00086                 clean_up();
00087                 createSurface(surface->w, surface->h);
00088                 copySurface(surface, 0, 0);
00089         }

Size mx::mxSurface::size (  ) 

size returns Size object variable for Size of surface

Returns:
Size object

Definition at line 412 of file mxsurface.cpp.

References height(), and width().

00413         {
00414                 return Size(width(), height());
00415         }

SDL_Surface* mx::mxSurface::surface (  )  const [inline]

surface returns this objects encapsulated surface

Returns:
this surface

Definition at line 62 of file mxsurface.h.

References object_surface.

00062 { return object_surface; }

void mx::mxSurface::unlockSurface (  ) 

unlockSurface - unlock the Surface

Definition at line 353 of file mxsurface.cpp.

References object_surface.

00354         {
00355                 if(object_surface == 0) throw mx::mxException<std::string>(" null surface passed to unlock operation\n ");
00356 
00357                 if(SDL_MUSTLOCK(object_surface))
00358                 {
00359                         SDL_UnlockSurface(object_surface);
00360                 }
00361                 is_locked = false;
00362         }

void mx::mxSurface::updateRect ( unsigned int  x,
unsigned int  y,
unsigned int  w,
unsigned int  h 
) const

Update Rectangle within Surface

Parameters:
x x coordinate
y y coordinate
w width in pixels
h height in pixels

Definition at line 323 of file mxsurface.cpp.

References object_surface.

00324         {
00325                 SDL_UpdateRect(object_surface, x, y, w, h);
00326         }

const int mx::mxSurface::width (  )  const

width return width of this surface

Returns:
width of this surface

Definition at line 394 of file mxsurface.cpp.

References object_surface.

Referenced by copySurface(), mx::mxWnd::initWnd(), linepos(), and size().

00395         {
00396                 if(object_surface == 0) return 0;
00397                 return object_surface->w;
00398         }


Member Data Documentation

SDL_Surface* mx::mxSurface::object_surface [protected]


The documentation for this class was generated from the following files:

Generated on Wed Jun 10 14:52:02 2009 for libmx by  doxygen 1.5.8