1
1

window.d 10 KB

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