1 /*
2     Copyright © 2020, Luna Nielsen
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4     
5     Authors: Luna Nielsen
6 */
7 module engine.core.window;
8 import bindbc.glfw;
9 import bindbc.opengl;
10 
11 /**
12     Static instance of the game window
13 */
14 static Window GameWindow;
15 
16 /**
17     A Window
18 */
19 class Window {
20 private:
21     GLFWwindow* window;
22     string title_;
23     int width_;
24     int height_;
25 
26     int fbWidth;
27     int fbHeight;
28 
29 public:
30 
31     /**
32         Destructor
33     */
34     ~this() {
35         glfwDestroyWindow(window);
36     }
37 
38     /**
39         Constructor
40     */
41     this(string title = "KitsuneMahjongEngine", int width = 640, int height = 480) {
42         this.title_ = title;
43         this.width_ = width;
44         this.height_ = height;
45         this.fbWidth = width;
46         this.fbHeight = height;
47 
48         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
49         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
50         glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); // To make macOS happy
51         glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
52         window = glfwCreateWindow(640, 480, this.title_.ptr, null, null);
53         
54     }
55 
56     /**
57         Hides the window
58     */
59     void hide() {
60         glfwHideWindow(window);
61     }
62 
63     /**
64         Show window
65     */
66     void show() {
67         glfwShowWindow(window);
68     }
69 
70     /**
71         Gets the title of the window
72     */
73     @property string title() {
74         return this.title_;
75     }
76 
77     /**
78         Sets the title of the window
79     */
80     @property void title(string value) {
81         this.title_ = value;
82         glfwSetWindowTitle(window, this.title_.ptr);
83     }
84 
85     /**
86         Gets the width of the window's framebuffer
87     */
88     @property int width() {
89         return this.fbWidth;
90     }
91 
92     /**
93         Gets the height of the window's framebuffer
94     */
95     @property int height() {
96         return this.fbHeight;
97     }
98 
99     /**
100         poll for new window events
101     */
102     void update() {
103         glfwPollEvents();
104         glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
105     }
106 
107     /**
108         Set the close request flag
109     */
110     void close() {
111         glfwSetWindowShouldClose(window, 1);
112     }
113 
114     /**
115         Gets whether the window has requested to close (aka the game is requested to exit)
116     */
117     bool isExitRequested() {
118         return cast(bool)glfwWindowShouldClose(window);
119     }
120 
121     /**
122         Makes the OpenGL context of the window current
123     */
124     void makeCurrent() {
125         glfwMakeContextCurrent(window);
126     }
127 
128     /**
129         Swaps the OpenGL buffers for the window
130     */
131     void swapBuffers() {
132         glfwSwapBuffers(window);
133     }
134 
135     /**
136         Sets the swap interval, by default vsync
137     */
138     void setSwapInterval(SwapInterval interval = SwapInterval.VSync) {
139         glfwSwapInterval(cast(int)interval);
140     }
141 
142     /**
143         Resets the OpenGL viewport to fit the window
144     */
145     void resetViewport() {
146         glViewport(0, 0, width, height);
147     }
148 
149     /**
150         Gets the glfw window pointer
151     */
152     GLFWwindow* winPtr() {
153         return window;
154     }
155 }
156 
157 /**
158     A swap interval
159 */
160 enum SwapInterval : int {
161     Unlimited = 0,
162     VSync = 1
163 }