1
1

window.d 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. module three.window;
  2. public import derelict.opengl3.gl3;
  3. public import derelict.glfw3.glfw3;
  4. public import std.experimental.logger;
  5. struct Window {
  6. private:
  7. GLFWwindow* _glfwWindow = null;
  8. uint _x, _y, _width, _height;
  9. KeyAction[int] _keyStates;
  10. ButtonAction[int] _buttonStates;
  11. public:
  12. void construct(string title, uint width, uint height) nothrow {
  13. this._x = 0;
  14. this._y = 0;
  15. this._width = width;
  16. this._height = height;
  17. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  18. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  19. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
  20. glfwDefaultWindowHints();
  21. glfwWindowHint(GLFW_RED_BITS, 8);
  22. glfwWindowHint(GLFW_GREEN_BITS, 8);
  23. glfwWindowHint(GLFW_BLUE_BITS, 8);
  24. glfwWindowHint(GLFW_ALPHA_BITS, 0);
  25. glfwWindowHint(GLFW_DEPTH_BITS, 24);
  26. glfwWindowHint(GLFW_STENCIL_BITS, 8);
  27. import std.string : toStringz;
  28. this._glfwWindow = glfwCreateWindow(width, height, title.toStringz(), null, null);
  29. assert(this._glfwWindow !is null);
  30. glfwSetWindowUserPointer(this._glfwWindow, cast(void*)&this);
  31. glfwSetWindowPosCallback(this._glfwWindow, cast(GLFWwindowposfun)&_GLFWwindowposfun);
  32. glfwSetWindowSizeCallback(this._glfwWindow, cast(GLFWwindowsizefun)&_GLFWwindowsizefun);
  33. glfwSetWindowCloseCallback(this._glfwWindow, cast(GLFWwindowclosefun)&_GLFWwindowclosefun);
  34. glfwSetWindowRefreshCallback(this._glfwWindow, cast(GLFWwindowrefreshfun)&_GLFWwindowrefreshfun);
  35. glfwSetWindowIconifyCallback(this._glfwWindow, cast(GLFWwindowiconifyfun)&_GLFWwindowiconifyfun);
  36. glfwSetMouseButtonCallback(this._glfwWindow, cast(GLFWmousebuttonfun)&_GLFWmousebuttonfun);
  37. glfwSetCursorPosCallback(this._glfwWindow, cast(GLFWcursorposfun)&_GLFWcursorposfun);
  38. //glfwSetCursorEnterCallback(this._glfwWindow, cast(GLFWcursorenterfunfun)&_GLFWcursorenterfunfun);
  39. glfwSetScrollCallback(this._glfwWindow, cast(GLFWscrollfun)&_GLFWscrollfun);
  40. glfwSetKeyCallback(this._glfwWindow, cast(GLFWkeyfun)&_GLFWkeyfun);
  41. glfwSetCharCallback(this._glfwWindow, cast(GLFWcharfun)&_GLFWcharfun);
  42. glfwMakeContextCurrent(this._glfwWindow);
  43. }
  44. void destruct() {
  45. // glfwSetWindowPosCallback(this._glfwWindow, null);
  46. // glfwSetWindowSizeCallback(this._glfwWindow, null);
  47. // glfwSetWindowCloseCallback(this._glfwWindow, null);
  48. // glfwSetWindowRefreshCallback(this._glfwWindow, null);
  49. // glfwSetWindowIconifyCallback(this._glfwWindow, null);
  50. // glfwSetMouseButtonCallback(this._glfwWindow, null);
  51. // glfwSetCursorPosCallback(this._glfwWindow, null);
  52. // //glfwSetCursorEnterCallback(this._glfwWindow, null);
  53. // glfwSetScrollCallback(this._glfwWindow, null);
  54. // glfwSetKeyCallback(this._glfwWindow, null);
  55. // glfwSetCharCallback(this._glfwWindow, null);
  56. glfwDestroyWindow(this._glfwWindow);
  57. }
  58. void makeAktiveRenderWindow() nothrow @nogc {
  59. glfwMakeContextCurrent(this._glfwWindow);
  60. }
  61. void swapBuffers() nothrow @nogc {
  62. glfwSwapBuffers(this._glfwWindow);
  63. }
  64. void pollEvents() nothrow @nogc {
  65. glfwPollEvents();
  66. }
  67. @property void setTitle(string title) nothrow {
  68. import std.string : toStringz;
  69. glfwSetWindowTitle(this._glfwWindow, title.toStringz());
  70. }
  71. KeyAction keyState(int key) pure @safe nothrow {
  72. try {
  73. return this._keyStates.get(key, KeyAction.Released);
  74. }
  75. catch(Exception) {
  76. return KeyAction.Released;
  77. }
  78. }
  79. ButtonAction buttonState(int button) pure @safe nothrow {
  80. try {
  81. return this._buttonStates.get(button, ButtonAction.Released);
  82. }
  83. catch(Exception) {
  84. return ButtonAction.Released;
  85. }
  86. }
  87. uint x() {
  88. return this._x;
  89. }
  90. uint y() {
  91. return this._y;
  92. }
  93. uint width() {
  94. return this._width;
  95. }
  96. uint height() {
  97. return this._height;
  98. }
  99. public:
  100. void delegate(ref Window, int, int) onPosition;
  101. void delegate(ref Window, int, int) onSize;
  102. void delegate(ref Window) onClose;
  103. void delegate(ref Window) onRefresh;
  104. void delegate(ref Window, FocusAction) onFocus;
  105. void delegate(ref Window, IconifyAction) onIconify;
  106. void delegate(ref Window, bool) onCursorEnter;
  107. void delegate(ref Window, int, ButtonAction) onButton;
  108. void delegate(ref Window, double, double) onCursorPos;
  109. void delegate(ref Window, double, double) onScroll;
  110. void delegate(ref Window, Key, ScanCode, KeyAction, KeyMod) onKey;
  111. void delegate(ref Window, int) onChar;
  112. }
  113. alias ScanCode = int;
  114. enum IconifyAction {
  115. Iconified,
  116. Restored
  117. }
  118. enum FocusAction {
  119. Focused,
  120. Defocused
  121. }
  122. enum CursorAction {
  123. Entered,
  124. Leaved
  125. }
  126. enum ButtonAction {
  127. Pressed,
  128. Released
  129. }
  130. enum KeyAction {
  131. Pressed,
  132. Released,
  133. Repeated
  134. }
  135. enum KeyMod {
  136. Shift = 0x0001,
  137. Control = 0x0002,
  138. Alt = 0x0004,
  139. Super = 0x0008,
  140. }
  141. enum Key {
  142. Unknown = -1,
  143. Space = 32,
  144. Apostrophe = 39,
  145. Comma = 44,
  146. Minus = 45,
  147. Period = 46,
  148. Slash = 47,
  149. Key0 = 48,
  150. Key1 = 49,
  151. Key2 = 50,
  152. Key3 = 51,
  153. Key4 = 52,
  154. Key5 = 53,
  155. Key6 = 54,
  156. Key7 = 55,
  157. Key8 = 56,
  158. Key9 = 57,
  159. Semicolon = 59,
  160. Equal = 61,
  161. KeyA = 65,
  162. KeyB = 66,
  163. KeyC = 67,
  164. KeyD = 68,
  165. KeyE = 69,
  166. KeyF = 70,
  167. KeyG = 71,
  168. KeyH = 72,
  169. KeyI = 73,
  170. KeyJ = 74,
  171. KeyK = 75,
  172. Keyl = 76,
  173. KeyM = 77,
  174. KeyN = 78,
  175. KeyO = 79,
  176. KeyP = 80,
  177. KeyQ = 81,
  178. KeyR = 82,
  179. KeyS = 83,
  180. KeyT = 84,
  181. KeyU = 85,
  182. KeyV = 86,
  183. KeyW = 87,
  184. KeyX = 88,
  185. KeyY = 89,
  186. KeyZ = 90,
  187. LeftBracket = 91,
  188. Backslash = 92,
  189. RightBracket = 93,
  190. GraveAccent = 96,
  191. World1 = 161,
  192. World2 = 162,
  193. Escape = 256,
  194. Enter = 257,
  195. Tab = 258,
  196. Backspace = 259,
  197. Insert = 260,
  198. Delete = 261,
  199. Right = 262,
  200. Left = 263,
  201. Down = 264,
  202. Up = 265,
  203. PageUp = 266,
  204. PageDown = 267,
  205. Home = 268,
  206. End = 269,
  207. CapsLock = 280,
  208. ScrollLock = 281,
  209. NumLock = 282,
  210. PrintScreen = 283,
  211. Pause = 284,
  212. F1 = 290,
  213. F2 = 291,
  214. F3 = 292,
  215. F4 = 293,
  216. F5 = 294,
  217. F6 = 295,
  218. F7 = 296,
  219. F8 = 297,
  220. F9 = 298,
  221. F10 = 299,
  222. F11 = 300,
  223. F12 = 301,
  224. F13 = 302,
  225. F14 = 303,
  226. F15 = 304,
  227. F16 = 305,
  228. F17 = 306,
  229. F18 = 307,
  230. F19 = 308,
  231. F20 = 309,
  232. F21 = 310,
  233. F22 = 311,
  234. F23 = 312,
  235. F24 = 313,
  236. F25 = 314,
  237. NumBlock0 = 320,
  238. NumBlock1 = 321,
  239. NumBlock2 = 322,
  240. NumBlock3 = 323,
  241. NumBlock4 = 324,
  242. NumBlock5 = 325,
  243. NumBlock6 = 326,
  244. NumBlock7 = 327,
  245. NumBlock8 = 328,
  246. NumBlock9 = 329,
  247. KpDecimal = 330,
  248. KpDivide = 331,
  249. KpMultiply = 332,
  250. KpSubtract = 333,
  251. KpAdd = 334,
  252. KpEnter = 335,
  253. KpEqual = 336,
  254. LeftShift = 340,
  255. LeftControl = 341,
  256. LeftAlt = 342,
  257. LeftSuper = 343,
  258. RightShift = 344,
  259. RightControl = 345,
  260. RightAlt = 346,
  261. RightSuper = 347,
  262. Menu = 348
  263. }
  264. private Window* _castWindow(GLFWwindow* window)
  265. out (result) { assert(result !is null, "glfwGetWindowUserPointer returned null"); }
  266. body {
  267. void* user_ptr = glfwGetWindowUserPointer(window);
  268. return cast(Window*)user_ptr;
  269. }
  270. private extern(C) void _GLFWwindowposfun(GLFWwindow* glfwWindow, int x, int y) {
  271. Window* window = _castWindow(glfwWindow);
  272. auto monitor = glfwGetPrimaryMonitor();
  273. if(monitor is null) return;
  274. auto videoMode = glfwGetVideoMode(monitor);
  275. y = videoMode.height - y;
  276. int w,h;
  277. glfwGetWindowSize(glfwWindow, &w, &h);
  278. window._x = x;
  279. window._y = y;
  280. window._width = w;
  281. window._height = h;
  282. if(window.onPosition) window.onPosition(*window, x, y);
  283. }
  284. private extern(C) void _GLFWwindowsizefun(GLFWwindow* glfwWindow, int width, int height) {
  285. Window* window = _castWindow(glfwWindow);
  286. window._width = width;
  287. window._height = height;
  288. if(window.onSize) window.onSize(*window, width, height);
  289. }
  290. private extern(C) void _GLFWwindowclosefun(GLFWwindow* glfwWindow) {
  291. Window* window = _castWindow(glfwWindow);
  292. if(window.onClose) window.onClose(*window);
  293. }
  294. private extern(C) void _GLFWwindowrefreshfun(GLFWwindow* glfwWindow) {
  295. Window* window = _castWindow(glfwWindow);
  296. if(window.onRefresh) window.onRefresh(*window);
  297. }
  298. private extern(C) void _GLFWwindowfocusfun(GLFWwindow* glfwWindow, int focused) {
  299. Window* window = _castWindow(glfwWindow);
  300. if(window.onFocus) window.onFocus(*window, (focused == GL_TRUE) ? FocusAction.Focused : FocusAction.Defocused);
  301. }
  302. private extern(C) void _GLFWwindowiconifyfun(GLFWwindow* glfwWindow, int iconified) {
  303. Window* window = _castWindow(glfwWindow);
  304. if(window.onIconify) window.onIconify(*window, (iconified == GL_TRUE) ? IconifyAction.Iconified : IconifyAction.Restored);
  305. }
  306. private extern(C) void _GLFWcursorenterfun(GLFWwindow* glfwWindow, int entered) {
  307. Window* window = _castWindow(glfwWindow);
  308. if(window.onCursorEnter) window.onCursorEnter(*window, (entered == GL_TRUE) ? CursorAction.Entered : CursorAction.Leaved);
  309. }
  310. private extern(C) void _GLFWmousebuttonfun(GLFWwindow* glfwWindow, int button, int action) {
  311. Window* window = _castWindow(glfwWindow);
  312. window._buttonStates[button] = (action == GLFW_PRESS) ? ButtonAction.Pressed : ButtonAction.Released;
  313. if(window.onButton) window.onButton(*window, button, (action == GLFW_PRESS) ? ButtonAction.Pressed : ButtonAction.Released);
  314. }
  315. private extern(C) void _GLFWcursorposfun(GLFWwindow* glfwWindow, double x, double y) {
  316. Window* window = _castWindow(glfwWindow);
  317. if(window.onCursorPos) window.onCursorPos(*window, x, window._height - y);
  318. }
  319. private extern(C) void _GLFWscrollfun(GLFWwindow* glfwWindow, double x, double y) {
  320. Window* window = _castWindow(glfwWindow);
  321. if(window.onScroll) window.onScroll(*window, x, y);
  322. }
  323. private extern(C) void _GLFWkeyfun(GLFWwindow* glfwWindow, int key, int scancode, int action, int mods) {
  324. Window* window = _castWindow(glfwWindow);
  325. window._keyStates[key] = (action == GLFW_PRESS || action == GLFW_REPEAT) ? KeyAction.Pressed : KeyAction.Released;
  326. if(window.onKey) window.onKey(*window, cast(Key)key, cast(ScanCode)scancode, cast(KeyAction)action, cast(KeyMod)mods);
  327. }
  328. private extern(C) void _GLFWcharfun(GLFWwindow* glfwWindow, uint character) {
  329. Window* window = _castWindow(glfwWindow);
  330. if(window.onChar) window.onChar(*window, character);
  331. }