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