Skip to content

Commit

Permalink
[Render/Window] The window's dimensions are recovered after creation
Browse files Browse the repository at this point in the history
- The requested size is not absolute and the actual one can be lower, e.g. when trying to create a window taller than the screen's height
  • Loading branch information
Razakhel committed Oct 27, 2024
1 parent 5784d74 commit 3e79f46
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
14 changes: 9 additions & 5 deletions include/RaZ/Render/RenderSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ class RenderSystem final : public System {
RenderSystem(unsigned int sceneWidth, unsigned int sceneHeight) : RenderSystem() { resizeViewport(sceneWidth, sceneHeight); }
#if !defined(RAZ_NO_WINDOW)
/// Creates a render system along with a window.
/// \param sceneWidth Width of the scene.
/// \param sceneHeight Height of the scene.
/// \param windowWidth Width of the window.
/// \param windowHeight Height of the window.
/// \param windowTitle Title of the window.
/// \param settings Settings to create the window with.
/// \param antiAliasingSampleCount Number of anti-aliasing samples.
RenderSystem(unsigned int sceneWidth, unsigned int sceneHeight,
/// \note The window's width & height are to be considered just hints; the window manager remains responsible for the actual dimensions, which may be lower.
/// This can notably happen when the requested window size exceeds what the screens can display. The actual window's size can be queried afterward.
/// \see getWindow(), Window::getWidth(), Window::getHeight()
RenderSystem(unsigned int windowWidth, unsigned int windowHeight,
const std::string& windowTitle,
WindowSetting settings = WindowSetting::DEFAULT,
WindowSetting windowSettings = WindowSetting::DEFAULT,
uint8_t antiAliasingSampleCount = 1)
: m_window{ Window::create(*this, sceneWidth, sceneHeight, windowTitle, settings, antiAliasingSampleCount) } { initialize(sceneWidth, sceneHeight); }
: m_window{ Window::create(*this, windowWidth, windowHeight, windowTitle, windowSettings, antiAliasingSampleCount) } { initialize(m_window->getWidth(),
m_window->getHeight()); }
#endif

#if !defined(RAZ_NO_WINDOW)
Expand Down
6 changes: 6 additions & 0 deletions include/RaZ/Render/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class Window {
/// \param title Title of the window.
/// \param settings Settings to create the window with.
/// \param antiAliasingSampleCount Number of anti-aliasing samples.
/// \note The width & height are to be considered just hints; the window manager remains responsible for the actual dimensions, which may be lower.
/// This can notably happen when the requested window size exceeds what the screens can display. The actual window's size can be queried afterward.
/// \see getWidth(), getHeight()
Window(RenderSystem& renderSystem,
unsigned int width, unsigned int height,
const std::string& title = {},
Expand Down Expand Up @@ -88,6 +91,9 @@ class Window {
/// Resizes the window.
/// \param width New window width.
/// \param height New window height.
/// \note The width & height are to be considered just hints; the window manager remains responsible for the actual dimensions, which may be lower.
/// This can notably happen when the requested window size exceeds what the screens can display. The actual window's size can be queried afterward.
/// \see getWidth(), getHeight()
void resize(unsigned int width, unsigned int height);
/// Sets the window in a fullscreen mode, taking the whole main monitor's screen.
/// \note To quit fullscreen, call makeWindowed().
Expand Down
3 changes: 0 additions & 3 deletions src/RaZ/Render/RenderGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ void RenderGraph::updateShaders() const {
}

void RenderGraph::execute(RenderSystem& renderSystem) {
assert("Error: The render system needs a camera for the render graph to be executed." && (renderSystem.m_cameraEntity != nullptr));
assert("Error: The camera referenced by the render system needs a transform component." && renderSystem.m_cameraEntity->hasComponent<Transform>());

ZoneScopedN("RenderGraph::execute");

{
Expand Down
3 changes: 2 additions & 1 deletion src/RaZ/Render/RenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ void RenderSystem::updateLight(const Entity& entity, unsigned int lightIndex) co
}

void RenderSystem::sendCameraInfo() const {
assert("Error: A camera must be given to a RenderSystem to send its info." && (m_cameraEntity != nullptr));
assert("Error: The render system needs a camera to send its info." && (m_cameraEntity != nullptr));
assert("Error: The camera must have a transform component to send its info." && m_cameraEntity->hasComponent<Transform>());

ZoneScopedN("RenderSystem::sendCameraInfo");

Expand Down
13 changes: 6 additions & 7 deletions src/RaZ/Render/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Window::Window(RenderSystem& renderSystem,
unsigned int width, unsigned int height,
const std::string& title,
WindowSetting settings,
uint8_t antiAliasingSampleCount) : m_renderSystem{ &renderSystem }, m_width{ static_cast<int>(width) }, m_height{ static_cast<int>(height) } {
uint8_t antiAliasingSampleCount) : m_renderSystem{ &renderSystem } {
ZoneScopedN("Window::Window");

Logger::debug("[Window] Initializing...");
Expand Down Expand Up @@ -90,7 +90,7 @@ Window::Window(RenderSystem& renderSystem,
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);

m_windowHandle = glfwCreateWindow(m_width, m_height, title.c_str(), nullptr, glfwGetCurrentContext());
m_windowHandle = glfwCreateWindow(static_cast<int>(width), static_cast<int>(height), title.c_str(), nullptr, glfwGetCurrentContext());

if (m_windowHandle)
break;
Expand All @@ -104,10 +104,11 @@ Window::Window(RenderSystem& renderSystem,
throw std::runtime_error("Error: Failed to create GLFW Window");
}
#else
m_windowHandle = glfwCreateWindow(m_width, m_height, title.c_str(), nullptr, glfwGetCurrentContext());
m_windowHandle = glfwCreateWindow(static_cast<int>(width), static_cast<int>(height), title.c_str(), nullptr, glfwGetCurrentContext());
#endif

glfwSetWindowUserPointer(m_windowHandle, this);
glfwGetWindowSize(m_windowHandle, &m_width, &m_height);
glfwGetWindowPos(m_windowHandle, &m_posX, &m_posY);

if (glfwGetCurrentContext() == nullptr)
Expand Down Expand Up @@ -161,10 +162,8 @@ void Window::setIcon(const Image& img) const {
}

void Window::resize(unsigned int width, unsigned int height) {
m_width = static_cast<int>(width);
m_height = static_cast<int>(height);

glfwSetWindowSize(m_windowHandle, static_cast<int>(width), static_cast<int>(height));
glfwGetWindowSize(m_windowHandle, &m_width, &m_height);
}

void Window::makeFullscreen() {
Expand Down Expand Up @@ -198,7 +197,7 @@ bool Window::recoverVerticalSyncState() const {
return true;
#elif defined(RAZ_PLATFORM_LINUX)
if (glXQueryExtensionsString(glXGetCurrentDisplay(), 0)) {
unsigned int interval;
unsigned int interval {};
glXQueryDrawable(glXGetCurrentDisplay(), glXGetCurrentDrawable(), GLX_SWAP_INTERVAL_EXT, &interval);

return static_cast<bool>(interval);
Expand Down

0 comments on commit 3e79f46

Please sign in to comment.