30
30
#include "sdlapp.h"
31
31
#include <iostream>
33
#if SDL_VERSION_ATLEAST(2,0,0)
34
#include "SDL_syswm.h"
33
37
SDLAppDisplay display;
35
39
SDLAppDisplay::SDLAppDisplay() {
36
clearColour = vec4(0.0f,0.0f,0.0f,1.0f);
40
clear_colour = vec4(0.0f,0.0f,0.0f,1.0f);
46
50
desktop_height = 0;
47
51
windowed_width = 0;
48
52
windowed_height = 0;
49
#if SDL_VERSION_ATLEAST(1,3,0)
53
#if SDL_VERSION_ATLEAST(2,0,0)
65
69
void SDLAppDisplay::setClearColour(vec4 colour) {
70
clear_colour = colour;
69
int SDLAppDisplay::SDLFlags(bool fullscreen) {
70
int flags = SDL_OPENGL | SDL_HWSURFACE | SDL_ANYFORMAT | SDL_DOUBLEBUF;
73
Uint32 SDLAppDisplay::SDLWindowFlags(bool fullscreen) {
74
#if SDL_VERSION_ATLEAST(2,0,0)
75
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
77
if (frameless) flags |= SDL_WINDOW_BORDERLESS;
78
if (resizable) flags |= SDL_WINDOW_RESIZABLE;
79
if (fullscreen) flags |= SDL_WINDOW_FULLSCREEN;
81
Uint32 flags = SDL_OPENGL | SDL_HWSURFACE | SDL_ANYFORMAT | SDL_DOUBLEBUF;
71
83
if (frameless) flags |= SDL_NOFRAME;
72
84
if (resizable && !fullscreen) flags |= SDL_RESIZABLE;
73
85
if (fullscreen) flags |= SDL_FULLSCREEN;
76
#ifdef SDL_RENDERER_PRESENTVSYNC
77
if (!vsync) flags |= SDL_RENDERER_PRESENTVSYNC;
129
#if SDL_VERSION_ATLEAST(2,0,0) && defined(_WIN32)
130
WNDPROC window_proc = 0;
132
LRESULT CALLBACK window_filter_proc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam) {
134
if (msg == WM_SYSCOMMAND && (wparam & 0xfff0) == SC_KEYMENU) {
138
return CallWindowProc(window_proc, wnd, msg, wparam, lparam);
122
142
void SDLAppDisplay::setVideoMode(int width, int height, bool fullscreen) {
123
#if SDL_VERSION_ATLEAST(1,3,0)
143
#if SDL_VERSION_ATLEAST(2,0,0)
125
145
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
126
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
146
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, zbuffer_depth);
128
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
129
if(resizable && !fullscreen) flags |= SDL_WINDOW_RESIZABLE;
130
if(fullscreen) flags |= SDL_WINDOW_FULLSCREEN;
148
if(multi_sample > 0) {
149
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
150
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, (GLuint) multi_sample);
154
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
157
Uint32 flags = SDLWindowFlags(fullscreen);
132
159
if(gl_context != 0) SDL_GL_DeleteContext(gl_context);
133
160
if(sdl_window != 0) SDL_DestroyWindow(sdl_window);
135
sdl_window = SDL_CreateWindow(
136
gSDLAppTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
137
width, height, flags);
162
sdl_window = SDL_CreateWindow(gSDLAppTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
140
164
if (!sdl_window) {
166
// retry without multi-sampling enabled
167
if(multi_sample > 0) {
169
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
170
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
172
sdl_window = SDL_CreateWindow(gSDLAppTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
176
std::string sdlerr(SDL_GetError());
177
throw SDLInitException(sdlerr);
181
gl_context = SDL_GL_CreateContext(sdl_window);
141
184
std::string sdlerr(SDL_GetError());
142
185
throw SDLInitException(sdlerr);
145
gl_context = SDL_GL_CreateContext(sdl_window);
147
188
if(vsync) SDL_GL_SetSwapInterval(1);
189
else SDL_GL_SetSwapInterval(0);
151
int flags = SDLFlags(fullscreen);
194
int flags = SDLWindowFlags(fullscreen);
153
196
if(vsync) SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
154
197
else SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
162
205
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
166
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
167
surface = SDL_SetVideoMode(width, height, depth, flags);
170
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
171
surface = SDL_SetVideoMode(width, height, depth, flags);
174
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
175
surface = SDL_SetVideoMode(width, height, depth, flags);
208
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, zbuffer_depth);
209
surface = SDL_SetVideoMode(width, height, bpp, flags);
180
212
if (multi_sample > 0) {
199
231
setupExtensions();
233
#if SDL_VERSION_ATLEAST(2,0,0) && defined(_WIN32)
234
// suppress 'ding' noise when doing alt+key combinations
235
// solution from: http://forums.libsdl.org/viewtopic.php?t=6075
237
SDL_SysWMinfo sys_window_info;
239
SDL_VERSION(&sys_window_info.version);
241
if(SDL_GetWindowWMInfo(sdl_window, &sys_window_info)) {
242
HWND wnd = sys_window_info.info.win.window;
243
window_proc = (WNDPROC) GetWindowLongPtr(wnd, GWLP_WNDPROC);
244
SetWindowLongPtr(wnd, GWLP_WNDPROC, (LONG_PTR) &window_filter_proc);
249
void SDLAppDisplay::getFullscreenResolution(int& width, int& height) {
251
int fullscreen_width = desktop_width;
252
int fullscreen_height = desktop_height;
254
#if SDL_VERSION_ATLEAST(2,0,0)
255
// TODO: SDL2 api will have a nice way to do this ...
257
float aspect_ratio = fullscreen_width / (float) fullscreen_height;
259
if(aspect_ratio > 2.5) {
261
SDL_Rect** modes = SDL_ListModes(0, SDLWindowFlags(true));
263
if(modes != (SDL_Rect**)0 && modes != (SDL_Rect**)-1) {
265
for (int i=0; modes[i]; i++) {
266
if(modes[i]->h == fullscreen_height && (modes[i]->w/(float)modes[i]->h) < 2.5) {
267
fullscreen_width = modes[i]->w;
274
width = fullscreen_width;
275
height = fullscreen_height;
202
278
void SDLAppDisplay::toggleFullscreen() {
210
286
windowed_width = width;
211
287
windowed_height = height;
213
int fullscreen_width = desktop_width;
214
int fullscreen_height = desktop_height;
216
float aspect_ratio = fullscreen_width / (float) fullscreen_height;
218
// if the aspect ratio suggests the person is using multiple monitors
219
// find a supported resolution with a lower aspect ratio with the same
222
#if SDL_VERSION_ATLEAST(1,3,0)
223
// TODO: do something with the 1.3 API here
225
if(aspect_ratio >= 2.5) {
227
SDL_Rect** modes = SDL_ListModes(0, SDLFlags(true));
229
if(modes != (SDL_Rect**)0 && modes != (SDL_Rect**)-1) {
231
for (int i=0; modes[i]; i++) {
232
if(modes[i]->h == fullscreen_height && (modes[i]->w/(float)modes[i]->h) < 2.5) {
233
fullscreen_width = modes[i]->w;
241
width = fullscreen_width;
242
height = fullscreen_height;
289
getFullscreenResolution(width, height);
245
292
//switch back to window dimensions, if known
246
293
if(windowed_width != 0) {
252
299
fullscreen = !fullscreen;
254
setVideoMode(width, height, fullscreen);
256
301
int resized_width, resized_height;
258
#if SDL_VERSION_ATLEAST(1,3,0)
303
#if SDL_VERSION_ATLEAST(2,0,0)
304
SDL_SetWindowFullscreen(sdl_window, fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
259
305
SDL_GetWindowSize(sdl_window, &resized_width, &resized_height);
307
setVideoMode(width, height, fullscreen);
261
309
const SDL_VideoInfo* display_info = SDL_GetVideoInfo();
263
311
resized_width = display_info->current_w;
276
324
int resized_width, resized_height;
278
#if SDL_VERSION_ATLEAST(1,3,0)
326
#if SDL_VERSION_ATLEAST(2,0,0)
279
327
SDL_GetWindowSize(sdl_window, &resized_width, &resized_height);
281
329
setVideoMode(width, height, fullscreen);
283
331
const SDL_VideoInfo* display_info = SDL_GetVideoInfo();
285
333
resized_width = display_info->current_w;
299
347
throw SDLInitException(SDL_GetError());
351
#if SDL_VERSION_ATLEAST(2,0,0)
353
// TODO: which display? is 0 the designated primary display always?
354
SDL_Rect display_rect;
355
SDL_GetDisplayBounds(0, &display_rect);
357
desktop_width = display_rect.w;
358
desktop_height = display_rect.h;
302
361
const SDL_VideoInfo* display_info = SDL_GetVideoInfo();
304
363
//save the desktop resolution
305
364
desktop_width = display_info->current_w;
306
365
desktop_height = display_info->current_h;
368
//initialize width and height to desktop resolution if un-specified
369
if(!width || !height) {
371
getFullscreenResolution(width, height);
373
if(!width) width = desktop_width;
374
if(!height) height = desktop_height;
308
378
atexit(SDL_Quit);
380
#if SDL_VERSION_ATLEAST(2,0,0)
310
383
SDL_EnableUNICODE(1);
312
#if SDL_VERSION_ATLEAST(1,3,0)
314
384
SDL_WM_SetCaption(window_title.c_str(),0);
323
393
this->width = viewport[2];
324
394
this->height = viewport[3];
325
395
this->fullscreen = fullscreen;
397
glViewport(0, 0, this->width, this->height);
328
400
void SDLAppDisplay::quit() {
330
#if SDL_VERSION_ATLEAST(1,3,0)
402
#if SDL_VERSION_ATLEAST(2,0,0)
331
403
if(gl_context != 0) SDL_GL_DeleteContext(gl_context);
332
404
if(sdl_window != 0) SDL_DestroyWindow(sdl_window);