window.d 11 KB

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