renderbackendsdl.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE 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.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 #include <SDL.h>
00026 
00027 // FIFE includes
00028 // These includes are split up in two parts, separated by one empty line
00029 // First block: files included from the FIFE root src directory
00030 // Second block: files included from the same folder
00031 #include "util/base/exception.h"
00032 #include "util/math/fife_math.h"
00033 #include "util/log/logger.h"
00034 
00035 #include "renderbackendsdl.h"
00036 #include "sdlimage.h"
00037 #include "SDL_image.h"
00038 
00039 namespace FIFE {
00040     static Logger _log(LM_VIDEO);
00041 
00042     RenderBackendSDL::RenderBackendSDL(const SDL_Color& colorkey) : RenderBackend(colorkey) {
00043     }
00044 
00045 
00046     RenderBackendSDL::~RenderBackendSDL() {
00047         deinit();
00048     }
00049 
00050     const std::string& RenderBackendSDL::getName() const {
00051         static std::string backend_name = "SDL";
00052         return backend_name;
00053     }
00054 
00055     void RenderBackendSDL::init() {
00056         if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
00057             throw SDLException(SDL_GetError());
00058 
00059         SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); // temporary hack
00060     }
00061 
00062     Image* RenderBackendSDL::createMainScreen(unsigned int width, unsigned int height, unsigned char bitsPerPixel, bool fs, const std::string& title, const std::string& icon) {
00063         Uint32 flags = 0;
00064         if (fs) {
00065             flags |= SDL_FULLSCREEN;
00066         }
00067 
00068         if(icon != "") {
00069             SDL_Surface *img = IMG_Load(icon.c_str());
00070             if(img != NULL) {
00071                 SDL_WM_SetIcon(img, 0);
00072             }
00073         }
00074 
00075         SDL_Surface* screen = NULL;
00076 
00077         if( 0 == bitsPerPixel ) {
00079             unsigned char possibleBitsPerPixel[] = {16, 24, 32, 0};
00080             int i = 0;
00081             while( true ) {
00082                 bitsPerPixel = possibleBitsPerPixel[i];
00083                 if( !bitsPerPixel ) {
00084                     // Last try, sometimes VideoModeOK seems to lie.
00085                     // Try bpp=0
00086                     screen = SDL_SetVideoMode(width, height, bitsPerPixel, flags);
00087                     if( !screen ) {
00088                         throw SDLException("Videomode not available");
00089                     }
00090                     break;
00091                 }
00092                 bitsPerPixel = SDL_VideoModeOK(width, height, bitsPerPixel, flags);
00093                 if ( bitsPerPixel ) {
00094                     screen = SDL_SetVideoMode(width, height, bitsPerPixel, flags);
00095                     if( screen ) {
00096                         break;
00097                     }
00098                 }
00099                 ++i;
00100             }
00101         } else {
00102             if ( !SDL_VideoModeOK(width, height, bitsPerPixel, flags) ) {
00103                 throw SDLException("Videomode not available");
00104             }
00105             screen = SDL_SetVideoMode(width, height, bitsPerPixel, flags);
00106         }
00107         FL_LOG(_log, LMsg("RenderBackendSDL")
00108             << "Videomode " << width << "x" << height
00109             << " at " << int(screen->format->BitsPerPixel) << " bpp");
00110 
00111         SDL_WM_SetCaption(title.c_str(), NULL);
00112 
00113         if (!screen) {
00114             throw SDLException(SDL_GetError());
00115         }
00116 
00117         m_screen = new SDLImage(screen);
00118         return m_screen;
00119     }
00120 
00121     void RenderBackendSDL::startFrame() {
00122         SDL_FillRect(m_screen->getSurface(), 0, 0x00);
00123     }
00124 
00125     void RenderBackendSDL::endFrame() {
00126         SDL_Flip(m_screen->getSurface());
00127     }
00128 
00129     Image* RenderBackendSDL::createImage(SDL_Surface* surface) {
00130         return new SDLImage(surface);
00131     }
00132 
00133     Image* RenderBackendSDL::createImage(const uint8_t* data, unsigned int width, unsigned int height) {
00134         return new SDLImage(data, width, height);
00135     }
00136 
00137     bool RenderBackendSDL::putPixel(int x, int y, int r, int g, int b) {
00138         return static_cast<SDLImage*>(m_screen)->putPixel(x, y, r, g, b);
00139     }
00140 
00141     void RenderBackendSDL::drawLine(const Point& p1, const Point& p2, int r, int g, int b) {
00142         static_cast<SDLImage*>(m_screen)->drawLine(p1, p2, r, g, b);
00143     }
00144 
00145     void RenderBackendSDL::drawQuad(const Point& p1, const Point& p2, const Point& p3, const Point& p4,  int r, int g, int b) {
00146         static_cast<SDLImage*>(m_screen)->drawQuad(p1, p2, p3, p4, r, g, b);
00147     }
00148 
00149     void RenderBackendSDL::drawVertex(const Point& p, const uint8_t size, int r, int g, int b){
00150         static_cast<SDLImage*>(m_screen)->drawVertex(p, 2, r, g, b);
00151     }
00152 
00153 }//FIFE
Generated by  doxygen 1.6.2-20100208