SDLFrameBuffer.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: SDLFrameBuffer.cpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * $Id: SDLFrameBuffer.cpp,v 1.9 2005/01/20 20:04:22 mkropfbe Exp $ 00041 * * 00042 ***********************************************************************/ 00043 00044 #include "global.hpp" 00045 #include "IO.hpp" 00046 #include "Frame.hpp" 00047 #include "SDLFrameBuffer.hpp" 00048 #include "VideoFrameBuffer.hpp" 00049 00050 00051 00052 /*------------ SDLFrameBuffer methods --------------------------------------- */ 00053 00054 SDLFrameBuffer::SDLFrameBuffer(SDL_Surface *surface, int depth, bool gray, int width, int height) 00055 : VideoFrameBuffer(gray) 00056 { 00057 this->surface = surface; 00058 this->width = width; 00059 this->height = height; 00060 assert(depth % 8 == 0); 00061 00062 /* 00063 #if SDL_BYTEORDER == SDL_BIG_ENDIAN 00064 rmask = 0xff000000; 00065 gmask = 0x00ff0000; 00066 bmask = 0x0000ff00; 00067 amask = 0x000000ff; 00068 #else 00069 rmask = 0x000000ff; 00070 gmask = 0x0000ff00; 00071 bmask = 0x00ff0000; 00072 amask = 0xff000000; 00073 #endif 00074 00075 rmask = 0x00000020; 00076 gmask = 0x000007E0; 00077 bmask = 0x0000F800; 00078 00079 rmask = 0x0000000F; 00080 gmask = 0x000000F0; 00081 bmask = 0x00000F00; 00082 00083 //rmask = 0xFFFFFFFF; 00084 //gmask = 0xFFFFFFFF; 00085 //bmask = 0xFFFFFFFF; 00086 */ 00087 00088 /* 00089 Uint32 rmask, gmask, bmask, amask; 00090 amask=rmask=gmask=bmask=0; 00091 image = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, depth, rmask, gmask, bmask, amask); 00092 00093 if (image == NULL) { 00094 dprintf_err("Couldn't initialize blitbuffer with dimension %ix%i depth %i\n",width,height, depth); 00095 ::exit(1); 00096 } 00097 SDL_SetAlpha(image,0,0xff); 00098 dprintf_full("SDLFrameBuffer: initialize blitbuffer with dimension %ix%i depth %i, bpp %i, no aplpha blending\n", 00099 width,height, image->format->BitsPerPixel, image->format->BytesPerPixel); 00100 */ 00101 } 00102 00103 00104 SDLFrameBuffer::~SDLFrameBuffer() { 00105 if(image) 00106 SDL_FreeSurface(image); 00107 image = NULL; 00108 } 00109 00110 00111 inline void SDLFrameBuffer::setPixel(int x, int y, uint value) { 00112 00113 int bpp = surface->format->BytesPerPixel; 00114 Uint8 *p = (Uint8 *)(surface->pixels) + y * surface->pitch + x * bpp; 00115 00116 // dprintf_full("SDLFrameBuffer::setPixel image pitch %i bpp %i %x %x\n",image->pitch,image->format->BytesPerPixel,p,value); 00117 // printf("%x\t",value); 00118 00119 switch(bpp) { 00120 case 1: 00121 *p = (Uint8)value; 00122 break; 00123 00124 case 2: 00125 *(Uint16 *)p = value; 00126 break; 00127 00128 case 3: 00129 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { 00130 p[0] = (value >> 16) & 0xff; 00131 p[1] = (value >> 8) & 0xff; 00132 p[2] = value & 0xff; 00133 } else { 00134 p[0] = value & 0xff; 00135 p[1] = (value >> 8) & 0xff; 00136 p[2] = (value >> 16) & 0xff; 00137 } 00138 break; 00139 00140 case 4: 00141 if (gray) 00142 value = value << 16 | value << 8 | value; 00143 *(Uint32 *)p = value; 00144 break; 00145 } 00146 } 00147 00148 00149 void SDLFrameBuffer::bitBlt(AU *au) { 00150 //dprintf_full("SDLFrameBuffer::bitBlt surface pitch %i bpp %i AU->size %i\n",surface->pitch,surface->format->BytesPerPixel, au->size); 00151 // dprintf_full("SDLFrameBuffer::bitBlt image pitch %i bpp %i AU->size %i\n",image->pitch,image->format->BytesPerPixel, au->size); 00152 memcpy(surface->pixels, au->payload, au->size); 00153 } 00154 00155 00156 void SDLFrameBuffer::postPaintEvent() { 00157 /* 00158 if (SDL_MUSTLOCK(surface)) { 00159 if (SDL_LockSurface(surface) < 0) { 00160 ::exit(2); 00161 } 00162 } 00163 00164 if(SDL_BlitSurface(image, NULL, surface, NULL) < 0) 00165 fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError()); 00166 00167 if (SDL_MUSTLOCK(surface)) { 00168 SDL_UnlockSurface(surface); 00169 } 00170 */ 00171 SDL_UpdateRect(surface, 0, 0, 0, 0); 00172 } 00173