refactor(asset_service): 移除未使用的algorithm头文件并清理代码格式
refactor(object_pool): 重构对象池实现并修复内存边界检查 chore: 删除废弃的renderer.h头文件
This commit is contained in:
parent
6195d57814
commit
24960516ae
|
|
@ -1,7 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <extra2d/core/types.h>
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <extra2d/core/types.h>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
@ -11,8 +11,7 @@ namespace extra2d {
|
||||||
* @tparam T 对象类型
|
* @tparam T 对象类型
|
||||||
* @tparam Size 池大小
|
* @tparam Size 池大小
|
||||||
*/
|
*/
|
||||||
template <typename T, size_t Size>
|
template <typename T, size_t Size> class ObjectPool {
|
||||||
class ObjectPool {
|
|
||||||
public:
|
public:
|
||||||
ObjectPool() {
|
ObjectPool() {
|
||||||
for (size_t i = 0; i < Size; ++i) {
|
for (size_t i = 0; i < Size; ++i) {
|
||||||
|
|
@ -24,11 +23,11 @@ public:
|
||||||
* @brief 获取对象
|
* @brief 获取对象
|
||||||
* @return 对象指针,池耗尽返回nullptr
|
* @return 对象指针,池耗尽返回nullptr
|
||||||
*/
|
*/
|
||||||
T* acquire() {
|
T *acquire() {
|
||||||
if (available_.empty()) {
|
if (available_.empty()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
T* obj = available_.front();
|
T *obj = available_.front();
|
||||||
available_.pop();
|
available_.pop();
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
@ -37,8 +36,8 @@ public:
|
||||||
* @brief 释放对象回池
|
* @brief 释放对象回池
|
||||||
* @param obj 对象指针
|
* @param obj 对象指针
|
||||||
*/
|
*/
|
||||||
void release(T* obj) {
|
void release(T *obj) {
|
||||||
if (obj >= pool_.data() && obj < pool_.data() + Size) {
|
if (obj >= pool_ && obj < pool_ + Size) {
|
||||||
obj->~T();
|
obj->~T();
|
||||||
new (obj) T();
|
new (obj) T();
|
||||||
available_.push(obj);
|
available_.push(obj);
|
||||||
|
|
@ -57,8 +56,8 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
alignas(alignof(T)) std::array<u8, sizeof(T) * Size> storage_;
|
alignas(alignof(T)) std::array<u8, sizeof(T) * Size> storage_;
|
||||||
T* pool_ = reinterpret_cast<T*>(storage_.data());
|
T *pool_ = reinterpret_cast<T *>(storage_.data());
|
||||||
std::queue<T*> available_;
|
std::queue<T *> available_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace extra2d
|
} // namespace extra2d
|
||||||
|
|
|
||||||
|
|
@ -1,130 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <extra2d/core/types.h>
|
|
||||||
#include <extra2d/core/math_types.h>
|
|
||||||
#include <extra2d/core/color.h>
|
|
||||||
#include <extra2d/render/render_types.h>
|
|
||||||
#include <extra2d/render/texture.h>
|
|
||||||
#include <extra2d/render/shader.h>
|
|
||||||
#include <extra2d/render/batch.h>
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace extra2d {
|
|
||||||
|
|
||||||
class Camera2D;
|
|
||||||
class TextureAsset;
|
|
||||||
class FontAsset;
|
|
||||||
class SceneNode;
|
|
||||||
|
|
||||||
using RendererRef = Ref<class IRenderer2D>;
|
|
||||||
|
|
||||||
class IRenderer2D {
|
|
||||||
public:
|
|
||||||
virtual ~IRenderer2D() = default;
|
|
||||||
|
|
||||||
virtual bool init(void* nativeWindow) = 0;
|
|
||||||
virtual void shutdown() = 0;
|
|
||||||
virtual void resize(i32 width, i32 height) = 0;
|
|
||||||
|
|
||||||
virtual void beginFrame() = 0;
|
|
||||||
virtual void endFrame() = 0;
|
|
||||||
virtual void present() = 0;
|
|
||||||
|
|
||||||
virtual void setViewport(const Viewport& viewport) = 0;
|
|
||||||
virtual void setScissor(const Scissor& scissor) = 0;
|
|
||||||
virtual void clear(const ClearCommand& cmd) = 0;
|
|
||||||
|
|
||||||
virtual void setCamera(const Camera2D* camera) = 0;
|
|
||||||
virtual void resetCamera() = 0;
|
|
||||||
|
|
||||||
virtual void setBlendMode(BlendMode mode) = 0;
|
|
||||||
virtual void setRenderState(const RenderState& state) = 0;
|
|
||||||
|
|
||||||
virtual TextureRef createTexture(const TextureDesc& desc) = 0;
|
|
||||||
virtual TextureRef createTextureFromAsset(Ref<TextureAsset> asset) = 0;
|
|
||||||
virtual void destroyTexture(TextureRef texture) = 0;
|
|
||||||
|
|
||||||
virtual Ref<ITextureArray> createTextureArray(i32 width, i32 height,
|
|
||||||
u32 maxLayers,
|
|
||||||
TextureFormat format) = 0;
|
|
||||||
|
|
||||||
virtual ShaderRef createShader(const std::string& vertexSrc,
|
|
||||||
const std::string& fragmentSrc) = 0;
|
|
||||||
virtual void destroyShader(ShaderRef shader) = 0;
|
|
||||||
|
|
||||||
virtual void drawSprite(TextureRef texture,
|
|
||||||
const Vec2& position,
|
|
||||||
const Vec2& size,
|
|
||||||
const Vec2& scale,
|
|
||||||
float rotation,
|
|
||||||
const Vec2& anchor,
|
|
||||||
const TextureRegion& region,
|
|
||||||
const Color& tint,
|
|
||||||
float opacity) = 0;
|
|
||||||
|
|
||||||
virtual void drawSpriteBatch(const std::vector<SpriteVertex>& vertices,
|
|
||||||
const std::vector<u32>& indices,
|
|
||||||
TextureRef texture) = 0;
|
|
||||||
|
|
||||||
virtual void drawText(Ref<FontAsset> font,
|
|
||||||
const std::string& text,
|
|
||||||
const Vec2& position,
|
|
||||||
float fontSize,
|
|
||||||
const Color& color,
|
|
||||||
float opacity) = 0;
|
|
||||||
|
|
||||||
virtual void drawRect(const Rect& rect, const Color& color, bool filled) = 0;
|
|
||||||
virtual void drawRect(const Vec2& position, const Vec2& size,
|
|
||||||
const Color& color, bool filled) = 0;
|
|
||||||
|
|
||||||
virtual void drawCircle(const Vec2& center, float radius,
|
|
||||||
const Color& color, bool filled,
|
|
||||||
u32 segments = 32) = 0;
|
|
||||||
|
|
||||||
virtual void drawLine(const Vec2& start, const Vec2& end,
|
|
||||||
const Color& color, float thickness = 1.0f) = 0;
|
|
||||||
|
|
||||||
virtual void drawLines(const std::vector<Vec2>& points,
|
|
||||||
const Color& color,
|
|
||||||
float thickness = 1.0f) = 0;
|
|
||||||
|
|
||||||
virtual void drawPolygon(const std::vector<Vec2>& points,
|
|
||||||
const Color& color,
|
|
||||||
bool filled) = 0;
|
|
||||||
|
|
||||||
virtual void drawShapeBatch(const std::vector<ShapeVertex>& vertices,
|
|
||||||
const std::vector<u32>& indices,
|
|
||||||
PrimitiveType primitive) = 0;
|
|
||||||
|
|
||||||
virtual void flushBatch() = 0;
|
|
||||||
|
|
||||||
virtual i32 width() const = 0;
|
|
||||||
virtual i32 height() const = 0;
|
|
||||||
|
|
||||||
virtual bool isVulkan() const = 0;
|
|
||||||
virtual const char* backendName() const = 0;
|
|
||||||
|
|
||||||
static RendererRef create(bool preferVulkan = true);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct RenderStats {
|
|
||||||
u32 drawCalls = 0;
|
|
||||||
u32 quadCount = 0;
|
|
||||||
u32 vertexCount = 0;
|
|
||||||
u32 indexCount = 0;
|
|
||||||
u32 textureSwitches = 0;
|
|
||||||
u32 shaderSwitches = 0;
|
|
||||||
|
|
||||||
void reset() {
|
|
||||||
drawCalls = 0;
|
|
||||||
quadCount = 0;
|
|
||||||
vertexCount = 0;
|
|
||||||
indexCount = 0;
|
|
||||||
textureSwitches = 0;
|
|
||||||
shaderSwitches = 0;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
#include "extra2d/services/asset_service.h"
|
#include "extra2d/services/asset_service.h"
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
@ -81,12 +80,12 @@ void AssetService::purge() {
|
||||||
std::unique_lock<std::shared_mutex> lock(mutex_);
|
std::unique_lock<std::shared_mutex> lock(mutex_);
|
||||||
|
|
||||||
std::vector<AssetID> toRemove;
|
std::vector<AssetID> toRemove;
|
||||||
for (const auto& [id, loaded] : assets_) {
|
for (const auto &[id, loaded] : assets_) {
|
||||||
if (loaded.asset.use_count() <= 2) {
|
if (loaded.asset.use_count() <= 2) {
|
||||||
toRemove.push_back(id);
|
toRemove.push_back(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const auto& id : toRemove) {
|
for (const auto &id : toRemove) {
|
||||||
assets_.erase(id);
|
assets_.erase(id);
|
||||||
states_.erase(id);
|
states_.erase(id);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue