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"
33 #include "Settings.hh"
45 Video::Video(const Attributes
& attribs
)
50 Video::Video(const std::string
& caption
, const std::string
& icon
)
52 if (mAttribs
.caption
== "Untitled")
54 mAttribs
.caption
= caption
;
56 if (mAttribs
.icon
== "")
64 void Video::init(const Attributes
& attribs
)
70 setFull(attribs
.fullscreen
);
71 setResizable(attribs
.resizable
);
72 setOpenGLAttributes();
73 setCaption(attribs
.caption
);
75 setCursorVisible(attribs
.cursorVisible
);
76 setCursorGrab(attribs
.cursorGrab
);
77 setVideoMode(attribs
.mode
);
82 void Video::recreateContext()
84 SDL_FreeSurface(mContext
);
86 setVideoMode(mAttribs
.mode
);
89 void Video::setOpenGLAttributes()
91 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, mAttribs
.colorBuffer
[0]);
92 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, mAttribs
.colorBuffer
[1]);
93 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, mAttribs
.colorBuffer
[2]);
94 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE
, mAttribs
.colorBuffer
[3]);
95 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE
, mAttribs
.frameBuffer
);
96 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, mAttribs
.doubleBuffer
);
97 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, mAttribs
.depthBuffer
);
98 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, mAttribs
.stencilBuffer
);
99 SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE
, mAttribs
.accumBuffer
[0]);
100 SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE
, mAttribs
.accumBuffer
[1]);
101 SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE
, mAttribs
.accumBuffer
[2]);
102 SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE
, mAttribs
.accumBuffer
[3]);
103 SDL_GL_SetAttribute(SDL_GL_STEREO
, mAttribs
.stereo
);
104 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS
, mAttribs
.multisampleBuffers
);
105 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES
, mAttribs
.multisampleSamples
);
106 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL
, mAttribs
.swapControl
);
107 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL
, mAttribs
.hardwareonly
);
113 SDL_FreeSurface(mContext
);
115 if (video
== this) video
= 0;
119 void Video::setVideoMode(const long mode
[3])
121 if (mode
!= mAttribs
.mode
|| !mContext
)
123 if (mContext
) SDL_FreeSurface(mContext
);
125 mContext
= SDL_SetVideoMode(mode
[0], mode
[1], mode
[2],
126 SDL_OPENGL
| mFlags
);
130 mAttribs
.mode
[0] = mode
[0];
131 mAttribs
.mode
[1] = mode
[1];
132 mAttribs
.mode
[2] = mode
[2];
134 #if !defined(linux) && !defined(__linux) && !defined(__linux__)
135 logInfo("video context recreated");
136 core
.dispatch("video.newcontext");
139 else Error(Error::SDL_VIDEOMODE
).raise();
143 Video::Attributes
Video::getAttributes() const
149 void Video::resize(int width
, int height
)
151 long mode
[] = {width
, height
, mAttribs
.mode
[2]};
155 bool Video::iconify()
157 return SDL_WM_IconifyWindow();
161 void Video::setCaption(const std::string
& caption
)
163 mAttribs
.caption
= caption
;
164 SDL_WM_SetCaption(caption
.c_str(), 0);
167 void Video::setIcon()
169 if (mAttribs
.icon
!= "")
171 Image
icon(mAttribs
.icon
);
176 std::string
Video::getCaption() const
178 return mAttribs
.caption
;
181 const std::string
& Video::getIcon() const
183 return mAttribs
.icon
;
187 void Video::setFull(bool full
)
189 if (full
!= isFull() || !mContext
)
193 mFlags
^= SDL_FULLSCREEN
;
195 #if defined(linux) || defined(__linux) || defined(__linux__)
196 if (SDL_WM_ToggleFullScreen(mContext
) == 0)
202 if (full
) mFlags
|= SDL_FULLSCREEN
;
203 else mFlags
&= ~SDL_FULLSCREEN
;
208 void Video::toggleFull()
213 bool Video::isFull() const
215 return mFlags
& SDL_FULLSCREEN
;
219 void Video::setCursorVisible(bool hasCursor
)
221 SDL_ShowCursor(hasCursor
? SDL_ENABLE
: SDL_DISABLE
);
224 void Video::toggleCursorVisible()
226 setCursorVisible(!isCursorVisible());
229 bool Video::isCursorVisible() const
231 return (SDL_ShowCursor(SDL_QUERY
) == SDL_ENABLE
);
235 void Video::setResizable(bool resizable
)
237 if (resizable
!= isResizable() || !mContext
)
241 mFlags
^= SDL_RESIZABLE
;
246 if (resizable
) mFlags
|= SDL_RESIZABLE
;
247 else mFlags
&= ~SDL_RESIZABLE
;
252 void Video::toggleResizable()
254 setResizable(!isResizable());
257 bool Video::isResizable() const
259 return mFlags
& SDL_RESIZABLE
;
263 bool Video::isCursorGrab() const
265 return (SDL_WM_GrabInput(SDL_GRAB_QUERY
) == SDL_GRAB_ON
);
268 void Video::toggleCursorGrab()
270 setCursorGrab(!isCursorGrab());
273 void Video::setCursorGrab(bool cursorGrab
)
275 SDL_WM_GrabInput(cursorGrab
? SDL_GRAB_ON
: SDL_GRAB_OFF
);
281 SDL_GL_SwapBuffers();
285 int Video::getWidth() const
290 int Video::getHeight() const
296 Video::Attributes::Attributes()
298 // set some sane GL and window defaults (see SDL_video.c:217)
312 multisampleBuffers
= 0;
313 multisampleSamples
= 0;
315 hardwareonly
= false;
321 cursorVisible
= true;
324 std::vector
<long> colors
;
325 settings
.get("colorbuffers", colors
);
326 if (colors
.size() > 0) colorBuffer
[0] = colors
[0];
327 if (colors
.size() > 1) colorBuffer
[1] = colors
[1];
328 if (colors
.size() > 2) colorBuffer
[2] = colors
[2];
329 if (colors
.size() > 3) colorBuffer
[3] = colors
[3];
331 settings
.get("framebuffer", frameBuffer
);
332 settings
.get("doublebuffer", doubleBuffer
);
333 settings
.get("depthbuffer", depthBuffer
);
334 settings
.get("stencilbuffer", stencilBuffer
);
336 std::vector
<long> accum
;
337 settings
.get("accumbuffers", accum
);
338 if (accum
.size() > 0) accumBuffer
[0] = accum
[0];
339 if (accum
.size() > 1) accumBuffer
[1] = accum
[1];
340 if (accum
.size() > 2) accumBuffer
[2] = accum
[2];
341 if (accum
.size() > 3) accumBuffer
[3] = accum
[3];
343 settings
.get("stereo", stereo
);
344 settings
.get("multiesamplebuffers", multisampleBuffers
);
345 settings
.get("multiesamplesamples", multisampleSamples
);
346 settings
.get("swapcontrol", swapControl
);
347 settings
.get("hardwareonly", hardwareonly
);
349 if (!settings
.get("caption", caption
))
351 caption
= "Untitled";
353 settings
.get("icon", icon
);
355 settings
.get("fullscreen", fullscreen
);
356 settings
.get("resizable", resizable
);
357 settings
.get("showcursor", cursorVisible
);
358 settings
.get("grab", cursorGrab
);
360 std::vector
<long> dimensions
;
361 settings
.get("videomode", dimensions
);
362 if (dimensions
.size() > 1)
364 mode
[0] = dimensions
[0];
365 mode
[1] = dimensions
[1];
369 SDL_Rect
** modes
= SDL_ListModes(NULL
, SDL_FULLSCREEN
| SDL_HWSURFACE
);
371 if (modes
== (SDL_Rect
**)0)
373 Mf::logError("no native video mode");
375 else if (modes
== (SDL_Rect
**)-1)
377 Mf::logWarning("any resolution allowed; choosing default 800x600");
383 mode
[0] = (*modes
)->w
;
384 mode
[1] = (*modes
)->h
;
385 Mf::logInfo
<< "choosing native resolution "
386 << mode
[0] << "x" << mode
[1] << std::endl
;
389 if (dimensions
.size() > 2) mode
[2] = dimensions
[2];
398 /** vim: set ts=4 sw=4 tw=80: *************************************************/