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_SDLSURFACE_H 00022 #define WT_SDLSURFACE_H 00023 00024 // SDL_Surface 00025 #include <SDL/SDL_video.h> 00026 #include <SDL/SDL_endian.h> 00027 00028 #include <vector> 00029 00030 #include <boost/shared_ptr.hpp> 00031 00032 #include "sdlrect.h" 00033 #include "sdlpixelformat.h" 00034 #include "flagmap.h" 00035 #include <wt/flags.h> 00036 00037 namespace Wt { 00038 00039 /* this is a thin layer without object management 00040 around SDL_Surface pointer */ 00041 class SDLSurface { 00042 00043 public: 00044 typedef enum { 00045 IgnoreEndian, 00046 BigEndian = SDL_BIG_ENDIAN, 00047 LittleEndian = SDL_LIL_ENDIAN 00048 } Endian; 00049 00050 typedef enum { 00051 ScaleFree, 00052 ScaleMin, 00053 ScaleMax 00054 } ScaleMode; 00055 00056 /// constructor for a sized surface 00057 SDLSurface(int w, int h, int depth = -1, int flags = 0); 00058 00059 /// constructor from SDL_Surface. we take over ownership 00060 SDLSurface(SDL_Surface* surface = 0); 00061 00062 /// constructor from file 00063 SDLSurface(const std::string& filename); 00064 00065 public: 00066 //cast operators for automatic downcasting of our childs 00067 operator const SDL_Surface* () const { 00068 return sdl_surface.get(); 00069 } 00070 00071 operator SDL_Surface* () { 00072 return sdl_surface.get(); 00073 } 00074 00075 //! \name Informative methods 00076 //@{ 00077 int flags() const { 00078 return sdl_surface->flags; 00079 } 00080 00081 bool isSWSurface() const { 00082 return sdl_surface->flags & SDL_SWSURFACE; 00083 } 00084 00085 bool isHWSurface() const { 00086 return sdl_surface->flags & SDL_HWSURFACE; 00087 } 00088 00089 bool hasAsyncBlit() const { 00090 return sdl_surface->flags & SDL_ASYNCBLIT; 00091 } 00092 00093 bool hasHWPalette() const { 00094 return sdl_surface->flags & SDL_HWPALETTE; 00095 } 00096 00097 bool hasHWAcceleration() const { 00098 return sdl_surface->flags & SDL_HWACCEL; 00099 } 00100 00101 bool hasTransparentColor() const { 00102 return sdl_surface->flags & SDL_SRCCOLORKEY; 00103 } 00104 00105 bool hasRLEAccell() const { 00106 return sdl_surface->flags & SDL_RLEACCEL; 00107 } 00108 00109 bool hasAlpha() const { 00110 return sdl_surface->flags & SDL_SRCALPHA; 00111 } 00112 00113 /*! There are 3 mechanisms of transparency 00114 - ColorKey 00115 - Per Surface Alpha 00116 - Alpha Channel 00117 This function returns true if anyone of 00118 them is enabled 00119 */ 00120 bool isTransparent() const { 00121 return (hasTransparentColor() || 00122 (hasAlpha() && 00123 (pixelFormat().alphaMask() || alpha() != SDLColor::Opaque))); 00124 } 00125 00126 bool isOpaque() const { 00127 return !isTransparent(); 00128 } 00129 00130 bool isPreallocated() const { 00131 return sdl_surface->flags & SDL_PREALLOC; 00132 } 00133 00134 const SDLPixelFormat pixelFormat() const { 00135 return sdl_surface->format; 00136 } 00137 00138 int width() const { 00139 return sdl_surface->w; 00140 } 00141 00142 int height() const { 00143 return sdl_surface->h; 00144 } 00145 00146 int bytesPerLine() const { 00147 return sdl_surface->pitch; 00148 } 00149 00150 const void *pixels() const { 00151 return sdl_surface->pixels; 00152 } 00153 00154 int depth() const { 00155 return pixelFormat().bitsPerPixel(); 00156 } 00157 00158 void *pixels() { 00159 return sdl_surface->pixels; 00160 } 00161 00162 void *bits() { 00163 return pixels(); 00164 } 00165 00166 int pixelOffset(int x, int y) const { 00167 return y * bytesPerLine() + x * pixelFormat().bytesPerPixel(); 00168 } 00169 00170 void *pixelAddress(int pixel_offset) const { 00171 return (Uint8 *) pixels() + pixel_offset; 00172 } 00173 00174 void *pixelAddress(int x, int y) const { 00175 return (Uint8 *) pixels() + pixelOffset(x, y); 00176 } 00177 00178 void *scanLine(int y) { 00179 return pixelAddress(y * bytesPerLine()); 00180 } 00181 00182 size_t numBytes() const { 00183 //return height() * pitch(); 00184 return (height() - 1) * bytesPerLine() + 00185 width() * pixelFormat().bytesPerPixel(); 00186 } 00187 00188 int serialNumber() const { 00189 return reinterpret_cast<int>( 00190 static_cast<const SDL_Surface *>(*this)); 00191 } 00192 //@} 00193 00194 //! \name Clipping Methods 00195 //@{ 00196 SDLRect clipRect() const; 00197 SDLRect setClipRect(const SDLRect& rect); 00198 //@} 00199 00200 //! \name Colors and transparency methods 00201 //@{ 00202 void setPalette(const std::vector<SDLColor>& ca, int offset, 00203 int flags = SDLPalette::Logical | SDLPalette::Physical); 00204 00205 SDLColor color(int index) const { 00206 return pixelFormat().palette()[index]; 00207 } 00208 00209 void setColor(int i, const SDLColor& c); 00210 00211 int numColors() const; 00212 void setNumColors(int ncolors); 00213 00214 bool isGrayscale() const; 00215 00216 void setTransparentColor(const SDLColor& color = SDLColor()); 00217 void clearTransparentColor(); 00218 SDLColor transparentColor() const { 00219 return pixelFormat().transparentColor(); 00220 } 00221 00222 void setAlpha(int alpha = SDLColor::Transparent); 00223 void clearAlpha(); 00224 int alpha() const { 00225 return pixelFormat().alpha(); 00226 } 00227 00228 //@} 00229 00230 int lock() 00231 ; 00232 int unlock(); 00233 00234 //! \name Painting and blitting methods 00235 //@{ 00236 int fill(int v); 00237 00238 int fill(const SDLRect& dst, int v); 00239 00240 /// full blit 00241 int blit(const SDLSurface& src); 00242 /// blit to 00243 int blit(int x, int y, const SDLSurface& src); 00244 /// blit to from 00245 int blit(int x, int y, const SDLSurface& src, const SDLRect& src_rect); 00246 00247 /// copy to from src 00248 int blitAlphaCopy(const SDLSurface& src); 00249 /// copy to from src 00250 int blitAlphaCopy(int x, int y, const SDLSurface& src); 00251 /// copy to from src rect 00252 int blitAlphaCopy(int x, int y, const SDLSurface& src, const SDLRect& src_rect); 00253 00254 /// load an image from a file 00255 bool load(const std::string& fileName); 00256 /// load an image from mem 00257 bool load(const void *p, size_t size); 00258 //@} 00259 00260 //! \name Scaling methods 00261 //@{ 00262 SDLSurface scale(int w, int h, ScaleMode mode = ScaleFree) const; 00263 SDLSurface smoothScale(int w, int h, ScaleMode mode = ScaleFree) const; 00264 SDLSurface scaleWidth(int w) const; 00265 SDLSurface scaleHeight(int h) const; 00266 //@} 00267 00268 //! \name Surface conversion methods 00269 //@{ 00270 int convertTo(const SDLSurface src); 00271 int convertToDisplay(); 00272 int convertDepth(int new_depth); 00273 void resize(int x, int y); 00274 //@} 00275 00276 void detach(); 00277 00278 int pixel(int pixel_offset) const { 00279 return (this->*read_pixel)(pixel_offset); 00280 } 00281 00282 SDLColor pixelColor(int pixel_offset) const { 00283 return pixelFormat().mapToColor((pixel(pixel_offset))); 00284 } 00285 00286 int pixel(int x, int y) const { 00287 return pixel(pixelOffset(x, y)); 00288 } 00289 00290 SDLColor pixelColor(int x, int y) const { 00291 return pixelFormat().mapToColor((pixel(x, y))); 00292 } 00293 00294 void setPixel(int pixel_offset, int pixel_value) { 00295 (this->*write_pixel)(pixel_offset, pixel_value); 00296 } 00297 00298 void setPixelColor(int pixel_offset, const SDLColor& color) { 00299 setPixel(pixel_offset, pixelFormat().mapToPixelValue(color)); 00300 } 00301 00302 void setPixel(int x, int y, int pixel_value) { 00303 setPixel(pixelOffset(x, y), pixel_value); 00304 } 00305 00306 void setPixelColor(int x, int y, const SDLColor& color) { 00307 setPixel(x, y, pixelFormat().mapToPixelValue(color)); 00308 } 00309 00310 static int defaultDepth(); 00311 00312 /// Determines the byte order of the display hardware 00313 static int systemByteOrder() { 00314 return SDL_BYTEORDER; 00315 } 00316 00317 /// Determines the bit order of the display hardware 00318 static int systemBitOrder() { 00319 return SDL_BYTEORDER; 00320 } 00321 00322 protected: 00323 int readPixel8bpp(int pixel_offset) const; 00324 void writePixel8bpp(int pixel_offset, int pixel_value); 00325 int readPixel16bpp(int pixel_offset) const; 00326 void writePixel16bpp(int pixel_offset, int pixel_value); 00327 int readPixel24bpp(int pixel_offset) const; 00328 void writePixel24bpp(int pixel_offset, int pixel_value); 00329 int readPixel32bpp(int pixel_offset) const; 00330 void writePixel32bpp(int pixel_offset, int pixel_value); 00331 00332 void onDepthChange(); 00333 00334 SDLSurface scale_surface(int w, int h, bool smooth, ScaleMode mode) const; 00335 00336 static const FlagMap flagMap; 00337 00338 private: 00339 /// wraps up the surface deletion so we can use 00340 /// boost smart pointers with it 00341 class Deleter { 00342 public: 00343 void operator()(SDL_Surface* s); 00344 }; 00345 00346 typedef boost::shared_ptr<SDL_Surface> Ptr; 00347 Ptr sdl_surface; 00348 00349 //provided for convenience only and for in class usage 00350 operator const Ptr () const { 00351 return sdl_surface; 00352 } 00353 00354 operator Ptr () { 00355 return sdl_surface; 00356 } 00357 00358 typedef int (SDLSurface::*readPixelFunction)(int) const; 00359 typedef void (SDLSurface::*writePixelFunction)(int, int); 00360 00361 readPixelFunction read_pixel; 00362 writePixelFunction write_pixel; 00363 }; 00364 00365 } // namespace Wt 00366 00367 #endif // WT_SDLSURFACE_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.