window.d 10 KB

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