]> Dogcows Code - chaz/yoink/blob - src/Moof/Video.cc
9e74acbf2b75dae0b09cb53f17883ed1eb4063b8
[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 "Dispatch.hh"
30 #include "Error.hh"
31 #include "Image.hh"
32 #include "Log.hh"
33 #include "Settings.hh"
34 #include "Video.hh"
35
36
37 namespace Mf {
38
39
40 Video::Video()
41 {
42 init(mAttribs);
43 }
44
45 Video::Video(const Attributes& attribs)
46 {
47 init(attribs);
48 }
49
50 Video::Video(const std::string& caption, const std::string& icon)
51 {
52 if (mAttribs.caption == "Untitled")
53 {
54 mAttribs.caption = caption;
55 }
56 if (mAttribs.icon == "")
57 {
58 mAttribs.icon = icon;
59 }
60
61 init(mAttribs);
62 }
63
64 void Video::init(const Attributes& attribs)
65 {
66 mContext = 0;
67 mFlags = 0;
68 mAttribs = attribs;
69
70 setFull(attribs.fullscreen);
71 setResizable(attribs.resizable);
72 setOpenGLAttributes();
73 setCaption(attribs.caption);
74 setIcon();
75 setCursorVisible(attribs.cursorVisible);
76 setCursorGrab(attribs.cursorGrab);
77 setVideoMode(attribs.mode);
78
79 video = this;
80 }
81
82 void Video::recreateContext()
83 {
84 SDL_FreeSurface(mContext);
85 mContext = 0;
86 setVideoMode(mAttribs.mode);
87 }
88
89 void Video::setOpenGLAttributes()
90 {
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);
108 }
109
110
111 Video::~Video()
112 {
113 SDL_FreeSurface(mContext);
114
115 if (video == this) video = 0;
116 }
117
118
119 void Video::setVideoMode(const long mode[3])
120 {
121 if (mode != mAttribs.mode || !mContext)
122 {
123 if (mContext) SDL_FreeSurface(mContext);
124
125 mContext = SDL_SetVideoMode(mode[0], mode[1], mode[2],
126 SDL_OPENGL | mFlags);
127
128 if (mContext)
129 {
130 mAttribs.mode[0] = mode[0];
131 mAttribs.mode[1] = mode[1];
132 mAttribs.mode[2] = mode[2];
133
134 #if defined(_WIN32) || defined(__WIN32__)
135 // on win32, creating a new context via SDL_SetVideoMode will wipe
136 // out the GL state, so we gotta notify everyone to reload their
137 // state after the change
138 core.dispatch("video.newcontext");
139 logInfo("video context recreated");
140 #endif
141 }
142 else throw Error(Error::SDL_VIDEOMODE);
143 }
144 }
145
146 Video::Attributes Video::getAttributes() const
147 {
148 return mAttribs;
149 }
150
151
152 void Video::resize(int width, int height)
153 {
154 long mode[] = {width, height, mAttribs.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 mAttribs.caption = caption;
167 SDL_WM_SetCaption(caption.c_str(), 0);
168 }
169
170 void Video::setIcon()
171 {
172 if (mAttribs.icon != "")
173 {
174 Image icon(mAttribs.icon);
175 icon.setAsIcon();
176 }
177 }
178
179 std::string Video::getCaption() const
180 {
181 return mAttribs.caption;
182 }
183
184 const std::string& Video::getIcon() const
185 {
186 return mAttribs.icon;
187 }
188
189
190 void Video::setFull(bool full)
191 {
192 if (full != isFull() || !mContext)
193 {
194 if (mContext)
195 {
196 mFlags ^= SDL_FULLSCREEN;
197
198 #if defined(linux) || defined(__linux) || defined(__linux__)
199 if (SDL_WM_ToggleFullScreen(mContext) == 0)
200 #endif
201 recreateContext();
202 }
203 else
204 {
205 if (full) mFlags |= SDL_FULLSCREEN;
206 else mFlags &= ~SDL_FULLSCREEN;
207 }
208 }
209 }
210
211 void Video::toggleFull()
212 {
213 setFull(!isFull());
214 }
215
216 bool Video::isFull() const
217 {
218 return mFlags & SDL_FULLSCREEN;
219 }
220
221
222 void Video::setCursorVisible(bool hasCursor)
223 {
224 SDL_ShowCursor(hasCursor? SDL_ENABLE : SDL_DISABLE);
225 }
226
227 void Video::toggleCursorVisible()
228 {
229 setCursorVisible(!isCursorVisible());
230 }
231
232 bool Video::isCursorVisible() const
233 {
234 return (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE);
235 }
236
237
238 void Video::setResizable(bool resizable)
239 {
240 if (resizable != isResizable() || !mContext)
241 {
242 if (mContext)
243 {
244 mFlags ^= SDL_RESIZABLE;
245 recreateContext();
246 }
247 else
248 {
249 if (resizable) mFlags |= SDL_RESIZABLE;
250 else mFlags &= ~SDL_RESIZABLE;
251 }
252 }
253 }
254
255 void Video::toggleResizable()
256 {
257 setResizable(!isResizable());
258 }
259
260 bool Video::isResizable() const
261 {
262 return mFlags & SDL_RESIZABLE;
263 }
264
265
266 bool Video::isCursorGrab() const
267 {
268 return (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON);
269 }
270
271 void Video::toggleCursorGrab()
272 {
273 setCursorGrab(!isCursorGrab());
274 }
275
276 void Video::setCursorGrab(bool cursorGrab)
277 {
278 SDL_WM_GrabInput(cursorGrab? SDL_GRAB_ON : SDL_GRAB_OFF);
279 }
280
281
282 void Video::swap()
283 {
284 SDL_GL_SwapBuffers();
285 }
286
287
288 int Video::getWidth() const
289 {
290 return mContext->w;
291 }
292
293 int Video::getHeight() const
294 {
295 return mContext->h;
296 }
297
298
299 Video::Attributes::Attributes()
300 {
301 // set some sane GL and window defaults (see SDL_video.c:217)
302 colorBuffer[0] = 3;
303 colorBuffer[1] = 3;
304 colorBuffer[2] = 2;
305 colorBuffer[3] = 0;
306 frameBuffer = 0;
307 doubleBuffer = true;
308 depthBuffer = 16;
309 stencilBuffer = 0;
310 accumBuffer[0] = 0;
311 accumBuffer[1] = 0;
312 accumBuffer[2] = 0;
313 accumBuffer[3] = 0;
314 stereo = false;
315 multisampleBuffers = 0;
316 multisampleSamples = 0;
317 swapControl = false;
318 hardwareonly = false;
319 mode[0] = 640;
320 mode[1] = 480;
321 mode[2] = 0;
322 fullscreen = false;
323 resizable = false;
324 cursorVisible = true;
325 cursorGrab = false;
326
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];
333
334 settings.get("framebuffer", frameBuffer);
335 settings.get("doublebuffer", doubleBuffer);
336 settings.get("depthbuffer", depthBuffer);
337 settings.get("stencilbuffer", stencilBuffer);
338
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];
345
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);
351
352 if (!settings.get("caption", caption))
353 {
354 caption = "Untitled";
355 }
356 settings.get("icon", icon);
357
358 settings.get("fullscreen", fullscreen);
359 settings.get("resizable", resizable);
360 settings.get("showcursor", cursorVisible);
361 settings.get("grab", cursorGrab);
362
363 std::vector<long> dimensions;
364 settings.get("videomode", dimensions);
365 if (dimensions.size() > 1)
366 {
367 mode[0] = dimensions[0];
368 mode[1] = dimensions[1];
369 }
370 else if (fullscreen)
371 {
372 SDL_Rect** modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
373
374 if (modes == (SDL_Rect**)0)
375 {
376 Mf::logError("no native video mode");
377 }
378 else if (modes == (SDL_Rect**)-1)
379 {
380 Mf::logWarning("any resolution allowed; choosing default 800x600");
381 mode[0] = 800;
382 mode[1] = 600;
383 }
384 else
385 {
386 while (*(modes + 1)) ++modes; // skip to the last
387
388 mode[0] = (*modes)->w;
389 mode[1] = (*modes)->h;
390 Mf::logInfo << "choosing native resolution "
391 << mode[0] << "x" << mode[1] << std::endl;
392 }
393 }
394 if (dimensions.size() > 2) mode[2] = dimensions[2];
395 }
396
397
398 Video* video = 0;
399
400
401 } // namespace Mf
402
403 /** vim: set ts=4 sw=4 tw=80: *************************************************/
404
This page took 0.046264 seconds and 3 git commands to generate.