TerraForge3D  2.3.1
3D Terrain And Landscape Generator
Window.cpp
1#include <Window.h>
2
3#include <GLFW/glfw3.h>
4
5#include <iostream>
6
7static bool isGLFWInitialized = false;
8
9static void GLFWErrorCallback(int error, const char *description)
10{
11 fprintf(stderr, "GLFW Error: %d: %s\n", error, description);
12}
13
14static void InitGLFW()
15{
16 if (isGLFWInitialized)
17 {
18 return;
19 }
20
21 if (!glfwInit())
22 {
23 std::cout << "Error in initializeing GLFW!" << std::endl;
24 exit(-1);
25 }
26
27 glfwSetErrorCallback(GLFWErrorCallback);
28 glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE);
29 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
30 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
31 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
32 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
33 isGLFWInitialized = true;
34}
35
36
37Window::Window(std::string title = "Window")
38{
39 InitGLFW();
40 m_Window = glfwCreateWindow(640, 480, title.c_str(), NULL, NULL);
41
42 if (!m_Window)
43 {
44 glfwTerminate();
45 std::cout << "Error in creating window!" << std::endl;
46 exit(-1);
47 }
48
49 isActive = true;
50 glfwMakeContextCurrent(m_Window);
51}
52
53void Window::SetVSync(bool enabled)
54{
55 if (enabled == vSyncState)
56 {
57 return;
58 }
59
60 if (enabled)
61 {
62 glfwSwapInterval(1);
63 vSyncState = enabled;
64 }
65
66 else
67 {
68 glfwSwapInterval(0);
69 vSyncState = enabled;
70 }
71}
72
73void Window::Clear()
74{
75 glClearColor(m_ClearColor.r, m_ClearColor.g, m_ClearColor.b, 1.0);
76 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
77}
78
79void Window::SetFullScreen(bool fullscreen)
80{
81 if (isFullscreen == fullscreen)
82 {
83 return;
84 }
85
86 static int x, y;
87 static int sx, sy;
88 isFullscreen = fullscreen;
89
90 if (fullscreen)
91 {
92 // backup window position and window size
93 glfwGetWindowPos(m_Window, &x, &y);
94 glfwGetWindowSize(m_Window, &sx, &sy);
95 // get resolution of monitor
96 const GLFWvidmode *mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
97 // switch to full screen
98 glfwSetWindowMonitor(m_Window, glfwGetPrimaryMonitor(), 0, 0, mode->width, mode->height, 0);
99 }
100
101 else
102 {
103 // restore last window size and position
104 glfwSetWindowMonitor(m_Window, nullptr, x, y, sx, sy, 0);
105 }
106}
107
108void Window::SetVisible(bool visibility)
109{
110 if (visibility)
111 {
112 glfwShowWindow(m_Window);
113 }
114
115 else
116 {
117 glfwHideWindow(m_Window);
118 }
119}
120
121void Window::Update()
122{
123 if (!isActive)
124 {
125 return;
126 }
127
128 if (glfwWindowShouldClose(m_Window))
129 {
130 if(m_CloseEventCallback)
131 {
132 m_CloseEventCallback(0, 0);
133 }
134
135 isActive = false;
136 }
137
138 glfwSwapBuffers(m_Window);
139 glfwPollEvents();
140}
141
142void Window::SetMouseCallback(EventFn callback)
143{
144 m_MouseEventCallback = callback;
145}
146
147void Window::SetResizeCallback(EventFn callback)
148{
149 m_ResizeEventCallback = callback;
150}
151
152void Window::SetShouldCloseCallback(EventFn callback)
153{
154 m_CloseEventCallback = callback;
155}
156
157void Window::SetClearColor(ClearColor color)
158{
159 m_ClearColor = color;
160}
161
162void Window::MakeCurrentContext()
163{
164 if (m_Window)
165 {
166 glfwMakeContextCurrent(m_Window);
167 }
168}
169
170Window::~Window()
171{
172 Close();
173 std::cout << "Destroying Window!" << std::endl;
174}
175
176void Window::Close()
177{
178 if (m_Window)
179 {
180 glfwDestroyWindow(m_Window);
181 }
182}