Skip to content

Commit

Permalink
[Render/Window] Window::setShouldClose()/close() are now private
Browse files Browse the repository at this point in the history
- They should never have been public in the first place, as they can stop the application from running or preemptively free the graphics context
  - The latter actually did happen in the scripting tests, where the window was closed to test the binding
  • Loading branch information
Razakhel committed Sep 3, 2024
1 parent 14470c4 commit 5f2e723
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
10 changes: 6 additions & 4 deletions include/RaZ/Render/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ MAKE_ENUM_FLAG(WindowSetting)

/// Graphical window to render the scenes on, with input custom actions.
class Window {
friend class RenderSystem;

public:
/// Creates a window.
/// \param renderSystem Render system containing this window.
Expand Down Expand Up @@ -163,10 +165,6 @@ class Window {
/// Fetches the mouse position onto the window.
/// \return 2D vector representing the mouse's position relative to the window.
Vec2f recoverMousePosition() const;
/// Tells the window that it should close.
void setShouldClose() const;
/// Closes the window.
void close();

Window& operator=(const Window&) = delete;
Window& operator=(Window&&) noexcept = default;
Expand All @@ -177,6 +175,10 @@ class Window {
/// Processes actions corresponding to keyboard & mouse inputs.
/// \param deltaTime Amount of time elapsed since the last frame.
void processInputs(float deltaTime);
/// Tells the window that it should close.
void setShouldClose() const;
/// Closes the window.
void close();

static inline int s_refCounter = 0;

Expand Down
56 changes: 28 additions & 28 deletions src/RaZ/Render/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,34 @@ Vec2f Window::recoverMousePosition() const {
return Vec2f(static_cast<float>(xPos), static_cast<float>(yPos));
}

void Window::processInputs(float deltaTime) {
ZoneScopedN("Window::processInputs");

{
ZoneScopedN("glfwPollEvents");
glfwPollEvents();
}

auto& actions = std::get<InputActions>(m_callbacks);
auto actionIter = actions.cbegin();

while (actionIter != actions.cend()) {
const auto& action = actionIter->second;

// An action consists of two parts:
// - A callback associated to the triggered key or button
// - A value indicating if it should be executed only once or every frame

action.first(deltaTime);

// Removing the current action if ONCE is given, or simply increment the iterator
if (action.second == Input::ONCE)
actionIter = actions.erase(actionIter); // std::unordered_map::erase(iter) returns an iterator on the next element
else
++actionIter;
}
}

void Window::setShouldClose() const {
glfwSetWindowShouldClose(m_windowHandle, true);
}
Expand Down Expand Up @@ -469,32 +497,4 @@ void Window::close() {
Logger::debug("[Window] Closed");
}

void Window::processInputs(float deltaTime) {
ZoneScopedN("Window::processInputs");

{
ZoneScopedN("glfwPollEvents");
glfwPollEvents();
}

auto& actions = std::get<InputActions>(m_callbacks);
auto actionIter = actions.cbegin();

while (actionIter != actions.cend()) {
const auto& action = actionIter->second;

// An action consists of two parts:
// - A callback associated to the triggered key or button
// - A value indicating if it should be executed only once or every frame

action.first(deltaTime);

// Removing the current action if ONCE is given, or simply increment the iterator
if (action.second == Input::ONCE)
actionIter = actions.erase(actionIter); // std::unordered_map::erase(iter) returns an iterator on the next element
else
++actionIter;
}
}

} // namespace Raz
2 changes: 0 additions & 2 deletions src/RaZ/Script/LuaWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ void LuaWrapper::registerWindowTypes() {
#endif
window["run"] = &Window::run;
window["recoverMousePosition"] = &Window::recoverMousePosition;
window["setShouldClose"] = &Window::setShouldClose;
window["close"] = &Window::close;

state.new_enum<WindowSetting>("WindowSetting", {
{ "FOCUSED", WindowSetting::FOCUSED },
Expand Down
2 changes: 0 additions & 2 deletions tests/src/RaZ/Script/LuaRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1100,8 +1100,6 @@ TEST_CASE("LuaRender Window", "[script][lua][render]") {
window:updateCallbacks()
assert(window:run(0))
assert(window:recoverMousePosition():x() >= 0)
window:setShouldClose()
window:close()
)"));

#if !defined(RAZ_NO_OVERLAY)
Expand Down

0 comments on commit 5f2e723

Please sign in to comment.