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

surface.h

00001 /* 00002 libwftk - Worldforge Toolkit - a widget library 00003 Copyright (C) 2002 Malcolm Walker <malcolm@worldforge.org> 00004 Based on code copyright (C) 1999-2002 Karsten Laux 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the 00018 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00019 Boston, MA 02111-1307, SA. 00020 */ 00021 // written by Karsten Laux, June 1999 00022 00023 #ifndef _SURFACE_H_ 00024 #define _SURFACE_H_ 00025 00026 #include <SDL/SDL.h> 00027 00028 #include <wftk/color.h> 00029 #include <wftk/point.h> 00030 #include <wftk/pixelformat.h> 00031 #include <wftk/rect.h> 00032 #include <wftk/resources.h> 00033 00034 00035 namespace wftk { 00036 00037 class Region; 00038 00042 class GammaFunction 00043 { 00044 public: 00046 GammaFunction(); 00048 GammaFunction(char** xpm) : shift_x(0), shift_y(0) {read(xpm);} 00050 GammaFunction(const GammaFunction&); 00051 00053 GammaFunction& operator=(const GammaFunction&); 00054 00056 void read(char** xpm); 00057 00059 void setOffset(int x, int y) {offset_x = x & 0xff; offset_y = y & 0xff;} 00061 void setOffset(const wftk::Point& p) {setOffset(p.x, p.y);} 00063 void shiftOffset(int x, int y) {offset_x += x & 0xff; offset_y += y & 0xff;} 00065 void shiftOffset(const wftk::Point& p) {shiftOffset(p.x, p.y);} 00066 00076 void setBitShift(char x, char y) {shift_x = x; shift_y = y;} 00077 00079 char operator()(int x, int y) const {return values[(x & 0xff) + ((y & 0xff) << 8)];} 00081 char& operator()(int x, int y) {return values[(x & 0xff) + ((y & 0xff) << 8)];} 00082 00083 private: 00084 friend class Surface; 00085 // An wftk::GammaFunction acts like a texture which 00086 // shades the target surface differently at different points. 00087 // It's a 256x256 tile. 00088 char getPoint(int x, int y) const { 00089 // Bitwise & with 0xff gives (mod 256). It's also 00090 // safe for negative values of the offset. 00091 x = (shift_x >= 0) ? (x >> shift_x) : (x << -shift_x); 00092 y = (shift_y >= 0) ? (y >> shift_y) : (y << -shift_y); 00093 return values[((offset_x + x) & 0xff) 00094 + (((offset_y + y) & 0xff) << 8)]; 00095 } 00096 00097 char values[256*256]; // array of 256*256 values 00098 unsigned char offset_x; // offset to use 00099 unsigned char offset_y; 00100 char shift_x, shift_y; 00101 }; 00102 00104 class Surface 00105 { 00106 public: 00107 00109 friend class Painter; 00110 00112 Surface(); 00114 Surface(unsigned w, unsigned h, const Pixelformat& pixelformat 00115 = Pixelformat::ABGR8888); 00117 Surface(const Surface& surf); 00119 00123 Surface(Surface& parent, const Rect& rect); 00124 00126 void setAsChild(Surface& parent, const Rect& rect); 00127 00129 void setSurface(unsigned w, unsigned h, const Pixelformat& pixelformat 00130 = Pixelformat::ABGR8888); 00131 00132 ~Surface(); 00137 bool readFromXPM(char** data); 00142 bool readFromHeader(unsigned char* header_data, unsigned int w, unsigned int h); 00148 bool readFromFile(const std::string& filename); 00150 bool writeToFile(const std::string& filename); 00151 00153 Surface& operator = (const Surface& surf); 00157 void setPalette(const SDL_Palette* pal); 00158 00162 bool empty() const { return sdlSurface_ == 0; } 00164 unsigned width() const { return ((sdlSurface_ == 0) ? 0 : sdlSurface_->w); } 00166 unsigned height() const { return ((sdlSurface_ == 0) ? 0 : sdlSurface_->h); } 00168 Rect rect() const {return Rect(0, 0, width(), height());} 00170 Uint16 pitch() const { return ((sdlSurface_ == 0) ? 0 : sdlSurface_->pitch);} 00171 00173 00184 Region opaqueRegion(Uint8 alpha_cutoff = (SDL_ALPHA_OPAQUE ? 127 : 128)) const; 00185 00187 Pixelformat pixelformat() const {return Pixelformat(sdlSurface_);} 00188 00194 bool convert(const Pixelformat& pixelformat, bool dither = true); 00195 00197 bool mirror(); 00199 bool scale(unsigned new_width, unsigned new_height); 00201 bool scale(float n); 00202 00204 void gammaShift(char g); 00206 void gammaShift(const GammaFunction &gamma); 00207 00209 void setColorKey(const Color& color) {doSetColorKey(&color);} 00211 void clearColorKey() {doSetColorKey(0);} 00213 Color transparentColor() const; 00214 00216 void setAlpha(unsigned char alpha); 00218 bool usesColorKey() const 00219 {return sdlSurface_ && (sdlSurface_->flags & SDL_SRCCOLORKEY);} 00221 bool hasAlphaChannel() const 00222 {return sdlSurface_ && sdlSurface_->format->Amask;} 00224 void useRLEAcceleration(bool flag); 00225 00228 Rect blit(Surface& s) const; 00232 Rect blit(Surface& s, const Rect& dest) const; 00237 Rect blit(Surface& s, const Rect& dest, const Rect& src) const; 00244 void blit(Surface& s, const Point& dest, const Region& destMask) const; 00246 Rect scaledBlit(Surface& s, bool smooth = false) const; 00248 Rect scaledBlit(Surface&, const Rect& dest, bool smooth = false) const; 00250 Rect scaledBlit(Surface&, const Rect& dest, const Rect& src, bool smooth = 00251 false) const; 00257 Rect textureBlit(Surface& dst, const Point& p1, const Point& p2, 00258 const Point& p3, const Point& p4) const; 00264 Rect textureBlit(Surface& dst, const Point& p1, const Point& p2, 00265 const Point& p3, const Point& p4, const Rect& src) const; 00266 00268 void clear(); 00270 void clear(const Rect&); 00272 void fill(const Color&); 00274 void fill(const Rect&, const Color&); 00276 void fill(const Region&, const Color&); 00278 void blend(const Color& c) {blend(rect(), c);} 00280 void blend(const Rect&, const Color&); 00282 void blend(const Region&, const Color&); 00283 00286 void setWMIcon() const {if(sdlSurface_) SDL_WM_SetIcon(sdlSurface_, 0);} 00287 00291 void* pixels() 00292 { return ((sdlSurface_ == NULL) ? 0 : sdlSurface_->pixels); }; 00296 const void* pixels() const 00297 { return ((sdlSurface_ == NULL) ? 0 : sdlSurface_->pixels); }; 00298 00303 void lock() const ; 00308 void unlock() const; 00309 00310 // you need to do your own locking for calls to readPixel()/writePixel() 00311 00313 void writePixel(Uint32 pixeladdr, Uint32 pixel); 00315 Uint32 readPixel(Uint32 pixeladdr) const; 00316 00317 // the next two do the locking for you, as well as color conversion 00318 00320 Color getPixel(const Point&) const; 00322 void setPixel(const Point&, const Color&); 00323 00324 struct ResLoad { 00325 std::pair<Surface*,bool> operator()(const std::string&); 00326 }; 00341 static ResourceRegistry<Surface*,ResLoad> registry; 00343 typedef Resource<Surface*> Resource; 00344 00345 void makeGLTexture(); 00346 00347 protected: 00349 SDL_Surface* sdlSurface_; 00351 SDL_Surface* glSurface_; 00352 unsigned int glTexture_; 00353 float glTexMaxX_; 00354 float glTexMaxY_; 00355 00356 private: 00358 SDL_Surface* parent_; 00359 00361 void doSetColorKey(const Color*); 00362 00364 void drawGL(SDL_Rect src, SDL_Rect dst) const; 00365 }; 00366 00367 } 00368 00369 #endif

Generated Wed Jul 28 17:28:43 2004.
Copyright © 1998-2003 by the respective authors.

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.