comparison engine/core/video/sdl/renderbackendsdl.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2008 by the FIFE team *
3 * http://www.fifengine.de *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23
24 // 3rd party library includes
25 #include <SDL.h>
26
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 #include "util/base/exception.h"
32 #include "util/math/fife_math.h"
33 #include "util/log/logger.h"
34
35 #include "renderbackendsdl.h"
36 #include "sdlimage.h"
37
38 namespace FIFE {
39 static Logger _log(LM_VIDEO);
40
41 RenderBackendSDL::RenderBackendSDL() : RenderBackend() {
42 }
43
44
45 RenderBackendSDL::~RenderBackendSDL() {
46 deinit();
47 }
48
49 const std::string& RenderBackendSDL::getName() const {
50 static std::string backend_name = "SDL";
51 return backend_name;
52 }
53
54 void RenderBackendSDL::init() {
55 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
56 throw SDLException(SDL_GetError());
57
58 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); // temporary hack
59 }
60
61 Image* RenderBackendSDL::createMainScreen(unsigned int width, unsigned int height, unsigned char bitsPerPixel, bool fs) {
62 Uint32 flags = 0;
63 if (fs) {
64 flags |= SDL_FULLSCREEN;
65 }
66
67 SDL_Surface* screen = NULL;
68
69 if( 0 == bitsPerPixel ) {
70 /// autodetect best mode
71 unsigned char possibleBitsPerPixel[] = {16, 24, 32, 0};
72 int i = 0;
73 while( true ) {
74 bitsPerPixel = possibleBitsPerPixel[i];
75 if( !bitsPerPixel ) {
76 // Last try, sometimes VideoModeOK seems to lie.
77 // Try bpp=0
78 screen = SDL_SetVideoMode(width, height, bitsPerPixel, flags);
79 if( !screen ) {
80 throw SDLException("Videomode not available");
81 }
82 break;
83 }
84 bitsPerPixel = SDL_VideoModeOK(width, height, bitsPerPixel, flags);
85 if ( bitsPerPixel ) {
86 screen = SDL_SetVideoMode(width, height, bitsPerPixel, flags);
87 if( screen ) {
88 break;
89 }
90 }
91 ++i;
92 }
93 } else {
94 if ( !SDL_VideoModeOK(width, height, bitsPerPixel, flags) ) {
95 throw SDLException("Videomode not available");
96 }
97 screen = SDL_SetVideoMode(width, height, bitsPerPixel, flags);
98 }
99 FL_LOG(_log, LMsg("RenderBackendSDL")
100 << "Videomode " << width << "x" << height
101 << " at " << int(screen->format->BitsPerPixel) << " bpp");
102
103 SDL_WM_SetCaption("FIFE", NULL);
104
105 if (!screen) {
106 throw SDLException(SDL_GetError());
107 }
108
109 m_screen = new SDLImage(screen);
110 return m_screen;
111 }
112
113 void RenderBackendSDL::startFrame() {
114 SDL_FillRect(m_screen->getSurface(), 0, 0x00);
115 }
116
117 void RenderBackendSDL::endFrame() {
118 SDL_Flip(m_screen->getSurface());
119 }
120
121 Image* RenderBackendSDL::createImage(SDL_Surface* surface) {
122 return new SDLImage(surface);
123 }
124
125 Image* RenderBackendSDL::createImage(const uint8_t* data, unsigned int width, unsigned int height) {
126 return new SDLImage(data, width, height);
127 }
128
129 bool RenderBackendSDL::putPixel(int x, int y, int r, int g, int b) {
130 return static_cast<SDLImage*>(m_screen)->putPixel(x, y, r, g, b);
131 }
132
133 void RenderBackendSDL::drawLine(const Point& p1, const Point& p2, int r, int g, int b) {
134 static_cast<SDLImage*>(m_screen)->drawLine(p1, p2, r, g, b);
135 }
136
137 void RenderBackendSDL::drawQuad(const Point& p1, const Point& p2, const Point& p3, const Point& p4, int r, int g, int b) {
138 static_cast<SDLImage*>(m_screen)->drawQuad(p1, p2, p3, p4, r, g, b);
139 }
140 }