From 74a94f869c06f6d59a84a9c2ad6072f2586c9ec5 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 6 Mar 2025 15:32:58 +0300 Subject: [PATCH] refactor: reduce Window references --- src/core_defs.cpp | 1 - src/frontend/hud.cpp | 2 +- src/frontend/menu.cpp | 1 - src/frontend/screens/MenuScreen.cpp | 7 +- src/graphics/core/Viewport.hpp | 4 ++ src/graphics/render/BlockWrapsRenderer.cpp | 1 - src/graphics/render/ModelBatch.cpp | 1 - src/graphics/render/TextsRenderer.cpp | 13 ++-- src/graphics/render/WorldRenderer.cpp | 4 +- src/graphics/ui/GUI.cpp | 7 +- src/graphics/ui/gui_util.cpp | 8 +-- src/logic/scripting/lua/libs/libcore.cpp | 1 - src/logic/scripting/lua/libs/libinput.cpp | 2 +- src/window/Camera.cpp | 20 +++--- src/window/Camera.hpp | 3 +- src/window/Events.cpp | 47 +++++++------ src/window/Events.hpp | 76 ++++++++++----------- src/window/Window.cpp | 31 +++++---- src/window/Window.hpp | 77 +++++++++------------- 19 files changed, 151 insertions(+), 155 deletions(-) diff --git a/src/core_defs.cpp b/src/core_defs.cpp index def96d69..dc6bb37f 100644 --- a/src/core_defs.cpp +++ b/src/core_defs.cpp @@ -5,7 +5,6 @@ #include "content/ContentBuilder.hpp" #include "io/io.hpp" #include "io/engine_paths.hpp" -#include "window/Window.hpp" #include "window/Events.hpp" #include "window/input.hpp" #include "voxels/Block.hpp" diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index ecf030dd..072e85ce 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -253,7 +253,7 @@ void Hud::updateHotbarControl() { i <= static_cast(keycode::NUM_9); i++ ) { - if (Events::jpressed(i)) { + if (Events::jpressed(static_cast(i))) { player.setChosenSlot(i - static_cast(keycode::NUM_1)); } } diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index 3b8e70fb..717b2c72 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -16,7 +16,6 @@ #include "settings.hpp" #include "coders/commons.hpp" #include "util/stringutil.hpp" -#include "window/Window.hpp" #include #include diff --git a/src/frontend/screens/MenuScreen.cpp b/src/frontend/screens/MenuScreen.cpp index f16f73ee..e8ce3f4e 100644 --- a/src/frontend/screens/MenuScreen.cpp +++ b/src/frontend/screens/MenuScreen.cpp @@ -34,14 +34,15 @@ void MenuScreen::draw(float delta) { Window::clear(); Window::setBgColor(glm::vec3(0.2f)); + uint width = Window::width; + uint height = Window::height; + uicamera->setFov(Window::height); + uicamera->setAspectRatio(width / static_cast(height)); auto uishader = assets->get("ui"); uishader->use(); uishader->uniformMatrix("u_projview", uicamera->getProjView()); - uint width = Window::width; - uint height = Window::height; - auto bg = assets->get("gui/menubg"); batch->begin(); batch->texture(bg); diff --git a/src/graphics/core/Viewport.hpp b/src/graphics/core/Viewport.hpp index 7ec826a1..d7579227 100644 --- a/src/graphics/core/Viewport.hpp +++ b/src/graphics/core/Viewport.hpp @@ -16,4 +16,8 @@ public: glm::ivec2 size() const { return glm::ivec2(width, height); } + + float getRatio() const { + return width / static_cast(height); + } }; diff --git a/src/graphics/render/BlockWrapsRenderer.cpp b/src/graphics/render/BlockWrapsRenderer.cpp index da363352..ca10a2b9 100644 --- a/src/graphics/render/BlockWrapsRenderer.cpp +++ b/src/graphics/render/BlockWrapsRenderer.cpp @@ -11,7 +11,6 @@ #include "objects/Player.hpp" #include "voxels/Block.hpp" #include "voxels/Chunks.hpp" -#include "window/Window.hpp" #include "world/Level.hpp" BlockWrapsRenderer::BlockWrapsRenderer( diff --git a/src/graphics/render/ModelBatch.cpp b/src/graphics/render/ModelBatch.cpp index 558393c6..08deb7bd 100644 --- a/src/graphics/render/ModelBatch.cpp +++ b/src/graphics/render/ModelBatch.cpp @@ -6,7 +6,6 @@ #include "graphics/core/Atlas.hpp" #include "graphics/core/Texture.hpp" #include "assets/Assets.hpp" -#include "window/Window.hpp" #include "voxels/Chunks.hpp" #include "lighting/Lightmap.hpp" #include "settings.hpp" diff --git a/src/graphics/render/TextsRenderer.cpp b/src/graphics/render/TextsRenderer.cpp index 283dacaa..205b12fa 100644 --- a/src/graphics/render/TextsRenderer.cpp +++ b/src/graphics/render/TextsRenderer.cpp @@ -4,11 +4,11 @@ #include "maths/util.hpp" #include "assets/Assets.hpp" #include "window/Camera.hpp" -#include "window/Window.hpp" #include "maths/FrustumCulling.hpp" #include "graphics/core/Font.hpp" #include "graphics/core/Batch3D.hpp" #include "graphics/core/Shader.hpp" +#include "graphics/core/DrawContext.hpp" #include "presets/NotePreset.hpp" #include "constants.hpp" @@ -66,6 +66,7 @@ void TextsRenderer::renderNote( xvec *= 1.0f + scale; yvec *= 1.0f + scale; } + const auto& viewport = context.getViewport(); if (preset.displayMode == NoteDisplayMode::PROJECTED) { float scale = 1.0f; if (glm::abs(preset.perspective) > 0.0001f) { @@ -84,14 +85,14 @@ void TextsRenderer::renderNote( } pos /= projpos.w; pos.z = 0; - xvec = {2.0f/Window::width*scale, 0, 0}; - yvec = {0, 2.0f/Window::height*scale, 0}; + xvec = {2.0f / viewport.getWidth() * scale, 0, 0}; + yvec = {0, 2.0f / viewport.getHeight() * scale, 0}; } else { auto matrix = camera.getProjView(); auto screenPos = matrix * glm::vec4(pos, 1.0f); - - xvec = glm::vec3(2.0f/Window::width*scale, 0, 0); - yvec = glm::vec3(0, 2.0f/Window::height*scale, 0); + + xvec = glm::vec3(2.0f / viewport.getWidth() * scale, 0, 0); + yvec = glm::vec3(0, 2.0f / viewport.getHeight() * scale, 0); pos = screenPos / screenPos.w; } diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index ab140c77..70464a26 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -338,8 +338,8 @@ void WorldRenderer::draw( auto world = level.getWorld(); - const Viewport& vp = pctx.getViewport(); - camera.aspect = vp.getWidth() / static_cast(vp.getHeight()); + const auto& vp = pctx.getViewport(); + camera.setAspectRatio(vp.getRatio()); const auto& settings = engine.getSettings(); const auto& worldInfo = world->getInfo(); diff --git a/src/graphics/ui/GUI.cpp b/src/graphics/ui/GUI.cpp index 95425da4..b4886830 100644 --- a/src/graphics/ui/GUI.cpp +++ b/src/graphics/ui/GUI.cpp @@ -115,8 +115,10 @@ void GUI::actMouse(float delta) { } if (hover) { hover->setHover(true); - if (Events::scroll) { - hover->scrolled(Events::scroll); + + int scroll = Events::getScroll(); + if (scroll) { + hover->scrolled(scroll); } } this->hover = hover; @@ -229,6 +231,7 @@ void GUI::draw(const DrawContext& pctx, const Assets& assets) { } menu->setPos((wsize - menu->getSize()) / 2.0f); uicamera->setFov(wsize.y); + uicamera->setAspectRatio(viewport.getRatio()); auto uishader = assets.get("ui"); uishader->use(); diff --git a/src/graphics/ui/gui_util.cpp b/src/graphics/ui/gui_util.cpp index 7be277c4..4e9bb70b 100644 --- a/src/graphics/ui/gui_util.cpp +++ b/src/graphics/ui/gui_util.cpp @@ -68,11 +68,11 @@ void guiutil::alert( )); panel->refresh(); - panel->keepAlive(Events::keyCallbacks[keycode::ENTER].add([on_hidden_final](){ + panel->keepAlive(Events::addKeyCallback(keycode::ENTER, [on_hidden_final](){ on_hidden_final(); return true; })); - panel->keepAlive(Events::keyCallbacks[keycode::ESCAPE].add([on_hidden_final](){ + panel->keepAlive(Events::addKeyCallback(keycode::ESCAPE, [on_hidden_final](){ on_hidden_final(); return true; })); @@ -130,11 +130,11 @@ void guiutil::confirm( })); panel->add(subpanel); - panel->keepAlive(Events::keyCallbacks[keycode::ENTER].add([=](){ + panel->keepAlive(Events::addKeyCallback(keycode::ENTER, [=](){ on_confirm_final(); return true; })); - panel->keepAlive(Events::keyCallbacks[keycode::ESCAPE].add([=](){ + panel->keepAlive(Events::addKeyCallback(keycode::ESCAPE, [=](){ on_deny_final(); return true; })); diff --git a/src/logic/scripting/lua/libs/libcore.cpp b/src/logic/scripting/lua/libs/libcore.cpp index 2a56644d..a874a4ba 100644 --- a/src/logic/scripting/lua/libs/libcore.cpp +++ b/src/logic/scripting/lua/libs/libcore.cpp @@ -18,7 +18,6 @@ #include "util/listutil.hpp" #include "util/platform.hpp" #include "window/Events.hpp" -#include "window/Window.hpp" #include "world/Level.hpp" #include "world/generator/WorldGenerator.hpp" diff --git a/src/logic/scripting/lua/libs/libinput.cpp b/src/logic/scripting/lua/libs/libinput.cpp index 09f762ef..f1a748fb 100644 --- a/src/logic/scripting/lua/libs/libinput.cpp +++ b/src/logic/scripting/lua/libs/libinput.cpp @@ -42,7 +42,7 @@ static int l_add_callback(lua::State* L) { std::string prefix = bindname.substr(0, pos); if (prefix == "key") { auto key = input_util::keycode_from(bindname.substr(pos + 1)); - handler = Events::keyCallbacks[key].add(actual_callback); + handler = Events::addKeyCallback(key, actual_callback); } } auto callback = [=]() -> bool { diff --git a/src/window/Camera.cpp b/src/window/Camera.cpp index 315acf56..042fa584 100644 --- a/src/window/Camera.cpp +++ b/src/window/Camera.cpp @@ -3,8 +3,6 @@ #include #include -#include "Window.hpp" - Camera::Camera(glm::vec3 position, float fov) : fov(fov), position(position) { updateVectors(); } @@ -30,17 +28,13 @@ void Camera::rotate(float x, float y, float z) { } glm::mat4 Camera::getProjection() const { - constexpr float epsilon = 1e-6f; // 0.000001 - float aspect_ratio = this->aspect; - if (std::fabs(aspect_ratio) < epsilon) { - aspect_ratio = Window::width / static_cast(Window::height); - } + constexpr float epsilon = 1e-6f; if (perspective) { - return glm::perspective(fov * zoom, aspect_ratio, near, far); + return glm::perspective(fov * zoom, ar, near, far); } else if (flipped) { - return glm::ortho(-0.5f, fov * aspect_ratio-0.5f, fov, 0.0f); + return glm::ortho(0.0f, fov * ar, fov, 0.0f); } else { - return glm::ortho(-0.5f, fov * aspect_ratio-0.5f, 0.0f, fov); + return glm::ortho(0.0f, fov * ar, 0.0f, fov); } } @@ -69,5 +63,9 @@ float Camera::getFov() const { } float Camera::getAspectRatio() const { - return aspect; + return ar; +} + +void Camera::setAspectRatio(float ar) { + this->ar = ar; } diff --git a/src/window/Camera.hpp b/src/window/Camera.hpp index 7d80cf8f..ba4a5331 100644 --- a/src/window/Camera.hpp +++ b/src/window/Camera.hpp @@ -5,6 +5,7 @@ class Camera { float fov = 1.0f; + float ar = 0.0f; public: glm::vec3 front {}; glm::vec3 up {}; @@ -17,7 +18,6 @@ public: glm::mat4 rotation {1.0f}; bool perspective = true; bool flipped = false; - float aspect = 0.0f; float near = 0.05f; float far = 1500.0f; @@ -37,4 +37,5 @@ public: float getFov() const; float getAspectRatio() const; + void setAspectRatio(float ar); }; diff --git a/src/window/Events.cpp b/src/window/Events.cpp index 49cdf929..a76909ec 100644 --- a/src/window/Events.cpp +++ b/src/window/Events.cpp @@ -12,18 +12,27 @@ static debug::Logger logger("events"); inline constexpr short _MOUSE_KEYS_OFFSET = 1024; -bool Events::keys[KEYS_BUFFER_SIZE] = {}; -uint Events::frames[KEYS_BUFFER_SIZE] = {}; -uint Events::currentFrame = 0; +namespace { + bool keys[KEYS_BUFFER_SIZE] = {}; + uint frames[KEYS_BUFFER_SIZE] = {}; + uint current_frame = 0; + bool cursor_drag = false; + bool cursor_locked = false; + std::unordered_map> key_callbacks; +} + int Events::scroll = 0; + glm::vec2 Events::delta = {}; glm::vec2 Events::cursor = {}; -bool Events::cursorDrag = false; -bool Events::cursorLocked = false; + std::vector Events::codepoints; std::vector Events::pressedKeys; std::unordered_map Events::bindings; -std::unordered_map> Events::keyCallbacks; + +int Events::getScroll() { + return scroll; +} bool Events::pressed(keycode keycode) { return pressed(static_cast(keycode)); @@ -41,7 +50,7 @@ bool Events::jpressed(keycode keycode) { } bool Events::jpressed(int keycode) { - return Events::pressed(keycode) && frames[keycode] == currentFrame; + return Events::pressed(keycode) && frames[keycode] == current_frame; } bool Events::clicked(mousecode button) { @@ -61,15 +70,15 @@ bool Events::jclicked(int button) { } void Events::toggleCursor() { - cursorDrag = false; - cursorLocked = !cursorLocked; + cursor_drag = false; + cursor_locked = !cursor_locked; Window::setCursorMode( - cursorLocked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL + cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL ); } void Events::pollEvents() { - currentFrame++; + current_frame++; delta.x = 0.f; delta.y = 0.f; scroll = 0; @@ -155,11 +164,11 @@ bool Events::jactive(const std::string& name) { } void Events::setKey(int key, bool b) { - Events::keys[key] = b; - Events::frames[key] = currentFrame; + ::keys[key] = b; + ::frames[key] = current_frame; if (b) { - const auto& callbacks = keyCallbacks.find(static_cast(key)); - if (callbacks != keyCallbacks.end()) { + const auto& callbacks = ::key_callbacks.find(static_cast(key)); + if (callbacks != ::key_callbacks.end()) { callbacks->second.notify(); } } @@ -170,18 +179,18 @@ void Events::setButton(int button, bool b) { } void Events::setPosition(float xpos, float ypos) { - if (Events::cursorDrag) { + if (::cursor_drag) { Events::delta.x += xpos - Events::cursor.x; Events::delta.y += ypos - Events::cursor.y; } else { - Events::cursorDrag = true; + ::cursor_drag = true; } Events::cursor.x = xpos; Events::cursor.y = ypos; } observer_handler Events::addKeyCallback(keycode key, KeyCallback callback) { - return keyCallbacks[key].add(std::move(callback)); + return ::key_callbacks[key].add(std::move(callback)); } #include "coders/json.hpp" @@ -251,5 +260,5 @@ void Events::enableBindings() { } bool Events::isCursorLocked() { - return cursorLocked; + return cursor_locked; } diff --git a/src/window/Events.hpp b/src/window/Events.hpp index 92a97ab0..a213cea0 100644 --- a/src/window/Events.hpp +++ b/src/window/Events.hpp @@ -15,56 +15,52 @@ enum class BindType { REBIND = 1 }; -class Events { - static bool keys[KEYS_BUFFER_SIZE]; - static uint frames[KEYS_BUFFER_SIZE]; - static uint currentFrame; - static bool cursorDrag; - static bool cursorLocked; -public: - static int scroll; - static glm::vec2 delta; - static glm::vec2 cursor; - static std::vector codepoints; - static std::vector pressedKeys; - static std::unordered_map bindings; - static std::unordered_map> keyCallbacks; +namespace Events { + extern int scroll; + extern glm::vec2 delta; + extern glm::vec2 cursor; + extern std::vector codepoints; + extern std::vector pressedKeys; + extern std::unordered_map bindings; - static void pollEvents(); + void pollEvents(); - static bool pressed(keycode keycode); - static bool pressed(int keycode); - static bool jpressed(keycode keycode); - static bool jpressed(int keycode); + int getScroll(); - static bool clicked(mousecode button); - static bool clicked(int button); - static bool jclicked(mousecode button); - static bool jclicked(int button); + bool pressed(keycode keycode); + bool pressed(int keycode); + bool jpressed(keycode keycode); + bool jpressed(int keycode); - static void toggleCursor(); + bool clicked(mousecode button); + bool clicked(int button); + bool jclicked(mousecode button); + bool jclicked(int button); - static Binding& getBinding(const std::string& name); - static void bind(const std::string& name, inputtype type, keycode code); - static void bind(const std::string& name, inputtype type, mousecode code); - static void bind(const std::string& name, inputtype type, int code); - static void rebind(const std::string& name, inputtype type, int code); - static bool active(const std::string& name); - static bool jactive(const std::string& name); + void toggleCursor(); - static observer_handler addKeyCallback(keycode key, KeyCallback callback); + Binding& getBinding(const std::string& name); + void bind(const std::string& name, inputtype type, keycode code); + void bind(const std::string& name, inputtype type, mousecode code); + void bind(const std::string& name, inputtype type, int code); + void rebind(const std::string& name, inputtype type, int code); + bool active(const std::string& name); + bool jactive(const std::string& name); - static void setKey(int key, bool b); - static void setButton(int button, bool b); + observer_handler addKeyCallback(keycode key, KeyCallback callback); - static void setPosition(float xpos, float ypos); + void setKey(int key, bool b); + void setButton(int button, bool b); - static std::string writeBindings(); - static void loadBindings( - const std::string& filename, const std::string& source, + void setPosition(float xpos, float ypos); + + std::string writeBindings(); + void loadBindings( + const std::string& filename, + const std::string& source, BindType bindType ); - static void enableBindings(); + void enableBindings(); - static bool isCursorLocked(); + bool isCursorLocked(); }; diff --git a/src/window/Window.cpp b/src/window/Window.cpp index 4e68877f..5a3d675f 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -19,18 +19,21 @@ static debug::Logger logger("window"); -GLFWwindow* Window::window = nullptr; -DisplaySettings* Window::settings = nullptr; -std::stack Window::scissorStack; -glm::vec4 Window::scissorArea; +namespace { + GLFWwindow* window = nullptr; + DisplaySettings* settings = nullptr; + std::stack scissorStack; + glm::vec4 scissorArea; + int framerate = -1; + double prevSwap = 0.0; + bool fullscreen = false; + CursorShape cursor = CursorShape::ARROW; + int posX = 0; + int posY = 0; +} + uint Window::width = 0; uint Window::height = 0; -int Window::posX = 0; -int Window::posY = 0; -int Window::framerate = -1; -double Window::prevSwap = 0.0; -bool Window::fullscreen = false; -CursorShape Window::cursor = CursorShape::ARROW; static util::ObjectsKeeper observers_keeper; static std::unordered_set extensionsCache; @@ -173,7 +176,7 @@ static void glfw_error_callback(int error, const char* description) { static GLFWcursor* standard_cursors[static_cast(CursorShape::LAST) + 1] = {}; int Window::initialize(DisplaySettings* settings) { - Window::settings = settings; + ::settings = settings; Window::width = settings->width.get(); Window::height = settings->height.get(); @@ -381,10 +384,10 @@ void Window::setShouldClose(bool flag) { } void Window::setFramerate(int framerate) { - if ((framerate != -1) != (Window::framerate != -1)) { + if ((framerate != -1) != (::framerate != -1)) { glfwSwapInterval(framerate == -1); } - Window::framerate = framerate; + ::framerate = framerate; } void Window::toggleFullscreen() { @@ -472,7 +475,7 @@ void Window::setClipboardText(const char* text) { glfwSetClipboardString(window, text); } -bool Window::tryToMaximize(GLFWwindow* window, GLFWmonitor* monitor) { +static bool try_to_maximize(GLFWwindow* window, GLFWmonitor* monitor) { glm::ivec4 windowFrame(0); glm::ivec4 workArea(0); glfwGetWindowFrameSize( diff --git a/src/window/Window.hpp b/src/window/Window.hpp index b81da395..289648f0 100644 --- a/src/window/Window.hpp +++ b/src/window/Window.hpp @@ -10,60 +10,45 @@ class ImageData; struct DisplaySettings; -struct GLFWwindow; -struct GLFWmonitor; -class Window { - static GLFWwindow* window; - static DisplaySettings* settings; - static std::stack scissorStack; - static glm::vec4 scissorArea; - static bool fullscreen; - static int framerate; - static double prevSwap; - static CursorShape cursor; +namespace Window { + extern uint width; + extern uint height; - static bool tryToMaximize(GLFWwindow* window, GLFWmonitor* monitor); - static bool isGlExtensionSupported(const char *extension); -public: - static int posX; - static int posY; - static uint width; - static uint height; - static int initialize(DisplaySettings* settings); - static void terminate(); + int initialize(DisplaySettings* settings); + void terminate(); - static void viewport(int x, int y, int width, int height); - static void setCursorMode(int mode); - static bool isShouldClose(); - static void setShouldClose(bool flag); - static void swapBuffers(); - static void setFramerate(int interval); - static void toggleFullscreen(); - static bool isFullscreen(); - static bool isMaximized(); - static bool isFocused(); - static bool isIconified(); + void viewport(int x, int y, int width, int height); + void setCursorMode(int mode); + bool isShouldClose(); + void setShouldClose(bool flag); + void swapBuffers(); + void setFramerate(int interval); + void toggleFullscreen(); + bool isFullscreen(); + bool isMaximized(); + bool isFocused(); + bool isIconified(); - static void pushScissor(glm::vec4 area); - static void popScissor(); - static void resetScissor(); + void pushScissor(glm::vec4 area); + void popScissor(); + void resetScissor(); - static void setCursor(CursorShape shape); + void setCursor(CursorShape shape); - static void clear(); - static void clearDepth(); - static void setBgColor(glm::vec3 color); - static void setBgColor(glm::vec4 color); - static double time(); - static const char* getClipboardText(); - static void setClipboardText(const char* text); - static DisplaySettings* getSettings(); - static void setIcon(const ImageData* image); + void clear(); + void clearDepth(); + void setBgColor(glm::vec3 color); + void setBgColor(glm::vec4 color); + double time(); + const char* getClipboardText(); + void setClipboardText(const char* text); + DisplaySettings* getSettings(); + void setIcon(const ImageData* image); - static glm::vec2 size() { + inline glm::vec2 size() { return glm::vec2(width, height); } - static std::unique_ptr takeScreenshot(); + std::unique_ptr takeScreenshot(); };