1
1

window.d 10 KB

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