Brenta Engine 1.1
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 is_mouse_captured,
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 set_context_version(3, 3); /* OpenGL 3.3 */
54 use_core_profile();
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 set_hints_apple();
70#endif
71
72 create_window(SCR_WIDTH, SCR_HEIGHT, title);
73 make_context_current();
74 set_mouse_capture(is_mouse_captured);
75
76 /* Set the callback for resizing the window */
77 screen::set_size_callback(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::get_window()
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::set_mouse_callback(GLFWcursorposfun callback)
116{
117 glfwSetCursorPosCallback(screen::window, callback);
118}
119
120void screen::set_size_callback(GLFWframebuffersizefun callback)
121{
122 glfwSetFramebufferSizeCallback(screen::window, callback);
123
124 INFO("Set framebuffer size callback");
125}
126
127void screen::set_mouse_capture(bool is_captured)
128{
129 if (is_captured)
130 {
131 glfwSetInputMode(screen::window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
132 INFO("Mouse captured");
133 }
134 else
135 {
136 glfwSetInputMode(screen::window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
137 INFO("Mouse not captured");
138 }
139}
140
142{
143 glfwSetWindowShouldClose(screen::window, GLFW_TRUE);
144}
145
147{
148 INFO("Terminating screen");
149 glfwDestroyWindow(screen::window);
150 glfwTerminate();
151 INFO("screen terminated");
152}
153
155{
156 glfwSwapBuffers(screen::window);
157}
158
160{
161 glfwPollEvents();
162}
163
164void screen::set_context_version(int major, int minor)
165{
166 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
167 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
168
169 INFO("Set context to OpenGL version: {}.{}", major, minor);
170}
171
172void screen::set_key_callback(GLFWkeyfun callback)
173{
174 glfwSetKeyCallback(screen::window, callback);
175}
176
177void screen::set_mouse_pos_callback(GLFWcursorposfun callback)
178{
179 glfwSetCursorPosCallback(screen::get_window(), callback);
180}
181
182void screen::use_core_profile()
183{
184 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
185
186 INFO("Set OpenGL profile to core");
187}
188
189void screen::set_hints_apple()
190{
191 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
192}
193
194void screen::create_window(int SCR_WIDTH, int SCR_HEIGHT, const char *title)
195{
196 screen::window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, title, NULL, NULL);
197 if (screen::window == NULL)
198 {
199 ERROR("Failed to create GLFW window");
200 terminate();
201 }
202}
203
204void screen::make_context_current()
205{
206 glfwMakeContextCurrent(screen::window);
207}
208
209void screen::framebuffer_size_callback(GLFWwindow *window, int width,
210 int height)
211{
212#ifndef USE_IMGUI
213 glViewport(0, 0, width, height);
214 screen::WIDTH = width;
215 screen::HEIGHT = height;
216#endif
217}
static int WIDTH
Width of the window.
Definition screen.hpp:53
static void poll_events()
Poll all pending events.
Definition screen.cpp:159
static void set_key_callback(GLFWkeyfun callback)
Set the key callback.
Definition screen.cpp:172
static GLFWwindow * get_window()
Get the window.
Definition screen.cpp:95
static void terminate()
Terminate the window.
Definition screen.cpp:146
static void set_close()
Set the window close flag.
Definition screen.cpp:141
static GLFWglproc get_proc_address()
Get the OpenGL function pointer.
Definition screen.cpp:100
static void init(int SCR_WIDTH, int SCR_HEIGHT, bool is_mouse_captured=false, const char *title="OpenGL", bool msaa=false, bool vsync=false)
Initialize the window.
Definition screen.cpp:42
static void set_mouse_capture(bool is_captured)
Set the mouse capture.
Definition screen.cpp:127
static void set_mouse_pos_callback(GLFWcursorposfun callback)
Set the mouse position callback.
Definition screen.cpp:177
static int get_width()
Get the width of the window.
Definition screen.cpp:105
static GLFWwindow * window
Pointer to the window.
Definition screen.hpp:61
static void set_mouse_callback(GLFWcursorposfun callback)
Set the mouse callback.
Definition screen.cpp:115
static int HEIGHT
Height of the window.
Definition screen.hpp:57
static void set_size_callback(GLFWframebuffersizefun callback)
Set the key callback.
Definition screen.cpp:120
static float get_time()
Get the time.
Definition screen.cpp:90
static bool is_window_closed()
Check if the window is closed.
Definition screen.cpp:80
static bool is_key_pressed(int key)
Check if a key is pressed.
Definition screen.cpp:85
static int get_height()
Get the height of the window.
Definition screen.cpp:110
static void swap_buffers()
Swap the front and back buffers.
Definition screen.cpp:154