|
1096
|
1 /**
|
|
|
2 * Copyright (c) 2006-2012 LOVE Development Team
|
|
|
3 *
|
|
|
4 * This software is provided 'as-is', without any express or implied
|
|
|
5 * warranty. In no event will the authors be held liable for any damages
|
|
|
6 * arising from the use of this software.
|
|
|
7 *
|
|
|
8 * Permission is granted to anyone to use this software for any purpose,
|
|
|
9 * including commercial applications, and to alter it and redistribute it
|
|
|
10 * freely, subject to the following restrictions:
|
|
|
11 *
|
|
|
12 * 1. The origin of this software must not be misrepresented; you must not
|
|
|
13 * claim that you wrote the original software. If you use this software
|
|
|
14 * in a product, an acknowledgment in the product documentation would be
|
|
|
15 * appreciated but is not required.
|
|
|
16 * 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
17 * misrepresented as being the original software.
|
|
|
18 * 3. This notice may not be removed or altered from any source distribution.
|
|
|
19 **/
|
|
|
20
|
|
|
21 // STL
|
|
|
22 #include <iostream>
|
|
|
23
|
|
|
24 // SDL
|
|
|
25 #include <SDL.h>
|
|
|
26
|
|
|
27 // OpenGL
|
|
|
28 #include <SDL/SDL_opengl.h>
|
|
|
29
|
|
|
30 // LOVE
|
|
|
31 #include "common/config.h"
|
|
|
32 #include "Window.h"
|
|
|
33
|
|
|
34 namespace love
|
|
|
35 {
|
|
|
36 namespace window
|
|
|
37 {
|
|
|
38 namespace sdl
|
|
|
39 {
|
|
|
40
|
|
|
41 Window::Window()
|
|
|
42 : windowTitle("")
|
|
|
43 , created(false)
|
|
|
44 , mouseVisible(true)
|
|
|
45 {
|
|
|
46 // Window should be centered.
|
|
|
47 SDL_putenv(const_cast<char *>("SDL_VIDEO_CENTERED=center"));
|
|
|
48
|
|
|
49 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
|
|
|
50 throw Exception(SDL_GetError());
|
|
|
51 }
|
|
|
52
|
|
|
53 Window::~Window()
|
|
|
54 {
|
|
|
55 SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
|
|
56 }
|
|
|
57
|
|
|
58 Window::_currentMode::_currentMode()
|
|
|
59 : width(800), height(600), fullscreen(false), vsync(true), fsaa(0)
|
|
|
60 {
|
|
|
61 }
|
|
|
62
|
|
|
63 bool Window::setWindow(int width, int height, bool fullscreen, bool vsync, int fsaa)
|
|
|
64 {
|
|
|
65 bool mouseVisible = getMouseVisible();
|
|
|
66
|
|
|
67 // We need to restart the subsystem for two reasons:
|
|
|
68 // 1) Special case for fullscreen -> windowed. Windows XP did not
|
|
|
69 // work well with "normal" display mode change in this case.
|
|
|
70 // The application window does leave fullscreen, but the desktop
|
|
|
71 // resolution does not revert to the correct one. Restarting the
|
|
|
72 // SDL video subsystem does the trick, though.
|
|
|
73 // 2) Restart the event system (for whatever reason the event system
|
|
|
74 // started and stopped with SDL_INIT_VIDEO, see:
|
|
|
75 // http://sdl.beuc.net/sdl.wiki/Introduction_to_Events)
|
|
|
76 // because the mouse position will not be able to exceed
|
|
|
77 // the previous' video mode window size (i.e. alway
|
|
|
78 // love.mouse.getX() < 800 when switching from 800x600 to a
|
|
|
79 // higher resolution)
|
|
|
80 SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
|
|
81 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
|
|
|
82 {
|
|
|
83 std::cout << "Could not init SDL_VIDEO: " << SDL_GetError() << std::endl;
|
|
|
84 return false;
|
|
|
85 }
|
|
|
86
|
|
|
87 // Set caption.
|
|
|
88 setWindowTitle(windowTitle);
|
|
|
89 setMouseVisible(mouseVisible);
|
|
|
90
|
|
|
91 // Set GL attributes
|
|
|
92 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
|
|
93 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
|
|
94 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
|
|
95 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
|
|
96 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
97 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, (vsync ? 1 : 0));
|
|
|
98 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
|
|
|
99
|
|
|
100 // FSAA
|
|
|
101 if (fsaa > 0)
|
|
|
102 {
|
|
|
103 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
|
|
104 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, fsaa);
|
|
|
105 }
|
|
|
106
|
|
|
107 // Fullscreen?
|
|
|
108 Uint32 sdlflags = fullscreen ? (SDL_OPENGL | SDL_FULLSCREEN) : SDL_OPENGL;
|
|
|
109
|
|
|
110 // Have SDL set the video mode.
|
|
|
111 if (SDL_SetVideoMode(width, height, 32, sdlflags) == 0)
|
|
|
112 {
|
|
|
113 bool failed = true;
|
|
|
114 if (fsaa > 0)
|
|
|
115 {
|
|
|
116 // FSAA might have failed, disable it and try again
|
|
|
117 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
|
|
|
118 failed = SDL_SetVideoMode(width, height, 32, sdlflags) == 0;
|
|
|
119 if (failed)
|
|
|
120 {
|
|
|
121 // There might be no FSAA at all
|
|
|
122 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
|
|
|
123 failed = SDL_SetVideoMode(width, height, 32, sdlflags) == 0;
|
|
|
124 }
|
|
|
125 }
|
|
|
126 if (failed)
|
|
|
127 {
|
|
|
128 std::cerr << "Could not set video mode: " << SDL_GetError() << std::endl;
|
|
|
129 return false;
|
|
|
130 }
|
|
|
131 }
|
|
|
132
|
|
|
133 created = true;
|
|
|
134
|
|
|
135 if (width == 0 || height == 0)
|
|
|
136 {
|
|
|
137 const SDL_VideoInfo *videoinfo = SDL_GetVideoInfo();
|
|
|
138 width = videoinfo->current_w;
|
|
|
139 height = videoinfo->current_h;
|
|
|
140 }
|
|
|
141
|
|
|
142 if (fsaa > 0)
|
|
|
143 glEnable(GL_MULTISAMPLE);
|
|
|
144
|
|
|
145 GLint buffers;
|
|
|
146 GLint samples;
|
|
|
147
|
|
|
148 glGetIntegerv(GL_SAMPLE_BUFFERS_ARB, &buffers);
|
|
|
149 glGetIntegerv(GL_SAMPLES_ARB, &samples);
|
|
|
150
|
|
|
151 // Don't fail because of this, but issue a warning.
|
|
|
152 if ((!buffers && fsaa) || (samples != fsaa))
|
|
|
153 {
|
|
|
154 std::cerr << "Warning, quality setting failed! (Result: buffers: " << buffers << ", samples: " << samples << ")" << std::endl;
|
|
|
155 fsaa = !buffers ? 0 : samples;
|
|
|
156 }
|
|
|
157
|
|
|
158 // Get the actual vsync status
|
|
|
159 int real_vsync;
|
|
|
160 SDL_GL_GetAttribute(SDL_GL_SWAP_CONTROL, &real_vsync);
|
|
|
161
|
|
|
162 // Set the new display mode as the current display mode.
|
|
|
163 currentMode.width = width;
|
|
|
164 currentMode.height = height;
|
|
|
165 currentMode.fsaa = fsaa;
|
|
|
166 currentMode.fullscreen = fullscreen;
|
|
|
167 currentMode.vsync = (real_vsync != 0);
|
|
|
168
|
|
|
169 return true;
|
|
|
170 }
|
|
|
171
|
|
|
172 void Window::getWindow(int &width, int &height, bool &fullscreen, bool &vsync, int &fsaa)
|
|
|
173 {
|
|
|
174 width = currentMode.width;
|
|
|
175 height = currentMode.height;
|
|
|
176 fullscreen = currentMode.fullscreen;
|
|
|
177 vsync = currentMode.vsync;
|
|
|
178 fsaa = currentMode.fsaa;
|
|
|
179 }
|
|
|
180
|
|
|
181 bool Window::checkWindowSize(int width, int height, bool fullscreen)
|
|
|
182 {
|
|
|
183 Uint32 sdlflags = fullscreen ? (SDL_OPENGL | SDL_FULLSCREEN) : SDL_OPENGL;
|
|
|
184
|
|
|
185 // Check if mode is supported
|
|
|
186 int bpp = SDL_VideoModeOK(width, height, 32, sdlflags);
|
|
|
187
|
|
|
188 return (bpp >= 16);
|
|
|
189 }
|
|
|
190
|
|
|
191 typedef Window::WindowSize WindowSize;
|
|
|
192
|
|
|
193 WindowSize **Window::getFullscreenSizes(int &n)
|
|
|
194 {
|
|
|
195 SDL_Rect **modes = SDL_ListModes(0, SDL_OPENGL | SDL_FULLSCREEN);
|
|
|
196
|
|
|
197 if (modes == (SDL_Rect **)0 || modes == (SDL_Rect **)-1)
|
|
|
198 {
|
|
|
199 n = 0;
|
|
|
200 return NULL;
|
|
|
201 }
|
|
|
202
|
|
|
203 n = 0;
|
|
|
204 for (int i = 0; modes[i]; i++)
|
|
|
205 n++;
|
|
|
206
|
|
|
207 WindowSize **sizes = new WindowSize*[n];
|
|
|
208
|
|
|
209 for (int i = 0; i < n; i++)
|
|
|
210 {
|
|
|
211 sizes[i] = new WindowSize;
|
|
|
212 sizes[i]->width = modes[i]->w;
|
|
|
213 sizes[i]->height = modes[i]->h;
|
|
|
214 }
|
|
|
215 return sizes;
|
|
|
216 }
|
|
|
217
|
|
|
218 int Window::getWidth()
|
|
|
219 {
|
|
|
220 return currentMode.width;
|
|
|
221 }
|
|
|
222
|
|
|
223 int Window::getHeight()
|
|
|
224 {
|
|
|
225 return currentMode.height;
|
|
|
226 }
|
|
|
227
|
|
|
228 bool Window::isCreated()
|
|
|
229 {
|
|
|
230 return created;
|
|
|
231 }
|
|
|
232
|
|
|
233 void Window::setWindowTitle(std::string &title)
|
|
|
234 {
|
|
|
235 windowTitle = title;
|
|
|
236 SDL_WM_SetCaption(windowTitle.c_str(), 0);
|
|
|
237 }
|
|
|
238
|
|
|
239 std::string Window::getWindowTitle()
|
|
|
240 {
|
|
|
241 // not a reference
|
|
|
242 // because we want this untouched
|
|
|
243 // const std::string& might be an option
|
|
|
244 return windowTitle;
|
|
|
245 }
|
|
|
246
|
|
|
247 bool Window::setIcon(love::image::ImageData *imgd)
|
|
|
248 {
|
|
|
249 Uint32 rmask, gmask, bmask, amask;
|
|
|
250 #ifdef LOVE_BIG_ENDIAN
|
|
|
251 rmask = 0xFF000000;
|
|
|
252 gmask = 0x00FF0000;
|
|
|
253 bmask = 0x0000FF00;
|
|
|
254 amask = 0x000000FF;
|
|
|
255 #else
|
|
|
256 rmask = 0x000000FF;
|
|
|
257 gmask = 0x0000FF00;
|
|
|
258 bmask = 0x00FF0000;
|
|
|
259 amask = 0xFF000000;
|
|
|
260 #endif
|
|
|
261
|
|
|
262 int w = static_cast<int>(imgd->getWidth());
|
|
|
263 int h = static_cast<int>(imgd->getHeight());
|
|
|
264 int pitch = static_cast<int>(imgd->getWidth() * 4);
|
|
|
265
|
|
|
266 SDL_Surface *icon = SDL_CreateRGBSurfaceFrom(imgd->getData(), w, h, 32, pitch, rmask, gmask, bmask, amask);
|
|
|
267 SDL_WM_SetIcon(icon, NULL);
|
|
|
268 SDL_FreeSurface(icon);
|
|
|
269 return true;
|
|
|
270 }
|
|
|
271
|
|
|
272 void Window::swapBuffers()
|
|
|
273 {
|
|
|
274 SDL_GL_SwapBuffers();
|
|
|
275 }
|
|
|
276
|
|
|
277 bool Window::hasFocus()
|
|
|
278 {
|
|
|
279 return (SDL_GetAppState() & SDL_APPINPUTFOCUS) != 0;
|
|
|
280 }
|
|
|
281
|
|
|
282 void Window::setMouseVisible(bool visible)
|
|
|
283 {
|
|
|
284 SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
|
|
|
285 }
|
|
|
286
|
|
|
287 bool Window::getMouseVisible()
|
|
|
288 {
|
|
|
289 return (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE) ? true : false;
|
|
|
290 }
|
|
|
291
|
|
|
292 love::window::Window *Window::getSingleton()
|
|
|
293 {
|
|
|
294 if (!singleton)
|
|
|
295 singleton = new Window();
|
|
|
296 else
|
|
|
297 singleton->retain();
|
|
|
298
|
|
|
299 return singleton;
|
|
|
300 }
|
|
|
301
|
|
|
302 const char *Window::getName() const
|
|
|
303 {
|
|
|
304 return "love.window.sdl";
|
|
|
305 }
|
|
|
306
|
|
|
307 } // sdl
|
|
|
308 } // window
|
|
|
309 } // love
|