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 *******************************************************************************/
29 #include "Dispatch.hh"
34 #include "Settings.hh"
46 Video::Video(const Attributes
& attribs
)
51 Video::Video(const std::string
& caption
, const std::string
& icon
)
53 if (mAttribs
.caption
== "Untitled")
55 mAttribs
.caption
= caption
;
57 if (mAttribs
.icon
== "")
65 void Video::init(const Attributes
& attribs
)
71 setFull(attribs
.fullscreen
);
72 setResizable(attribs
.resizable
);
73 setOpenGLAttributes();
74 setCaption(attribs
.caption
);
76 setCursorVisible(attribs
.cursorVisible
);
77 setCursorGrab(attribs
.cursorGrab
);
78 setVideoMode(attribs
.mode
);
81 void Video::recreateContext()
83 SDL_FreeSurface(mContext
);
85 setVideoMode(mAttribs
.mode
);
88 void Video::setOpenGLAttributes()
90 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, mAttribs
.colorBuffer
[0]);
91 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, mAttribs
.colorBuffer
[1]);
92 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, mAttribs
.colorBuffer
[2]);
93 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE
, mAttribs
.colorBuffer
[3]);
94 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE
, mAttribs
.frameBuffer
);
95 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, mAttribs
.doubleBuffer
);
96 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, mAttribs
.depthBuffer
);
97 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, mAttribs
.stencilBuffer
);
98 SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE
, mAttribs
.accumBuffer
[0]);
99 SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE
, mAttribs
.accumBuffer
[1]);
100 SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE
, mAttribs
.accumBuffer
[2]);
101 SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE
, mAttribs
.accumBuffer
[3]);
102 SDL_GL_SetAttribute(SDL_GL_STEREO
, mAttribs
.stereo
);
103 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS
, mAttribs
.multisampleBuffers
);
104 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES
, mAttribs
.multisampleSamples
);
105 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL
, mAttribs
.swapControl
);
106 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL
, mAttribs
.hardwareonly
);
112 SDL_FreeSurface(mContext
);
116 void Video::setVideoMode(const long mode
[3])
118 if (mode
!= mAttribs
.mode
|| !mContext
)
120 if (mContext
) SDL_FreeSurface(mContext
);
122 mContext
= SDL_SetVideoMode(mode
[0], mode
[1], mode
[2],
123 SDL_OPENGL
| mFlags
);
127 mAttribs
.mode
[0] = mode
[0];
128 mAttribs
.mode
[1] = mode
[1];
129 mAttribs
.mode
[2] = mode
[2];
131 #if defined(_WIN32) || defined(__WIN32__)
132 // on win32, creating a new context via SDL_SetVideoMode will wipe
133 // out the GL state, so we gotta notify everyone to reload their
134 // state after the change
135 Engine::getInstance().dispatch("video.newcontext");
137 logInfo("video context recreated");
140 else throw Error(Error::SDL_VIDEOMODE
);
144 Video::Attributes
Video::getAttributes() const
150 void Video::resize(int width
, int height
)
152 long mode
[] = {width
, height
, mAttribs
.mode
[2]};
156 bool Video::iconify()
158 return SDL_WM_IconifyWindow();
162 void Video::setCaption(const std::string
& caption
)
164 mAttribs
.caption
= caption
;
165 SDL_WM_SetCaption(caption
.c_str(), 0);
168 void Video::setIcon()
170 if (mAttribs
.icon
!= "")
172 Image
icon(mAttribs
.icon
);
177 std::string
Video::getCaption() const
179 return mAttribs
.caption
;
182 const std::string
& Video::getIcon() const
184 return mAttribs
.icon
;
188 void Video::setFull(bool full
)
190 if (full
!= isFull() || !mContext
)
194 mFlags
^= SDL_FULLSCREEN
;
196 #if defined(linux) || defined(__linux) || defined(__linux__)
197 if (SDL_WM_ToggleFullScreen(mContext
) == 0)
203 if (full
) mFlags
|= SDL_FULLSCREEN
;
204 else mFlags
&= ~SDL_FULLSCREEN
;
209 void Video::toggleFull()
214 bool Video::isFull() const
216 return mFlags
& SDL_FULLSCREEN
;
220 void Video::setCursorVisible(bool hasCursor
)
222 SDL_ShowCursor(hasCursor
? SDL_ENABLE
: SDL_DISABLE
);
225 void Video::toggleCursorVisible()
227 setCursorVisible(!isCursorVisible());
230 bool Video::isCursorVisible() const
232 return (SDL_ShowCursor(SDL_QUERY
) == SDL_ENABLE
);
236 void Video::setResizable(bool resizable
)
238 if (resizable
!= isResizable() || !mContext
)
242 mFlags
^= SDL_RESIZABLE
;
247 if (resizable
) mFlags
|= SDL_RESIZABLE
;
248 else mFlags
&= ~SDL_RESIZABLE
;
253 void Video::toggleResizable()
255 setResizable(!isResizable());
258 bool Video::isResizable() const
260 return mFlags
& SDL_RESIZABLE
;
264 bool Video::isCursorGrab() const
266 return (SDL_WM_GrabInput(SDL_GRAB_QUERY
) == SDL_GRAB_ON
);
269 void Video::toggleCursorGrab()
271 setCursorGrab(!isCursorGrab());
274 void Video::setCursorGrab(bool cursorGrab
)
276 SDL_WM_GrabInput(cursorGrab
? SDL_GRAB_ON
: SDL_GRAB_OFF
);
282 SDL_GL_SwapBuffers();
286 int Video::getWidth() const
291 int Video::getHeight() const
297 Video::Attributes::Attributes()
299 // Set some sane GL and window defaults (see SDL_video.c:217)
313 multisampleBuffers
= 0;
314 multisampleSamples
= 0;
316 hardwareonly
= false;
322 cursorVisible
= true;
325 Settings
& settings
= Settings::getInstance();
327 std::vector
<long> colors
;
328 settings
.get("colorbuffers", colors
);
329 if (colors
.size() > 0) colorBuffer
[0] = colors
[0];
330 if (colors
.size() > 1) colorBuffer
[1] = colors
[1];
331 if (colors
.size() > 2) colorBuffer
[2] = colors
[2];
332 if (colors
.size() > 3) colorBuffer
[3] = colors
[3];
334 settings
.get("framebuffer", frameBuffer
);
335 settings
.get("doublebuffer", doubleBuffer
);
336 settings
.get("depthbuffer", depthBuffer
);
337 settings
.get("stencilbuffer", stencilBuffer
);
339 std::vector
<long> accum
;
340 settings
.get("accumbuffers", accum
);
341 if (accum
.size() > 0) accumBuffer
[0] = accum
[0];
342 if (accum
.size() > 1) accumBuffer
[1] = accum
[1];
343 if (accum
.size() > 2) accumBuffer
[2] = accum
[2];
344 if (accum
.size() > 3) accumBuffer
[3] = accum
[3];
346 settings
.get("stereo", stereo
);
347 settings
.get("multiesamplebuffers", multisampleBuffers
);
348 settings
.get("multiesamplesamples", multisampleSamples
);
349 settings
.get("swapcontrol", swapControl
);
350 settings
.get("hardwareonly", hardwareonly
);
352 if (!settings
.get("caption", caption
))
354 caption
= "Untitled";
356 settings
.get("icon", icon
);
358 std::vector
<long> dimensions
;
359 settings
.get("videomode", dimensions
);
360 if (dimensions
.size() > 1)
362 mode
[0] = dimensions
[0];
363 mode
[1] = dimensions
[1];
365 if (dimensions
.size() > 2) mode
[2] = dimensions
[2];
367 settings
.get("fullscreen", fullscreen
);
368 settings
.get("resizable", resizable
);
369 settings
.get("showcursor", cursorVisible
);
370 settings
.get("grab", cursorGrab
);
376 /** vim: set ts=4 sw=4 tw=80: *************************************************/