window.d 10 KB

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