]> Dogcows Code - chaz/yoink/blob - src/Moof/Video.cc
simplified win32 installer build script
[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 "Engine.hh"
31 #include "Exception.hh"
32 #include "Image.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 // make sure the engine is initialized before setting up the video
68 Engine::getInstance();
69
70 mContext = 0;
71 mFlags = 0;
72 mAttribs = attribs;
73
74 setFull(attribs.fullscreen);
75 setResizable(attribs.resizable);
76 setOpenGLAttributes();
77 setCaption(attribs.caption);
78 setIcon();
79 setCursorVisible(attribs.cursorVisible);
80 setCursorGrab(attribs.cursorGrab);
81 setVideoMode(attribs.mode);
82 }
83
84 void Video::recreateContext()
85 {
86 SDL_FreeSurface(mContext);
87 mContext = 0;
88 setVideoMode(mAttribs.mode);
89 }
90
91 void Video::setOpenGLAttributes()
92 {
93 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, mAttribs.colorBuffer[0]);
94 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, mAttribs.colorBuffer[1]);
95 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, mAttribs.colorBuffer[2]);
96 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, mAttribs.colorBuffer[3]);
97 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, mAttribs.frameBuffer);
98 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, mAttribs.doubleBuffer);
99 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, mAttribs.depthBuffer);
100 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, mAttribs.stencilBuffer);
101 SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, mAttribs.accumBuffer[0]);
102 SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, mAttribs.accumBuffer[1]);
103 SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, mAttribs.accumBuffer[2]);
104 SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, mAttribs.accumBuffer[3]);
105 SDL_GL_SetAttribute(SDL_GL_STEREO, mAttribs.stereo);
106 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, mAttribs.multisampleBuffers);
107 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, mAttribs.multisampleSamples);
108 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, mAttribs.swapControl);
109 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, mAttribs.hardwareonly);
110 }
111
112
113 Video::~Video()
114 {
115 SDL_FreeSurface(mContext);
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 Engine::getInstance().dispatch("video.newcontext");
139
140 logInfo("video context recreated");
141 #endif
142 }
143 else throw Exception(ErrorCode::SDL_VIDEOMODE);
144 }
145 }
146
147 Video::Attributes Video::getAttributes() const
148 {
149 return mAttribs;
150 }
151
152
153 void Video::resize(int width, int height)
154 {
155 long mode[] = {width, height, mAttribs.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 mAttribs.caption = caption;
168 SDL_WM_SetCaption(caption.c_str(), 0);
169 }
170
171 void Video::setIcon()
172 {
173 if (mAttribs.icon != "")
174 {
175 Image icon(mAttribs.icon);
176 icon.setAsIcon();
177 }
178 }
179
180 std::string Video::getCaption() const
181 {
182 return mAttribs.caption;
183 }
184
185 const std::string& Video::getIcon() const
186 {
187 return mAttribs.icon;
188 }
189
190
191 void Video::setFull(bool full)
192 {
193 if (full != isFull() || !mContext)
194 {
195 if (mContext)
196 {
197 mFlags ^= SDL_FULLSCREEN;
198
199 #if defined(linux) || defined(__linux) || defined(__linux__)
200 if (SDL_WM_ToggleFullScreen(mContext) == 0)
201 #endif
202 recreateContext();
203 }
204 else
205 {
206 if (full) mFlags |= SDL_FULLSCREEN;
207 else mFlags &= ~SDL_FULLSCREEN;
208 }
209 }
210 }
211
212 void Video::toggleFull()
213 {
214 setFull(!isFull());
215 }
216
217 bool Video::isFull() const
218 {
219 return mFlags & SDL_FULLSCREEN;
220 }
221
222
223 void Video::setCursorVisible(bool hasCursor)
224 {
225 SDL_ShowCursor(hasCursor? SDL_ENABLE : SDL_DISABLE);
226 }
227
228 void Video::toggleCursorVisible()
229 {
230 setCursorVisible(!isCursorVisible());
231 }
232
233 bool Video::isCursorVisible() const
234 {
235 return (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE);
236 }
237
238
239 void Video::setResizable(bool resizable)
240 {
241 if (resizable != isResizable() || !mContext)
242 {
243 if (mContext)
244 {
245 mFlags ^= SDL_RESIZABLE;
246 recreateContext();
247 }
248 else
249 {
250 if (resizable) mFlags |= SDL_RESIZABLE;
251 else mFlags &= ~SDL_RESIZABLE;
252 }
253 }
254 }
255
256 void Video::toggleResizable()
257 {
258 setResizable(!isResizable());
259 }
260
261 bool Video::isResizable() const
262 {
263 return mFlags & SDL_RESIZABLE;
264 }
265
266
267 bool Video::isCursorGrab() const
268 {
269 return (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON);
270 }
271
272 void Video::toggleCursorGrab()
273 {
274 setCursorGrab(!isCursorGrab());
275 }
276
277 void Video::setCursorGrab(bool cursorGrab)
278 {
279 SDL_WM_GrabInput(cursorGrab? SDL_GRAB_ON : SDL_GRAB_OFF);
280 }
281
282
283 void Video::swap()
284 {
285 SDL_GL_SwapBuffers();
286 }
287
288
289 int Video::getWidth() const
290 {
291 return mContext->w;
292 }
293
294 int Video::getHeight() const
295 {
296 return mContext->h;
297 }
298
299
300 Video::Attributes::Attributes()
301 {
302 // Set some sane GL and window defaults (see SDL_video.c:217)
303 colorBuffer[0] = 3;
304 colorBuffer[1] = 3;
305 colorBuffer[2] = 2;
306 colorBuffer[3] = 0;
307 frameBuffer = 0;
308 doubleBuffer = true;
309 depthBuffer = 16;
310 stencilBuffer = 0;
311 accumBuffer[0] = 0;
312 accumBuffer[1] = 0;
313 accumBuffer[2] = 0;
314 accumBuffer[3] = 0;
315 stereo = false;
316 multisampleBuffers = 0;
317 multisampleSamples = 0;
318 swapControl = false;
319 hardwareonly = false;
320 mode[0] = 640;
321 mode[1] = 480;
322 mode[2] = 0;
323 fullscreen = false;
324 resizable = false;
325 cursorVisible = true;
326 cursorGrab = false;
327
328 Settings& settings = Settings::getInstance();
329
330 std::vector<long> colors;
331 settings.get("colorbuffers", colors);
332 if (colors.size() > 0) colorBuffer[0] = colors[0];
333 if (colors.size() > 1) colorBuffer[1] = colors[1];
334 if (colors.size() > 2) colorBuffer[2] = colors[2];
335 if (colors.size() > 3) colorBuffer[3] = colors[3];
336
337 settings.get("framebuffer", frameBuffer);
338 settings.get("doublebuffer", doubleBuffer);
339 settings.get("depthbuffer", depthBuffer);
340 settings.get("stencilbuffer", stencilBuffer);
341
342 std::vector<long> accum;
343 settings.get("accumbuffers", accum);
344 if (accum.size() > 0) accumBuffer[0] = accum[0];
345 if (accum.size() > 1) accumBuffer[1] = accum[1];
346 if (accum.size() > 2) accumBuffer[2] = accum[2];
347 if (accum.size() > 3) accumBuffer[3] = accum[3];
348
349 settings.get("stereo", stereo);
350 settings.get("multiesamplebuffers", multisampleBuffers);
351 settings.get("multiesamplesamples", multisampleSamples);
352 settings.get("swapcontrol", swapControl);
353 settings.get("hardwareonly", hardwareonly);
354
355 if (!settings.get("caption", caption))
356 {
357 caption = "Untitled";
358 }
359 settings.get("icon", icon);
360
361 std::vector<long> dimensions;
362 settings.get("videomode", dimensions);
363 if (dimensions.size() > 1)
364 {
365 mode[0] = dimensions[0];
366 mode[1] = dimensions[1];
367 }
368 if (dimensions.size() > 2) mode[2] = dimensions[2];
369
370 settings.get("fullscreen", fullscreen);
371 settings.get("resizable", resizable);
372 settings.get("showcursor", cursorVisible);
373 settings.get("grab", cursorGrab);
374 }
375
376
377 } // namespace Mf
378
379 /** vim: set ts=4 sw=4 tw=80: *************************************************/
380
This page took 0.046857 seconds and 5 git commands to generate.