]> Dogcows Code - chaz/yoink/blob - src/video.cc
new classes; yajl library
[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
34 #include "video.hh"
35
36
37 namespace dc {
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 }
97
98 void video::setOpenGLAttributes()
99 {
100 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, attribs_.colorBuffer[0]);
101 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, attribs_.colorBuffer[1]);
102 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, attribs_.colorBuffer[2]);
103 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, attribs_.colorBuffer[3]);
104 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, attribs_.frameBuffer);
105 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, attribs_.doubleBuffer);
106 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, attribs_.depthBuffer);
107 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, attribs_.stencilBuffer);
108 SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, attribs_.accumBuffer[0]);
109 SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, attribs_.accumBuffer[1]);
110 SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, attribs_.accumBuffer[2]);
111 SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, attribs_.accumBuffer[3]);
112 SDL_GL_SetAttribute(SDL_GL_STEREO, attribs_.stereo);
113 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, attribs_.multisampleBuffers);
114 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, attribs_.multisampleSamples);
115 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, attribs_.swapControl);
116 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, attribs_.hardwareonly);
117 }
118
119
120 video::~video()
121 {
122 SDL_FreeSurface(context_);
123 }
124
125
126 void video::setVideoMode(const long mode[3])
127 {
128 if (mode != attribs_.mode || !context_)
129 {
130 if (context_) SDL_FreeSurface(context_);
131
132 context_ = SDL_SetVideoMode(mode[0], mode[1], mode[2],
133 SDL_OPENGL | flags_);
134
135 if (context_)
136 {
137 attribs_.mode[0] = mode[0];
138 attribs_.mode[1] = mode[1];
139 attribs_.mode[2] = mode[2];
140 }
141 else throw std::runtime_error(SDL_GetError());
142 }
143 }
144
145 video::attributes video::getAttributes() const
146 {
147 return attribs_;
148 }
149
150
151 void video::resize(int width, int height)
152 {
153 long mode[] = {width, height, attribs_.mode[2]};
154 setVideoMode(mode);
155 }
156
157 bool video::iconify()
158 {
159 return SDL_WM_IconifyWindow();
160 }
161
162
163 void video::setCaption(const std::string& caption)
164 {
165 SDL_WM_SetCaption(caption.c_str(), 0);
166 }
167
168 std::string video::getCaption() const
169 {
170 char* str;
171 SDL_WM_GetCaption(&str, 0);
172 return std::string(str);
173 }
174
175
176 void video::setFull(bool full)
177 {
178 if (full != isFull() || !context_)
179 {
180 if (context_)
181 {
182 flags_ ^= SDL_FULLSCREEN;
183
184 #if defined(linux) || defined(__linux) || defined(__linux__)
185 if (SDL_WM_ToggleFullScreen(context_) == 0)
186 #endif
187 recreateContext();
188 }
189 else
190 {
191 if (full) flags_ |= SDL_FULLSCREEN;
192 else flags_ &= ~SDL_FULLSCREEN;
193 }
194 }
195 }
196
197 void video::toggleFull()
198 {
199 setFull(!isFull());
200 }
201
202 bool video::isFull() const
203 {
204 return flags_ & SDL_FULLSCREEN;
205 }
206
207
208 void video::setCursorVisible(bool hasCursor)
209 {
210 SDL_ShowCursor(hasCursor? SDL_ENABLE : SDL_DISABLE);
211 }
212
213 void video::toggleCursorVisible()
214 {
215 setCursorVisible(!isCursorVisible());
216 }
217
218 bool video::isCursorVisible() const
219 {
220 return (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE);
221 }
222
223
224 void video::setResizable(bool resizable)
225 {
226 if (resizable != isResizable() || !context_)
227 {
228 if (context_)
229 {
230 flags_ ^= SDL_RESIZABLE;
231 recreateContext();
232 }
233 else
234 {
235 if (resizable) flags_ |= SDL_RESIZABLE;
236 else flags_ &= ~SDL_RESIZABLE;
237 }
238 }
239 }
240
241 void video::toggleResizable()
242 {
243 setResizable(!isResizable());
244 }
245
246 bool video::isResizable() const
247 {
248 return flags_ & SDL_RESIZABLE;
249 }
250
251
252 bool video::isCursorGrab() const
253 {
254 return (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON);
255 }
256
257 void video::toggleCursorGrab()
258 {
259 setCursorGrab(!isCursorGrab());
260 }
261
262 void video::setCursorGrab(bool cursorGrab)
263 {
264 SDL_WM_GrabInput(cursorGrab? SDL_GRAB_ON : SDL_GRAB_OFF);
265 }
266
267
268 void video::makeActive()
269 {
270 // NOP until the day SDL supports more than only one window.
271 // Still waiting...
272 }
273
274 void video::swap()
275 {
276 SDL_GL_SwapBuffers();
277 }
278
279
280 video::attributes::attributes()
281 {
282 // Set some sane GL and window defaults (see SDL_video.c:217)
283 colorBuffer[0] = 3;
284 colorBuffer[1] = 3;
285 colorBuffer[2] = 2;
286 colorBuffer[3] = 0;
287 frameBuffer = 0;
288 doubleBuffer = true;
289 depthBuffer = 16;
290 stencilBuffer = 0;
291 accumBuffer[0] = 0;
292 accumBuffer[1] = 0;
293 accumBuffer[2] = 0;
294 accumBuffer[3] = 0;
295 stereo = false;
296 multisampleBuffers = 0;
297 multisampleSamples = 0;
298 swapControl = false;
299 hardwareonly = false;
300 mode[0] = 640;
301 mode[1] = 480;
302 mode[2] = 0;
303 fullscreen = false;
304 resizable = false;
305 cursorVisible = true;
306 cursorGrab = false;
307
308 std::vector<serializable_ptr> colors;
309 settings::instance().get("video.colorbuffers", colors);
310 if (colors.size() > 0) colors[0]->get(colorBuffer[0]);
311 if (colors.size() > 1) colors[1]->get(colorBuffer[1]);
312 if (colors.size() > 2) colors[2]->get(colorBuffer[2]);
313 if (colors.size() > 3) colors[3]->get(colorBuffer[3]);
314
315 settings::instance().get("video.framebuffer", frameBuffer);
316 settings::instance().get("video.doublebuffer", doubleBuffer);
317 settings::instance().get("video.depthbuffer", depthBuffer);
318 settings::instance().get("video.stencilbuffer", stencilBuffer);
319
320 std::vector<serializable_ptr> accum;
321 settings::instance().get("video.accumbuffers", accum);
322 if (accum.size() > 0) accum[0]->get(accumBuffer[0]);
323 if (accum.size() > 1) accum[1]->get(accumBuffer[1]);
324 if (accum.size() > 2) accum[2]->get(accumBuffer[2]);
325 if (accum.size() > 3) accum[3]->get(accumBuffer[3]);
326
327 settings::instance().get("video.stereo", stereo);
328 settings::instance().get("video.multiesamplebuffers", multisampleBuffers);
329 settings::instance().get("video.multiesamplesamples", multisampleSamples);
330 settings::instance().get("video.swapcontrol", swapControl);
331 settings::instance().get("video.hardwareonly", hardwareonly);
332
333 std::vector<serializable_ptr> dimensions;
334 settings::instance().get("video.mode", dimensions);
335 if (dimensions.size() > 0) dimensions[0]->get(mode[0]);
336 if (dimensions.size() > 1) dimensions[1]->get(mode[1]);
337 if (dimensions.size() > 2) dimensions[2]->get(mode[2]);
338
339 settings::instance().get("video.fullscreen", fullscreen);
340 settings::instance().get("video.resizable", resizable);
341 settings::instance().get("video.cursor", cursorVisible);
342 settings::instance().get("video.grab", cursorGrab);
343 }
344
345
346 } // namespace dc
347
This page took 0.043876 seconds and 4 git commands to generate.