FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
renderbackendsdl.cpp
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 *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library 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 GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, 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 #include "video/devicecaps.h"
35 
36 #include "renderbackendsdl.h"
37 #include "sdlimage.h"
38 #include "SDL_image.h"
39 #include "SDL_getenv.h"
40 
41 namespace FIFE {
42  static Logger _log(LM_VIDEO);
43 
44  RenderBackendSDL::RenderBackendSDL(const SDL_Color& colorkey) : RenderBackend(colorkey) {
45  }
46 
47 
48  RenderBackendSDL::~RenderBackendSDL() {
49  deinit();
50  }
51 
52  const std::string& RenderBackendSDL::getName() const {
53  static std::string backend_name = "SDL";
54  return backend_name;
55  }
56 
57  void RenderBackendSDL::init(const std::string& driver) {
58  char* buf;
59  if (driver != "") {
60  std::string envVar = std::string("SDL_VIDEODRIVER=") + driver;
61  buf = const_cast<char*>(envVar.c_str());
62  putenv(buf);
63  }
64 
65  if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
66  throw SDLException(SDL_GetError());
67 
68  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); // temporary hack
69  }
70 
72  SDL_Rect rect;
73  rect.x = 0;
74  rect.y = 0;
75  rect.w = getWidth();
76  rect.h = getHeight();
77  SDL_SetClipRect(m_screen->getSurface(), &rect);
78  SDL_FillRect(m_screen->getSurface(), 0, 0x00);
79  }
80 
81  Image* RenderBackendSDL::createMainScreen(const ScreenMode& mode, const std::string& title, const std::string& icon){
82  if(icon != "") {
83  SDL_Surface *img = IMG_Load(icon.c_str());
84  if(img != NULL) {
85  SDL_WM_SetIcon(img, 0);
86  }
87  }
88 
89  Image *image = setScreenMode(mode);
90 
91  SDL_WM_SetCaption(title.c_str(), 0);
92 
93  return image;
94  }
95 
96  Image* RenderBackendSDL::setScreenMode(const ScreenMode& mode) {
97  uint16_t width = mode.getWidth();
98  uint16_t height = mode.getHeight();
99  uint16_t bitsPerPixel = mode.getBPP();
100  bool fs = mode.isFullScreen();
101  uint32_t flags = mode.getSDLFlags();
102 
103  SDL_Surface* screen = NULL;
104 
105  if (bitsPerPixel != 0) {
106  uint16_t bpp = SDL_VideoModeOK(width, height, bitsPerPixel, flags);
107  if (!bpp){
108  throw SDLException("Selected video mode not supported!");
109  }
110  }
111 
112  screen = SDL_SetVideoMode(width, height, bitsPerPixel, flags);
113  if( !screen ) {
114  throw SDLException("Unable to set video mode selected!");
115  }
116 
117  FL_LOG(_log, LMsg("RenderBackendSDL")
118  << "Videomode " << width << "x" << height
119  << " at " << int(screen->format->BitsPerPixel) << " bpp");
120 
121  //update the screen mode with the actual flags used
122  m_screenMode = ScreenMode(width,
123  height,
124  bitsPerPixel,
125  screen->flags);
126 
127  if (!screen) {
128  throw SDLException(SDL_GetError());
129  }
130 
131  delete m_screen;
132  m_screen = new SDLImage(screen);
133  return m_screen;
134  }
135 
137  }
138 
140  SDL_Flip(m_screen->getSurface());
141  }
142 
143  Image* RenderBackendSDL::createImage(SDL_Surface* surface) {
144  return new SDLImage(surface);
145  }
146 
147  Image* RenderBackendSDL::createImage(const uint8_t* data, unsigned int width, unsigned int height) {
148  return new SDLImage(data, width, height);
149  }
150 
151  void RenderBackendSDL::setLightingModel(unsigned int lighting) {
152  SDLException("Lighting not available under SDL");
153  }
154 
155  unsigned int RenderBackendSDL::getLightingModel() const {
156  return 0;
157  }
158 
160  }
161 
163  }
164 
165  void RenderBackendSDL::setLighting(float red, float green, float blue, float alpha) {
166  }
167 
169  }
170 
172  }
173 
175  }
176 
177  void RenderBackendSDL::setStencilTest(uint8_t stencil_ref, unsigned int stencil_op, unsigned int stencil_func) {
178  }
179 
180  void RenderBackendSDL::resetStencilBuffer(uint8_t buffer) {
181  }
182 
184  return 0;
185  }
186 
188  }
189 
191  }
192 
193  void RenderBackendSDL::setAlphaTest(float ref_alpha) {
194  }
195 
196  void RenderBackendSDL::changeBlending(int scr, int dst){
197  }
198 
199  bool RenderBackendSDL::putPixel(int x, int y, int r, int g, int b, int a) {
200  return static_cast<SDLImage*>(m_screen)->putPixel(x, y, r, g, b, a);
201  }
202 
203  void RenderBackendSDL::drawLine(const Point& p1, const Point& p2, int r, int g, int b, int a) {
204  static_cast<SDLImage*>(m_screen)->drawLine(p1, p2, r, g, b, a);
205  }
206 
207  void RenderBackendSDL::drawTriangle(const Point& p1, const Point& p2, const Point& p3, int r, int g, int b, int a) {
208  static_cast<SDLImage*>(m_screen)->drawTriangle(p1, p2, p3, r, g, b, a);
209  }
210 
211  void RenderBackendSDL::drawRectangle(const Point& p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
212  static_cast<SDLImage*>(m_screen)->drawRectangle(p, w, h, r, g, b, a);
213  }
214 
215  void RenderBackendSDL::fillRectangle(const Point& p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
216  static_cast<SDLImage*>(m_screen)->fillRectangle(p, w, h, r, g, b, a);
217  }
218 
219  void RenderBackendSDL::drawQuad(const Point& p1, const Point& p2, const Point& p3, const Point& p4, int r, int g, int b, int a) {
220  static_cast<SDLImage*>(m_screen)->drawQuad(p1, p2, p3, p4, r, g, b, a);
221  }
222 
223  void RenderBackendSDL::drawVertex(const Point& p, const uint8_t size, int r, int g, int b, int a){
224  static_cast<SDLImage*>(m_screen)->drawVertex(p, 2, r, g, b, a);
225  }
226 
227  void RenderBackendSDL::drawLightPrimitive(const Point& p, uint8_t intensity, float radius, int subdivisions, float xstretch, float ystretch, uint8_t red, uint8_t green, uint8_t blue){
228  static_cast<SDLImage*>(m_screen)->drawLightPrimitive(p, intensity, radius, subdivisions, xstretch, ystretch, red, green, blue);
229  }
230 }//FIFE