]> Dogcows Code - chaz/yoink/blob - src/video.cc
big batch of progress
[chaz/yoink] / src / 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 "serializable.hh"
32 #include "settings.hh"
33 #include "dispatcher.hh"
34
35 #include "video.hh"
36
37
38 namespace dc {
39
40
41 video::video()
42 {
43 std::string caption;
44 if (settings::instance().get("video.caption", caption))
45 {
46 init(attribs_, caption);
47 }
48 else
49 {
50 init(attribs_, "Untitled");
51 }
52 }
53
54 video::video(const attributes& attribs, const std::string& caption)
55 {
56 init(attribs, caption);
57 }
58
59 video::video(const attributes& attribs)
60 {
61 std::string caption;
62 if (settings::instance().get("video.caption", caption))
63 {
64 init(attribs, caption);
65 }
66 else
67 {
68 init(attribs, "Untitled");
69 }
70 }
71
72 video::video(const std::string& caption)
73 {
74 init(attribs_, caption);
75 }
76
77 void video::init(const attributes& attribs, const std::string& caption)
78 {
79 context_ = 0;
80 flags_ = 0;
81 attribs_ = attribs;
82
83 setFull(attribs.fullscreen);
84 setResizable(attribs.resizable);
85 setOpenGLAttributes();
86 setCaption(caption);
87 setCursorVisible(attribs.cursorVisible);
88 setCursorGrab(attribs.cursorGrab);
89 setVideoMode(attribs.mode);
90 }
91
92 void video::recreateContext()
93 {
94 SDL_FreeSurface(context_);
95 context_ = 0;
96 setVideoMode(attribs_.mode);
97 dc::dispatcher::instance().dispatch("video.context_recreated");
98 }
99
100 void video::setOpenGLAttributes()
101 {
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);
119 }
120
121
122 video::~video()
123 {
124 SDL_FreeSurface(context_);
125 }
126
127
128 void video::setVideoMode(const long mode[3])
129 {
130 if (mode != attribs_.mode || !context_)
131 {
132 if (context_) SDL_FreeSurface(context_);
133
134 context_ = SDL_SetVideoMode(mode[0], mode[1], mode[2],
135 SDL_OPENGL | flags_);
136
137 if (context_)
138 {
139 attribs_.mode[0] = mode[0];
140 attribs_.mode[1] = mode[1];
141 attribs_.mode[2] = mode[2];
142 }
143 else throw std::runtime_error(SDL_GetError());
144 }
145 }
146
147 video::attributes video::getAttributes() const
148 {
149 return attribs_;
150 }
151
152
153 void video::resize(int width, int height)
154 {
155 long mode[] = {width, height, attribs_.mode[2]};
156 setVideoMode(mode);
157 }
158
159 bool video::iconify()
160 {
161 return SDL_WM_IconifyWindow();
162 }
163
164
165 void video::setCaption(const std::string& caption)
166 {
167 SDL_WM_SetCaption(caption.c_str(), 0);
168 }
169
170 std::string video::getCaption() const
171 {
172 char* str;
173 SDL_WM_GetCaption(&str, 0);
174 return std::string(str);
175 }
176
177
178 void video::setFull(bool full)
179 {
180 if (full != isFull() || !context_)
181 {
182 if (context_)
183 {
184 flags_ ^= SDL_FULLSCREEN;
185
186 #if defined(linux) || defined(__linux) || defined(__linux__)
187 if (SDL_WM_ToggleFullScreen(context_) == 0)
188 #endif
189 recreateContext();
190 }
191 else
192 {
193 if (full) flags_ |= SDL_FULLSCREEN;
194 else flags_ &= ~SDL_FULLSCREEN;
195 }
196 }
197 }
198
199 void video::toggleFull()
200 {
201 setFull(!isFull());
202 }
203
204 bool video::isFull() const
205 {
206 return flags_ & SDL_FULLSCREEN;
207 }
208
209
210 void video::setCursorVisible(bool hasCursor)
211 {
212 SDL_ShowCursor(hasCursor? SDL_ENABLE : SDL_DISABLE);
213 }
214
215 void video::toggleCursorVisible()
216 {
217 setCursorVisible(!isCursorVisible());
218 }
219
220 bool video::isCursorVisible() const
221 {
222 return (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE);
223 }
224
225
226 void video::setResizable(bool resizable)
227 {
228 if (resizable != isResizable() || !context_)
229 {
230 if (context_)
231 {
232 flags_ ^= SDL_RESIZABLE;
233 recreateContext();
234 }
235 else
236 {
237 if (resizable) flags_ |= SDL_RESIZABLE;
238 else flags_ &= ~SDL_RESIZABLE;
239 }
240 }
241 }
242
243 void video::toggleResizable()
244 {
245 setResizable(!isResizable());
246 }
247
248 bool video::isResizable() const
249 {
250 return flags_ & SDL_RESIZABLE;
251 }
252
253
254 bool video::isCursorGrab() const
255 {
256 return (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON);
257 }
258
259 void video::toggleCursorGrab()
260 {
261 setCursorGrab(!isCursorGrab());
262 }
263
264 void video::setCursorGrab(bool cursorGrab)
265 {
266 SDL_WM_GrabInput(cursorGrab? SDL_GRAB_ON : SDL_GRAB_OFF);
267 }
268
269
270 void video::makeActive()
271 {
272 // NOP until the day SDL supports more than only one window.
273 // Still waiting...
274 }
275
276 void video::swap()
277 {
278 SDL_GL_SwapBuffers();
279 }
280
281
282 video::attributes::attributes()
283 {
284 // Set some sane GL and window defaults (see SDL_video.c:217)
285 colorBuffer[0] = 3;
286 colorBuffer[1] = 3;
287 colorBuffer[2] = 2;
288 colorBuffer[3] = 0;
289 frameBuffer = 0;
290 doubleBuffer = true;
291 depthBuffer = 16;
292 stencilBuffer = 0;
293 accumBuffer[0] = 0;
294 accumBuffer[1] = 0;
295 accumBuffer[2] = 0;
296 accumBuffer[3] = 0;
297 stereo = false;
298 multisampleBuffers = 0;
299 multisampleSamples = 0;
300 swapControl = false;
301 hardwareonly = false;
302 mode[0] = 640;
303 mode[1] = 480;
304 mode[2] = 0;
305 fullscreen = false;
306 resizable = false;
307 cursorVisible = true;
308 cursorGrab = false;
309
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]);
316
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);
321
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]);
328
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);
334
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]);
340
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);
345 }
346
347
348 } // namespace dc
349
350 /** vim: set ts=4 sw=4 tw=80: *************************************************/
351
This page took 0.044637 seconds and 4 git commands to generate.