1 /** 2 * Retrograde Engine 3 * 4 * Authors: 5 * Mike Bierlee, m.bierlee@lostmoment.com 6 * Copyright: 2014-2021 Mike Bierlee 7 * License: 8 * This software is licensed under the terms of the MIT license. 9 * The full terms of the license can be found in the LICENSE.txt file. 10 */ 11 12 module retrograde.graphics.threedee.opengl.sdl2.viewport; 13 14 version(Have_derelict_sdl2) { 15 version(Have_derelict_gl3) { 16 17 import retrograde.viewport; 18 import retrograde.sdl2.window; 19 import retrograde.math; 20 21 import derelict.sdl2.sdl; 22 import derelict.opengl3.gl3; 23 24 import poodinis; 25 26 class SdlOpenglViewport : Viewport { 27 private SDL_Window* window; 28 private SDL_GLContext glContext; 29 30 this(SDL_Window* window, SDL_GLContext glContext) { 31 this.window = window; 32 this.glContext = glContext; 33 } 34 35 public @property RectangleI dimensions() { 36 int windowXPosition, windowYPosition, windowWidth, windowHeight; 37 SDL_GetWindowPosition(window, &windowXPosition, &windowYPosition); 38 SDL_GetWindowSize(window, &windowWidth, &windowHeight); 39 return RectangleI(windowXPosition, windowYPosition, windowWidth, windowHeight); 40 } 41 42 public void swapBuffers() { 43 SDL_GL_SwapWindow(window); 44 } 45 46 public void cleanup() { 47 if (glContext) { 48 SDL_GL_DeleteContext(glContext); 49 } 50 51 if (window) { 52 SDL_DestroyWindow(window); 53 } 54 } 55 } 56 57 class SdlOpenglViewportFactory : ViewportFactory { 58 59 @Autowire 60 private Sdl2WindowCreator windowCreator; 61 62 public Viewport create() { 63 auto window = windowCreator.createWindow(SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); 64 if (!window) { 65 throw new Exception("Could not create SDL2 window"); 66 } 67 68 auto glContext = SDL_GL_CreateContext(window); 69 if (!glContext) { 70 throw new Exception("Could not create OpenGL context"); 71 } 72 73 return new SdlOpenglViewport(window, glContext); 74 } 75 } 76 } 77 }