VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
ResolutionManager.cpp
1// resolution.cpp
3#include "Window.hpp"
4
5namespace vex {
6
7ResolutionManager::ResolutionManager(SDL_Window* window) : m_p_window(window) {
8 update();
9}
10
12 m_currentMode = mode;
13 update();
14}
15
17 int width, height;
18 SDL_GetWindowSizeInPixels(m_p_window, &width, &height);
19
20 static constexpr uint32_t MIN_DIMENSION = 64;
21 static constexpr uint32_t MAX_DIMENSION = 32768;
22
23 if (width <= 0 || height <= 0) {
24 log(LogLevel::WARNING, "ResolutionManager::update() called with invalid window dimensions: %dx%d. Skipping update.",
25 width, height);
26 return;
27 }
28
29 if (width < MIN_DIMENSION || height < MIN_DIMENSION) {
30 log(LogLevel::WARNING, "Window resolution %dx%d is below minimum %u. Clamping.",
31 width, height, MIN_DIMENSION);
32 width = std::max(width, (int)MIN_DIMENSION);
33 height = std::max(height, (int)MIN_DIMENSION);
34 }
35
36 if (width > MAX_DIMENSION || height > MAX_DIMENSION) {
37 log(LogLevel::WARNING, "Window resolution %dx%d exceeds maximum %u. Clamping.",
38 width, height, MAX_DIMENSION);
39 width = std::min(width, (int)MAX_DIMENSION);
40 height = std::min(height, (int)MAX_DIMENSION);
41 }
42
43 m_windowResolution = {static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
44
45 switch(m_currentMode) {
47 m_renderResolution = m_windowResolution;
48 m_upscaleRatio = 1.0f;
49 break;
50
52 if (height <= 0) {
53 log(LogLevel::ERROR, "Invalid height for RES_240P calculation: %d", height);
54 m_renderResolution = m_windowResolution;
55 m_upscaleRatio = 1.0f;
56 break;
57 }
58 float aspect = width / static_cast<float>(height);
59 m_renderResolution.y = 240;
60 uint32_t calculatedWidth = static_cast<uint32_t>(240 * aspect);
61 m_renderResolution.x = std::clamp(calculatedWidth, 64u, 32768u);
62 m_upscaleRatio = std::clamp(height / 240.0f, 0.5f, 136.5f);
63 break;
64 }
65
67 if (height <= 0) {
68 log(LogLevel::ERROR, "Invalid height for RES_480P calculation: %d", height);
69 m_renderResolution = m_windowResolution;
70 m_upscaleRatio = 1.0f;
71 break;
72 }
73 float aspect = width / static_cast<float>(height);
74 m_renderResolution.y = 480;
75 uint32_t calculatedWidth = static_cast<uint32_t>(480 * aspect);
76 m_renderResolution.x = std::clamp(calculatedWidth, 64u, 32768u);
77 m_upscaleRatio = std::clamp(height / 480.0f, 0.25f, 68.27f);
78 break;
79 }
80
83 break;
84 }
85
86 log("Resolution mode updated: %dx%d (render) -> %dx%d (window), scale: %.2f",
87 m_renderResolution.x, m_renderResolution.y,
88 m_windowResolution.x, m_windowResolution.y,
89 m_upscaleRatio);
90}
91
93 if (m_windowResolution.x == 0 || m_windowResolution.y == 0) {
94 log(LogLevel::ERROR, "getPotencialUpscaleRatio() called with invalid window resolution: %ux%u",
95 m_windowResolution.x, m_windowResolution.y);
96 m_renderResolution = m_windowResolution;
97 m_upscaleRatio = 1.0f;
98 return 1.0f;
99 }
100
101 int yscale = static_cast<int>(floor(m_windowResolution.y / 240.0f));
102 int maxScale = std::max(1, yscale);
103
104 return std::floor(maxScale);
105}
106
108 if (m_windowResolution.x == 0 || m_windowResolution.y == 0) {
109 log(LogLevel::ERROR, "calculatePS1SharpResolution() called with invalid window resolution: %ux%u. Using native resolution.",
110 m_windowResolution.x, m_windowResolution.y);
111 m_renderResolution = m_windowResolution;
112 m_upscaleRatio = 1.0f;
113 return;
114 }
115
116 float maxScale = getPotencialUpscaleRatio();
117
118 m_renderResolution = m_windowResolution / static_cast<unsigned int>(maxScale);
119 m_upscaleRatio = maxScale;
120
121 if (m_renderResolution.y < 240) {
122 if (m_windowResolution.y <= 0) {
123 log(LogLevel::ERROR, "Invalid window height for PS1_SHARP aspect calculation: %u", m_windowResolution.y);
124 m_renderResolution = m_windowResolution;
125 m_upscaleRatio = 1.0f;
126 return;
127 }
128
129 float aspect = m_windowResolution.x / static_cast<float>(m_windowResolution.y);
130 m_renderResolution.y = 240;
131 uint32_t calculatedWidth = static_cast<uint32_t>(240 * aspect);
132 m_renderResolution.x = std::clamp(calculatedWidth, 64u, 32768u);
133 m_upscaleRatio = std::clamp(m_windowResolution.y / 240.0f, 0.5f, 136.5f);
134 }
135}
136
137} // namespace vex
This file defines ResolutionManager class and ResolutionMode struct.
This file defines Window class.
void setMode(ResolutionMode mode)
Function to set the resolution mode. It is called by engine method.
ResolutionManager(SDL_Window *p_window)
Constructor.
void calculatePS1SharpResolution()
Internal helper to calculate integer-scaled resolutions for the PS1_SHARP mode.
float getPotencialUpscaleRatio()
Calculates the potential upscale ratio for a given height. @description allows you to create consista...
void update()
Updates the internal resolution state based on the current window size and mode.
ResolutionMode
Enumeration representing different resolution modes.
@ PS1_SHARP
Integer-scaled resolution closest to PS1.
@ RES_240P
240p (426x240 for 16:9).
@ NATIVE
Use window resolution.
@ RES_480P
480p (854x480 for 16:9).
void VEX_EXPORT log(const char *fmt,...)
Logs a formatted message.