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
20 waker@users.sourceforge.net
28 #include "f_sdlrenderer.h"
29 #include "f_baseviewport.h"
31 #include "f_sdlviewport.h"
32 #include "f_sdlviewportdata.h"
34 #include "f_application.h"
35 #include "f_console.h"
37 #include "f_fragileobject.h"
38 #include "f_resourcemgr.h"
40 #include "f_resourcemgr.h"
47 //-----------------------------------
49 //-----------------------------------
51 sdlRendererData::sdlRendererData (void)
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
);
65 modes
= SDL_ListModes (&fmt16
, SDL_FULLSCREEN
| SDL_HWSURFACE
| SDL_DOUBLEBUF
| SDL_OPENGL
);
69 sys_error ("no suitable video modes found!\n");
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)
78 if (mVidModes
[i
].w
== 640 && mVidModes
[i
].h
== 480)
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
;
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
);
105 sdlRendererData::addModes (SDL_Rect
**modes
, int bpp
)
110 if (modes
== (void*) (-1))
111 sys_error ("SDL_ListModes returned -1. evil.");
113 for (; *modes
; modes
++)
115 SDL_Rect
*mode
= *modes
;
118 for (i
= 0; i
< mNumVidModes
; i
++)
120 if (mVidModes
[i
].w
== mode
->w
&& mVidModes
[i
].h
== mode
->h
&& mVidModes
[i
].bpp
== bpp
)
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
++];
136 sdlRendererData::~sdlRendererData (void)
140 size_t sdlRendererData::numVidModes (void) const
145 size_t sdlRendererData::getCurrentVidMode (void) const
150 void sdlRendererData::setCurrentVidMode (size_t 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)
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");
175 cvarManager
*cvars
= g_engine
->getCVarManager ();
176 cvars
->setInt ("r_fullscreen", 0, true);
177 cvars
->setInt ("r_vidmode", curmode
, true);
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)
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
);
221 s
.printf ("%d x %d x %d", vmode
.w
, vmode
.h
, vmode
.bpp
);
225 ulong
sdlRenderer::numDevices (ulong adapter
) const
230 cStr
sdlRenderer::getDeviceDescr (ulong adapter
, ulong device
) const
235 ulong
sdlRenderer::numAdapters (void) const
240 cStr
sdlRenderer::getAdapterDescr (ulong adapter
) const
242 return "default adapter";
245 ulong
sdlRenderer::getCurrentAdapter (void) const
250 ulong
sdlRenderer::getCurrentDevice (ulong adapter
) const
255 ulong
sdlRenderer::getCurrentMode (ulong adapter
, ulong device
) const
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
;