VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
Window.cpp
1#include "Window.hpp"
2
3namespace vex {
4 Window::Window(std::string title, int resx, int resy){
5 log("Initializing SDL...");
6
7 #ifdef __linux__
8 SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "wayland,x11");
9 #endif
10
11 if (!SDL_Init(SDL_INIT_VIDEO)) {
12 const char* error = SDL_GetError();
13 log("SDL_Init failed: %s", error);
14 throw_error(error);
15 }
16
17 const char* driver = SDL_GetCurrentVideoDriver();
18 log("Active Video Driver: %s", driver ? driver : "Unknown");
19
20 log("Creating window with resolution: %i X %i", resx, resy);
21
22 log("Creating window...");
23 window = SDL_CreateWindow(
24 title.c_str(),
25 resx,
26 resy,
27 SDL_WINDOW_VULKAN | SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_RESIZABLE
28 );
29 if (!window) {
30 throw_error(SDL_GetError());
31 }
32
33 if (window == NULL) {
34 log(LogLevel::ERROR, "Could not create window: %s", SDL_GetError());
35 return;
36 }
37 }
38
39 Window::~Window(){
40 SDL_DestroyWindow(window);
41 SDL_Quit();
42
43 std::cout << "Window closed" << std::endl;
44 }
45
46 SDL_Window* Window::GetSDLWindow(){
47 return window;
48 }
49
50 void Window::setFullscreen(bool enabled, bool exclusive) {
51 if (m_cachedFullscreenState == enabled && m_cachedExclusiveMode == exclusive) {
52 return;
53 }
54
55 Uint64 currentTime = SDL_GetPerformanceCounter();
56 Uint64 timeSinceLastChangeMS = (currentTime - m_lastFullscreenChangeTime) / (SDL_GetPerformanceFrequency() / 1000);
57
58 if (timeSinceLastChangeMS < FULLSCREEN_DEBOUNCE_MS) {
59 log(LogLevel::WARNING, "Fullscreen toggle requested too rapidly (debounced). Last toggle was %lld ms ago, minimum interval is %llu ms.",
60 timeSinceLastChangeMS, FULLSCREEN_DEBOUNCE_MS);
61 return;
62 }
63
64 m_lastFullscreenChangeTime = currentTime;
65
66 log("Attempting fullscreen state change: %s (mode: %s)",
67 enabled ? "ENTER fullscreen" : "EXIT fullscreen",
68 exclusive ? "exclusive" : "borderless windowed");
69
70 if (enabled) {
71 if (exclusive) {
72 const SDL_DisplayMode* mode = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(window));
73 if (mode) {
74 SDL_SetWindowFullscreenMode(window, mode);
75 log("Set exclusive fullscreen mode: %ux%u @ %.1f Hz",
76 mode->w, mode->h, mode->refresh_rate);
77 } else {
78 log(LogLevel::WARNING, "Could not query current display mode for exclusive fullscreen");
79 }
80 } else {
81 SDL_SetWindowFullscreenMode(window, NULL);
82 log("Set borderless windowed fullscreen mode");
83 }
84
85 if (!SDL_SetWindowFullscreen(window, true)) {
86 log(LogLevel::ERROR, "Failed to enter fullscreen: %s", SDL_GetError());
87 return;
88 }
89 log("Successfully entered fullscreen mode");
90 } else {
91 if (!SDL_SetWindowFullscreen(window, false)) {
92 log(LogLevel::ERROR, "Failed to exit fullscreen: %s", SDL_GetError());
93 return;
94 }
95 log("Successfully exited fullscreen mode");
96 }
97
99 m_cachedExclusiveMode = exclusive;
100 }
101
103 return (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0;
104 }
105
107 SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
108 if (displayID == 0) {
109 log(LogLevel::ERROR, "Could not get display for window: %s", SDL_GetError());
110 return 60.0f;
111 }
112
113 const SDL_DisplayMode* mode = SDL_GetCurrentDisplayMode(displayID);
114 if (mode) {
115 return mode->refresh_rate > 0.0f ? mode->refresh_rate : 60.0f;
116 }
117
118 return 60.0f;
119 }
120}
This file defines Window class.
bool isFullscreen()
Getter for fullscreen mode.
Definition Window.cpp:102
bool m_cachedExclusiveMode
Cached exclusive mode setting.
Definition Window.hpp:26
float getRefreshRate()
Gets the refresh rate of the display the window is currently on.
Definition Window.cpp:106
void setFullscreen(bool enabled, bool exclusive=false)
Setter for fullscreen mode.
Definition Window.cpp:50
SDL_Window * GetSDLWindow()
Getter for SDL_Window pointer.
Definition Window.cpp:46
static constexpr Uint64 FULLSCREEN_DEBOUNCE_MS
Debounce interval in milliseconds to prevent rapid fullscreen toggles.
Definition Window.hpp:30
Window(std::string title, int resx=480, int resy=640)
Constructor for Window class.
Definition Window.cpp:4
Uint64 m_lastFullscreenChangeTime
Timestamp of last fullscreen state change (in SDL performance counter units).
Definition Window.hpp:28
bool m_cachedFullscreenState
Cached fullscreen state to prevent redundant SDL calls.
Definition Window.hpp:24
void VEX_EXPORT throw_error(const std::string &msg)
Throws an error or terminates the application.
void VEX_EXPORT log(const char *fmt,...)
Logs a formatted message.