1 module daque.graphics.sdl; 2 3 import std.string; 4 5 import core.stdc.stdlib; 6 7 import derelict.sdl2.sdl; 8 import derelict.sdl2.image; 9 import derelict.opengl; 10 11 static this() 12 { 13 DerelictSDL2.load(SharedLibVersion(2, 0, 2)); 14 DerelictSDL2Image.load(); 15 16 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) // error initializing SDL2 17 { 18 exit(-1); 19 } 20 } 21 /// Canvas for drawing 22 class Window 23 { 24 public: 25 26 /++ 27 Constructs a new window with the specified dimensions and name. 28 29 Params: 30 name = name of the window to be constructed 31 width = width of the window to be constructed 32 height = height of the window to be constructed 33 +/ 34 this(string name, uint width, uint height) 35 { 36 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 37 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); 38 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); 39 40 m_window = cast(immutable(SDL_Window*)) SDL_CreateWindow(name.toStringz(), SDL_WINDOWPOS_CENTERED, 41 SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); 42 m_isOpen = m_window != null; 43 44 m_glContext = SDL_GL_CreateContext(getWindow); 45 46 DerelictGL3.reload(); 47 } 48 /// Closes the window 49 ~this() 50 { 51 this.close(); 52 } 53 /// Closes the window if it is open, deallocating it's resources. 54 void close() 55 { 56 if (isOpen()) 57 SDL_DestroyWindow(getWindow()); 58 m_isOpen = false; 59 } 60 /// Tells wether the window is currently open 61 bool isOpen() 62 { 63 return m_isOpen; 64 } 65 66 /// Clear window buffer contents 67 void clear() 68 { 69 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 70 } 71 /// Prints current buffer contents into screen 72 void print() 73 { 74 SDL_GL_SwapWindow(getWindow); 75 } 76 77 private: 78 /// reference to SDL window representation 79 immutable(SDL_Window*) m_window; 80 bool m_isOpen; 81 /// get non immutable reference to the window 82 SDL_Window* getWindow() 83 { 84 return cast(SDL_Window*) m_window; 85 } 86 /// SDL-GL context handle 87 SDL_GLContext m_glContext; 88 } 89 90