refactor(platform): 替换IWindow接口为具体GLFWWindow实现
移除抽象的IWindow接口,直接使用GLFWWindow作为窗口实现 更新所有相关头文件和实现文件中的引用 简化GLFWWindow类,移除不必要的虚函数声明
This commit is contained in:
parent
a38bbf78c9
commit
b34df8bdd1
|
|
@ -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 获取渲染器
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -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字体专用着色器
|
||||
|
|
|
|||
|
|
@ -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 窗口句柄
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 初始化成功返回true,失败返回false
|
||||
*/
|
||||
bool GLRenderer::init(IWindow *window) {
|
||||
bool GLRenderer::init(GLFWWindow *window) {
|
||||
window_ = window;
|
||||
|
||||
// 初始化 OpenGL 上下文(Switch 平台已通过 SDL2 + EGL 初始化,GLContext
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Reference in New Issue