2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
31 #include "serializable.hh"
32 #include "settings.hh"
33 #include "dispatcher.hh"
44 if (settings::instance().get("video.caption", caption
))
46 init(attribs_
, caption
);
50 init(attribs_
, "Untitled");
54 video::video(const attributes
& attribs
, const std::string
& caption
)
56 init(attribs
, caption
);
59 video::video(const attributes
& attribs
)
62 if (settings::instance().get("video.caption", caption
))
64 init(attribs
, caption
);
68 init(attribs
, "Untitled");
72 video::video(const std::string
& caption
)
74 init(attribs_
, caption
);
77 void video::init(const attributes
& attribs
, const std::string
& caption
)
83 setFull(attribs
.fullscreen
);
84 setResizable(attribs
.resizable
);
85 setOpenGLAttributes();
87 setCursorVisible(attribs
.cursorVisible
);
88 setCursorGrab(attribs
.cursorGrab
);
89 setVideoMode(attribs
.mode
);
92 void video::recreateContext()
94 SDL_FreeSurface(context_
);
96 setVideoMode(attribs_
.mode
);
97 dc::dispatcher::instance().dispatch("video.context_recreated");
100 void video::setOpenGLAttributes()
102 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, attribs_
.colorBuffer
[0]);
103 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, attribs_
.colorBuffer
[1]);
104 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, attribs_
.colorBuffer
[2]);
105 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE
, attribs_
.colorBuffer
[3]);
106 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE
, attribs_
.frameBuffer
);
107 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, attribs_
.doubleBuffer
);
108 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, attribs_
.depthBuffer
);
109 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, attribs_
.stencilBuffer
);
110 SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE
, attribs_
.accumBuffer
[0]);
111 SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE
, attribs_
.accumBuffer
[1]);
112 SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE
, attribs_
.accumBuffer
[2]);
113 SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE
, attribs_
.accumBuffer
[3]);
114 SDL_GL_SetAttribute(SDL_GL_STEREO
, attribs_
.stereo
);
115 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS
, attribs_
.multisampleBuffers
);
116 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES
, attribs_
.multisampleSamples
);
117 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL
, attribs_
.swapControl
);
118 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL
, attribs_
.hardwareonly
);
124 SDL_FreeSurface(context_
);
128 void video::setVideoMode(const long mode
[3])
130 if (mode
!= attribs_
.mode
|| !context_
)
132 if (context_
) SDL_FreeSurface(context_
);
134 context_
= SDL_SetVideoMode(mode
[0], mode
[1], mode
[2],
135 SDL_OPENGL
| flags_
);
139 attribs_
.mode
[0] = mode
[0];
140 attribs_
.mode
[1] = mode
[1];
141 attribs_
.mode
[2] = mode
[2];
143 else throw std::runtime_error(SDL_GetError());
147 video::attributes
video::getAttributes() const
153 void video::resize(int width
, int height
)
155 long mode
[] = {width
, height
, attribs_
.mode
[2]};
159 bool video::iconify()
161 return SDL_WM_IconifyWindow();
165 void video::setCaption(const std::string
& caption
)
167 SDL_WM_SetCaption(caption
.c_str(), 0);
170 std::string
video::getCaption() const
173 SDL_WM_GetCaption(&str
, 0);
174 return std::string(str
);
178 void video::setFull(bool full
)
180 if (full
!= isFull() || !context_
)
184 flags_
^= SDL_FULLSCREEN
;
186 #if defined(linux) || defined(__linux) || defined(__linux__)
187 if (SDL_WM_ToggleFullScreen(context_
) == 0)
193 if (full
) flags_
|= SDL_FULLSCREEN
;
194 else flags_
&= ~SDL_FULLSCREEN
;
199 void video::toggleFull()
204 bool video::isFull() const
206 return flags_
& SDL_FULLSCREEN
;
210 void video::setCursorVisible(bool hasCursor
)
212 SDL_ShowCursor(hasCursor
? SDL_ENABLE
: SDL_DISABLE
);
215 void video::toggleCursorVisible()
217 setCursorVisible(!isCursorVisible());
220 bool video::isCursorVisible() const
222 return (SDL_ShowCursor(SDL_QUERY
) == SDL_ENABLE
);
226 void video::setResizable(bool resizable
)
228 if (resizable
!= isResizable() || !context_
)
232 flags_
^= SDL_RESIZABLE
;
237 if (resizable
) flags_
|= SDL_RESIZABLE
;
238 else flags_
&= ~SDL_RESIZABLE
;
243 void video::toggleResizable()
245 setResizable(!isResizable());
248 bool video::isResizable() const
250 return flags_
& SDL_RESIZABLE
;
254 bool video::isCursorGrab() const
256 return (SDL_WM_GrabInput(SDL_GRAB_QUERY
) == SDL_GRAB_ON
);
259 void video::toggleCursorGrab()
261 setCursorGrab(!isCursorGrab());
264 void video::setCursorGrab(bool cursorGrab
)
266 SDL_WM_GrabInput(cursorGrab
? SDL_GRAB_ON
: SDL_GRAB_OFF
);
270 void video::makeActive()
272 // NOP until the day SDL supports more than only one window.
278 SDL_GL_SwapBuffers();
282 video::attributes::attributes()
284 // Set some sane GL and window defaults (see SDL_video.c:217)
298 multisampleBuffers
= 0;
299 multisampleSamples
= 0;
301 hardwareonly
= false;
307 cursorVisible
= true;
310 std::vector
<serializable_ptr
> colors
;
311 settings::instance().get("video.colorbuffers", colors
);
312 if (colors
.size() > 0) colors
[0]->get(colorBuffer
[0]);
313 if (colors
.size() > 1) colors
[1]->get(colorBuffer
[1]);
314 if (colors
.size() > 2) colors
[2]->get(colorBuffer
[2]);
315 if (colors
.size() > 3) colors
[3]->get(colorBuffer
[3]);
317 settings::instance().get("video.framebuffer", frameBuffer
);
318 settings::instance().get("video.doublebuffer", doubleBuffer
);
319 settings::instance().get("video.depthbuffer", depthBuffer
);
320 settings::instance().get("video.stencilbuffer", stencilBuffer
);
322 std::vector
<serializable_ptr
> accum
;
323 settings::instance().get("video.accumbuffers", accum
);
324 if (accum
.size() > 0) accum
[0]->get(accumBuffer
[0]);
325 if (accum
.size() > 1) accum
[1]->get(accumBuffer
[1]);
326 if (accum
.size() > 2) accum
[2]->get(accumBuffer
[2]);
327 if (accum
.size() > 3) accum
[3]->get(accumBuffer
[3]);
329 settings::instance().get("video.stereo", stereo
);
330 settings::instance().get("video.multiesamplebuffers", multisampleBuffers
);
331 settings::instance().get("video.multiesamplesamples", multisampleSamples
);
332 settings::instance().get("video.swapcontrol", swapControl
);
333 settings::instance().get("video.hardwareonly", hardwareonly
);
335 std::vector
<serializable_ptr
> dimensions
;
336 settings::instance().get("video.mode", dimensions
);
337 if (dimensions
.size() > 0) dimensions
[0]->get(mode
[0]);
338 if (dimensions
.size() > 1) dimensions
[1]->get(mode
[1]);
339 if (dimensions
.size() > 2) dimensions
[2]->get(mode
[2]);
341 settings::instance().get("video.fullscreen", fullscreen
);
342 settings::instance().get("video.resizable", resizable
);
343 settings::instance().get("video.cursor", cursorVisible
);
344 settings::instance().get("video.grab", cursorGrab
);
350 /** vim: set ts=4 sw=4 tw=80: *************************************************/