]> Dogcows Code - chaz/yoink/blob - src/Moof/Video.cc
extreme refactoring
[chaz/yoink] / src / Moof / Video.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
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.
15
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.
26
27 *******************************************************************************/
28
29 #include <stdexcept>
30
31 #include "Dispatcher.hh"
32 #include "Serializable.hh"
33 #include "Settings.hh"
34 #include "Video.hh"
35
36
37 namespace Mf {
38
39
40 Video::Video()
41 {
42 std::string caption;
43 if (Settings::instance().get("video.caption", caption))
44 {
45 init(attribs_, caption);
46 }
47 else
48 {
49 init(attribs_, "Untitled");
50 }
51 }
52
53 Video::Video(const Attributes& attribs, const std::string& caption)
54 {
55 init(attribs, caption);
56 }
57
58 Video::Video(const Attributes& attribs)
59 {
60 std::string caption;
61 if (Settings::instance().get("video.caption", caption))
62 {
63 init(attribs, caption);
64 }
65 else
66 {
67 init(attribs, "Untitled");
68 }
69 }
70
71 Video::Video(const std::string& caption)
72 {
73 init(attribs_, caption);
74 }
75
76 void Video::init(const Attributes& attribs, const std::string& caption)
77 {
78 context_ = 0;
79 flags_ = 0;
80 attribs_ = attribs;
81
82 setFull(attribs.fullscreen);
83 setResizable(attribs.resizable);
84 setOpenGLAttributes();
85 setCaption(caption);
86 setCursorVisible(attribs.cursorVisible);
87 setCursorGrab(attribs.cursorGrab);
88 setVideoMode(attribs.mode);
89 }
90
91 void Video::recreateContext()
92 {
93 SDL_FreeSurface(context_);
94 context_ = 0;
95 setVideoMode(attribs_.mode);
96 Mf::Dispatcher::instance().dispatch("video.context_recreated");
97 }
98
99 void Video::setOpenGLAttributes()
100 {
101 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, attribs_.colorBuffer[0]);
102 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, attribs_.colorBuffer[1]);
103 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, attribs_.colorBuffer[2]);
104 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, attribs_.colorBuffer[3]);
105 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, attribs_.frameBuffer);
106 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, attribs_.doubleBuffer);
107 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, attribs_.depthBuffer);
108 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, attribs_.stencilBuffer);
109 SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, attribs_.accumBuffer[0]);
110 SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, attribs_.accumBuffer[1]);
111 SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, attribs_.accumBuffer[2]);
112 SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, attribs_.accumBuffer[3]);
113 SDL_GL_SetAttribute(SDL_GL_STEREO, attribs_.stereo);
114 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, attribs_.multisampleBuffers);
115 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, attribs_.multisampleSamples);
116 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, attribs_.swapControl);
117 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, attribs_.hardwareonly);
118 }
119
120
121 Video::~Video()
122 {
123 SDL_FreeSurface(context_);
124 }
125
126
127 void Video::setVideoMode(const long mode[3])
128 {
129 if (mode != attribs_.mode || !context_)
130 {
131 if (context_) SDL_FreeSurface(context_);
132
133 context_ = SDL_SetVideoMode(mode[0], mode[1], mode[2],
134 SDL_OPENGL | flags_);
135
136 if (context_)
137 {
138 attribs_.mode[0] = mode[0];
139 attribs_.mode[1] = mode[1];
140 attribs_.mode[2] = mode[2];
141 }
142 else throw Exception(SDL_GetError());
143 }
144 }
145
146 Video::Attributes Video::getAttributes() const
147 {
148 return attribs_;
149 }
150
151
152 void Video::resize(int width, int height)
153 {
154 long mode[] = {width, height, attribs_.mode[2]};
155 setVideoMode(mode);
156 }
157
158 bool Video::iconify()
159 {
160 return SDL_WM_IconifyWindow();
161 }
162
163
164 void Video::setCaption(const std::string& caption)
165 {
166 SDL_WM_SetCaption(caption.c_str(), 0);
167 }
168
169 std::string Video::getCaption() const
170 {
171 char* str;
172 SDL_WM_GetCaption(&str, 0);
173 return std::string(str);
174 }
175
176
177 void Video::setFull(bool full)
178 {
179 if (full != isFull() || !context_)
180 {
181 if (context_)
182 {
183 flags_ ^= SDL_FULLSCREEN;
184
185 #if defined(linux) || defined(__linux) || defined(__linux__)
186 if (SDL_WM_ToggleFullScreen(context_) == 0)
187 #endif
188 recreateContext();
189 }
190 else
191 {
192 if (full) flags_ |= SDL_FULLSCREEN;
193 else flags_ &= ~SDL_FULLSCREEN;
194 }
195 }
196 }
197
198 void Video::toggleFull()
199 {
200 setFull(!isFull());
201 }
202
203 bool Video::isFull() const
204 {
205 return flags_ & SDL_FULLSCREEN;
206 }
207
208
209 void Video::setCursorVisible(bool hasCursor)
210 {
211 SDL_ShowCursor(hasCursor? SDL_ENABLE : SDL_DISABLE);
212 }
213
214 void Video::toggleCursorVisible()
215 {
216 setCursorVisible(!isCursorVisible());
217 }
218
219 bool Video::isCursorVisible() const
220 {
221 return (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE);
222 }
223
224
225 void Video::setResizable(bool resizable)
226 {
227 if (resizable != isResizable() || !context_)
228 {
229 if (context_)
230 {
231 flags_ ^= SDL_RESIZABLE;
232 recreateContext();
233 }
234 else
235 {
236 if (resizable) flags_ |= SDL_RESIZABLE;
237 else flags_ &= ~SDL_RESIZABLE;
238 }
239 }
240 }
241
242 void Video::toggleResizable()
243 {
244 setResizable(!isResizable());
245 }
246
247 bool Video::isResizable() const
248 {
249 return flags_ & SDL_RESIZABLE;
250 }
251
252
253 bool Video::isCursorGrab() const
254 {
255 return (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON);
256 }
257
258 void Video::toggleCursorGrab()
259 {
260 setCursorGrab(!isCursorGrab());
261 }
262
263 void Video::setCursorGrab(bool cursorGrab)
264 {
265 SDL_WM_GrabInput(cursorGrab? SDL_GRAB_ON : SDL_GRAB_OFF);
266 }
267
268
269 void Video::makeActive()
270 {
271 // NOP until the day SDL supports more than only one window.
272 // Still waiting...
273 }
274
275 void Video::swap()
276 {
277 SDL_GL_SwapBuffers();
278 }
279
280
281 Video::Attributes::Attributes()
282 {
283 // Set some sane GL and window defaults (see SDL_video.c:217)
284 colorBuffer[0] = 3;
285 colorBuffer[1] = 3;
286 colorBuffer[2] = 2;
287 colorBuffer[3] = 0;
288 frameBuffer = 0;
289 doubleBuffer = true;
290 depthBuffer = 16;
291 stencilBuffer = 0;
292 accumBuffer[0] = 0;
293 accumBuffer[1] = 0;
294 accumBuffer[2] = 0;
295 accumBuffer[3] = 0;
296 stereo = false;
297 multisampleBuffers = 0;
298 multisampleSamples = 0;
299 swapControl = false;
300 hardwareonly = false;
301 mode[0] = 640;
302 mode[1] = 480;
303 mode[2] = 0;
304 fullscreen = false;
305 resizable = false;
306 cursorVisible = true;
307 cursorGrab = false;
308
309 std::vector<SerializablePtr> colors;
310 Settings::instance().get("video.colorbuffers", colors);
311 if (colors.size() > 0) colors[0]->get(colorBuffer[0]);
312 if (colors.size() > 1) colors[1]->get(colorBuffer[1]);
313 if (colors.size() > 2) colors[2]->get(colorBuffer[2]);
314 if (colors.size() > 3) colors[3]->get(colorBuffer[3]);
315
316 Settings::instance().get("video.framebuffer", frameBuffer);
317 Settings::instance().get("video.doublebuffer", doubleBuffer);
318 Settings::instance().get("video.depthbuffer", depthBuffer);
319 Settings::instance().get("video.stencilbuffer", stencilBuffer);
320
321 std::vector<SerializablePtr> accum;
322 Settings::instance().get("video.accumbuffers", accum);
323 if (accum.size() > 0) accum[0]->get(accumBuffer[0]);
324 if (accum.size() > 1) accum[1]->get(accumBuffer[1]);
325 if (accum.size() > 2) accum[2]->get(accumBuffer[2]);
326 if (accum.size() > 3) accum[3]->get(accumBuffer[3]);
327
328 Settings::instance().get("video.stereo", stereo);
329 Settings::instance().get("video.multiesamplebuffers", multisampleBuffers);
330 Settings::instance().get("video.multiesamplesamples", multisampleSamples);
331 Settings::instance().get("video.swapcontrol", swapControl);
332 Settings::instance().get("video.hardwareonly", hardwareonly);
333
334 std::vector<SerializablePtr> dimensions;
335 Settings::instance().get("video.mode", dimensions);
336 if (dimensions.size() > 0) dimensions[0]->get(mode[0]);
337 if (dimensions.size() > 1) dimensions[1]->get(mode[1]);
338 if (dimensions.size() > 2) dimensions[2]->get(mode[2]);
339
340 Settings::instance().get("video.fullscreen", fullscreen);
341 Settings::instance().get("video.resizable", resizable);
342 Settings::instance().get("video.showcursor", cursorVisible);
343 Settings::instance().get("input.grab", cursorGrab);
344 }
345
346
347 } // namespace Mf
348
349 /** vim: set ts=4 sw=4 tw=80: *************************************************/
350
This page took 0.050968 seconds and 5 git commands to generate.