~ubuntu-branches/debian/sid/gource/sid

« back to all changes in this revision

Viewing changes to src/core/display.cpp

  • Committer: Package Import Robot
  • Author(s): Andrew Caudwell
  • Date: 2014-04-15 16:30:17 UTC
  • mfrom: (1.2.15)
  • Revision ID: package-import@ubuntu.com-20140415163017-ucdr2josj1spzrga
Tags: 0.41-1
* New upstream release
* Made VCS URIs canonical
* Changed watch file to look at Github for releases

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include "sdlapp.h"
31
31
#include <iostream>
32
32
 
 
33
#if SDL_VERSION_ATLEAST(2,0,0)
 
34
#include "SDL_syswm.h"
 
35
#endif
 
36
 
33
37
SDLAppDisplay display;
34
38
 
35
39
SDLAppDisplay::SDLAppDisplay() {
36
 
    clearColour = vec4(0.0f,0.0f,0.0f,1.0f);
37
 
    enable_shaders=false;
38
 
    enable_alpha=false;
39
 
    vsync=false;
40
 
    resizable=false;
41
 
    frameless=false;
42
 
    multi_sample = 0;
43
 
    width = 0;
44
 
    height = 0;
 
40
    clear_colour    = vec4(0.0f,0.0f,0.0f,1.0f);
 
41
    zbuffer_depth   = 16;
 
42
    enable_alpha    = false;
 
43
    vsync           = false;
 
44
    resizable       = false;
 
45
    frameless       = false;
 
46
    multi_sample    = 0;
 
47
    width           = 0;
 
48
    height          = 0;
45
49
    desktop_width   = 0;
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)
50
54
    sdl_window = 0;
51
55
    gl_context = 0;
52
56
#else
63
67
}
64
68
 
65
69
void SDLAppDisplay::setClearColour(vec4 colour) {
66
 
    clearColour = colour;
 
70
    clear_colour = colour;
67
71
}
68
72
 
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;
 
76
 
 
77
    if (frameless) flags |= SDL_WINDOW_BORDERLESS;
 
78
    if (resizable) flags |= SDL_WINDOW_RESIZABLE;
 
79
    if (fullscreen) flags |= SDL_WINDOW_FULLSCREEN;
 
80
#else
 
81
    Uint32 flags = SDL_OPENGL | SDL_HWSURFACE | SDL_ANYFORMAT | SDL_DOUBLEBUF;
 
82
 
71
83
    if (frameless) flags |= SDL_NOFRAME;
72
84
    if (resizable && !fullscreen) flags |= SDL_RESIZABLE;
73
85
    if (fullscreen) flags |= SDL_FULLSCREEN;
74
 
 
75
 
    //SDL 1.3 vsync
76
 
#ifdef SDL_RENDERER_PRESENTVSYNC
77
 
    if (!vsync) flags |= SDL_RENDERER_PRESENTVSYNC;
78
86
#endif
79
 
 
80
87
    return flags;
81
88
}
82
89
 
84
91
    this->vsync = vsync;
85
92
}
86
93
 
87
 
void SDLAppDisplay::enableShaders(bool enable) {
88
 
    enable_shaders = enable;
 
94
void SDLAppDisplay::setZBufferDepth(int zbuffer_depth) {
 
95
    this->zbuffer_depth = zbuffer_depth;
89
96
}
90
97
 
91
98
void SDLAppDisplay::enableResize(bool resizable) {
119
126
    return value==1;
120
127
}
121
128
 
 
129
#if SDL_VERSION_ATLEAST(2,0,0) && defined(_WIN32)
 
130
WNDPROC window_proc = 0;
 
131
 
 
132
LRESULT CALLBACK window_filter_proc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam) {
 
133
 
 
134
   if (msg == WM_SYSCOMMAND && (wparam & 0xfff0) == SC_KEYMENU) {
 
135
        return 0;
 
136
   }
 
137
 
 
138
   return CallWindowProc(window_proc, wnd, msg, wparam, lparam);
 
139
}
 
140
#endif
 
141
 
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)
124
144
 
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);
127
147
 
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);
 
151
    }
 
152
    
 
153
    if(enable_alpha) {
 
154
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
 
155
    }
 
156
    
 
157
    Uint32 flags = SDLWindowFlags(fullscreen);
131
158
 
132
159
    if(gl_context != 0) SDL_GL_DeleteContext(gl_context);
133
160
    if(sdl_window != 0) SDL_DestroyWindow(sdl_window);
134
161
 
135
 
    sdl_window = SDL_CreateWindow(
136
 
        gSDLAppTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
137
 
        width, height, flags);
138
 
 
 
162
    sdl_window = SDL_CreateWindow(gSDLAppTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
139
163
 
140
164
    if (!sdl_window) {
 
165
 
 
166
        // retry without multi-sampling enabled
 
167
        if(multi_sample > 0) {
 
168
            multi_sample = 0;
 
169
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
 
170
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
 
171
 
 
172
            sdl_window = SDL_CreateWindow(gSDLAppTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
 
173
        }
 
174
 
 
175
        if(!sdl_window) {
 
176
            std::string sdlerr(SDL_GetError());
 
177
            throw SDLInitException(sdlerr);
 
178
        }
 
179
    }
 
180
 
 
181
    gl_context = SDL_GL_CreateContext(sdl_window);
 
182
 
 
183
    if(!gl_context) {
141
184
        std::string sdlerr(SDL_GetError());
142
185
        throw SDLInitException(sdlerr);
143
186
    }
144
187
 
145
 
    gl_context = SDL_GL_CreateContext(sdl_window);
146
 
 
147
188
    if(vsync) SDL_GL_SetSwapInterval(1);
 
189
    else SDL_GL_SetSwapInterval(0);
 
190
 
148
191
#else
149
 
    int depth = 32;
 
192
    int bpp = 32;
150
193
 
151
 
    int flags = SDLFlags(fullscreen);
 
194
    int flags = SDLWindowFlags(fullscreen);
152
195
 
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);
163
206
    }
164
207
 
165
 
#ifdef _WIN32
166
 
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
167
 
    surface = SDL_SetVideoMode(width, height, depth, flags);
168
 
#else
169
 
    if(enable_shaders) {
170
 
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
171
 
        surface = SDL_SetVideoMode(width, height, depth, flags);
172
 
    } else {
173
 
        depth = 24;
174
 
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
175
 
        surface = SDL_SetVideoMode(width, height, depth, flags);
176
 
    }
177
 
#endif
 
208
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, zbuffer_depth);
 
209
    surface = SDL_SetVideoMode(width, height, bpp, flags);
178
210
 
179
211
    if (!surface) {
180
212
        if (multi_sample > 0) {
186
218
            multi_sample = 0;
187
219
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
188
220
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
189
 
            surface = SDL_SetVideoMode(width, height, depth, flags);
 
221
            surface = SDL_SetVideoMode(width, height, bpp, flags);
190
222
        }
191
223
 
192
224
        if (!surface) {
197
229
#endif
198
230
 
199
231
    setupExtensions();
 
232
 
 
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
 
236
 
 
237
    SDL_SysWMinfo sys_window_info;
 
238
 
 
239
    SDL_VERSION(&sys_window_info.version);
 
240
 
 
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);
 
245
    }
 
246
#endif
 
247
}
 
248
 
 
249
void SDLAppDisplay::getFullscreenResolution(int& width, int& height) {
 
250
 
 
251
    int fullscreen_width  = desktop_width;
 
252
    int fullscreen_height = desktop_height;
 
253
 
 
254
#if SDL_VERSION_ATLEAST(2,0,0)
 
255
    // TODO: SDL2 api will have a nice way to do this ...
 
256
#else
 
257
    float aspect_ratio = fullscreen_width / (float) fullscreen_height;
 
258
 
 
259
    if(aspect_ratio > 2.5) {
 
260
 
 
261
        SDL_Rect** modes = SDL_ListModes(0, SDLWindowFlags(true));
 
262
 
 
263
        if(modes != (SDL_Rect**)0 && modes != (SDL_Rect**)-1) {
 
264
 
 
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;
 
268
                    break;
 
269
                }
 
270
            }
 
271
        }
 
272
    }
 
273
#endif
 
274
    width  = fullscreen_width;
 
275
    height = fullscreen_height;
200
276
}
201
277
 
202
278
void SDLAppDisplay::toggleFullscreen() {
210
286
        windowed_width  = width;
211
287
        windowed_height = height;
212
288
 
213
 
        int fullscreen_width  = desktop_width;
214
 
        int fullscreen_height = desktop_height;
215
 
            
216
 
        float aspect_ratio = fullscreen_width / (float) fullscreen_height;
217
 
        
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
220
 
        // fullscreen height
221
 
 
222
 
#if SDL_VERSION_ATLEAST(1,3,0)
223
 
        // TODO: do something with the 1.3 API here
224
 
#else
225
 
        if(aspect_ratio >= 2.5) {
226
 
        
227
 
            SDL_Rect** modes = SDL_ListModes(0, SDLFlags(true));
228
 
        
229
 
            if(modes != (SDL_Rect**)0 && modes != (SDL_Rect**)-1) {
230
 
            
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;
234
 
                        break;
235
 
                    }
236
 
                }
237
 
            }
238
 
        }
239
 
#endif
240
 
 
241
 
        width  = fullscreen_width;
242
 
        height = fullscreen_height;
243
 
        
 
289
        getFullscreenResolution(width, height);
 
290
 
244
291
    } else {
245
292
        //switch back to window dimensions, if known
246
293
        if(windowed_width != 0) {
251
298
 
252
299
    fullscreen = !fullscreen;
253
300
 
254
 
    setVideoMode(width, height, fullscreen);
255
 
 
256
301
    int resized_width, resized_height;
257
302
 
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);
260
306
#else
 
307
    setVideoMode(width, height, fullscreen);
 
308
 
261
309
    const SDL_VideoInfo* display_info = SDL_GetVideoInfo();
262
310
 
263
311
    resized_width  = display_info->current_w;
275
323
 
276
324
    int resized_width, resized_height;
277
325
 
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);
280
328
#else
281
329
    setVideoMode(width, height, fullscreen);
282
 
    
 
330
 
283
331
    const SDL_VideoInfo* display_info = SDL_GetVideoInfo();
284
332
 
285
333
    resized_width  = display_info->current_w;
299
347
        throw SDLInitException(SDL_GetError());
300
348
    }
301
349
 
 
350
 
 
351
#if SDL_VERSION_ATLEAST(2,0,0)
 
352
 
 
353
    // TODO: which display? is 0 the designated primary display always?
 
354
    SDL_Rect display_rect;
 
355
    SDL_GetDisplayBounds(0, &display_rect);
 
356
 
 
357
    desktop_width  = display_rect.w;
 
358
    desktop_height = display_rect.h;
 
359
 
 
360
#else
302
361
    const SDL_VideoInfo* display_info = SDL_GetVideoInfo();
303
362
 
304
363
    //save the desktop resolution
305
364
    desktop_width  = display_info->current_w;
306
365
    desktop_height = display_info->current_h;
307
 
      
 
366
#endif
 
367
 
 
368
    //initialize width and height to desktop resolution if un-specified
 
369
    if(!width || !height) {
 
370
        if(fullscreen) {
 
371
            getFullscreenResolution(width, height);
 
372
        } else {
 
373
            if(!width) width   = desktop_width;
 
374
            if(!height) height = desktop_height;
 
375
        }
 
376
    }
 
377
 
308
378
    atexit(SDL_Quit);
309
379
 
 
380
#if SDL_VERSION_ATLEAST(2,0,0)
 
381
 
 
382
#else
310
383
    SDL_EnableUNICODE(1);
311
 
 
312
 
#if SDL_VERSION_ATLEAST(1,3,0)
313
 
#else
314
384
    SDL_WM_SetCaption(window_title.c_str(),0);
315
385
#endif
316
386
 
323
393
    this->width      = viewport[2];
324
394
    this->height     = viewport[3];
325
395
    this->fullscreen = fullscreen;
 
396
 
 
397
    glViewport(0, 0, this->width, this->height);
326
398
}
327
399
 
328
400
void SDLAppDisplay::quit() {
329
401
 
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);
333
405
#endif
339
411
}
340
412
 
341
413
void SDLAppDisplay::update() {
342
 
#if SDL_VERSION_ATLEAST(1,3,0)
 
414
#if SDL_VERSION_ATLEAST(2,0,0)
343
415
    SDL_GL_SwapWindow(sdl_window);
344
416
#else
345
417
    SDL_GL_SwapBuffers();
347
419
}
348
420
 
349
421
void SDLAppDisplay::clear() {
350
 
    glClearColor(clearColour.x, clearColour.y, clearColour.z, clearColour.w);
 
422
    glClearColor(clear_colour.x, clear_colour.y, clear_colour.z, clear_colour.w);
351
423
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
352
424
}
353
425