openal and fontft memleak fixes from keltar
[fegdk.git] / core / code / video / sdl / f_sdlrenderer.cpp
blob8f32d779793e160062a2cf8ca693c3a75ca929e5
1 /*
2 fegdk: FE Game Development Kit
3 Copyright (C) 2001-2008 Alexey "waker" Yakovenko
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 Alexey Yakovenko
20 waker@users.sourceforge.net
23 // $Header$
24 #include "pch.h"
25 #include "config.h"
27 #include "f_types.h"
28 #include "f_sdlrenderer.h"
29 #include "f_baseviewport.h"
30 #include "f_math.h"
31 #include "f_sdlviewport.h"
32 #include "f_sdlviewportdata.h"
33 #include "f_engine.h"
34 #include "f_application.h"
35 #include "f_console.h"
36 #include "f_error.h"
37 #include "f_fragileobject.h"
38 #include "f_resourcemgr.h"
39 #include <GL/glew.h>
40 #include "f_resourcemgr.h"
41 #include <SDL/SDL.h>
42 #include <cvars.h>
44 namespace fe
47 //-----------------------------------
48 // sdlRendererData
49 //-----------------------------------
51 sdlRendererData::sdlRendererData (void)
53 // enum vidmodes
55 mNumVidModes = 0;
56 mbWindowed = true;
58 SDL_Rect **modes;
59 SDL_PixelFormat fmt32 = {NULL, 32, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
60 SDL_PixelFormat fmt16 = {NULL, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
62 modes = SDL_ListModes (&fmt32, SDL_FULLSCREEN | SDL_HWSURFACE |
63 SDL_DOUBLEBUF | SDL_OPENGL);
64 addModes (modes, 32);
65 modes = SDL_ListModes (&fmt16, SDL_FULLSCREEN | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_OPENGL);
66 addModes (modes, 16);
68 if (!mNumVidModes)
69 sys_error ("no suitable video modes found!\n");
70 size_t i;
71 int mode_800x600 = -1;
72 int mode_640x480 = -1;
73 for (i = 0; i < mNumVidModes; i++)
75 fprintf (stderr, "found vidmode %dx%dx%d\n", mVidModes[i].w, mVidModes[i].h, mVidModes[i].bpp);
76 if (mVidModes[i].w == 800 && mVidModes[i].h == 600)
77 mode_800x600 = i;
78 if (mVidModes[i].w == 640 && mVidModes[i].h == 480)
79 mode_640x480 = i;
82 mCurrentMode = 0;
83 if (mode_800x600 != -1)
84 mCurrentMode = mode_800x600;
85 else if (mode_640x480 != -1)
86 mCurrentMode = mode_640x480;
89 void sdlRendererData::reset (void)
91 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
92 // SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
93 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
95 int flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_OPENGL;
96 if (!mbWindowed)
97 flags |= SDL_FULLSCREEN;
98 SDL_SetVideoMode (mVidModes[mCurrentMode].w, mVidModes[mCurrentMode].h, mVidModes[mCurrentMode].bpp, flags);
100 // fullscreen viewport
101 glViewport (0, 0, mVidModes[mCurrentMode].w, mVidModes[mCurrentMode].h);
104 void
105 sdlRendererData::addModes (SDL_Rect **modes, int bpp)
107 if (!modes)
108 return;
110 if (modes == (void*) (-1))
111 sys_error ("SDL_ListModes returned -1. evil.");
113 for (; *modes; modes++)
115 SDL_Rect *mode = *modes;
117 size_t i;
118 for (i = 0; i < mNumVidModes; i++)
120 if (mVidModes[i].w == mode->w && mVidModes[i].h == mode->h && mVidModes[i].bpp == bpp)
121 break;
124 if (i == mNumVidModes)
126 if (mNumVidModes == SDL_MAX_VIDMODES)
127 sys_error ("too many vidmodes for sdl driver (limit of %d modes exceeded)", SDL_MAX_VIDMODES);
128 vidMode_t *vmode = &mVidModes[mNumVidModes++];
129 vmode->w = mode->w;
130 vmode->h = mode->h;
131 vmode->bpp = bpp;
136 sdlRendererData::~sdlRendererData (void)
140 size_t sdlRendererData::numVidModes (void) const
142 return mNumVidModes;
145 size_t sdlRendererData::getCurrentVidMode (void) const
147 return mCurrentMode;
150 void sdlRendererData::setCurrentVidMode (size_t mode)
152 mCurrentMode = mode;
155 const sdlRendererData::vidMode_t& sdlRendererData::getVidModeDescr (int mode) const
157 return mVidModes[mode];
160 void sdlRendererData::setWindowed (bool windowed)
162 mbWindowed = windowed;
165 sdlRenderer::sdlRenderer (void)
167 mbResetting = false;
168 /* Setup our viewport. */
169 size_t curmode = mRendererData.getCurrentVidMode ();
170 const sdlRendererData::vidMode_t &descr = mRendererData.getVidModeDescr ( (int)curmode);
172 fprintf (stderr, "SDL: setting render-related cvars...\n");
174 // set cvars
175 cvarManager *cvars = g_engine->getCVarManager ();
176 cvars->setInt ("r_fullscreen", 0, true);
177 cvars->setInt ("r_vidmode", curmode, true);
179 glRenderer::init ();
181 fprintf (stderr, "sdl_opengl renderer initialization done.\n");
184 sdlRenderer::~sdlRenderer (void)
188 void sdlRenderer::present (void)
190 SDL_GL_SwapBuffers ();
193 bool sdlRenderer::isLost (void) const
195 // for now - assume opengl buffers are restored automatically (though remember it is not really true)
196 return false;
199 void sdlRenderer::resize (void)
201 int mode = r_vidmode->ivalue;
202 int fullscreen = r_fullscreen->ivalue;
204 // set modes to rendererdata
205 mRendererData.setCurrentVidMode (mode);
206 mRendererData.setWindowed (fullscreen ? false : true);
207 mRendererData.reset ();
209 glRenderer::resize ();
212 ulong sdlRenderer::numDisplayModes (ulong adapter, ulong device) const
214 return (ulong)mRendererData.numVidModes ();
217 cStr sdlRenderer::getDisplayModeDescr (ulong adapter, ulong device, ulong mode) const
219 const sdlRendererData::vidMode_t &vmode = mRendererData.getVidModeDescr (mode);
220 cStr s;
221 s.printf ("%d x %d x %d", vmode.w, vmode.h, vmode.bpp);
222 return s;
225 ulong sdlRenderer::numDevices (ulong adapter) const
227 return 1;
230 cStr sdlRenderer::getDeviceDescr (ulong adapter, ulong device) const
232 return "SDL-opengl";
235 ulong sdlRenderer::numAdapters (void) const
237 return 1;
240 cStr sdlRenderer::getAdapterDescr (ulong adapter) const
242 return "default adapter";
245 ulong sdlRenderer::getCurrentAdapter (void) const
247 return 0;
250 ulong sdlRenderer::getCurrentDevice (ulong adapter) const
252 return 0;
255 ulong sdlRenderer::getCurrentMode (ulong adapter, ulong device) const
257 return 0;
260 void sdlRenderer::setWindow (baseViewport *vp)
262 // setting external window is not supported for opengl
263 // FIXME: should this method be completely abandoned?
267 sdlRendererData* sdlRenderer::getData (void)
269 return &mRendererData;