Brenta Engine 1.0
Loading...
Searching...
No Matches
screen.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2024 Giovanni Santini
5
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#include "screen.hpp"
28
29#include "camera.hpp"
30#include "engine_audio.hpp"
31#include "engine_input.hpp"
32#include "engine_logger.hpp"
33
34#include <cstdio>
35
36using namespace Brenta;
37
38GLFWwindow *Screen::window;
41
42void Screen::Init(int SCR_WIDTH, int SCR_HEIGHT, bool isMouseCaptured,
43 const char *title, bool msaa, bool vsync)
44{
45 Screen::WIDTH = SCR_WIDTH;
46 Screen::HEIGHT = SCR_HEIGHT;
47
48 if (glfwInit() == GLFW_FALSE)
49 {
50 ERROR("Failed to initialize GLFW on init");
51 }
52
53 SetContextVersion(3, 3); /* OpenGL 3.3 */
54 UseCoreProfile();
55
56 if (msaa)
57 {
58 glfwWindowHint(GLFW_SAMPLES, 4); /* MSAA */
59 INFO("Enabled MSAA");
60 }
61
62 if (!vsync)
63 {
64 glfwSwapInterval(0); /* Disable VSync */
65 INFO("Disabled VSync");
66 }
67
68#ifdef __APPLE__
69 SetHintsApple();
70#endif
71
72 CreateWindow(SCR_WIDTH, SCR_HEIGHT, title);
73 MakeContextCurrent();
74 SetMouseCapture(isMouseCaptured);
75
76 /* Set the callback for resizing the window */
77 Screen::SetSizeCallback(Framebuffer_size_callback);
78}
79
81{
82 return glfwWindowShouldClose(Screen::window);
83}
84
86{
87 return glfwGetKey(Screen::window, key) == GLFW_PRESS;
88}
89
91{
92 return glfwGetTime();
93}
94
95GLFWwindow *Screen::GetWindow()
96{
97 return Screen::window;
98}
99
101{
102 return reinterpret_cast<void (*)()>(glfwGetProcAddress);
103}
104
106{
107 return Screen::WIDTH;
108}
109
111{
112 return Screen::HEIGHT;
113}
114
115void Screen::SetMouseCallback(GLFWcursorposfun callback)
116{
117 glfwSetCursorPosCallback(Screen::window, callback);
118}
119
120void Screen::SetSizeCallback(GLFWframebuffersizefun callback)
121{
122 glfwSetFramebufferSizeCallback(Screen::window, callback);
123
124 INFO("Set framebuffer size callback");
125}
126
127void Screen::SetMouseCapture(bool isCaptured)
128{
129 if (isCaptured)
130 {
131 glfwSetInputMode(Screen::window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
132
133 INFO("Mouse captured");
134 }
135 else
136 {
137 glfwSetInputMode(Screen::window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
138 INFO("Mouse not captured");
139 }
140}
141
143{
144 glfwSetWindowShouldClose(Screen::window, GLFW_TRUE);
145}
146
148{
149 glfwTerminate();
150 INFO("Screen terminated");
151}
152
154{
155 glfwSwapBuffers(Screen::window);
156}
157
159{
160 glfwPollEvents();
161}
162
163void Screen::SetContextVersion(int major, int minor)
164{
165 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
166 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
167
168 INFO("Set context to OpenGL version: ", major, ".", minor);
169}
170
171void Screen::SetKeyCallback(GLFWkeyfun callback)
172{
173 glfwSetKeyCallback(Screen::window, callback);
174}
175
176void Screen::SetMousePosCallback(GLFWcursorposfun callback)
177{
178 glfwSetCursorPosCallback(Screen::GetWindow(), callback);
179}
180
181void Screen::UseCoreProfile()
182{
183 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
184
185 INFO("Set OpenGL profile to core");
186}
187
188void Screen::SetHintsApple()
189{
190 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
191}
192
193void Screen::CreateWindow(int SCR_WIDTH, int SCR_HEIGHT, const char *title)
194{
195 Screen::window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, title, NULL, NULL);
196 if (Screen::window == NULL)
197 {
198 ERROR("Failed to create GLFW window");
199 Terminate();
200 }
201}
202
203void Screen::MakeContextCurrent()
204{
205 glfwMakeContextCurrent(Screen::window);
206}
207
208void Screen::Framebuffer_size_callback(GLFWwindow *window, int width,
209 int height)
210{
211 glViewport(0, 0, width, height);
212 Screen::WIDTH = width;
213 Screen::HEIGHT = height;
214
215 INFO("Set viewport: ", width, "x", height);
216}
static void SetClose()
Set the window close flag.
Definition screen.cpp:142
static int GetHeight()
Get the height of the window.
Definition screen.cpp:110
static GLFWglproc GetProcAddress()
Get the OpenGL function pointer.
Definition screen.cpp:100
static void SetMouseCapture(bool isCaptured)
Set the mouse capture.
Definition screen.cpp:127
static int HEIGHT
Height of the window.
Definition screen.hpp:57
static void SetMousePosCallback(GLFWcursorposfun callback)
Set the mouse position callback.
Definition screen.cpp:176
static void SetMouseCallback(GLFWcursorposfun callback)
Set the mouse callback.
Definition screen.cpp:115
static int GetWidth()
Get the width of the window.
Definition screen.cpp:105
static bool isWindowClosed()
Check if the window is closed.
Definition screen.cpp:80
static void PollEvents()
Poll all pending events.
Definition screen.cpp:158
static GLFWwindow * GetWindow()
Get the window.
Definition screen.cpp:95
static GLFWwindow * window
Pointer to the window.
Definition screen.hpp:61
static float GetTime()
Get the time.
Definition screen.cpp:90
static void SetSizeCallback(GLFWframebuffersizefun callback)
Set the key callback.
Definition screen.cpp:120
static void Init(int SCR_WIDTH, int SCR_HEIGHT, bool isMouseCaptured=false, const char *title="OpenGL", bool msaa=false, bool vsync=false)
Initialize the window.
Definition screen.cpp:42
static void Terminate()
Terminate the window.
Definition screen.cpp:147
static bool isKeyPressed(int key)
Check if a key is pressed.
Definition screen.cpp:85
static void SetKeyCallback(GLFWkeyfun callback)
Set the key callback.
Definition screen.cpp:171
static void SwapBuffers()
Swap the front and back buffers.
Definition screen.cpp:153
static int WIDTH
Width of the window.
Definition screen.hpp:53