window.d 10 KB

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