1 /* 2 Copyright (c) 2014-2015 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module dgl.core.application; 30 31 import std.stdio; 32 import std.conv; 33 import std.process; 34 import std.string; 35 36 import dlib.core.memory; 37 import dlib.image.color; 38 39 import derelict.opengl.gl; 40 import derelict.opengl.glu; 41 import derelict.sdl.sdl; 42 import derelict.freetype.ft; 43 44 import dgl.core.interfaces; 45 import dgl.core.event; 46 47 /* 48 * Basic SDL/OpenGL application. 49 * GC-free, but may throw on initialization failure 50 */ 51 class Application: EventListener 52 { 53 Color4f clearColor; 54 bool exitOnEscapePress = true; 55 56 // TODO: configuration manager 57 this( 58 uint width, 59 uint height, 60 string caption = "DGL application", 61 bool unicodeInput = true, 62 bool showCursor = true, 63 bool resizableWindow = true) 64 { 65 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) 66 throw new Exception("Failed to init SDL: " ~ to!string(SDL_GetError())); 67 68 SDL_EnableUNICODE(unicodeInput); 69 70 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 71 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); 72 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); 73 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); 74 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32); 75 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); 76 77 environment["SDL_VIDEO_WINDOW_POS"] = ""; 78 environment["SDL_VIDEO_CENTERED"] = "1"; 79 80 auto screen = SDL_SetVideoMode(width, height, 0, SDL_OPENGL | SDL_RESIZABLE); // | SDL_FULLSCREEN 81 if (screen is null) 82 throw new Exception("Failed to set video mode: " ~ to!string(SDL_GetError())); 83 84 SDL_WM_SetCaption(toStringz(caption), null); 85 SDL_ShowCursor(showCursor); 86 87 DerelictGL.loadClassicVersions(GLVersion.GL12); 88 DerelictGL.loadBaseExtensions(); 89 90 clearColor = Color4f(0, 0, 0); 91 92 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 93 glEnable(GL_BLEND); 94 glEnable(GL_NORMALIZE); 95 glShadeModel(GL_SMOOTH); 96 glAlphaFunc(GL_GREATER, 0.0); 97 glEnable(GL_ALPHA_TEST); 98 glEnable(GL_DEPTH_TEST); 99 glDepthFunc(GL_LEQUAL); 100 //glDepthFunc(GL_LESS); 101 glEnable(GL_CULL_FACE); 102 103 EventManager emngr = New!EventManager(width, height); 104 super(emngr); 105 } 106 107 void run() 108 { 109 while(eventManager.running) 110 { 111 eventManager.update(); 112 processEvents(); 113 glViewport(0, 0, eventManager.windowWidth, eventManager.windowHeight); 114 glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); 115 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 116 glLoadIdentity(); 117 118 onUpdate(); 119 onRedraw(); 120 121 SDL_GL_SwapBuffers(); 122 } 123 124 SDL_Quit(); 125 } 126 127 // Override me 128 void onUpdate() {} 129 130 // Override me 131 void onRedraw() {} 132 133 // Override me 134 override void onKeyDown(int key) 135 { 136 if (key == SDLK_ESCAPE && exitOnEscapePress) 137 { 138 eventManager.running = false; 139 onQuit(); 140 } 141 } 142 143 void exit() 144 { 145 eventManager.running = false; 146 onQuit(); 147 } 148 149 override void onResize(int width, int height) 150 { 151 writefln("Application resized to %s, %s", width, height); 152 SDL_Surface* screen = SDL_SetVideoMode(width, 153 height, 154 0, SDL_OPENGL | SDL_RESIZABLE); 155 if (screen is null) 156 throw new Exception("failed to set video mode: " ~ to!string(SDL_GetError())); 157 } 158 159 ~this() 160 { 161 Delete(eventManager); 162 } 163 164 override void free() 165 { 166 Delete(this); 167 } 168 } 169 170 // TODO: 171 // Under Linunx, user should be able to select 172 // between system libraries and local ones 173 void loadLibraries() 174 { 175 DerelictGL.load(); 176 DerelictGLU.load(); 177 178 version(Windows) 179 { 180 DerelictSDL.load("lib/SDL.dll"); 181 DerelictFT.load("lib/freetype.dll"); 182 } 183 version(linux) 184 { 185 DerelictSDL.load("./lib/libsdl.so"); 186 DerelictFT.load("./lib/libfreetype.so"); 187 } 188 version(OSX) 189 { 190 DerelictSDL.load(); 191 DerelictFT.load(); 192 } 193 }