Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

sdlmm_basesurface.cpp

Go to the documentation of this file.
00001 /*
00002  * SDLmm - a C++ wrapper for SDL and related libraries
00003  * Copyright  2001 David Hedbor <david@hedbor.org>
00004  * 
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  *
00019  */
00020 
00021 #include "sdlmm_config.h"
00022 
00023 #include <SDL.h>
00024 #include "sdlmm_basesurface.h"
00025 
00026 // The SDLmm::BaseSurface class.
00027 
00028 namespace SDLmm {
00029 
00030   bool BaseSurface::Lock() {
00031     if(SDL_MUSTLOCK(me)) {
00032       return SDL_LockSurface(me) == 0;
00033     }
00034     else return true;
00035   }
00036 
00037   void BaseSurface::Unlock() {
00038     if(SDL_MUSTLOCK(me)) {
00039       SDL_UnlockSurface(me);
00040     }
00041   }
00042 
00043   void BaseSurface::SetPixel1(int x, int y, Color color) {
00044     // ASSERT(color < GetPixelFormat()->???);
00045     ASSERT(x >= 0);
00046     ASSERT(x < w());
00047     ASSERT(y >= 0);
00048     ASSERT(y < h());
00049     ASSERT(GetPixelFormat().BytesPerPixel() == 1);
00050     *((Uint8 *)pixels() + y * pitch() + x) = color;
00051   }
00052 
00053   void BaseSurface::SetPixel2(int x, int y, Color color) {
00054     // ASSERT(color < GetPixelFormat()->???);
00055     ASSERT(x >= 0);
00056     ASSERT(x < w());
00057     ASSERT(y >= 0);
00058     ASSERT(y < h());
00059     ASSERT(GetPixelFormat().BytesPerPixel() == 2);
00060     *((Uint16 *)pixels() + y * pitch()/2 + x) = color;
00061   }
00062 
00063   void BaseSurface::SetPixel3(int x, int y, Color color) {
00064     // ASSERT(color < GetPixelFormat()->???);
00065     ASSERT(x >= 0);
00066     ASSERT(x < w());
00067     ASSERT(y >= 0);
00068     ASSERT(y < h());
00069     ASSERT(GetPixelFormat().BytesPerPixel() == 3);
00070     Uint8 *pix = (Uint8 *)pixels() + y * pitch() + x * 3;
00071 
00072     if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
00073       pix[2] = color;
00074       pix[1] = color >> 8;
00075       pix[0] = color >> 16;
00076     } else {
00077       pix[0] = color;
00078       pix[1] = color >> 8;
00079       pix[2] = color >> 16;
00080     }
00081   }
00082 
00083   void BaseSurface::SetPixel4(int x, int y, Color color) {
00084     // ASSERT(color < GetPixelFormat()->???);
00085     ASSERT(x >= 0);
00086     ASSERT(x < w());
00087     ASSERT(y >= 0);
00088     ASSERT(y < h());
00089     ASSERT(GetPixelFormat().BytesPerPixel() == 4);
00090     *((Uint32 *)pixels() + y * pitch()/4 + x) = color;
00091   }
00092 
00093   void BaseSurface::SetPixel(int x, int y, Color color) {
00094     // ASSERT(color < GetPixelFormat()->???);
00095     ASSERT(x >= 0);
00096     ASSERT(x < w());
00097     ASSERT(y >= 0);
00098     ASSERT(y < h());
00099 
00100     switch(GetPixelFormat().BytesPerPixel())
00101     {
00102     case 1:
00103       SetPixel1(x, y, color);
00104       break;
00105 
00106     case 2:
00107       SetPixel2(x, y, color);
00108       break;
00109 
00110     case 3:
00111       SetPixel3(x, y, color);
00112       break;
00113 
00114     case 4:
00115       SetPixel4(x, y, color);
00116       break;
00117 
00118     default:
00119       ASSERT(false);
00120       break;
00121     }
00122   }
00123 
00124   Color BaseSurface::GetPixel(int x, int y) const {
00125     ASSERT(x >= 0);
00126     ASSERT(x < w());
00127     ASSERT(y >= 0);
00128     ASSERT(y < h());
00129 
00130     PixelFormat pf(GetPixelFormat());
00131 
00132     const Uint8* the_pixel = (const Uint8*)pixels() + y * pitch() + x * pf.BytesPerPixel();
00133 
00134     switch(pf.BytesPerPixel())
00135     {
00136     case 1:
00137       return *((const Uint8 *)the_pixel);
00138       break;
00139 
00140     case 2:
00141       return *((const Uint16 *)the_pixel);
00142       break;
00143 
00144     case 3:
00145       { /* Format/endian independent */
00146         Uint32 r, g, b;
00147         Color color;
00148         r = *(the_pixel + pf.Rshift()/8);
00149         g = *(the_pixel + pf.Gshift()/8);
00150         b = *(the_pixel + pf.Bshift()/8);
00151         color = r << pf.Rshift();
00152         color |= g << pf.Rshift();
00153         color |= b << pf.Bshift();
00154         return color;
00155       }
00156       break;
00157 
00158     case 4:
00159       return *((const Uint32 *)(the_pixel));
00160       break;
00161 
00162     default:
00163       ASSERT(false);
00164       return 0;
00165       break;
00166     }  
00167   }
00168 
00169   int BaseSurface::Blit(const BaseSurface& src, const SDL_Rect& srcrect, SDL_Rect& dstrect) {
00170     return SDL_BlitSurface(src.me, const_cast<SDL_Rect*>(&srcrect), me, &dstrect);
00171   }
00172 
00173   int BaseSurface::Blit(const BaseSurface& src, const SDL_Rect& srcrect, const SPoint& dstpoint) {
00174     SDL_Rect  rect;
00175     rect.x = dstpoint.x; rect.y = dstpoint.y;
00176     return Blit(src, srcrect, rect);
00177   }
00178 
00179   int BaseSurface::Blit(const BaseSurface& src, SDL_Rect& dstrect) {
00180     return SDL_BlitSurface(src.me, 0, me, &dstrect);
00181   }
00182 
00183   int BaseSurface::Blit(const BaseSurface& src, const SPoint& dstpoint) {
00184     SDL_Rect  rect;
00185     rect.x = dstpoint.x; rect.y = dstpoint.y;
00186     return Blit(src, rect);
00187   }
00188   
00189   int BaseSurface::Blit(const BaseSurface& src) {
00190     return SDL_BlitSurface(src.me, 0, me, 0);
00191   }
00192 
00193   bool BaseSurface::Fill(Color color) {
00194     return SDL_FillRect(me, 0, color) == 0;
00195   }
00196 
00197   bool BaseSurface::FillRect(SDL_Rect& dstrect, Color color) {
00198     return SDL_FillRect(me, &dstrect, color) == 0;
00199   }
00200 
00201   // Set various things
00202   bool BaseSurface::SetColorKey(Uint32 flag, Color key) {
00203     return SDL_SetColorKey(me, flag, key) == 0;
00204   }
00205   
00206   bool BaseSurface::SetAlpha(Uint32 flag, Uint8 alpha) {
00207     return SDL_SetAlpha(me, flag, alpha) == 0;
00208   }
00209   
00210   void BaseSurface::SetClipRect(const SDL_Rect& rect) {
00211     SDL_SetClipRect(me, const_cast<SDL_Rect*>(&rect));
00212   }
00213   
00214   void BaseSurface::ResetClipRect() {
00215     SDL_SetClipRect(me, 0);
00216   }
00217   
00218   void BaseSurface::GetClipRect(SDL_Rect& rect) const {
00219     SDL_GetClipRect(me, &rect);
00220   }
00221 
00222   bool BaseSurface::SaveBMP(const char *file) const {
00223     if(me) {
00224       return SDL_SaveBMP(me, file) == 0;
00225     }
00226     return false;
00227   } 
00228 }
00229 

Documentation automatically generated by doxygen written by Dimitri van Heesch. Project hosted at
Hosted by SourceForge