1 //          Copyright Ferdinand Majerech 2014.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE_1_0.txt or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5 
6 /// SDL2-only utility functions. (Note: there is more SDL2 code in the Platform package)
7 module platform.sdl2;
8 
9 import derelict.sdl2.sdl;
10 
11 
12 import std.typecons;
13 
14 
15 package:
16 
17 /// Create an OpenGL window.
18 ///
19 /// This only creates a window, not a GL context. The caller has to do that.
20 ///
21 /// Params:
22 ///
23 /// w           = Window width in pixels.
24 /// h           = Window height in pixels.
25 /// fullscreen) = If true, create a fullscreen window.
26 ///
27 /// Returns: An SDL window. It's the caller's responsibility to destroy the window.
28 SDL_Window* createGLWindow(size_t w, size_t h, Flag!"fullscreen" fullscreen)
29     @system nothrow @nogc
30 {
31     assert(w > 0 && h > 0, "Can't create a zero-sized window");
32 
33     // OpenGL 3.0.
34     // and the core profile (i.e. no deprecated functions)
35     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
36     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
37     SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
38 
39     // 32bit RGBA window
40     SDL_GL_SetAttribute(SDL_GL_RED_SIZE,     8);
41     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   8);
42     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,    8);
43     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,   8);
44     // Double buffering to avoid tearing
45     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
46     // Depth buffer. Needed for 3D.
47     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,   24);
48 
49     auto flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
50 
51     if(fullscreen) { flags |= SDL_WINDOW_FULLSCREEN; }
52     // Create a centered 640x480 OpenGL window named "Tharsis-Game"
53     return SDL_CreateWindow("tharsis-game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
54                             cast(int)w, cast(int)h, flags);
55 }