1 /* 2 Copyright (c) 2013 Andrey Penechko 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 anchovy.graphics.windows.sdlwindow; 30 31 private{ 32 import std.stdio; 33 34 import derelict.sdl2.sdl; 35 import derelict.sdl2.image; 36 import derelict.opengl3.gl3; 37 38 import anchovy.core.interfaces.iwindow; 39 40 import anchovy.utils.string; 41 } 42 /* 43 class SDLWindow : IWindow 44 { 45 override 46 void init(in uint width, in uint height, in string caption) 47 { 48 w = width; h = height; 49 50 DerelictSDL2.load(); 51 DerelictGL3.load(); 52 DerelictSDL2Image.load(); 53 54 if(SDL_Init(SDL_INIT_VIDEO) < 0) 55 { 56 throw new Error("Error initializing SDL"); 57 } 58 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 59 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); 60 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 61 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); 62 63 window=SDL_CreateWindow(caption.ptr, SDL_WINDOWPOS_CENTERED, 64 SDL_WINDOWPOS_CENTERED, w, h, _flags); 65 66 if(!window) 67 { 68 throw new Error("Error creating SDL window"); 69 } 70 71 _context=SDL_GL_CreateContext(window); 72 SDL_GL_SetSwapInterval(1); 73 74 glClearColor(1.0, 1.0, 1.0, 1.0); 75 glViewport(0, 0, w, h); 76 77 DerelictGL3.reload(); 78 } 79 80 override 81 void reshape(in uint width, in uint height) 82 { 83 glViewport(0, 0, cast(GLsizei) width, cast(GLsizei) height); 84 } 85 86 override 87 void releaseWindow() 88 { 89 SDL_GL_DeleteContext(_context); 90 SDL_DestroyWindow(window); 91 SDL_Quit(); 92 } 93 94 override 95 void setMousePosition(in int x, in int y) 96 { 97 SDL_WarpMouseInWindow(window, x, y); 98 } 99 100 override 101 ivec2 getMousePosition() 102 { 103 int x, y; 104 SDL_GetMouseState(&x, &y); 105 return ivec2(x, y); 106 } 107 108 override 109 void swapBuffer(){ 110 SDL_GL_SwapWindow(window); 111 } 112 113 override 114 void grabMouse(){} 115 116 override 117 void releaseMouse(){} 118 119 override 120 ivec2 getSize() 121 { 122 int width, height; 123 SDL_GetWindowSize(window, &width, &height); 124 return ivec2(width, height); 125 } 126 127 override void processEvents() 128 { 129 assert(false); 130 } 131 132 override double getTime() 133 { 134 return cast(double)SDL_GetTicks() / 1000; 135 } 136 137 override bool isKeyPressed(uint key) 138 { 139 assert(false); 140 } 141 142 @property{ 143 override 144 uint width(){ return w; } 145 146 override 147 uint width(in uint newWidth){ return w=newWidth;} 148 149 override 150 uint height(){ return h; } 151 152 override 153 uint height(in uint newHeight){ return h=newHeight;} 154 } 155 156 override string getClipboard() 157 { 158 const(char*) data = SDL_GetClipboardText(); 159 if (data is null) return ""; 160 return ZToString(data); 161 } 162 override void setClipboard(string newClipboardString) 163 { 164 assert(false); 165 } 166 167 private: 168 SDL_Window *window; 169 SDL_GLContext _context; 170 int w = 800, h = 600; 171 int _flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE; 172 173 bool _mouseGrabbed = false; 174 } 175 */