refactor(platform): 替换IWindow接口为具体GLFWWindow实现

移除抽象的IWindow接口,直接使用GLFWWindow作为窗口实现
更新所有相关头文件和实现文件中的引用
简化GLFWWindow类,移除不必要的虚函数声明
This commit is contained in:
ChestnutYueyue 2026-02-20 14:43:34 +08:00
parent a38bbf78c9
commit b34df8bdd1
12 changed files with 182 additions and 268 deletions

View File

@ -8,7 +8,7 @@
namespace extra2d {
class IWindow;
class GLFWWindow;
class RenderBackend;
class WindowModule;
class RenderModule;
@ -85,7 +85,7 @@ public:
* @brief
* @return
*/
IWindow *window();
GLFWWindow *window();
/**
* @brief

View File

@ -13,7 +13,7 @@
// Config removed - app info now in Application class
// Platform
#include <extra2d/platform/iwindow.h>
#include <extra2d/platform/glfw/glfw_window.h>
#include <extra2d/platform/keys.h>
#include <extra2d/platform/window_module.h>

View File

@ -9,7 +9,7 @@
namespace extra2d {
// 前向声明
class IWindow;
class GLFWWindow;
class Texture;
class FontAtlas;
class Shader;
@ -26,7 +26,7 @@ public:
// ------------------------------------------------------------------------
// 生命周期
// ------------------------------------------------------------------------
virtual bool init(IWindow* window) = 0;
virtual bool init(GLFWWindow* window) = 0;
virtual void shutdown() = 0;
// ------------------------------------------------------------------------

View File

@ -15,7 +15,7 @@
namespace extra2d {
// 前向声明
class IWindow;
class GLFWWindow;
class GLContext;
class GLFramebuffer;
@ -28,7 +28,7 @@ public:
~GLRenderer() override;
// RenderBackend 接口实现
bool init(IWindow *window) override;
bool init(GLFWWindow *window) override;
void shutdown() override;
void beginFrame(const Color &clearColor) override;
@ -132,7 +132,7 @@ private:
float r, g, b, a;
};
IWindow *window_;
GLFWWindow *window_;
GLSpriteBatch spriteBatch_;
Ptr<IShader> shapeShader_;
Ptr<IShader> sdfFontShader_; // SDF字体专用着色器

View File

@ -1,54 +1,187 @@
#pragma once
#include <extra2d/platform/iwindow.h>
#include <extra2d/core/types.h>
#include <extra2d/core/math_types.h>
#include <GLFW/glfw3.h>
#include <functional>
#include <string>
namespace extra2d {
/**
* @brief
*/
enum class Cursor {
Arrow,
IBeam,
Crosshair,
Hand,
HResize,
VResize,
Hidden
};
/**
* @brief GLFW
*/
class GLFWWindow : public IWindow {
class GLFWWindow {
public:
using ResizeCb = std::function<void(int, int)>;
using CloseCb = std::function<void()>;
using FocusCb = std::function<void(bool)>;
GLFWWindow();
~GLFWWindow() override;
~GLFWWindow();
bool create(const std::string& title, int width, int height, bool vsync = true) override;
void destroy() override;
/**
* @brief
* @param title
* @param width
* @param height
* @param vsync
* @return
*/
bool create(const std::string& title, int width, int height, bool vsync = true);
void poll() override;
void swap() override;
bool shouldClose() const override;
void close() override;
/**
* @brief
*/
void destroy();
void setTitle(const std::string& title) override;
void setSize(int w, int h) override;
void setPos(int x, int y) override;
void setFullscreen(bool fs) override;
void setVSync(bool vsync) override;
void setVisible(bool visible) override;
/**
* @brief
*/
void poll();
int width() const override;
int height() const override;
Size size() const override;
Vec2 pos() const override;
bool fullscreen() const override;
bool vsync() const override;
bool focused() const override;
bool minimized() const override;
/**
* @brief
*/
void swap();
float scaleX() const override;
float scaleY() const override;
/**
* @brief
*/
bool shouldClose() const;
void setCursor(Cursor cursor) override;
void showCursor(bool show) override;
void lockCursor(bool lock) override;
/**
* @brief
*/
void close();
void onResize(ResizeCb cb) override;
void onClose(CloseCb cb) override;
void onFocus(FocusCb cb) override;
/**
* @brief
*/
void setTitle(const std::string& title);
void* native() const override;
/**
* @brief
*/
void setSize(int w, int h);
/**
* @brief
*/
void setPos(int x, int y);
/**
* @brief
*/
void setFullscreen(bool fs);
/**
* @brief
*/
void setVSync(bool vsync);
/**
* @brief
*/
void setVisible(bool visible);
/**
* @brief
*/
int width() const { return width_; }
/**
* @brief
*/
int height() const { return height_; }
/**
* @brief
*/
Size size() const;
/**
* @brief
*/
Vec2 pos() const;
/**
* @brief
*/
bool fullscreen() const { return fullscreen_; }
/**
* @brief
*/
bool vsync() const { return vsync_; }
/**
* @brief
*/
bool focused() const { return focused_; }
/**
* @brief
*/
bool minimized() const { return minimized_; }
/**
* @brief X
*/
float scaleX() const { return scaleX_; }
/**
* @brief Y
*/
float scaleY() const { return scaleY_; }
/**
* @brief
*/
void setCursor(Cursor cursor);
/**
* @brief /
*/
void showCursor(bool show);
/**
* @brief /
*/
void lockCursor(bool lock);
/**
* @brief
*/
void onResize(ResizeCb cb) { resizeCb_ = cb; }
/**
* @brief
*/
void onClose(CloseCb cb) { closeCb_ = cb; }
/**
* @brief
*/
void onFocus(FocusCb cb) { focusCb_ = cb; }
/**
* @brief
*/
void* native() const;
/**
* @brief GLFW

View File

@ -1,197 +0,0 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/core/math_types.h>
#include <functional>
#include <string>
namespace extra2d {
/**
* @brief
*/
enum class Cursor {
Arrow,
IBeam,
Crosshair,
Hand,
HResize,
VResize,
Hidden
};
/**
* @brief
*
*/
class IWindow {
public:
virtual ~IWindow() = default;
/**
* @brief
* @param title
* @param width
* @param height
* @param vsync
* @return
*/
virtual bool create(const std::string& title, int width, int height, bool vsync = true) = 0;
/**
* @brief
*/
virtual void destroy() = 0;
/**
* @brief
*/
virtual void poll() = 0;
/**
* @brief
*/
virtual void swap() = 0;
/**
* @brief
*/
virtual bool shouldClose() const = 0;
/**
* @brief
*/
virtual void close() = 0;
/**
* @brief
*/
virtual void setTitle(const std::string& title) = 0;
/**
* @brief
*/
virtual void setSize(int w, int h) = 0;
/**
* @brief
*/
virtual void setPos(int x, int y) = 0;
/**
* @brief
*/
virtual void setFullscreen(bool fs) = 0;
/**
* @brief
*/
virtual void setVSync(bool vsync) = 0;
/**
* @brief
*/
virtual void setVisible(bool visible) = 0;
/**
* @brief
*/
virtual int width() const = 0;
/**
* @brief
*/
virtual int height() const = 0;
/**
* @brief
*/
virtual Size size() const = 0;
/**
* @brief
*/
virtual Vec2 pos() const = 0;
/**
* @brief
*/
virtual bool fullscreen() const = 0;
/**
* @brief
*/
virtual bool vsync() const = 0;
/**
* @brief
*/
virtual bool focused() const = 0;
/**
* @brief
*/
virtual bool minimized() const = 0;
/**
* @brief X
*/
virtual float scaleX() const = 0;
/**
* @brief Y
*/
virtual float scaleY() const = 0;
/**
* @brief
*/
virtual void setCursor(Cursor cursor) = 0;
/**
* @brief /
*/
virtual void showCursor(bool show) = 0;
/**
* @brief /
*/
virtual void lockCursor(bool lock) = 0;
/**
* @brief
*/
using ResizeCb = std::function<void(int, int)>;
/**
* @brief
*/
using CloseCb = std::function<void()>;
/**
* @brief
*/
using FocusCb = std::function<void(bool)>;
/**
* @brief
*/
virtual void onResize(ResizeCb cb) = 0;
/**
* @brief
*/
virtual void onClose(CloseCb cb) = 0;
/**
* @brief
*/
virtual void onFocus(FocusCb cb) = 0;
/**
* @brief
*/
virtual void* native() const = 0;
};
} // namespace extra2d

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/module.h>
#include <extra2d/platform/iwindow.h>
#include <extra2d/platform/glfw/glfw_window.h>
#include <functional>
#include <string>
@ -48,11 +48,11 @@ public:
* @brief
* @return
*/
IWindow *win() const { return win_.get(); }
GLFWWindow *win() const { return win_.get(); }
private:
WindowCfg cfg_;
UniquePtr<IWindow> win_;
UniquePtr<GLFWWindow> win_;
bool initialized_ = false;
};

View File

@ -3,7 +3,7 @@
#include <extra2d/graphics/core/render_backend.h>
#include <extra2d/graphics/core/render_module.h>
#include <extra2d/graphics/memory/vram_manager.h>
#include <extra2d/platform/iwindow.h>
#include <extra2d/platform/glfw/glfw_window.h>
#include <extra2d/platform/window_module.h>
#include <extra2d/services/camera_service.h>
#include <extra2d/services/event_service.h>
@ -213,7 +213,7 @@ void Application::render() {
winMod->win()->swap();
}
IWindow *Application::window() {
GLFWWindow *Application::window() {
auto *winMod = get<WindowModule>();
return winMod ? winMod->win() : nullptr;
}

View File

@ -9,7 +9,7 @@
#include <extra2d/graphics/opengl/gl_renderer.h>
#include <extra2d/graphics/opengl/gl_texture.h>
#include <extra2d/graphics/shader/shader_manager.h>
#include <extra2d/platform/iwindow.h>
#include <extra2d/platform/glfw/glfw_window.h>
#include <extra2d/services/logger_service.h>
#include <vector>
@ -45,7 +45,7 @@ GLRenderer::~GLRenderer() { shutdown(); }
* @param window
* @return truefalse
*/
bool GLRenderer::init(IWindow *window) {
bool GLRenderer::init(GLFWWindow *window) {
window_ = window;
// 初始化 OpenGL 上下文Switch 平台已通过 SDL2 + EGL 初始化GLContext

View File

@ -200,10 +200,6 @@ void GLFWWindow::setVisible(bool visible) {
#endif
}
int GLFWWindow::width() const { return width_; }
int GLFWWindow::height() const { return height_; }
Size GLFWWindow::size() const {
return Size(static_cast<float>(width_), static_cast<float>(height_));
}
@ -218,18 +214,6 @@ Vec2 GLFWWindow::pos() const {
return Vec2(static_cast<float>(x), static_cast<float>(y));
}
bool GLFWWindow::fullscreen() const { return fullscreen_; }
bool GLFWWindow::vsync() const { return vsync_; }
bool GLFWWindow::focused() const { return focused_; }
bool GLFWWindow::minimized() const { return minimized_; }
float GLFWWindow::scaleX() const { return scaleX_; }
float GLFWWindow::scaleY() const { return scaleY_; }
void GLFWWindow::setCursor(Cursor cursor) {
#ifndef __SWITCH__
if (!glfwWindow_)
@ -299,12 +283,6 @@ void GLFWWindow::lockCursor(bool lock) {
#endif
}
void GLFWWindow::onResize(ResizeCb cb) { resizeCb_ = cb; }
void GLFWWindow::onClose(CloseCb cb) { closeCb_ = cb; }
void GLFWWindow::onFocus(FocusCb cb) { focusCb_ = cb; }
void *GLFWWindow::native() const { return glfwWindow_; }
bool GLFWWindow::initGLFW() {

View File

@ -2,7 +2,7 @@
#include <extra2d/app/application.h>
#include <extra2d/core/color.h>
#include <extra2d/graphics/core/render_backend.h>
#include <extra2d/platform/iwindow.h>
#include <extra2d/platform/glfw/glfw_window.h>
#include <algorithm>
#include <glm/gtc/matrix_transform.hpp>

View File

@ -1,7 +1,7 @@
#include <extra2d/app/application.h>
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/core/render_backend.h>
#include <extra2d/platform/iwindow.h>
#include <extra2d/platform/glfw/glfw_window.h>
#include <extra2d/scene/transition_fade_scene.h>
#include <extra2d/services/logger_service.h>
#include <glm/gtc/matrix_transform.hpp>