1 /*
2 Copyright (c) 2017-2018 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 /++
29     Base class to inherit Dagon applications from.
30 +/
31 module dagon.core.application;
32 
33 import std.stdio;
34 import std.conv;
35 import std.getopt;
36 import std.string;
37 import std.file;
38 import core.stdc.stdlib;
39 
40 import dagon.core.libs;
41 import dagon.core.event;
42 
43 void exitWithError(string message)
44 {
45     writeln(message);
46     core.stdc.stdlib.exit(1);
47 }
48 
49 enum DagonEvent
50 {
51     Exit = -1
52 }
53 
54 enum string[GLenum] GLErrorStrings = [
55     GL_NO_ERROR: "GL_NO_ERROR",
56     GL_INVALID_ENUM: "GL_INVALID_ENUM",
57     GL_INVALID_VALUE: "GL_INVALID_VALUE",
58     GL_INVALID_OPERATION: "GL_INVALID_OPERATION",
59     GL_INVALID_FRAMEBUFFER_OPERATION: "GL_INVALID_FRAMEBUFFER_OPERATION",
60     GL_OUT_OF_MEMORY: "GL_OUT_OF_MEMORY"
61 ];
62 
63 /++
64     Base class to inherit Dagon applications from.
65     `Application` wraps SDL2 window, loads dynamic link libraries using Derelict,
66     is responsible for initializing OpenGL context and doing main game loop.
67 +/
68 class Application: EventListener
69 {
70     uint width;
71     uint height;
72     SDL_Window* window = null;
73     SDL_GLContext glcontext;
74     string libdir;
75 
76     /++
77         Constructor.
78         * `winWidth` - window width
79         * `winHeight` - window height
80         * `fullscreen` - if true, the application will run in fullscreen mode
81         * `windowTitle` - window title
82         * `args` - command line arguments
83     +/
84     this(uint winWidth, uint winHeight, bool fullscreen, string windowTitle, string[] args)
85     {
86         FreetypeSupport ftsup = loadFreetype();
87         if (ftsup != freetypeSupport)
88         {
89             if (ftsup == FreetypeSupport.badLibrary)
90                 writeln("Warning: failed to load some Freetype functions. It seems that you have an old version of Freetype. Dagon will try to use it, but it is recommended to install Freetype 2.8.1 or higher");
91             else
92                 exitWithError("Error: Freetype library is not found. Please, install Freetype 2.8.1");
93         }
94 
95         SDLSupport sdlsup = loadSDL();
96         if (sdlsup != sdlSupport)
97         {
98             if (sdlsup == SDLSupport.badLibrary)
99                 writeln("Warning: failed to load some SDL functions. It seems that you have an old version of SDL. Dagon will try to use it, but it is recommended to install SDL 2.0.5 or higher");
100             else
101                 exitWithError("Error: SDL library is not found. Please, install SDL 2.0.5");
102         }
103 
104         if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
105             exitWithError("Error: failed to init SDL: " ~ to!string(SDL_GetError()));
106 
107         width = winWidth;
108         height = winHeight;
109 
110         SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
111 
112         SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
113         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
114         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
115         SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
116         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
117         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
118         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
119 
120         window = SDL_CreateWindow(toStringz(windowTitle),
121             SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
122         if (window is null)
123             exitWithError("Error: failed to create window: " ~ to!string(SDL_GetError()));
124 
125         SDL_GL_SetSwapInterval(1);
126 
127         glcontext = SDL_GL_CreateContext(window);
128         if (glcontext is null)
129             exitWithError("Error: failed to create OpenGL context: " ~ to!string(SDL_GetError()));
130 
131         SDL_GL_MakeCurrent(window, glcontext);
132 
133         GLSupport glsup = loadOpenGL();
134         if (isOpenGLLoaded())
135         {
136             if (glsup < GLSupport.gl40)
137             {
138                 exitWithError("Error: Dagon requires OpenGL 4.0, but it seems that your graphics card does not support it");
139             }
140         }
141         else
142         {
143             exitWithError("Error: failed to load OpenGL functions. Please, update graphics card driver and make sure it supports OpenGL 4.0");
144         }
145 
146         if (fullscreen)
147             SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
148 
149         EventManager eventManager = new EventManager(window, width, height);
150         super(eventManager, null);
151 
152         // Initialize OpenGL
153         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
154         glClearDepth(1.0);
155         glDepthFunc(GL_LESS);
156         glEnable(GL_DEPTH_TEST);
157         glEnable(GL_POLYGON_OFFSET_FILL);
158         glCullFace(GL_BACK);
159         glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
160         
161         glClear(GL_COLOR_BUFFER_BIT);
162         SDL_GL_SwapWindow(window);
163 
164         //checkGLError();
165     }
166 
167     override void onUserEvent(int code)
168     {
169         if (code == DagonEvent.Exit)
170         {
171             exit();
172         }
173     }
174 
175     void onUpdate(double dt)
176     {
177         // Override me
178     }
179 
180     void onRender()
181     {
182         // Override me
183     }
184 
185     void checkGLError()
186     {
187         GLenum error = GL_NO_ERROR;
188         error = glGetError();
189         if (error != GL_NO_ERROR)
190         {
191             writefln("OpenGL error %s: %s", error, GLErrorStrings[error]);
192             eventManager.running = false;
193         }
194     }
195 
196     void run()
197     {
198         while(eventManager.running)
199         {
200             beginRender();
201             onUpdate(eventManager.deltaTime);
202             onRender();
203             endRender();
204         }
205     }
206 
207     void beginRender()
208     {
209         eventManager.update();
210         processEvents();
211     }
212 
213     void endRender()
214     {
215         debug checkGLError();
216         SDL_GL_SwapWindow(window);
217     }
218 
219     void exit()
220     {
221         eventManager.running = false;
222     }
223 
224     ~this()
225     {
226         SDL_GL_DeleteContext(glcontext);
227         SDL_DestroyWindow(window);
228         SDL_Quit();
229     }
230 }