refactor(渲染器): 将RenderBackend重命名为Renderer并重构相关代码
重构渲染器接口命名,将RenderBackend统一更名为Renderer,同时更新所有相关文件和文档引用。此变更旨在提供更简洁清晰的接口命名,并保持代码一致性。 - 重命名RenderBackend类为Renderer - 更新所有相关文件中的类型引用和文档 - 保持原有功能不变,仅进行命名和结构调整 - 修复因重命名导致的编译错误和警告
This commit is contained in:
parent
b34df8bdd1
commit
9e911db53c
|
|
@ -9,7 +9,7 @@
|
|||
namespace extra2d {
|
||||
|
||||
class GLFWWindow;
|
||||
class RenderBackend;
|
||||
class Renderer;
|
||||
class WindowModule;
|
||||
class RenderModule;
|
||||
|
||||
|
|
@ -91,7 +91,7 @@ public:
|
|||
* @brief 获取渲染器
|
||||
* @return 渲染器指针
|
||||
*/
|
||||
RenderBackend *renderer();
|
||||
Renderer *renderer();
|
||||
|
||||
/**
|
||||
* @brief 进入场景
|
||||
|
|
|
|||
|
|
@ -20,10 +20,10 @@
|
|||
// Graphics
|
||||
#include <extra2d/graphics/camera/camera.h>
|
||||
#include <extra2d/graphics/camera/viewport_adapter.h>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/graphics/core/render_module.h>
|
||||
#include <extra2d/graphics/core/render_target.h>
|
||||
#include <extra2d/graphics/memory/vram_manager.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/graphics/shader/shader_manager.h>
|
||||
#include <extra2d/graphics/texture/font.h>
|
||||
#include <extra2d/graphics/texture/texture.h>
|
||||
|
|
|
|||
|
|
@ -1,125 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <extra2d/core/color.h>
|
||||
#include <extra2d/core/math_types.h>
|
||||
#include <extra2d/core/types.h>
|
||||
#include <extra2d/graphics/resources/pipeline.h>
|
||||
#include <glm/mat4x4.hpp>
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
// 前向声明
|
||||
class GLFWWindow;
|
||||
class Texture;
|
||||
class FontAtlas;
|
||||
class Shader;
|
||||
|
||||
// BlendMode 定义在 pipeline.h 中
|
||||
|
||||
// ============================================================================
|
||||
// 渲染后端抽象接口
|
||||
// ============================================================================
|
||||
class RenderBackend {
|
||||
public:
|
||||
virtual ~RenderBackend() = default;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 生命周期
|
||||
// ------------------------------------------------------------------------
|
||||
virtual bool init(GLFWWindow* window) = 0;
|
||||
virtual void shutdown() = 0;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 帧管理
|
||||
// ------------------------------------------------------------------------
|
||||
virtual void beginFrame(const Color &clearColor) = 0;
|
||||
virtual void endFrame() = 0;
|
||||
virtual void setViewport(int x, int y, int width, int height) = 0;
|
||||
virtual void setVSync(bool enabled) = 0;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 状态设置
|
||||
// ------------------------------------------------------------------------
|
||||
virtual void setBlendMode(BlendMode mode) = 0;
|
||||
virtual void setViewProjection(const glm::mat4 &matrix) = 0;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 变换矩阵栈
|
||||
// ------------------------------------------------------------------------
|
||||
virtual void pushTransform(const glm::mat4 &transform) = 0;
|
||||
virtual void popTransform() = 0;
|
||||
virtual glm::mat4 getCurrentTransform() const = 0;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 纹理
|
||||
// ------------------------------------------------------------------------
|
||||
virtual Ptr<Texture> createTexture(int width, int height,
|
||||
const uint8_t *pixels, int channels) = 0;
|
||||
virtual Ptr<Texture> loadTexture(const std::string &filepath) = 0;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 精灵批渲染
|
||||
// ------------------------------------------------------------------------
|
||||
/**
|
||||
* @brief 开始手动精灵批处理(高级用法)
|
||||
* @note 一般情况下不需要调用,drawSprite/drawText 会自动管理批处理
|
||||
*/
|
||||
virtual void beginSpriteBatch() = 0;
|
||||
virtual void drawSprite(const Texture &texture, const Rect &destRect,
|
||||
const Rect &srcRect, const Color &tint,
|
||||
float rotation, const Vec2 &anchor) = 0;
|
||||
virtual void drawSprite(const Texture &texture, const Vec2 &position,
|
||||
const Color &tint) = 0;
|
||||
virtual void endSpriteBatch() = 0;
|
||||
|
||||
/**
|
||||
* @brief 立即提交当前批处理
|
||||
* @note 手动控制批处理提交时机,一般情况下不需要调用
|
||||
*/
|
||||
virtual void flush() = 0;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 形状渲染
|
||||
// ------------------------------------------------------------------------
|
||||
virtual void drawLine(const Vec2 &start, const Vec2 &end, const Color &color,
|
||||
float width = 1.0f) = 0;
|
||||
virtual void drawRect(const Rect &rect, const Color &color,
|
||||
float width = 1.0f) = 0;
|
||||
virtual void fillRect(const Rect &rect, const Color &color) = 0;
|
||||
virtual void drawCircle(const Vec2 ¢er, float radius, const Color &color,
|
||||
int segments = 32, float width = 1.0f) = 0;
|
||||
virtual void fillCircle(const Vec2 ¢er, float radius, const Color &color,
|
||||
int segments = 32) = 0;
|
||||
virtual void drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3,
|
||||
const Color &color, float width = 1.0f) = 0;
|
||||
virtual void fillTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3,
|
||||
const Color &color) = 0;
|
||||
virtual void drawPolygon(const std::vector<Vec2> &points, const Color &color,
|
||||
float width = 1.0f) = 0;
|
||||
virtual void fillPolygon(const std::vector<Vec2> &points,
|
||||
const Color &color) = 0;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 文字渲染
|
||||
// ------------------------------------------------------------------------
|
||||
virtual Ptr<FontAtlas> createFontAtlas(const std::string &filepath,
|
||||
int fontSize, bool useSDF = false) = 0;
|
||||
virtual void drawText(const FontAtlas &font, const std::string &text,
|
||||
const Vec2 &position, const Color &color) = 0;
|
||||
virtual void drawText(const FontAtlas &font, const std::string &text, float x,
|
||||
float y, const Color &color) = 0;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 统计信息
|
||||
// ------------------------------------------------------------------------
|
||||
struct Stats {
|
||||
uint32_t drawCalls = 0;
|
||||
uint32_t triangleCount = 0;
|
||||
uint32_t textureBinds = 0;
|
||||
uint32_t shaderBinds = 0;
|
||||
};
|
||||
virtual Stats getStats() const = 0;
|
||||
virtual void resetStats() = 0;
|
||||
};
|
||||
|
||||
} // namespace extra2d
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <extra2d/core/module.h>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/platform/window_module.h>
|
||||
#include <functional>
|
||||
#include <typeindex>
|
||||
|
|
@ -17,17 +17,12 @@ struct RenderCfg {
|
|||
int multisamples;
|
||||
int priority;
|
||||
|
||||
RenderCfg()
|
||||
: targetFPS(60)
|
||||
, vsync(true)
|
||||
, multisamples(0)
|
||||
, priority(10)
|
||||
{}
|
||||
RenderCfg() : targetFPS(60), vsync(true), multisamples(0), priority(10) {}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 渲染模块
|
||||
* 管理 OpenGL 渲染后端
|
||||
* 管理 OpenGL 渲染器
|
||||
*/
|
||||
class RenderModule : public Module {
|
||||
public:
|
||||
|
|
@ -65,13 +60,13 @@ public:
|
|||
|
||||
/**
|
||||
* @brief 获取渲染器
|
||||
* @return 渲染后端指针
|
||||
* @return 渲染器指针
|
||||
*/
|
||||
RenderBackend* renderer() const { return renderer_.get(); }
|
||||
Renderer *renderer() const { return renderer_.get(); }
|
||||
|
||||
private:
|
||||
RenderCfg cfg_;
|
||||
UniquePtr<RenderBackend> renderer_;
|
||||
UniquePtr<Renderer> renderer_;
|
||||
bool initialized_ = false;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,132 +1,111 @@
|
|||
#pragma once
|
||||
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/core/color.h>
|
||||
#include <extra2d/core/math_types.h>
|
||||
#include <extra2d/core/types.h>
|
||||
#include <extra2d/graphics/opengl/gl_buffer.h>
|
||||
#include <extra2d/graphics/opengl/gl_framebuffer.h>
|
||||
#include <extra2d/graphics/opengl/gl_pipeline.h>
|
||||
#include <extra2d/graphics/opengl/gl_sprite_batch.h>
|
||||
#include <extra2d/graphics/resources/pipeline.h>
|
||||
#include <extra2d/graphics/shader/shader_interface.h>
|
||||
|
||||
|
||||
#include <array>
|
||||
#include <glad/glad.h>
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
// 前向声明
|
||||
class GLFWWindow;
|
||||
class GLContext;
|
||||
class GLFramebuffer;
|
||||
class FontAtlas;
|
||||
|
||||
// ============================================================================
|
||||
// OpenGL 渲染器实现
|
||||
// ============================================================================
|
||||
class GLRenderer : public RenderBackend {
|
||||
/**
|
||||
* @brief 渲染统计信息
|
||||
*/
|
||||
struct RenderStats {
|
||||
uint32_t drawCalls = 0;
|
||||
uint32_t triangleCount = 0;
|
||||
uint32_t textureBinds = 0;
|
||||
uint32_t shaderBinds = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief OpenGL 渲染器实现
|
||||
*/
|
||||
class Renderer {
|
||||
public:
|
||||
GLRenderer();
|
||||
~GLRenderer() override;
|
||||
Renderer();
|
||||
~Renderer();
|
||||
|
||||
// RenderBackend 接口实现
|
||||
bool init(GLFWWindow *window) override;
|
||||
void shutdown() override;
|
||||
bool init(GLFWWindow *window);
|
||||
void shutdown();
|
||||
|
||||
void beginFrame(const Color &clearColor) override;
|
||||
void endFrame() override;
|
||||
void setViewport(int x, int y, int width, int height) override;
|
||||
void setVSync(bool enabled) override;
|
||||
void beginFrame(const Color &clearColor);
|
||||
void endFrame();
|
||||
void setViewport(int x, int y, int width, int height);
|
||||
void setVSync(bool enabled);
|
||||
|
||||
void setBlendMode(BlendMode mode) override;
|
||||
void setViewProjection(const glm::mat4 &matrix) override;
|
||||
void setBlendMode(BlendMode mode);
|
||||
void setViewProjection(const glm::mat4 &matrix);
|
||||
|
||||
// 变换矩阵栈
|
||||
void pushTransform(const glm::mat4 &transform) override;
|
||||
void popTransform() override;
|
||||
glm::mat4 getCurrentTransform() const override;
|
||||
void pushTransform(const glm::mat4 &transform);
|
||||
void popTransform();
|
||||
glm::mat4 getCurrentTransform() const;
|
||||
|
||||
Ptr<Texture> createTexture(int width, int height, const uint8_t *pixels,
|
||||
int channels) override;
|
||||
Ptr<Texture> loadTexture(const std::string &filepath) override;
|
||||
int channels);
|
||||
Ptr<Texture> loadTexture(const std::string &filepath);
|
||||
|
||||
void beginSpriteBatch() override;
|
||||
void beginSpriteBatch();
|
||||
void drawSprite(const Texture &texture, const Rect &destRect,
|
||||
const Rect &srcRect, const Color &tint, float rotation,
|
||||
const Vec2 &anchor) override;
|
||||
const Vec2 &anchor);
|
||||
void drawSprite(const Texture &texture, const Vec2 &position,
|
||||
const Color &tint) override;
|
||||
void endSpriteBatch() override;
|
||||
void flush() override;
|
||||
const Color &tint);
|
||||
void endSpriteBatch();
|
||||
void flush();
|
||||
|
||||
void drawLine(const Vec2 &start, const Vec2 &end, const Color &color,
|
||||
float width) override;
|
||||
void drawRect(const Rect &rect, const Color &color, float width) override;
|
||||
void fillRect(const Rect &rect, const Color &color) override;
|
||||
float width = 1.0f);
|
||||
void drawRect(const Rect &rect, const Color &color, float width = 1.0f);
|
||||
void fillRect(const Rect &rect, const Color &color);
|
||||
void drawCircle(const Vec2 ¢er, float radius, const Color &color,
|
||||
int segments, float width) override;
|
||||
int segments = 32, float width = 1.0f);
|
||||
void fillCircle(const Vec2 ¢er, float radius, const Color &color,
|
||||
int segments) override;
|
||||
int segments = 32);
|
||||
void drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3,
|
||||
const Color &color, float width) override;
|
||||
const Color &color, float width = 1.0f);
|
||||
void fillTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3,
|
||||
const Color &color) override;
|
||||
const Color &color);
|
||||
void drawPolygon(const std::vector<Vec2> &points, const Color &color,
|
||||
float width) override;
|
||||
void fillPolygon(const std::vector<Vec2> &points,
|
||||
const Color &color) override;
|
||||
float width = 1.0f);
|
||||
void fillPolygon(const std::vector<Vec2> &points, const Color &color);
|
||||
|
||||
Ptr<FontAtlas> createFontAtlas(const std::string &filepath, int fontSize,
|
||||
bool useSDF = false) override;
|
||||
bool useSDF = false);
|
||||
void drawText(const FontAtlas &font, const std::string &text,
|
||||
const Vec2 &position, const Color &color) override;
|
||||
const Vec2 &position, const Color &color);
|
||||
void drawText(const FontAtlas &font, const std::string &text, float x,
|
||||
float y, const Color &color) override;
|
||||
float y, const Color &color);
|
||||
|
||||
Stats getStats() const override { return stats_; }
|
||||
void resetStats() override;
|
||||
RenderStats getStats() const { return stats_; }
|
||||
void resetStats();
|
||||
|
||||
// GLFramebuffer 相关方法
|
||||
|
||||
/**
|
||||
* @brief 创建帧缓冲对象
|
||||
* @param desc 帧缓冲描述
|
||||
* @return 创建的帧缓冲智能指针
|
||||
*/
|
||||
Ptr<GLFramebuffer> createFramebuffer(const FramebufferDesc &desc);
|
||||
|
||||
/**
|
||||
* @brief 绑定帧缓冲(作为渲染目标)
|
||||
* @param framebuffer 帧缓冲对象指针,传入 nullptr 则绑定默认帧缓冲
|
||||
*/
|
||||
void bindFramebuffer(GLFramebuffer *framebuffer);
|
||||
|
||||
/**
|
||||
* @brief 解绑帧缓冲(恢复到默认帧缓冲)
|
||||
*/
|
||||
void unbindFramebuffer();
|
||||
|
||||
/**
|
||||
* @brief 获取默认帧缓冲
|
||||
* @return 默认帧缓冲智能指针
|
||||
*/
|
||||
Ptr<GLFramebuffer> getDefaultFramebuffer() const;
|
||||
|
||||
/**
|
||||
* @brief 清除当前绑定的帧缓冲
|
||||
* @param color 清除颜色
|
||||
* @param clearColor 是否清除颜色缓冲
|
||||
* @param clearDepth 是否清除深度缓冲
|
||||
* @param clearStencil 是否清除模板缓冲
|
||||
*/
|
||||
void clearFramebuffer(const Color &color, bool clearColor = true,
|
||||
bool clearDepth = true, bool clearStencil = false);
|
||||
|
||||
private:
|
||||
// 形状批处理常量
|
||||
static constexpr size_t MAX_CIRCLE_SEGMENTS = 128;
|
||||
static constexpr size_t MAX_SHAPE_VERTICES = 8192; // 最大形状顶点数
|
||||
static constexpr size_t MAX_LINE_VERTICES = 16384; // 最大线条顶点数
|
||||
static constexpr size_t MAX_SHAPE_VERTICES = 8192;
|
||||
static constexpr size_t MAX_LINE_VERTICES = 16384;
|
||||
|
||||
// 形状顶点结构(包含颜色)
|
||||
struct ShapeVertex {
|
||||
float x, y;
|
||||
float r, g, b, a;
|
||||
|
|
@ -135,45 +114,40 @@ private:
|
|||
GLFWWindow *window_;
|
||||
GLSpriteBatch spriteBatch_;
|
||||
Ptr<IShader> shapeShader_;
|
||||
Ptr<IShader> sdfFontShader_; // SDF字体专用着色器
|
||||
Ptr<IShader> sdfFontShader_;
|
||||
|
||||
GLuint shapeVao_; // 形状 VAO(手动管理,用于顶点属性配置)
|
||||
GLBuffer shapeBuffer_; // 形状 VBO(使用 GLBuffer 管理)
|
||||
GLuint lineVao_; // 线条 VAO(手动管理,用于顶点属性配置)
|
||||
GLBuffer lineBuffer_; // 线条 VBO(使用 GLBuffer 管理)
|
||||
GLuint shapeVao_;
|
||||
GLBuffer shapeBuffer_;
|
||||
GLuint lineVao_;
|
||||
GLBuffer lineBuffer_;
|
||||
|
||||
glm::mat4 viewProjection_;
|
||||
std::vector<glm::mat4> transformStack_;
|
||||
Stats stats_;
|
||||
RenderStats stats_;
|
||||
bool vsync_;
|
||||
|
||||
// 形状批处理缓冲区(预分配,避免每帧内存分配)
|
||||
std::array<ShapeVertex, MAX_SHAPE_VERTICES> shapeVertexCache_;
|
||||
size_t shapeVertexCount_ = 0;
|
||||
GLenum currentShapeMode_ = GL_TRIANGLES;
|
||||
|
||||
// 线条批处理缓冲区
|
||||
std::array<ShapeVertex, MAX_LINE_VERTICES> lineVertexCache_;
|
||||
size_t lineVertexCount_ = 0;
|
||||
float currentLineWidth_ = 1.0f;
|
||||
|
||||
// OpenGL 管线状态管理
|
||||
GLPipeline pipeline_;
|
||||
|
||||
// 自动批处理状态
|
||||
bool batchActive_ = false; // 批处理是否激活
|
||||
bool autoBatchEnabled_ = true; // 是否启用自动批处理
|
||||
const Texture *currentBatchTexture_ = nullptr; // 当前批处理的纹理
|
||||
std::vector<SpriteData> pendingSprites_; // 待提交的精灵
|
||||
static constexpr size_t MAX_BATCH_SPRITES = 1000; // 最大批处理精灵数
|
||||
bool batchActive_ = false;
|
||||
bool autoBatchEnabled_ = true;
|
||||
const Texture *currentBatchTexture_ = nullptr;
|
||||
std::vector<SpriteData> pendingSprites_;
|
||||
static constexpr size_t MAX_BATCH_SPRITES = 1000;
|
||||
|
||||
// 帧缓冲管理
|
||||
mutable Ptr<GLFramebuffer> defaultFramebuffer_; // 默认帧缓冲(延迟创建)
|
||||
GLFramebuffer *currentFramebuffer_ = nullptr; // 当前绑定的帧缓冲
|
||||
mutable Ptr<GLFramebuffer> defaultFramebuffer_;
|
||||
GLFramebuffer *currentFramebuffer_ = nullptr;
|
||||
|
||||
void initShapeRendering();
|
||||
void ensureBatchActive(); // 确保批处理已激活
|
||||
void submitPendingSprites(); // 提交待处理的精灵
|
||||
void ensureBatchActive();
|
||||
void submitPendingSprites();
|
||||
void flushShapeBatch();
|
||||
void flushLineBatch();
|
||||
void addShapeVertex(float x, float y, const Color &color);
|
||||
|
|
|
|||
|
|
@ -13,12 +13,10 @@
|
|||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
// 前向声明
|
||||
class Scene;
|
||||
class RenderBackend;
|
||||
class Renderer;
|
||||
|
||||
// ============================================================================
|
||||
// 纹理加载选项
|
||||
|
|
|
|||
|
|
@ -4,16 +4,15 @@
|
|||
#include <extra2d/core/math_types.h>
|
||||
#include <extra2d/core/types.h>
|
||||
#include <extra2d/event/event_dispatcher.h>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
// 前向声明
|
||||
class Scene;
|
||||
class RenderBackend;
|
||||
class Renderer;
|
||||
struct RenderCommand;
|
||||
|
||||
// ============================================================================
|
||||
|
|
@ -137,7 +136,7 @@ public:
|
|||
virtual void onEnter();
|
||||
virtual void onExit();
|
||||
virtual void onUpdate(float dt);
|
||||
virtual void onRender(RenderBackend &renderer);
|
||||
virtual void onRender(Renderer &renderer);
|
||||
virtual void onAttachToScene(Scene *scene);
|
||||
virtual void onDetachFromScene();
|
||||
|
||||
|
|
@ -155,7 +154,7 @@ public:
|
|||
// 内部方法
|
||||
// ------------------------------------------------------------------------
|
||||
void update(float dt);
|
||||
void render(RenderBackend &renderer);
|
||||
void render(Renderer &renderer);
|
||||
void sortChildren();
|
||||
|
||||
bool isRunning() const { return running_; }
|
||||
|
|
@ -167,7 +166,7 @@ public:
|
|||
|
||||
protected:
|
||||
// 子类重写
|
||||
virtual void onDraw(RenderBackend &renderer) {}
|
||||
virtual void onDraw(Renderer &renderer) {}
|
||||
virtual void onUpdateNode(float dt) {}
|
||||
virtual void generateRenderCommand(std::vector<RenderCommand> &commands,
|
||||
int zOrder) {};
|
||||
|
|
|
|||
|
|
@ -54,8 +54,8 @@ public:
|
|||
// ------------------------------------------------------------------------
|
||||
// 渲染和更新
|
||||
// ------------------------------------------------------------------------
|
||||
void renderScene(RenderBackend &renderer);
|
||||
virtual void renderContent(RenderBackend &renderer);
|
||||
void renderScene(Renderer &renderer);
|
||||
virtual void renderContent(Renderer &renderer);
|
||||
void updateScene(float dt);
|
||||
void collectRenderCommands(std::vector<RenderCommand> &commands,
|
||||
int parentZOrder = 0) override;
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ public:
|
|||
// 更新和渲染
|
||||
// ------------------------------------------------------------------------
|
||||
void update(float dt);
|
||||
void render(RenderBackend &renderer);
|
||||
void render(Renderer &renderer);
|
||||
void collectRenderCommands(std::vector<RenderCommand> &commands);
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
|
@ -143,7 +143,8 @@ private:
|
|||
* @param inScene 目标场景
|
||||
* @return 过渡场景智能指针
|
||||
*/
|
||||
Ptr<TransitionScene> createTransitionScene(TransitionType type, float duration,
|
||||
Ptr<TransitionScene> createTransitionScene(TransitionType type,
|
||||
float duration,
|
||||
Ptr<Scene> inScene);
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ public:
|
|||
Rect getBounds() const override;
|
||||
|
||||
protected:
|
||||
void onDraw(RenderBackend &renderer) override;
|
||||
void onDraw(Renderer &renderer) override;
|
||||
void generateRenderCommand(std::vector<RenderCommand> &commands,
|
||||
int zOrder) override;
|
||||
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ public:
|
|||
Rect getBounds() const override;
|
||||
|
||||
protected:
|
||||
void onDraw(RenderBackend &renderer) override;
|
||||
void onDraw(Renderer &renderer) override;
|
||||
void generateRenderCommand(std::vector<RenderCommand> &commands,
|
||||
int zOrder) override;
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ public:
|
|||
|
||||
protected:
|
||||
void onTransitionStart() override;
|
||||
void renderContent(RenderBackend &renderer) override;
|
||||
void renderContent(Renderer &renderer) override;
|
||||
void updateTransition(float dt) override;
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ protected:
|
|||
* @brief 渲染内容
|
||||
* 根据进度控制新旧场景的显示
|
||||
*/
|
||||
void renderContent(RenderBackend &renderer) override;
|
||||
void renderContent(Renderer &renderer) override;
|
||||
|
||||
private:
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ public:
|
|||
|
||||
protected:
|
||||
void onTransitionStart() override;
|
||||
void renderContent(RenderBackend &renderer) override;
|
||||
void renderContent(Renderer &renderer) override;
|
||||
void updateTransition(float dt) override;
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ public:
|
|||
|
||||
protected:
|
||||
void onTransitionStart() override;
|
||||
void renderContent(RenderBackend &renderer) override;
|
||||
void renderContent(Renderer &renderer) override;
|
||||
void updateTransition(float dt) override;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -63,7 +63,9 @@ public:
|
|||
/**
|
||||
* @brief 设置过渡完成回调
|
||||
*/
|
||||
void setFinishCallback(FinishCallback callback) { finishCallback_ = callback; }
|
||||
void setFinishCallback(FinishCallback callback) {
|
||||
finishCallback_ = callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取过渡持续时间
|
||||
|
|
@ -99,7 +101,7 @@ public:
|
|||
// ------------------------------------------------------------------------
|
||||
// 渲染 - 在 TransitionScene 上渲染新旧两个子场景
|
||||
// ------------------------------------------------------------------------
|
||||
void renderContent(RenderBackend &renderer) override;
|
||||
void renderContent(Renderer &renderer) override;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 生命周期
|
||||
|
|
@ -117,12 +119,12 @@ protected:
|
|||
/**
|
||||
* @brief 绘制源场景(旧场景)
|
||||
*/
|
||||
virtual void drawOutScene(RenderBackend &renderer);
|
||||
virtual void drawOutScene(Renderer &renderer);
|
||||
|
||||
/**
|
||||
* @brief 绘制目标场景(新场景)
|
||||
*/
|
||||
virtual void drawInScene(RenderBackend &renderer);
|
||||
virtual void drawInScene(Renderer &renderer);
|
||||
|
||||
/**
|
||||
* @brief 更新过渡进度(子类重写此方法更新动画)
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ public:
|
|||
|
||||
protected:
|
||||
void onTransitionStart() override;
|
||||
void renderContent(RenderBackend &renderer) override;
|
||||
void renderContent(Renderer &renderer) override;
|
||||
void updateTransition(float dt) override;
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <extra2d/core/service_interface.h>
|
||||
#include <extra2d/core/service_locator.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/scene/scene_manager.h>
|
||||
|
||||
namespace extra2d {
|
||||
|
|
@ -30,11 +31,12 @@ public:
|
|||
virtual bool isEmpty() const = 0;
|
||||
virtual bool hasScene(const std::string &name) const = 0;
|
||||
|
||||
virtual void render(RenderBackend& renderer) = 0;
|
||||
virtual void render(Renderer &renderer) = 0;
|
||||
virtual void collectRenderCommands(std::vector<RenderCommand> &commands) = 0;
|
||||
|
||||
virtual bool isTransitioning() const = 0;
|
||||
virtual void setTransitionCallback(SceneManager::TransitionCallback callback) = 0;
|
||||
virtual void
|
||||
setTransitionCallback(SceneManager::TransitionCallback callback) = 0;
|
||||
|
||||
virtual void end() = 0;
|
||||
virtual void purgeCachedScenes() = 0;
|
||||
|
|
@ -72,11 +74,12 @@ public:
|
|||
bool isEmpty() const override;
|
||||
bool hasScene(const std::string &name) const override;
|
||||
|
||||
void render(RenderBackend& renderer) override;
|
||||
void render(Renderer &renderer) override;
|
||||
void collectRenderCommands(std::vector<RenderCommand> &commands) override;
|
||||
|
||||
bool isTransitioning() const override;
|
||||
void setTransitionCallback(SceneManager::TransitionCallback callback) override;
|
||||
void
|
||||
setTransitionCallback(SceneManager::TransitionCallback callback) override;
|
||||
|
||||
void end() override;
|
||||
void purgeCachedScenes() override;
|
||||
|
|
@ -92,4 +95,4 @@ private:
|
|||
E2D_AUTO_REGISTER_SERVICE(ISceneService, SceneService);
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace extra2d
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
#include <extra2d/app/application.h>
|
||||
#include <extra2d/core/registry.h>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/graphics/core/render_module.h>
|
||||
#include <extra2d/graphics/memory/vram_manager.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/platform/glfw/glfw_window.h>
|
||||
#include <extra2d/platform/window_module.h>
|
||||
#include <extra2d/services/camera_service.h>
|
||||
|
|
@ -11,7 +11,6 @@
|
|||
#include <extra2d/services/scene_service.h>
|
||||
#include <extra2d/services/timer_service.h>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
static double getTimeSeconds() {
|
||||
|
|
@ -179,9 +178,7 @@ void Application::mainLoop() {
|
|||
}
|
||||
}
|
||||
|
||||
void Application::update() {
|
||||
ServiceLocator::instance().updateAll(deltaTime_);
|
||||
}
|
||||
void Application::update() { ServiceLocator::instance().updateAll(deltaTime_); }
|
||||
|
||||
void Application::render() {
|
||||
auto *renderMod = get<RenderModule>();
|
||||
|
|
@ -218,7 +215,7 @@ GLFWWindow *Application::window() {
|
|||
return winMod ? winMod->win() : nullptr;
|
||||
}
|
||||
|
||||
RenderBackend *Application::renderer() {
|
||||
Renderer *Application::renderer() {
|
||||
auto *renderMod = get<RenderModule>();
|
||||
return renderMod ? renderMod->renderer() : nullptr;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ bool RenderModule::init() {
|
|||
}
|
||||
|
||||
E2D_LOG_INFO("正在创建 OpenGL 渲染后端");
|
||||
renderer_ = makeUnique<GLRenderer>();
|
||||
renderer_ = makeUnique<Renderer>();
|
||||
|
||||
if (!renderer_) {
|
||||
E2D_LOG_ERROR("创建渲染后端失败");
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@
|
|||
#include <extra2d/services/logger_service.h>
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
// VBO 初始大小(用于 VRAM 跟踪)
|
||||
|
|
@ -22,7 +21,7 @@ static constexpr size_t SHAPE_VBO_SIZE = 1024 * sizeof(float);
|
|||
/**
|
||||
* @brief 构造函数,初始化OpenGL渲染器成员变量
|
||||
*/
|
||||
GLRenderer::GLRenderer()
|
||||
Renderer::Renderer()
|
||||
: window_(nullptr), shapeVao_(0), lineVao_(0), vsync_(true),
|
||||
shapeVertexCount_(0), currentShapeMode_(GL_TRIANGLES),
|
||||
lineVertexCount_(0), currentLineWidth_(1.0f) {
|
||||
|
|
@ -38,14 +37,14 @@ GLRenderer::GLRenderer()
|
|||
/**
|
||||
* @brief 析构函数,调用shutdown释放资源
|
||||
*/
|
||||
GLRenderer::~GLRenderer() { shutdown(); }
|
||||
Renderer::~Renderer() { shutdown(); }
|
||||
|
||||
/**
|
||||
* @brief 初始化OpenGL渲染器
|
||||
* @param window 窗口指针
|
||||
* @return 初始化成功返回true,失败返回false
|
||||
*/
|
||||
bool GLRenderer::init(GLFWWindow *window) {
|
||||
bool Renderer::init(GLFWWindow *window) {
|
||||
window_ = window;
|
||||
|
||||
// 初始化 OpenGL 上下文(Switch 平台已通过 SDL2 + EGL 初始化,GLContext
|
||||
|
|
@ -89,7 +88,7 @@ bool GLRenderer::init(GLFWWindow *window) {
|
|||
/**
|
||||
* @brief 关闭渲染器,释放所有GPU资源
|
||||
*/
|
||||
void GLRenderer::shutdown() {
|
||||
void Renderer::shutdown() {
|
||||
// 标记 GPU 上下文为无效
|
||||
// 这会在销毁 OpenGL 上下文之前通知所有 GPU 资源
|
||||
GPUContext::get().markInvalid();
|
||||
|
|
@ -118,7 +117,7 @@ void GLRenderer::shutdown() {
|
|||
* @brief 开始新帧,清除颜色缓冲区并重置统计信息
|
||||
* @param clearColor 清屏颜色
|
||||
*/
|
||||
void GLRenderer::beginFrame(const Color &clearColor) {
|
||||
void Renderer::beginFrame(const Color &clearColor) {
|
||||
// 应用管线状态
|
||||
pipeline_.applyAllStates();
|
||||
|
||||
|
|
@ -130,7 +129,7 @@ void GLRenderer::beginFrame(const Color &clearColor) {
|
|||
/**
|
||||
* @brief 结束当前帧,刷新所有待处理的渲染批次
|
||||
*/
|
||||
void GLRenderer::endFrame() {
|
||||
void Renderer::endFrame() {
|
||||
// 刷新所有待处理的精灵批次(自动批处理)
|
||||
if (autoBatchEnabled_ && batchActive_) {
|
||||
flush();
|
||||
|
|
@ -148,7 +147,7 @@ void GLRenderer::endFrame() {
|
|||
* @param width 视口宽度
|
||||
* @param height 视口高度
|
||||
*/
|
||||
void GLRenderer::setViewport(int x, int y, int width, int height) {
|
||||
void Renderer::setViewport(int x, int y, int width, int height) {
|
||||
// 使用 GLPipeline 管理视口状态
|
||||
pipeline_.setViewport(x, y, width, height);
|
||||
}
|
||||
|
|
@ -157,7 +156,7 @@ void GLRenderer::setViewport(int x, int y, int width, int height) {
|
|||
* @brief 设置垂直同步
|
||||
* @param enabled true启用垂直同步,false禁用
|
||||
*/
|
||||
void GLRenderer::setVSync(bool enabled) {
|
||||
void Renderer::setVSync(bool enabled) {
|
||||
vsync_ = enabled;
|
||||
// 通过窗口接口设置垂直同步
|
||||
if (window_) {
|
||||
|
|
@ -169,7 +168,7 @@ void GLRenderer::setVSync(bool enabled) {
|
|||
* @brief 设置混合模式
|
||||
* @param mode 混合模式枚举值
|
||||
*/
|
||||
void GLRenderer::setBlendMode(BlendMode mode) {
|
||||
void Renderer::setBlendMode(BlendMode mode) {
|
||||
// 使用 GLPipeline 管理混合状态
|
||||
pipeline_.setBlendMode(mode);
|
||||
}
|
||||
|
|
@ -178,7 +177,7 @@ void GLRenderer::setBlendMode(BlendMode mode) {
|
|||
* @brief 设置视图投影矩阵
|
||||
* @param matrix 4x4视图投影矩阵
|
||||
*/
|
||||
void GLRenderer::setViewProjection(const glm::mat4 &matrix) {
|
||||
void Renderer::setViewProjection(const glm::mat4 &matrix) {
|
||||
viewProjection_ = matrix;
|
||||
}
|
||||
|
||||
|
|
@ -186,7 +185,7 @@ void GLRenderer::setViewProjection(const glm::mat4 &matrix) {
|
|||
* @brief 压入变换矩阵到变换栈
|
||||
* @param transform 变换矩阵
|
||||
*/
|
||||
void GLRenderer::pushTransform(const glm::mat4 &transform) {
|
||||
void Renderer::pushTransform(const glm::mat4 &transform) {
|
||||
if (transformStack_.empty()) {
|
||||
transformStack_.push_back(transform);
|
||||
} else {
|
||||
|
|
@ -197,7 +196,7 @@ void GLRenderer::pushTransform(const glm::mat4 &transform) {
|
|||
/**
|
||||
* @brief 从变换栈弹出顶部变换矩阵
|
||||
*/
|
||||
void GLRenderer::popTransform() {
|
||||
void Renderer::popTransform() {
|
||||
if (!transformStack_.empty()) {
|
||||
transformStack_.pop_back();
|
||||
}
|
||||
|
|
@ -207,7 +206,7 @@ void GLRenderer::popTransform() {
|
|||
* @brief 获取当前累积的变换矩阵
|
||||
* @return 当前变换矩阵,如果栈为空则返回单位矩阵
|
||||
*/
|
||||
glm::mat4 GLRenderer::getCurrentTransform() const {
|
||||
glm::mat4 Renderer::getCurrentTransform() const {
|
||||
if (transformStack_.empty()) {
|
||||
return glm::mat4(1.0f);
|
||||
}
|
||||
|
|
@ -222,7 +221,7 @@ glm::mat4 GLRenderer::getCurrentTransform() const {
|
|||
* @param channels 颜色通道数
|
||||
* @return 创建的纹理智能指针
|
||||
*/
|
||||
Ptr<Texture> GLRenderer::createTexture(int width, int height,
|
||||
Ptr<Texture> Renderer::createTexture(int width, int height,
|
||||
const uint8_t *pixels, int channels) {
|
||||
return makePtr<GLTexture>(width, height, pixels, channels);
|
||||
}
|
||||
|
|
@ -232,14 +231,14 @@ Ptr<Texture> GLRenderer::createTexture(int width, int height,
|
|||
* @param filepath 纹理文件路径
|
||||
* @return 加载的纹理智能指针
|
||||
*/
|
||||
Ptr<Texture> GLRenderer::loadTexture(const std::string &filepath) {
|
||||
Ptr<Texture> Renderer::loadTexture(const std::string &filepath) {
|
||||
return makePtr<GLTexture>(filepath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 确保批处理已激活(自动批处理内部使用)
|
||||
*/
|
||||
void GLRenderer::ensureBatchActive() {
|
||||
void Renderer::ensureBatchActive() {
|
||||
if (!batchActive_) {
|
||||
spriteBatch_.begin(viewProjection_);
|
||||
batchActive_ = true;
|
||||
|
|
@ -251,7 +250,7 @@ void GLRenderer::ensureBatchActive() {
|
|||
/**
|
||||
* @brief 提交待处理的精灵(自动批处理内部使用)
|
||||
*/
|
||||
void GLRenderer::submitPendingSprites() {
|
||||
void Renderer::submitPendingSprites() {
|
||||
if (pendingSprites_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -266,7 +265,7 @@ void GLRenderer::submitPendingSprites() {
|
|||
* @brief 开始手动精灵批处理(高级用法)
|
||||
* @note 一般情况下不需要调用,drawSprite/drawText 会自动管理批处理
|
||||
*/
|
||||
void GLRenderer::beginSpriteBatch() {
|
||||
void Renderer::beginSpriteBatch() {
|
||||
// 如果自动批处理已激活,先提交
|
||||
if (autoBatchEnabled_ && batchActive_) {
|
||||
flush();
|
||||
|
|
@ -286,7 +285,7 @@ void GLRenderer::beginSpriteBatch() {
|
|||
* @param rotation 旋转角度(度)
|
||||
* @param anchor 锚点位置(0-1范围)
|
||||
*/
|
||||
void GLRenderer::drawSprite(const Texture &texture, const Rect &destRect,
|
||||
void Renderer::drawSprite(const Texture &texture, const Rect &destRect,
|
||||
const Rect &srcRect, const Color &tint,
|
||||
float rotation, const Vec2 &anchor) {
|
||||
// 自动批处理模式
|
||||
|
|
@ -356,7 +355,7 @@ void GLRenderer::drawSprite(const Texture &texture, const Rect &destRect,
|
|||
* @param position 绘制位置
|
||||
* @param tint 着色颜色
|
||||
*/
|
||||
void GLRenderer::drawSprite(const Texture &texture, const Vec2 &position,
|
||||
void Renderer::drawSprite(const Texture &texture, const Vec2 &position,
|
||||
const Color &tint) {
|
||||
Rect destRect(position.x, position.y, static_cast<float>(texture.getWidth()),
|
||||
static_cast<float>(texture.getHeight()));
|
||||
|
|
@ -369,7 +368,7 @@ void GLRenderer::drawSprite(const Texture &texture, const Vec2 &position,
|
|||
* @brief 结束手动精灵批处理并提交绘制
|
||||
* @note 一般情况下不需要调用
|
||||
*/
|
||||
void GLRenderer::endSpriteBatch() {
|
||||
void Renderer::endSpriteBatch() {
|
||||
if (autoBatchEnabled_) {
|
||||
// 自动模式下,只是标记批处理结束
|
||||
flush();
|
||||
|
|
@ -386,7 +385,7 @@ void GLRenderer::endSpriteBatch() {
|
|||
* @brief 立即提交当前批处理
|
||||
* @note 手动控制批处理提交时机,一般情况下不需要调用
|
||||
*/
|
||||
void GLRenderer::flush() {
|
||||
void Renderer::flush() {
|
||||
if (autoBatchEnabled_ && batchActive_) {
|
||||
submitPendingSprites();
|
||||
spriteBatch_.end();
|
||||
|
|
@ -403,8 +402,8 @@ void GLRenderer::flush() {
|
|||
* @param color 线条颜色
|
||||
* @param width 线条宽度
|
||||
*/
|
||||
void GLRenderer::drawLine(const Vec2 &start, const Vec2 &end,
|
||||
const Color &color, float width) {
|
||||
void Renderer::drawLine(const Vec2 &start, const Vec2 &end, const Color &color,
|
||||
float width) {
|
||||
// 如果线宽改变,需要先刷新线条批次
|
||||
if (width != currentLineWidth_) {
|
||||
flushLineBatch();
|
||||
|
|
@ -416,7 +415,7 @@ void GLRenderer::drawLine(const Vec2 &start, const Vec2 &end,
|
|||
addLineVertex(end.x, end.y, color);
|
||||
}
|
||||
|
||||
void GLRenderer::drawRect(const Rect &rect, const Color &color, float width) {
|
||||
void Renderer::drawRect(const Rect &rect, const Color &color, float width) {
|
||||
// 如果线宽改变,需要先刷新线条批次
|
||||
if (width != currentLineWidth_) {
|
||||
flushLineBatch();
|
||||
|
|
@ -448,7 +447,7 @@ void GLRenderer::drawRect(const Rect &rect, const Color &color, float width) {
|
|||
* @param rect 矩形区域
|
||||
* @param color 填充颜色
|
||||
*/
|
||||
void GLRenderer::fillRect(const Rect &rect, const Color &color) {
|
||||
void Renderer::fillRect(const Rect &rect, const Color &color) {
|
||||
// 提交当前批次(如果模式不同)
|
||||
submitShapeBatch(GL_TRIANGLES);
|
||||
|
||||
|
|
@ -477,8 +476,8 @@ void GLRenderer::fillRect(const Rect &rect, const Color &color) {
|
|||
* @param segments 分段数
|
||||
* @param width 线条宽度
|
||||
*/
|
||||
void GLRenderer::drawCircle(const Vec2 ¢er, float radius,
|
||||
const Color &color, int segments, float width) {
|
||||
void Renderer::drawCircle(const Vec2 ¢er, float radius, const Color &color,
|
||||
int segments, float width) {
|
||||
// 限制段数不超过缓存大小
|
||||
if (segments > static_cast<int>(MAX_CIRCLE_SEGMENTS)) {
|
||||
segments = static_cast<int>(MAX_CIRCLE_SEGMENTS);
|
||||
|
|
@ -511,8 +510,8 @@ void GLRenderer::drawCircle(const Vec2 ¢er, float radius,
|
|||
* @param color 填充颜色
|
||||
* @param segments 分段数
|
||||
*/
|
||||
void GLRenderer::fillCircle(const Vec2 ¢er, float radius,
|
||||
const Color &color, int segments) {
|
||||
void Renderer::fillCircle(const Vec2 ¢er, float radius, const Color &color,
|
||||
int segments) {
|
||||
// 限制段数不超过缓存大小
|
||||
if (segments > static_cast<int>(MAX_CIRCLE_SEGMENTS)) {
|
||||
segments = static_cast<int>(MAX_CIRCLE_SEGMENTS);
|
||||
|
|
@ -546,7 +545,7 @@ void GLRenderer::fillCircle(const Vec2 ¢er, float radius,
|
|||
* @param color 边框颜色
|
||||
* @param width 线条宽度
|
||||
*/
|
||||
void GLRenderer::drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3,
|
||||
void Renderer::drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3,
|
||||
const Color &color, float width) {
|
||||
drawLine(p1, p2, color, width);
|
||||
drawLine(p2, p3, color, width);
|
||||
|
|
@ -560,7 +559,7 @@ void GLRenderer::drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3,
|
|||
* @param p3 第三个顶点
|
||||
* @param color 填充颜色
|
||||
*/
|
||||
void GLRenderer::fillTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3,
|
||||
void Renderer::fillTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3,
|
||||
const Color &color) {
|
||||
submitShapeBatch(GL_TRIANGLES);
|
||||
|
||||
|
|
@ -575,8 +574,8 @@ void GLRenderer::fillTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3,
|
|||
* @param color 边框颜色
|
||||
* @param width 线条宽度
|
||||
*/
|
||||
void GLRenderer::drawPolygon(const std::vector<Vec2> &points,
|
||||
const Color &color, float width) {
|
||||
void Renderer::drawPolygon(const std::vector<Vec2> &points, const Color &color,
|
||||
float width) {
|
||||
if (points.size() < 2)
|
||||
return;
|
||||
|
||||
|
|
@ -600,7 +599,7 @@ void GLRenderer::drawPolygon(const std::vector<Vec2> &points,
|
|||
* @param points 顶点数组
|
||||
* @param color 填充颜色
|
||||
*/
|
||||
void GLRenderer::fillPolygon(const std::vector<Vec2> &points,
|
||||
void Renderer::fillPolygon(const std::vector<Vec2> &points,
|
||||
const Color &color) {
|
||||
if (points.size() < 3)
|
||||
return;
|
||||
|
|
@ -623,7 +622,7 @@ void GLRenderer::fillPolygon(const std::vector<Vec2> &points,
|
|||
* @param useSDF 是否使用SDF渲染
|
||||
* @return 创建的字体图集智能指针
|
||||
*/
|
||||
Ptr<FontAtlas> GLRenderer::createFontAtlas(const std::string &filepath,
|
||||
Ptr<FontAtlas> Renderer::createFontAtlas(const std::string &filepath,
|
||||
int fontSize, bool useSDF) {
|
||||
return makePtr<GLFontAtlas>(filepath, fontSize, useSDF);
|
||||
}
|
||||
|
|
@ -635,7 +634,7 @@ Ptr<FontAtlas> GLRenderer::createFontAtlas(const std::string &filepath,
|
|||
* @param position 绘制位置
|
||||
* @param color 文本颜色
|
||||
*/
|
||||
void GLRenderer::drawText(const FontAtlas &font, const std::string &text,
|
||||
void Renderer::drawText(const FontAtlas &font, const std::string &text,
|
||||
const Vec2 &position, const Color &color) {
|
||||
drawText(font, text, position.x, position.y, color);
|
||||
}
|
||||
|
|
@ -648,8 +647,8 @@ void GLRenderer::drawText(const FontAtlas &font, const std::string &text,
|
|||
* @param y Y坐标
|
||||
* @param color 文本颜色
|
||||
*/
|
||||
void GLRenderer::drawText(const FontAtlas &font, const std::string &text,
|
||||
float x, float y, const Color &color) {
|
||||
void Renderer::drawText(const FontAtlas &font, const std::string &text, float x,
|
||||
float y, const Color &color) {
|
||||
float cursorX = x;
|
||||
float cursorY = y;
|
||||
float baselineY = cursorY + font.getAscent();
|
||||
|
|
@ -789,12 +788,12 @@ void GLRenderer::drawText(const FontAtlas &font, const std::string &text,
|
|||
/**
|
||||
* @brief 重置渲染统计信息
|
||||
*/
|
||||
void GLRenderer::resetStats() { stats_ = Stats{}; }
|
||||
void Renderer::resetStats() { stats_ = RenderStats{}; }
|
||||
|
||||
/**
|
||||
* @brief 初始化形状渲染所需的OpenGL资源(VAO、VBO、着色器)
|
||||
*/
|
||||
void GLRenderer::initShapeRendering() {
|
||||
void Renderer::initShapeRendering() {
|
||||
// 从ShaderManager获取形状着色器
|
||||
shapeShader_ = ShaderManager::getInstance().getBuiltin("shape");
|
||||
if (!shapeShader_) {
|
||||
|
|
@ -871,7 +870,7 @@ void GLRenderer::initShapeRendering() {
|
|||
* @param y Y坐标
|
||||
* @param color 顶点颜色
|
||||
*/
|
||||
void GLRenderer::addShapeVertex(float x, float y, const Color &color) {
|
||||
void Renderer::addShapeVertex(float x, float y, const Color &color) {
|
||||
if (shapeVertexCount_ >= MAX_SHAPE_VERTICES) {
|
||||
flushShapeBatch();
|
||||
}
|
||||
|
|
@ -896,7 +895,7 @@ void GLRenderer::addShapeVertex(float x, float y, const Color &color) {
|
|||
* @param y Y坐标
|
||||
* @param color 顶点颜色
|
||||
*/
|
||||
void GLRenderer::addLineVertex(float x, float y, const Color &color) {
|
||||
void Renderer::addLineVertex(float x, float y, const Color &color) {
|
||||
if (lineVertexCount_ >= MAX_LINE_VERTICES) {
|
||||
flushLineBatch();
|
||||
}
|
||||
|
|
@ -919,7 +918,7 @@ void GLRenderer::addLineVertex(float x, float y, const Color &color) {
|
|||
* @brief 提交形状批次(如果需要切换绘制模式)
|
||||
* @param mode OpenGL绘制模式
|
||||
*/
|
||||
void GLRenderer::submitShapeBatch(GLenum mode) {
|
||||
void Renderer::submitShapeBatch(GLenum mode) {
|
||||
if (shapeVertexCount_ == 0)
|
||||
return;
|
||||
|
||||
|
|
@ -933,7 +932,7 @@ void GLRenderer::submitShapeBatch(GLenum mode) {
|
|||
/**
|
||||
* @brief 刷新形状批次,执行实际的OpenGL绘制调用
|
||||
*/
|
||||
void GLRenderer::flushShapeBatch() {
|
||||
void Renderer::flushShapeBatch() {
|
||||
if (shapeVertexCount_ == 0)
|
||||
return;
|
||||
|
||||
|
|
@ -966,7 +965,7 @@ void GLRenderer::flushShapeBatch() {
|
|||
/**
|
||||
* @brief 刷新线条批次,执行实际的OpenGL绘制调用
|
||||
*/
|
||||
void GLRenderer::flushLineBatch() {
|
||||
void Renderer::flushLineBatch() {
|
||||
if (lineVertexCount_ == 0)
|
||||
return;
|
||||
|
||||
|
|
@ -1004,7 +1003,7 @@ void GLRenderer::flushLineBatch() {
|
|||
* @param desc 帧缓冲描述
|
||||
* @return 创建的帧缓冲智能指针
|
||||
*/
|
||||
Ptr<GLFramebuffer> GLRenderer::createFramebuffer(const FramebufferDesc &desc) {
|
||||
Ptr<GLFramebuffer> Renderer::createFramebuffer(const FramebufferDesc &desc) {
|
||||
auto framebuffer = makePtr<GLFramebuffer>();
|
||||
if (!framebuffer->init(desc)) {
|
||||
E2D_LOG_ERROR("创建帧缓冲区失败");
|
||||
|
|
@ -1017,7 +1016,7 @@ Ptr<GLFramebuffer> GLRenderer::createFramebuffer(const FramebufferDesc &desc) {
|
|||
* @brief 绑定帧缓冲(作为渲染目标)
|
||||
* @param framebuffer 帧缓冲对象指针,传入 nullptr 则绑定默认帧缓冲
|
||||
*/
|
||||
void GLRenderer::bindFramebuffer(GLFramebuffer *framebuffer) {
|
||||
void Renderer::bindFramebuffer(GLFramebuffer *framebuffer) {
|
||||
// 先刷新所有待处理的渲染批次
|
||||
flush();
|
||||
flushShapeBatch();
|
||||
|
|
@ -1039,13 +1038,13 @@ void GLRenderer::bindFramebuffer(GLFramebuffer *framebuffer) {
|
|||
/**
|
||||
* @brief 解绑帧缓冲(恢复到默认帧缓冲)
|
||||
*/
|
||||
void GLRenderer::unbindFramebuffer() { bindFramebuffer(nullptr); }
|
||||
void Renderer::unbindFramebuffer() { bindFramebuffer(nullptr); }
|
||||
|
||||
/**
|
||||
* @brief 获取默认帧缓冲
|
||||
* @return 默认帧缓冲智能指针
|
||||
*/
|
||||
Ptr<GLFramebuffer> GLRenderer::getDefaultFramebuffer() const {
|
||||
Ptr<GLFramebuffer> Renderer::getDefaultFramebuffer() const {
|
||||
if (!defaultFramebuffer_) {
|
||||
// 延迟创建默认帧缓冲对象(代表系统默认帧缓冲,ID 为 0)
|
||||
defaultFramebuffer_ = makePtr<GLFramebuffer>();
|
||||
|
|
@ -1061,7 +1060,7 @@ Ptr<GLFramebuffer> GLRenderer::getDefaultFramebuffer() const {
|
|||
* @param clearDepth 是否清除深度缓冲
|
||||
* @param clearStencil 是否清除模板缓冲
|
||||
*/
|
||||
void GLRenderer::clearFramebuffer(const Color &color, bool clearColor,
|
||||
void Renderer::clearFramebuffer(const Color &color, bool clearColor,
|
||||
bool clearDepth, bool clearStencil) {
|
||||
GLbitfield mask = 0;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/graphics/texture/texture_pool.h>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/scene/scene.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
|
@ -17,13 +17,8 @@ namespace extra2d {
|
|||
* 创建一个未初始化的纹理池
|
||||
*/
|
||||
TexturePool::TexturePool()
|
||||
: scene_(nullptr)
|
||||
, maxMemoryUsage_(0)
|
||||
, currentMemoryUsage_(0)
|
||||
, cacheHits_(0)
|
||||
, cacheMisses_(0)
|
||||
, evictionCount_(0) {
|
||||
}
|
||||
: scene_(nullptr), maxMemoryUsage_(0), currentMemoryUsage_(0),
|
||||
cacheHits_(0), cacheMisses_(0), evictionCount_(0) {}
|
||||
|
||||
/**
|
||||
* @brief 构造函数
|
||||
|
|
@ -33,12 +28,8 @@ TexturePool::TexturePool()
|
|||
* 创建一个指定场景和内存限制的纹理池
|
||||
*/
|
||||
TexturePool::TexturePool(Scene *scene, size_t maxMemoryUsage)
|
||||
: scene_(scene)
|
||||
, maxMemoryUsage_(maxMemoryUsage)
|
||||
, currentMemoryUsage_(0)
|
||||
, cacheHits_(0)
|
||||
, cacheMisses_(0)
|
||||
, evictionCount_(0) {
|
||||
: scene_(scene), maxMemoryUsage_(maxMemoryUsage), currentMemoryUsage_(0),
|
||||
cacheHits_(0), cacheMisses_(0), evictionCount_(0) {
|
||||
E2D_LOG_INFO("TexturePool 已创建,最大内存: {} 字节", maxMemoryUsage);
|
||||
}
|
||||
|
||||
|
|
@ -77,7 +68,8 @@ TexturePool::~TexturePool() {
|
|||
*
|
||||
* 加载完整纹理文件到纹理池
|
||||
*/
|
||||
TextureRef TexturePool::load(const std::string& path, const TextureLoadOptions& options) {
|
||||
TextureRef TexturePool::load(const std::string &path,
|
||||
const TextureLoadOptions &options) {
|
||||
return load(path, Rect::Zero(), options);
|
||||
}
|
||||
|
||||
|
|
@ -112,11 +104,11 @@ TextureRef TexturePool::load(const std::string& path, const Rect& region,
|
|||
cacheMisses_.fetch_add(1, std::memory_order_relaxed);
|
||||
|
||||
// 获取渲染后端
|
||||
RenderBackend* backend = nullptr;
|
||||
Renderer *backend = nullptr;
|
||||
if (scene_) {
|
||||
// 假设 Scene 有获取 RenderBackend 的方法
|
||||
// 假设 Scene 有获取 Renderer 的方法
|
||||
// 这里需要根据实际接口调整
|
||||
backend = nullptr; // TODO: 从 Scene 获取 RenderBackend
|
||||
backend = nullptr; // TODO: 从 Scene 获取 Renderer
|
||||
}
|
||||
|
||||
if (!backend) {
|
||||
|
|
@ -135,7 +127,8 @@ TextureRef TexturePool::load(const std::string& path, const Rect& region,
|
|||
size_t memorySize = calculateTextureMemory(texture.get());
|
||||
|
||||
// 检查内存限制
|
||||
if (maxMemoryUsage_ > 0 && currentMemoryUsage_ + memorySize > maxMemoryUsage_) {
|
||||
if (maxMemoryUsage_ > 0 &&
|
||||
currentMemoryUsage_ + memorySize > maxMemoryUsage_) {
|
||||
// 尝试淘汰
|
||||
evictLRU(currentMemoryUsage_ + memorySize - maxMemoryUsage_);
|
||||
|
||||
|
|
@ -172,8 +165,9 @@ TextureRef TexturePool::load(const std::string& path, const Rect& region,
|
|||
*
|
||||
* 从内存中的像素数据创建纹理并加入纹理池
|
||||
*/
|
||||
TextureRef TexturePool::loadFromMemory(const uint8_t* data, int width, int height,
|
||||
int channels, const std::string& key) {
|
||||
TextureRef TexturePool::loadFromMemory(const uint8_t *data, int width,
|
||||
int height, int channels,
|
||||
const std::string &key) {
|
||||
TextureKey textureKey(key);
|
||||
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
|
@ -190,9 +184,9 @@ TextureRef TexturePool::loadFromMemory(const uint8_t* data, int width, int heigh
|
|||
cacheMisses_.fetch_add(1, std::memory_order_relaxed);
|
||||
|
||||
// 获取渲染后端
|
||||
RenderBackend* backend = nullptr;
|
||||
Renderer *backend = nullptr;
|
||||
if (scene_) {
|
||||
backend = nullptr; // TODO: 从 Scene 获取 RenderBackend
|
||||
backend = nullptr; // TODO: 从 Scene 获取 Renderer
|
||||
}
|
||||
|
||||
if (!backend) {
|
||||
|
|
@ -211,7 +205,8 @@ TextureRef TexturePool::loadFromMemory(const uint8_t* data, int width, int heigh
|
|||
size_t memorySize = calculateTextureMemory(texture.get());
|
||||
|
||||
// 检查内存限制
|
||||
if (maxMemoryUsage_ > 0 && currentMemoryUsage_ + memorySize > maxMemoryUsage_) {
|
||||
if (maxMemoryUsage_ > 0 &&
|
||||
currentMemoryUsage_ + memorySize > maxMemoryUsage_) {
|
||||
evictLRU(currentMemoryUsage_ + memorySize - maxMemoryUsage_);
|
||||
|
||||
if (currentMemoryUsage_ + memorySize > maxMemoryUsage_) {
|
||||
|
|
@ -221,7 +216,8 @@ TextureRef TexturePool::loadFromMemory(const uint8_t* data, int width, int heigh
|
|||
}
|
||||
|
||||
// 创建缓存条目
|
||||
auto result = cache_.emplace(textureKey, TexturePoolEntry(nullptr, textureKey, 0));
|
||||
auto result =
|
||||
cache_.emplace(textureKey, TexturePoolEntry(nullptr, textureKey, 0));
|
||||
if (result.second) {
|
||||
result.first->second.texture = texture;
|
||||
result.first->second.memorySize = memorySize;
|
||||
|
|
@ -243,7 +239,8 @@ TextureRef TexturePool::loadFromMemory(const uint8_t* data, int width, int heigh
|
|||
*
|
||||
* 如果纹理已缓存则返回缓存,否则加载纹理
|
||||
*/
|
||||
TextureRef TexturePool::getOrLoad(const std::string& path, const TextureLoadOptions& options) {
|
||||
TextureRef TexturePool::getOrLoad(const std::string &path,
|
||||
const TextureLoadOptions &options) {
|
||||
return getOrLoad(path, Rect::Zero(), options);
|
||||
}
|
||||
|
||||
|
|
@ -277,9 +274,9 @@ TextureRef TexturePool::getOrLoad(const std::string& path, const Rect& region,
|
|||
|
||||
cacheMisses_.fetch_add(1, std::memory_order_relaxed);
|
||||
|
||||
RenderBackend* backend = nullptr;
|
||||
Renderer *backend = nullptr;
|
||||
if (scene_) {
|
||||
backend = nullptr; // TODO: 从 Scene 获取 RenderBackend
|
||||
backend = nullptr; // TODO: 从 Scene 获取 Renderer
|
||||
}
|
||||
|
||||
if (!backend) {
|
||||
|
|
@ -295,7 +292,8 @@ TextureRef TexturePool::getOrLoad(const std::string& path, const Rect& region,
|
|||
|
||||
size_t memorySize = calculateTextureMemory(texture.get());
|
||||
|
||||
if (maxMemoryUsage_ > 0 && currentMemoryUsage_ + memorySize > maxMemoryUsage_) {
|
||||
if (maxMemoryUsage_ > 0 &&
|
||||
currentMemoryUsage_ + memorySize > maxMemoryUsage_) {
|
||||
evictLRU(currentMemoryUsage_ + memorySize - maxMemoryUsage_);
|
||||
|
||||
if (currentMemoryUsage_ + memorySize > maxMemoryUsage_) {
|
||||
|
|
@ -352,7 +350,8 @@ uint32_t TexturePool::release(const TextureKey& key) {
|
|||
|
||||
auto it = cache_.find(key);
|
||||
if (it != cache_.end()) {
|
||||
uint32_t count = it->second.refCount.fetch_sub(1, std::memory_order_relaxed);
|
||||
uint32_t count =
|
||||
it->second.refCount.fetch_sub(1, std::memory_order_relaxed);
|
||||
return count > 0 ? count - 1 : 0;
|
||||
}
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -511,12 +511,12 @@ void Node::onUpdate(float dt) {
|
|||
}
|
||||
|
||||
/**
|
||||
* @brief 渲染回调
|
||||
* @param renderer 渲染后端引用
|
||||
* @brief 渲染节点及其子节点
|
||||
* @param renderer 渲染器引用
|
||||
*
|
||||
* 如果可见则绘制自身,然后递归渲染所有子节点
|
||||
*/
|
||||
void Node::onRender(RenderBackend &renderer) {
|
||||
void Node::onRender(Renderer &renderer) {
|
||||
if (!visible_)
|
||||
return;
|
||||
|
||||
|
|
@ -579,7 +579,7 @@ void Node::update(float dt) { onUpdate(dt); }
|
|||
*
|
||||
* 如果需要则对子节点排序,然后调用onRender进行渲染
|
||||
*/
|
||||
void Node::render(RenderBackend &renderer) {
|
||||
void Node::render(Renderer &renderer) {
|
||||
if (childrenOrderDirty_) {
|
||||
sortChildren();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/graphics/core/render_command.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/scene/scene.h>
|
||||
#include <extra2d/services/logger_service.h>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
/**
|
||||
|
|
@ -48,7 +49,7 @@ void Scene::setViewportSize(const Size &size) {
|
|||
*
|
||||
* 如果场景不可见则直接返回,否则开始帧渲染、渲染内容并结束帧
|
||||
*/
|
||||
void Scene::renderScene(RenderBackend &renderer) {
|
||||
void Scene::renderScene(Renderer &renderer) {
|
||||
if (!isVisible())
|
||||
return;
|
||||
|
||||
|
|
@ -65,7 +66,7 @@ void Scene::renderScene(RenderBackend &renderer) {
|
|||
* 批量更新节点变换,开始精灵批处理并渲染
|
||||
* 注意:视图投影矩阵由 Application 通过 CameraService 设置
|
||||
*/
|
||||
void Scene::renderContent(RenderBackend &renderer) {
|
||||
void Scene::renderContent(Renderer &renderer) {
|
||||
if (!isVisible())
|
||||
return;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#include <algorithm>
|
||||
#include <extra2d/app/application.h>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/graphics/core/render_command.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/platform/keys.h>
|
||||
#include <extra2d/scene/scene_manager.h>
|
||||
#include <extra2d/scene/transition_box_scene.h>
|
||||
|
|
@ -13,6 +13,7 @@
|
|||
#include <extra2d/services/event_service.h>
|
||||
#include <extra2d/services/logger_service.h>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
namespace {
|
||||
|
|
@ -89,13 +90,15 @@ void SceneManager::setupEventListeners() {
|
|||
return;
|
||||
}
|
||||
|
||||
mouseMoveListener_ = eventService->addListener(EventType::MouseMoved, [this](Event &e) {
|
||||
mouseMoveListener_ =
|
||||
eventService->addListener(EventType::MouseMoved, [this](Event &e) {
|
||||
auto &mouseEvent = std::get<MouseMoveEvent>(e.data);
|
||||
mousePos_ = mouseEvent.position;
|
||||
mouseDelta_ = mouseEvent.delta;
|
||||
});
|
||||
|
||||
mousePressListener_ = eventService->addListener(EventType::MouseButtonPressed, [this](Event &e) {
|
||||
mousePressListener_ = eventService->addListener(
|
||||
EventType::MouseButtonPressed, [this](Event &e) {
|
||||
auto &mouseEvent = std::get<MouseButtonEvent>(e.data);
|
||||
if (mouseEvent.button == static_cast<int>(Mouse::Left)) {
|
||||
mouseLeftPressed_ = true;
|
||||
|
|
@ -103,7 +106,8 @@ void SceneManager::setupEventListeners() {
|
|||
}
|
||||
});
|
||||
|
||||
mouseReleaseListener_ = eventService->addListener(EventType::MouseButtonReleased, [this](Event &e) {
|
||||
mouseReleaseListener_ = eventService->addListener(
|
||||
EventType::MouseButtonReleased, [this](Event &e) {
|
||||
auto &mouseEvent = std::get<MouseButtonEvent>(e.data);
|
||||
if (mouseEvent.button == static_cast<int>(Mouse::Left)) {
|
||||
mouseLeftReleased_ = true;
|
||||
|
|
@ -111,7 +115,8 @@ void SceneManager::setupEventListeners() {
|
|||
}
|
||||
});
|
||||
|
||||
scrollListener_ = eventService->addListener(EventType::MouseScrolled, [this](Event &e) {
|
||||
scrollListener_ =
|
||||
eventService->addListener(EventType::MouseScrolled, [this](Event &e) {
|
||||
auto &scrollEvent = std::get<MouseScrollEvent>(e.data);
|
||||
scrollDelta_ = scrollEvent.offset.y;
|
||||
});
|
||||
|
|
@ -610,7 +615,7 @@ void SceneManager::update(float dt) {
|
|||
*
|
||||
* 使用当前场景的背景色清除帧缓冲并渲染场景内容
|
||||
*/
|
||||
void SceneManager::render(RenderBackend &renderer) {
|
||||
void SceneManager::render(Renderer &renderer) {
|
||||
Color clearColor = Colors::Black;
|
||||
if (!sceneStack_.empty()) {
|
||||
clearColor = sceneStack_.top()->getBackgroundColor();
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/graphics/core/render_command.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/scene/shape_node.h>
|
||||
#include <limits>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
/**
|
||||
|
|
@ -196,24 +197,18 @@ Ptr<ShapeNode> ShapeNode::createFilledPolygon(const std::vector<Vec2> &points,
|
|||
* @brief 设置形状的所有顶点
|
||||
* @param points 顶点坐标数组
|
||||
*/
|
||||
void ShapeNode::setPoints(const std::vector<Vec2> &points) {
|
||||
points_ = points;
|
||||
}
|
||||
void ShapeNode::setPoints(const std::vector<Vec2> &points) { points_ = points; }
|
||||
|
||||
/**
|
||||
* @brief 添加一个顶点到形状
|
||||
* @param point 要添加的顶点坐标
|
||||
*/
|
||||
void ShapeNode::addPoint(const Vec2 &point) {
|
||||
points_.push_back(point);
|
||||
}
|
||||
void ShapeNode::addPoint(const Vec2 &point) { points_.push_back(point); }
|
||||
|
||||
/**
|
||||
* @brief 清除所有顶点
|
||||
*/
|
||||
void ShapeNode::clearPoints() {
|
||||
points_.clear();
|
||||
}
|
||||
void ShapeNode::clearPoints() { points_.clear(); }
|
||||
|
||||
/**
|
||||
* @brief 获取形状的边界矩形
|
||||
|
|
@ -271,7 +266,7 @@ Rect ShapeNode::getBounds() const {
|
|||
* 注意:变换矩阵已由 Node::onRender 通过 pushTransform 应用,
|
||||
* 此处直接使用本地坐标即可。
|
||||
*/
|
||||
void ShapeNode::onDraw(RenderBackend &renderer) {
|
||||
void ShapeNode::onDraw(Renderer &renderer) {
|
||||
if (points_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -361,16 +356,16 @@ void ShapeNode::generateRenderCommand(std::vector<RenderCommand> &commands,
|
|||
case ShapeType::Point:
|
||||
if (!points_.empty()) {
|
||||
cmd.type = RenderCommandType::FilledCircle;
|
||||
cmd.data =
|
||||
CircleCommandData{points_[0] + offset, lineWidth_ * 0.5f, color_, 8, 0.0f, true};
|
||||
cmd.data = CircleCommandData{
|
||||
points_[0] + offset, lineWidth_ * 0.5f, color_, 8, 0.0f, true};
|
||||
}
|
||||
break;
|
||||
|
||||
case ShapeType::Line:
|
||||
if (points_.size() >= 2) {
|
||||
cmd.type = RenderCommandType::Line;
|
||||
cmd.data = LineCommandData{points_[0] + offset, points_[1] + offset, color_,
|
||||
lineWidth_};
|
||||
cmd.data = LineCommandData{points_[0] + offset, points_[1] + offset,
|
||||
color_, lineWidth_};
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -380,14 +375,14 @@ void ShapeNode::generateRenderCommand(std::vector<RenderCommand> &commands,
|
|||
cmd.type = RenderCommandType::FilledRect;
|
||||
Rect rect(points_[0].x, points_[0].y, points_[2].x - points_[0].x,
|
||||
points_[2].y - points_[0].y);
|
||||
cmd.data =
|
||||
RectCommandData{Rect(rect.origin + offset, rect.size), color_, 0.0f, true};
|
||||
cmd.data = RectCommandData{Rect(rect.origin + offset, rect.size),
|
||||
color_, 0.0f, true};
|
||||
} else {
|
||||
cmd.type = RenderCommandType::Rect;
|
||||
Rect rect(points_[0].x, points_[0].y, points_[2].x - points_[0].x,
|
||||
points_[2].y - points_[0].y);
|
||||
cmd.data =
|
||||
RectCommandData{Rect(rect.origin + offset, rect.size), color_, lineWidth_, false};
|
||||
cmd.data = RectCommandData{Rect(rect.origin + offset, rect.size),
|
||||
color_, lineWidth_, false};
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
@ -397,12 +392,12 @@ void ShapeNode::generateRenderCommand(std::vector<RenderCommand> &commands,
|
|||
float radius = points_[1].x;
|
||||
if (filled_) {
|
||||
cmd.type = RenderCommandType::FilledCircle;
|
||||
cmd.data =
|
||||
CircleCommandData{points_[0] + offset, radius, color_, segments_, 0.0f, true};
|
||||
cmd.data = CircleCommandData{points_[0] + offset, radius, color_,
|
||||
segments_, 0.0f, true};
|
||||
} else {
|
||||
cmd.type = RenderCommandType::Circle;
|
||||
cmd.data = CircleCommandData{points_[0] + offset, radius, color_, segments_,
|
||||
lineWidth_, false};
|
||||
cmd.data = CircleCommandData{points_[0] + offset, radius, color_,
|
||||
segments_, lineWidth_, false};
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
@ -435,7 +430,8 @@ void ShapeNode::generateRenderCommand(std::vector<RenderCommand> &commands,
|
|||
cmd.data = PolygonCommandData{transformedPoints, color_, 0.0f, true};
|
||||
} else {
|
||||
cmd.type = RenderCommandType::Polygon;
|
||||
cmd.data = PolygonCommandData{transformedPoints, color_, lineWidth_, false};
|
||||
cmd.data =
|
||||
PolygonCommandData{transformedPoints, color_, lineWidth_, false};
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/graphics/core/render_command.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/graphics/texture/texture.h>
|
||||
#include <extra2d/scene/sprite.h>
|
||||
|
||||
|
|
@ -127,7 +127,7 @@ Rect Sprite::getBounds() const {
|
|||
*
|
||||
* 使用世界变换计算最终位置、缩放和旋转,然后绘制精灵
|
||||
*/
|
||||
void Sprite::onDraw(RenderBackend &renderer) {
|
||||
void Sprite::onDraw(Renderer &renderer) {
|
||||
if (!texture_ || !texture_->isValid()) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -151,7 +151,7 @@ void Sprite::onDraw(RenderBackend &renderer) {
|
|||
|
||||
auto anchor = getAnchor();
|
||||
|
||||
// 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸
|
||||
// 锚点由 Renderer 在绘制时处理,这里只传递位置和尺寸
|
||||
Rect destRect(worldX, worldY, width * worldScaleX, height * worldScaleY);
|
||||
|
||||
// Adjust source rect for flipping
|
||||
|
|
@ -204,7 +204,7 @@ void Sprite::generateRenderCommand(std::vector<RenderCommand> &commands,
|
|||
|
||||
auto anchor = getAnchor();
|
||||
|
||||
// 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸
|
||||
// 锚点由 Renderer 在绘制时处理,这里只传递位置和尺寸
|
||||
Rect destRect(worldX, worldY, width * worldScaleX, height * worldScaleY);
|
||||
|
||||
// 调整源矩形(翻转)
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
#include <extra2d/scene/transition_box_scene.h>
|
||||
#include <algorithm>
|
||||
#include <extra2d/app/application.h>
|
||||
#include <extra2d/core/color.h>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/platform/glfw/glfw_window.h>
|
||||
#include <algorithm>
|
||||
#include <extra2d/scene/transition_box_scene.h>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
/**
|
||||
|
|
@ -25,9 +26,8 @@ TransitionBoxScene::TransitionBoxScene(float duration, Ptr<Scene> inScene,
|
|||
* @param divisions 方块分割数
|
||||
* @return 过渡场景智能指针
|
||||
*/
|
||||
Ptr<TransitionBoxScene> TransitionBoxScene::create(float duration,
|
||||
Ptr<Scene> inScene,
|
||||
int divisions) {
|
||||
Ptr<TransitionBoxScene>
|
||||
TransitionBoxScene::create(float duration, Ptr<Scene> inScene, int divisions) {
|
||||
return makePtr<TransitionBoxScene>(duration, inScene, divisions);
|
||||
}
|
||||
|
||||
|
|
@ -36,8 +36,7 @@ Ptr<TransitionBoxScene> TransitionBoxScene::create(float duration,
|
|||
*
|
||||
* 方块过渡不需要特殊的初始化
|
||||
*/
|
||||
void TransitionBoxScene::onTransitionStart() {
|
||||
}
|
||||
void TransitionBoxScene::onTransitionStart() {}
|
||||
|
||||
/**
|
||||
* @brief 更新过渡进度
|
||||
|
|
@ -53,7 +52,7 @@ void TransitionBoxScene::updateTransition(float dt) {
|
|||
*
|
||||
* 先渲染新场景,然后绘制方块遮罩逐渐消失
|
||||
*/
|
||||
void TransitionBoxScene::renderContent(RenderBackend &renderer) {
|
||||
void TransitionBoxScene::renderContent(Renderer &renderer) {
|
||||
auto &app = Application::get();
|
||||
float windowWidth = static_cast<float>(app.window()->width());
|
||||
float windowHeight = static_cast<float>(app.window()->height());
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include <extra2d/app/application.h>
|
||||
#include <extra2d/core/service_locator.h>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/platform/glfw/glfw_window.h>
|
||||
#include <extra2d/scene/transition_fade_scene.h>
|
||||
#include <extra2d/services/logger_service.h>
|
||||
|
|
@ -50,7 +50,7 @@ void TransitionFadeScene::updateTransition(float dt) {
|
|||
}
|
||||
}
|
||||
|
||||
void TransitionFadeScene::renderContent(RenderBackend &renderer) {
|
||||
void TransitionFadeScene::renderContent(Renderer &renderer) {
|
||||
auto &app = Application::get();
|
||||
float windowWidth = static_cast<float>(app.window()->width());
|
||||
float windowHeight = static_cast<float>(app.window()->height());
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
#include <extra2d/scene/transition_flip_scene.h>
|
||||
#include <extra2d/core/math_types.h>
|
||||
#include <extra2d/graphics/camera/camera.h>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/scene/transition_flip_scene.h>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
|
|
@ -22,9 +23,8 @@ TransitionFlipScene::TransitionFlipScene(float duration, Ptr<Scene> inScene,
|
|||
* @param axis 翻转轴
|
||||
* @return 过渡场景智能指针
|
||||
*/
|
||||
Ptr<TransitionFlipScene> TransitionFlipScene::create(float duration,
|
||||
Ptr<Scene> inScene,
|
||||
Axis axis) {
|
||||
Ptr<TransitionFlipScene>
|
||||
TransitionFlipScene::create(float duration, Ptr<Scene> inScene, Axis axis) {
|
||||
return makePtr<TransitionFlipScene>(duration, inScene, axis);
|
||||
}
|
||||
|
||||
|
|
@ -33,8 +33,7 @@ Ptr<TransitionFlipScene> TransitionFlipScene::create(float duration,
|
|||
*
|
||||
* 翻页过渡不需要特殊的初始化
|
||||
*/
|
||||
void TransitionFlipScene::onTransitionStart() {
|
||||
}
|
||||
void TransitionFlipScene::onTransitionStart() {}
|
||||
|
||||
/**
|
||||
* @brief 更新过渡进度
|
||||
|
|
@ -50,8 +49,9 @@ void TransitionFlipScene::updateTransition(float dt) {
|
|||
*
|
||||
* 根据进度控制新旧场景的翻转角度
|
||||
*/
|
||||
void TransitionFlipScene::renderContent(RenderBackend &renderer) {
|
||||
float easeProgress = progress_ < 0.5f ? 2.0f * progress_ * progress_
|
||||
void TransitionFlipScene::renderContent(Renderer &renderer) {
|
||||
float easeProgress = progress_ < 0.5f
|
||||
? 2.0f * progress_ * progress_
|
||||
: -1.0f + (4.0f - 2.0f * progress_) * progress_;
|
||||
|
||||
float angle = easeProgress * PI_F;
|
||||
|
|
@ -103,7 +103,6 @@ void TransitionFlipScene::renderContent(RenderBackend &renderer) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace extra2d
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
#include <extra2d/scene/transition_scale_scene.h>
|
||||
#include <extra2d/graphics/camera/camera.h>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <algorithm>
|
||||
#include <extra2d/graphics/camera/camera.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/scene/transition_scale_scene.h>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
|
|
@ -29,8 +30,7 @@ Ptr<TransitionScaleScene> TransitionScaleScene::create(float duration,
|
|||
*
|
||||
* 缩放过渡不需要特殊的初始化
|
||||
*/
|
||||
void TransitionScaleScene::onTransitionStart() {
|
||||
}
|
||||
void TransitionScaleScene::onTransitionStart() {}
|
||||
|
||||
/**
|
||||
* @brief 更新过渡进度
|
||||
|
|
@ -46,8 +46,9 @@ void TransitionScaleScene::updateTransition(float dt) {
|
|||
*
|
||||
* 根据进度控制新旧场景的缩放比例
|
||||
*/
|
||||
void TransitionScaleScene::renderContent(RenderBackend &renderer) {
|
||||
float easeProgress = progress_ < 0.5f ? 2.0f * progress_ * progress_
|
||||
void TransitionScaleScene::renderContent(Renderer &renderer) {
|
||||
float easeProgress = progress_ < 0.5f
|
||||
? 2.0f * progress_ * progress_
|
||||
: -1.0f + (4.0f - 2.0f * progress_) * progress_;
|
||||
|
||||
if (outScene_) {
|
||||
|
|
@ -83,7 +84,6 @@ void TransitionScaleScene::renderContent(RenderBackend &renderer) {
|
|||
camera->setZoom(originalZoom);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace extra2d
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#include <extra2d/core/service_locator.h>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/scene/transition_scene.h>
|
||||
#include <extra2d/services/logger_service.h>
|
||||
|
||||
|
|
@ -119,7 +119,7 @@ void TransitionScene::cancel(bool immediate) {
|
|||
*
|
||||
* 默认先渲染退出场景,再渲染进入场景
|
||||
*/
|
||||
void TransitionScene::renderContent(RenderBackend &renderer) {
|
||||
void TransitionScene::renderContent(Renderer &renderer) {
|
||||
drawOutScene(renderer);
|
||||
drawInScene(renderer);
|
||||
}
|
||||
|
|
@ -128,7 +128,7 @@ void TransitionScene::renderContent(RenderBackend &renderer) {
|
|||
* @brief 绘制退出场景
|
||||
* @param renderer 渲染后端引用
|
||||
*/
|
||||
void TransitionScene::drawOutScene(RenderBackend &renderer) {
|
||||
void TransitionScene::drawOutScene(Renderer &renderer) {
|
||||
if (outScene_) {
|
||||
outScene_->renderContent(renderer);
|
||||
}
|
||||
|
|
@ -138,7 +138,7 @@ void TransitionScene::drawOutScene(RenderBackend &renderer) {
|
|||
* @brief 绘制进入场景
|
||||
* @param renderer 渲染后端引用
|
||||
*/
|
||||
void TransitionScene::drawInScene(RenderBackend &renderer) {
|
||||
void TransitionScene::drawInScene(Renderer &renderer) {
|
||||
if (inScene_) {
|
||||
inScene_->renderContent(renderer);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
#include <extra2d/scene/transition_slide_scene.h>
|
||||
#include <extra2d/graphics/camera/camera.h>
|
||||
#include <extra2d/graphics/core/render_backend.h>
|
||||
#include <algorithm>
|
||||
#include <extra2d/graphics/camera/camera.h>
|
||||
#include <extra2d/graphics/opengl/gl_renderer.h>
|
||||
#include <extra2d/scene/transition_slide_scene.h>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
|
|
@ -22,8 +23,9 @@ TransitionSlideScene::TransitionSlideScene(float duration, Ptr<Scene> inScene,
|
|||
* @param direction 滑动方向
|
||||
* @return 过渡场景智能指针
|
||||
*/
|
||||
Ptr<TransitionSlideScene> TransitionSlideScene::create(
|
||||
float duration, Ptr<Scene> inScene, TransitionDirection direction) {
|
||||
Ptr<TransitionSlideScene>
|
||||
TransitionSlideScene::create(float duration, Ptr<Scene> inScene,
|
||||
TransitionDirection direction) {
|
||||
return makePtr<TransitionSlideScene>(duration, inScene, direction);
|
||||
}
|
||||
|
||||
|
|
@ -32,8 +34,7 @@ Ptr<TransitionSlideScene> TransitionSlideScene::create(
|
|||
*
|
||||
* 滑动过渡不需要特殊的初始化
|
||||
*/
|
||||
void TransitionSlideScene::onTransitionStart() {
|
||||
}
|
||||
void TransitionSlideScene::onTransitionStart() {}
|
||||
|
||||
/**
|
||||
* @brief 更新过渡进度
|
||||
|
|
@ -49,7 +50,7 @@ void TransitionSlideScene::updateTransition(float dt) {
|
|||
*
|
||||
* 根据进度控制新旧场景的滑动位置
|
||||
*/
|
||||
void TransitionSlideScene::renderContent(RenderBackend &renderer) {
|
||||
void TransitionSlideScene::renderContent(Renderer &renderer) {
|
||||
float screenWidth = 800.0f;
|
||||
float screenHeight = 600.0f;
|
||||
|
||||
|
|
@ -67,7 +68,8 @@ void TransitionSlideScene::renderContent(RenderBackend &renderer) {
|
|||
}
|
||||
}
|
||||
|
||||
float easeProgress = progress_ < 0.5f ? 2.0f * progress_ * progress_
|
||||
float easeProgress = progress_ < 0.5f
|
||||
? 2.0f * progress_ * progress_
|
||||
: -1.0f + (4.0f - 2.0f * progress_) * progress_;
|
||||
|
||||
if (outScene_) {
|
||||
|
|
@ -135,7 +137,6 @@ void TransitionSlideScene::renderContent(RenderBackend &renderer) {
|
|||
camera->setPos(originalPos);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace extra2d
|
||||
|
|
|
|||
|
|
@ -68,9 +68,7 @@ bool SceneService::hasScene(const std::string &name) const {
|
|||
return manager_.hasScene(name);
|
||||
}
|
||||
|
||||
void SceneService::render(RenderBackend &renderer) {
|
||||
manager_.render(renderer);
|
||||
}
|
||||
void SceneService::render(Renderer &renderer) { manager_.render(renderer); }
|
||||
|
||||
void SceneService::collectRenderCommands(std::vector<RenderCommand> &commands) {
|
||||
manager_.collectRenderCommands(commands);
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ flowchart TB
|
|||
subgraph Graphics["Graphics (图形系统)"]
|
||||
direction TB
|
||||
RESOURCES[Resources<br/>资源抽象层]
|
||||
BACKEND[RenderBackend<br/>渲染后端]
|
||||
BACKEND[Renderer<br/>渲染后端]
|
||||
BATCH[Batch Layer<br/>批处理层]
|
||||
GL[OpenGL Backend<br/>OpenGL 后端]
|
||||
VK[Vulkan Backend<br/>Vulkan 后端]
|
||||
|
|
|
|||
|
|
@ -229,9 +229,9 @@ cameraService->setZoom(2.0f);
|
|||
### 渲染后端接口
|
||||
|
||||
```cpp
|
||||
class RenderBackend {
|
||||
class Renderer {
|
||||
public:
|
||||
virtual ~RenderBackend() = default;
|
||||
virtual ~Renderer() = default;
|
||||
|
||||
// 生命周期
|
||||
virtual bool init(IWindow* window) = 0;
|
||||
|
|
@ -276,7 +276,7 @@ public:
|
|||
virtual void drawText(const FontAtlas &font, const std::string &text, const Vec2 &position, const Color &color) = 0;
|
||||
|
||||
// 工厂方法
|
||||
static UniquePtr<RenderBackend> create(BackendType type);
|
||||
static UniquePtr<Renderer> create(BackendType type);
|
||||
};
|
||||
```
|
||||
|
||||
|
|
@ -407,7 +407,7 @@ public:
|
|||
virtual void onEnter();
|
||||
virtual void onExit();
|
||||
virtual void onUpdate(float dt);
|
||||
virtual void onRender(RenderBackend& renderer);
|
||||
virtual void onRender(Renderer& renderer);
|
||||
};
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ public:
|
|||
* @brief 自定义渲染逻辑
|
||||
* @param renderer 渲染后端
|
||||
*/
|
||||
void onRender(RenderBackend &renderer) override { Scene::onRender(renderer); }
|
||||
void onRender(Renderer &renderer) override { Scene::onRender(renderer); }
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
@ -107,9 +107,7 @@ int main(int argc, char *argv[]) {
|
|||
cfg.priority = 0;
|
||||
});
|
||||
|
||||
app.use<RenderModule>([](auto &cfg) {
|
||||
cfg.priority = 10;
|
||||
});
|
||||
app.use<RenderModule>([](auto &cfg) { cfg.priority = 10; });
|
||||
|
||||
std::cout << "Initializing application..." << std::endl;
|
||||
if (!app.init()) {
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ public:
|
|||
time_ = 0.0f;
|
||||
}
|
||||
}
|
||||
void onRender(RenderBackend &renderer) override { Scene::onRender(renderer); }
|
||||
void onRender(Renderer &renderer) override { Scene::onRender(renderer); }
|
||||
|
||||
private:
|
||||
float time_ = 0.0f;
|
||||
|
|
@ -44,9 +44,7 @@ int main(int argc, char *argv[]) {
|
|||
cfg.w = 800;
|
||||
cfg.h = 600;
|
||||
});
|
||||
app.use<RenderModule>([](auto &cfg) {
|
||||
cfg.priority = 10;
|
||||
});
|
||||
app.use<RenderModule>([](auto &cfg) { cfg.priority = 10; });
|
||||
app.use<HelloModule>([](auto &cfg) {
|
||||
cfg.greeting = "Hello from custom module!";
|
||||
cfg.repeatCount = 3;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
* @file main.cpp
|
||||
* @brief Extra2D 图片显示示例
|
||||
*
|
||||
* 演示如何使用 RenderBackend 抽象接口加载和显示图片
|
||||
* 演示如何使用 Renderer 抽象接口加载和显示图片
|
||||
* 此示例不依赖任何特定渲染后端(如 OpenGL)
|
||||
*/
|
||||
|
||||
|
|
@ -40,14 +40,14 @@ public:
|
|||
|
||||
void onExit() override { texture_.reset(); }
|
||||
|
||||
void onRender(RenderBackend &renderer) override {
|
||||
void onRender(Renderer &renderer) override {
|
||||
Scene::onRender(renderer);
|
||||
|
||||
if (!texture_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 使用 RenderBackend 的抽象接口绘制图片
|
||||
// 使用 Renderer 的抽象接口绘制图片
|
||||
// 不依赖任何特定后端(如 OpenGL)
|
||||
// 自动批处理:无需手动调用 begin/endSpriteBatch
|
||||
|
||||
|
|
@ -70,7 +70,7 @@ public:
|
|||
float x = (windowWidth - displayWidth) * 0.5f;
|
||||
float y = (windowHeight - displayHeight) * 0.5f;
|
||||
|
||||
// 使用 RenderBackend 的 drawSprite 方法绘制图片
|
||||
// 使用 Renderer 的 drawSprite 方法绘制图片
|
||||
// 参数:纹理、目标矩形、源矩形、颜色、旋转角度、锚点
|
||||
Rect destRect(x, y, displayWidth, displayHeight);
|
||||
Rect srcRect(0, 0, imgWidth, imgHeight);
|
||||
|
|
@ -80,11 +80,11 @@ public:
|
|||
// 注意:无需手动调用 renderer.endSpriteBatch(),帧结束时会自动刷新
|
||||
}
|
||||
|
||||
void setRenderer(RenderBackend *renderer) { renderer_ = renderer; }
|
||||
void setRenderer(Renderer *renderer) { renderer_ = renderer; }
|
||||
|
||||
private:
|
||||
Ptr<Texture> texture_;
|
||||
RenderBackend *renderer_ = nullptr;
|
||||
Renderer *renderer_ = nullptr;
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
@ -103,9 +103,7 @@ int main(int argc, char *argv[]) {
|
|||
cfg.priority = 0;
|
||||
});
|
||||
|
||||
app.use<RenderModule>([](auto &cfg) {
|
||||
cfg.priority = 10;
|
||||
});
|
||||
app.use<RenderModule>([](auto &cfg) { cfg.priority = 10; });
|
||||
|
||||
if (!app.init()) {
|
||||
std::cerr << "Failed to initialize application!" << std::endl;
|
||||
|
|
@ -127,7 +125,7 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
// 获取渲染器
|
||||
RenderBackend *renderer = app.renderer();
|
||||
Renderer *renderer = app.renderer();
|
||||
|
||||
// 创建并配置场景
|
||||
auto scene = makeShared<ImageDisplayScene>();
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
* @file main.cpp
|
||||
* @brief Extra2D 文字渲染示例
|
||||
*
|
||||
* 演示如何使用 RenderBackend 抽象接口渲染文字
|
||||
* 演示如何使用 Renderer 抽象接口渲染文字
|
||||
* 此示例不依赖任何特定渲染后端(如 OpenGL)
|
||||
*/
|
||||
|
||||
|
|
@ -40,7 +40,7 @@ public:
|
|||
|
||||
void onExit() override { font_.reset(); }
|
||||
|
||||
void onRender(RenderBackend &renderer) override {
|
||||
void onRender(Renderer &renderer) override {
|
||||
Scene::onRender(renderer);
|
||||
|
||||
if (!font_) {
|
||||
|
|
@ -85,21 +85,21 @@ public:
|
|||
Color(0.5f, 0.5f, 0.5f, 1.0f));
|
||||
}
|
||||
|
||||
void setRenderer(RenderBackend *renderer) { renderer_ = renderer; }
|
||||
void setRenderer(Renderer *renderer) { renderer_ = renderer; }
|
||||
|
||||
private:
|
||||
void renderText(RenderBackend &renderer, const std::string &text, float x,
|
||||
float y, const Color &color) {
|
||||
void renderText(Renderer &renderer, const std::string &text, float x, float y,
|
||||
const Color &color) {
|
||||
if (!font_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 使用 RenderBackend 的 drawText 方法
|
||||
// 使用 Renderer 的 drawText 方法
|
||||
renderer.drawText(*font_, text, Vec2(x, y), color);
|
||||
}
|
||||
|
||||
Ptr<FontAtlas> font_;
|
||||
RenderBackend *renderer_ = nullptr;
|
||||
Renderer *renderer_ = nullptr;
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
@ -139,7 +139,7 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
// 获取渲染器
|
||||
RenderBackend *renderer = app.renderer();
|
||||
Renderer *renderer = app.renderer();
|
||||
|
||||
// 创建并配置场景
|
||||
auto scene = makeShared<TextRenderingScene>();
|
||||
|
|
|
|||
Loading…
Reference in New Issue