00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <SDL.h>
00026
00027
00028
00029
00030
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);
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
00085
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 }