diff --git a/Extra2D/include/extra2d/core/types.h b/Extra2D/include/extra2d/core/types.h index fb75de7..ee93b7d 100644 --- a/Extra2D/include/extra2d/core/types.h +++ b/Extra2D/include/extra2d/core/types.h @@ -17,13 +17,13 @@ template using UniquePtr = std::unique_ptr; template using WeakPtr = std::weak_ptr; /// 创建 shared_ptr 的便捷函数 -template inline Ptr makePtr(Args &&...args) { +template inline Ptr shared(Args &&...args) { return std::make_shared(std::forward(args)...); } /// 创建 unique_ptr 的便捷函数 template -inline UniquePtr makeUnique(Args &&...args) { +inline UniquePtr unique(Args &&...args) { return std::make_unique(std::forward(args)...); } diff --git a/Extra2D/src/app/application.cpp b/Extra2D/src/app/application.cpp index 436bc0f..05e4755 100644 --- a/Extra2D/src/app/application.cpp +++ b/Extra2D/src/app/application.cpp @@ -34,7 +34,7 @@ bool Application::init(const AppConfig &config) { config_ = config; - window_ = makeUnique(); + window_ = unique(); WindowConfig winConfig; winConfig.title = config.title; winConfig.width = config.width; @@ -47,15 +47,15 @@ bool Application::init(const AppConfig &config) { return false; } - renderer_ = makeUnique(); + renderer_ = unique(); if (!renderer_->init(window_.get())) { E2D_LOG_ERROR("Failed to initialize renderer"); window_->destroy(); return false; } - eventQueue_ = makeUnique(); - eventDispatcher_ = makeUnique(); + eventQueue_ = unique(); + eventDispatcher_ = unique(); AudioEngine::getInstance().initialize(); @@ -106,9 +106,7 @@ void Application::run() { } } -void Application::quit() { - running_ = false; -} +void Application::quit() { running_ = false; } void Application::pause() { if (!paused_) { @@ -173,9 +171,9 @@ void Application::render() { } renderer_->beginFrame(Color::Black); - + // 渲染内容可以在这里添加 - + renderer_->endFrame(); window_->swapBuffers(); } diff --git a/Extra2D/src/graphics/opengl/gl_renderer.cpp b/Extra2D/src/graphics/opengl/gl_renderer.cpp index 2be034c..79d63fc 100644 --- a/Extra2D/src/graphics/opengl/gl_renderer.cpp +++ b/Extra2D/src/graphics/opengl/gl_renderer.cpp @@ -210,11 +210,11 @@ glm::mat4 GLRenderer::getCurrentTransform() const { Ptr GLRenderer::createTexture(int width, int height, const uint8_t *pixels, int channels) { - return makePtr(width, height, pixels, channels); + return shared(width, height, pixels, channels); } Ptr GLRenderer::loadTexture(const std::string &filepath) { - return makePtr(filepath); + return shared(filepath); } void GLRenderer::beginSpriteBatch() { spriteBatch_.begin(viewProjection_); } @@ -430,7 +430,7 @@ void GLRenderer::fillPolygon(const std::vector &points, Ptr GLRenderer::createFontAtlas(const std::string &filepath, int fontSize, bool useSDF) { - return makePtr(filepath, fontSize, useSDF); + return shared(filepath, fontSize, useSDF); } void GLRenderer::drawText(const FontAtlas &font, const std::string &text, @@ -446,7 +446,7 @@ void GLRenderer::drawText(const FontAtlas &font, const std::string &text, // 收集所有字符数据用于批处理 std::vector sprites; - sprites.reserve(text.size()); // 预分配空间 + sprites.reserve(text.size()); // 预分配空间 for (char32_t codepoint : utf8ToUtf32(text)) { if (codepoint == '\n') { @@ -477,7 +477,7 @@ void GLRenderer::drawText(const FontAtlas &font, const std::string &text, data.rotation = 0.0f; data.anchor = glm::vec2(0.0f, 0.0f); data.isSDF = font.isSDF(); - + sprites.push_back(data); } } diff --git a/Extra2D/src/graphics/opengl/gl_texture.cpp b/Extra2D/src/graphics/opengl/gl_texture.cpp index d914d07..4e48d28 100644 --- a/Extra2D/src/graphics/opengl/gl_texture.cpp +++ b/Extra2D/src/graphics/opengl/gl_texture.cpp @@ -1,6 +1,7 @@ -#include #include +#include #include + #define STB_IMAGE_IMPLEMENTATION #include #include @@ -455,7 +456,7 @@ Ptr GLTexture::create(int width, int height, PixelFormat format) { channels = 4; break; } - return makePtr(width, height, nullptr, channels); + return shared(width, height, nullptr, channels); } } // namespace extra2d diff --git a/Extra2D/src/graphics/texture_atlas.cpp b/Extra2D/src/graphics/texture_atlas.cpp index ba44b81..87d1980 100644 --- a/Extra2D/src/graphics/texture_atlas.cpp +++ b/Extra2D/src/graphics/texture_atlas.cpp @@ -1,7 +1,8 @@ -#include -#include #include #include +#include +#include + namespace extra2d { @@ -13,124 +14,134 @@ TextureAtlasPage::TextureAtlasPage(int width, int height) : width_(width), height_(height), isFull_(false), usedArea_(0) { // 创建空白纹理 std::vector emptyData(width * height * 4, 0); - texture_ = makePtr(width, height, emptyData.data(), 4); - + texture_ = shared(width, height, emptyData.data(), 4); + // 初始化矩形打包根节点 root_ = std::make_unique(0, 0, width, height); - + E2D_LOG_INFO("Created texture atlas page: {}x{}", width, height); } TextureAtlasPage::~TextureAtlasPage() = default; -bool TextureAtlasPage::tryAddTexture(const std::string& name, int texWidth, int texHeight, - const uint8_t* pixels, Rect& outUvRect) { +bool TextureAtlasPage::tryAddTexture(const std::string &name, int texWidth, + int texHeight, const uint8_t *pixels, + Rect &outUvRect) { if (isFull_) { return false; } - + // 添加边距 int paddedWidth = texWidth + 2 * PADDING; int paddedHeight = texHeight + 2 * PADDING; - + // 如果纹理太大,无法放入 if (paddedWidth > width_ || paddedHeight > height_) { return false; } - + // 尝试插入 - PackNode* node = insert(root_.get(), paddedWidth, paddedHeight); + PackNode *node = insert(root_.get(), paddedWidth, paddedHeight); if (node == nullptr) { // 无法放入,标记为满 isFull_ = true; return false; } - + // 写入像素数据(跳过边距区域) - writePixels(node->x + PADDING, node->y + PADDING, texWidth, texHeight, pixels); - + writePixels(node->x + PADDING, node->y + PADDING, texWidth, texHeight, + pixels); + // 创建条目 AtlasEntry entry; entry.name = name; - entry.originalSize = Vec2(static_cast(texWidth), static_cast(texHeight)); + entry.originalSize = + Vec2(static_cast(texWidth), static_cast(texHeight)); entry.padding = PADDING; - + // 计算 UV 坐标(考虑边距) float u1 = static_cast(node->x + PADDING) / width_; float v1 = static_cast(node->y + PADDING) / height_; float u2 = static_cast(node->x + PADDING + texWidth) / width_; float v2 = static_cast(node->y + PADDING + texHeight) / height_; - + entry.uvRect = Rect(u1, v1, u2 - u1, v2 - v1); outUvRect = entry.uvRect; - + entries_[name] = std::move(entry); usedArea_ += paddedWidth * paddedHeight; - - E2D_LOG_DEBUG("Added texture '{}' to atlas: {}x{} at ({}, {})", - name, texWidth, texHeight, node->x, node->y); - + + E2D_LOG_DEBUG("Added texture '{}' to atlas: {}x{} at ({}, {})", name, + texWidth, texHeight, node->x, node->y); + return true; } -TextureAtlasPage::PackNode* TextureAtlasPage::insert(PackNode* node, int width, int height) { +TextureAtlasPage::PackNode *TextureAtlasPage::insert(PackNode *node, int width, + int height) { if (node == nullptr) { return nullptr; } - + // 如果节点已被使用,尝试子节点 if (node->used) { - PackNode* result = insert(node->left.get(), width, height); + PackNode *result = insert(node->left.get(), width, height); if (result != nullptr) { return result; } return insert(node->right.get(), width, height); } - + // 检查是否适合 if (width > node->width || height > node->height) { return nullptr; } - + // 如果刚好合适,使用此节点 if (width == node->width && height == node->height) { node->used = true; return node; } - + // 需要分割节点 int dw = node->width - width; int dh = node->height - height; - + if (dw > dh) { // 水平分割 - node->left = std::make_unique(node->x, node->y, width, node->height); - node->right = std::make_unique(node->x + width, node->y, dw, node->height); + node->left = + std::make_unique(node->x, node->y, width, node->height); + node->right = + std::make_unique(node->x + width, node->y, dw, node->height); } else { // 垂直分割 - node->left = std::make_unique(node->x, node->y, node->width, height); - node->right = std::make_unique(node->x, node->y + height, node->width, dh); + node->left = + std::make_unique(node->x, node->y, node->width, height); + node->right = + std::make_unique(node->x, node->y + height, node->width, dh); } - + // 递归插入到左子节点 return insert(node->left.get(), width, height); } -void TextureAtlasPage::writePixels(int x, int y, int w, int h, const uint8_t* pixels) { +void TextureAtlasPage::writePixels(int x, int y, int w, int h, + const uint8_t *pixels) { if (texture_ == nullptr || pixels == nullptr) { return; } - + // 使用 glTexSubImage2D 更新纹理数据 GLuint texID = static_cast( reinterpret_cast(texture_->getNativeHandle())); - + glBindTexture(GL_TEXTURE_2D, texID); - glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, + pixels); glBindTexture(GL_TEXTURE_2D, 0); } -const AtlasEntry* TextureAtlasPage::getEntry(const std::string& name) const { +const AtlasEntry *TextureAtlasPage::getEntry(const std::string &name) const { auto it = entries_.find(name); if (it != entries_.end()) { return &it->second; @@ -147,11 +158,8 @@ float TextureAtlasPage::getUsageRatio() const { // ============================================================================ TextureAtlas::TextureAtlas() - : pageSize_(TextureAtlasPage::DEFAULT_SIZE), - sizeThreshold_(256), - enabled_(true), - initialized_(false) { -} + : pageSize_(TextureAtlasPage::DEFAULT_SIZE), sizeThreshold_(256), + enabled_(true), initialized_(false) {} TextureAtlas::~TextureAtlas() = default; @@ -161,33 +169,33 @@ void TextureAtlas::init(int pageSize) { E2D_LOG_INFO("TextureAtlas initialized with page size: {}", pageSize); } -bool TextureAtlas::addTexture(const std::string& name, int width, int height, - const uint8_t* pixels) { +bool TextureAtlas::addTexture(const std::string &name, int width, int height, + const uint8_t *pixels) { if (!enabled_ || !initialized_) { return false; } - + // 检查是否已存在 if (contains(name)) { return true; } - + // 检查纹理大小 if (width > sizeThreshold_ || height > sizeThreshold_) { - E2D_LOG_DEBUG("Texture '{}' too large for atlas ({}x{} > {}), skipping", + E2D_LOG_DEBUG("Texture '{}' too large for atlas ({}x{} > {}), skipping", name, width, height, sizeThreshold_); return false; } - + // 尝试添加到现有页面 Rect uvRect; - for (auto& page : pages_) { + for (auto &page : pages_) { if (page->tryAddTexture(name, width, height, pixels, uvRect)) { entryToPage_[name] = page.get(); return true; } } - + // 创建新页面 auto newPage = std::make_unique(pageSize_, pageSize_); if (newPage->tryAddTexture(name, width, height, pixels, uvRect)) { @@ -195,16 +203,16 @@ bool TextureAtlas::addTexture(const std::string& name, int width, int height, pages_.push_back(std::move(newPage)); return true; } - + E2D_LOG_WARN("Failed to add texture '{}' to atlas", name); return false; } -bool TextureAtlas::contains(const std::string& name) const { +bool TextureAtlas::contains(const std::string &name) const { return entryToPage_.find(name) != entryToPage_.end(); } -const Texture* TextureAtlas::getAtlasTexture(const std::string& name) const { +const Texture *TextureAtlas::getAtlasTexture(const std::string &name) const { auto it = entryToPage_.find(name); if (it != entryToPage_.end()) { return it->second->getTexture().get(); @@ -212,10 +220,10 @@ const Texture* TextureAtlas::getAtlasTexture(const std::string& name) const { return nullptr; } -Rect TextureAtlas::getUVRect(const std::string& name) const { +Rect TextureAtlas::getUVRect(const std::string &name) const { auto it = entryToPage_.find(name); if (it != entryToPage_.end()) { - const AtlasEntry* entry = it->second->getEntry(name); + const AtlasEntry *entry = it->second->getEntry(name); if (entry != nullptr) { return entry->uvRect; } @@ -223,10 +231,10 @@ Rect TextureAtlas::getUVRect(const std::string& name) const { return Rect(0, 0, 1, 1); // 默认 UV } -Vec2 TextureAtlas::getOriginalSize(const std::string& name) const { +Vec2 TextureAtlas::getOriginalSize(const std::string &name) const { auto it = entryToPage_.find(name); if (it != entryToPage_.end()) { - const AtlasEntry* entry = it->second->getEntry(name); + const AtlasEntry *entry = it->second->getEntry(name); if (entry != nullptr) { return entry->originalSize; } @@ -238,9 +246,9 @@ float TextureAtlas::getTotalUsageRatio() const { if (pages_.empty()) { return 0.0f; } - + float total = 0.0f; - for (const auto& page : pages_) { + for (const auto &page : pages_) { total += page->getUsageRatio(); } return total / pages_.size(); @@ -256,7 +264,7 @@ void TextureAtlas::clear() { // TextureAtlasManager 单例实现 // ============================================================================ -TextureAtlasManager& TextureAtlasManager::getInstance() { +TextureAtlasManager &TextureAtlasManager::getInstance() { static TextureAtlasManager instance; return instance; } diff --git a/Extra2D/src/resource/resource_manager.cpp b/Extra2D/src/resource/resource_manager.cpp index 29e6c4e..d8ddf7c 100644 --- a/Extra2D/src/resource/resource_manager.cpp +++ b/Extra2D/src/resource/resource_manager.cpp @@ -41,7 +41,8 @@ static std::string getExecutableDirectory() { } #endif -// 解析资源路径(优先尝试 romfs:/ 前缀,然后 sdmc:/,最后尝试相对于可执行文件的路径) +// 解析资源路径(优先尝试 romfs:/ 前缀,然后 +// sdmc:/,最后尝试相对于可执行文件的路径) static std::string resolveResourcePath(const std::string &filepath) { // 如果已经是 romfs 或 sdmc 路径,直接返回 if (isRomfsPath(filepath) || filepath.find("sdmc:/") == 0) { @@ -81,9 +82,7 @@ static std::string resolveResourcePath(const std::string &filepath) { ResourceManager::ResourceManager() = default; -ResourceManager::~ResourceManager() { - shutdownAsyncLoader(); -} +ResourceManager::~ResourceManager() { shutdownAsyncLoader(); } ResourceManager &ResourceManager::getInstance() { static ResourceManager instance; @@ -98,9 +97,10 @@ void ResourceManager::initAsyncLoader() { if (asyncRunning_) { return; } - + asyncRunning_ = true; - asyncThread_ = std::make_unique(&ResourceManager::asyncLoadLoop, this); + asyncThread_ = + std::make_unique(&ResourceManager::asyncLoadLoop, this); E2D_LOG_INFO("ResourceManager: async loader initialized"); } @@ -108,14 +108,14 @@ void ResourceManager::shutdownAsyncLoader() { if (!asyncRunning_) { return; } - + asyncRunning_ = false; asyncCondition_.notify_all(); - + if (asyncThread_ && asyncThread_->joinable()) { asyncThread_->join(); } - + E2D_LOG_INFO("ResourceManager: async loader shutdown"); } @@ -132,34 +132,35 @@ bool ResourceManager::hasPendingAsyncLoads() const { void ResourceManager::asyncLoadLoop() { while (asyncRunning_) { AsyncLoadTask task; - + { std::unique_lock lock(asyncQueueMutex_); - asyncCondition_.wait(lock, [this] { return !asyncTaskQueue_.empty() || !asyncRunning_; }); - + asyncCondition_.wait( + lock, [this] { return !asyncTaskQueue_.empty() || !asyncRunning_; }); + if (!asyncRunning_) { break; } - + if (asyncTaskQueue_.empty()) { continue; } - + task = std::move(asyncTaskQueue_.front()); asyncTaskQueue_.pop(); } - + // 执行加载 auto texture = loadTextureInternal(task.filepath, task.format); - + // 回调 if (task.callback) { task.callback(texture); } - + // 设置 promise task.promise.set_value(texture); - + pendingAsyncLoads_--; } } @@ -172,25 +173,30 @@ Ptr ResourceManager::loadTexture(const std::string &filepath) { return loadTexture(filepath, false, TextureFormat::Auto); } -Ptr ResourceManager::loadTexture(const std::string &filepath, bool async) { +Ptr ResourceManager::loadTexture(const std::string &filepath, + bool async) { return loadTexture(filepath, async, TextureFormat::Auto); } -Ptr ResourceManager::loadTexture(const std::string &filepath, bool async, TextureFormat format) { +Ptr ResourceManager::loadTexture(const std::string &filepath, + bool async, TextureFormat format) { if (async) { // 异步加载:返回空指针,实际纹理通过回调获取 loadTextureAsync(filepath, format, nullptr); return nullptr; } - + return loadTextureInternal(filepath, format); } -void ResourceManager::loadTextureAsync(const std::string &filepath, TextureLoadCallback callback) { +void ResourceManager::loadTextureAsync(const std::string &filepath, + TextureLoadCallback callback) { loadTextureAsync(filepath, TextureFormat::Auto, callback); } -void ResourceManager::loadTextureAsync(const std::string &filepath, TextureFormat format, TextureLoadCallback callback) { +void ResourceManager::loadTextureAsync(const std::string &filepath, + TextureFormat format, + TextureLoadCallback callback) { // 确保异步加载系统已启动 if (!asyncRunning_) { initAsyncLoader(); @@ -229,7 +235,8 @@ void ResourceManager::loadTextureAsync(const std::string &filepath, TextureForma E2D_LOG_DEBUG("ResourceManager: queued async texture load: {}", filepath); } -Ptr ResourceManager::loadTextureInternal(const std::string &filepath, TextureFormat format) { +Ptr ResourceManager::loadTextureInternal(const std::string &filepath, + TextureFormat format) { std::lock_guard lock(textureMutex_); // 检查缓存 @@ -255,7 +262,7 @@ Ptr ResourceManager::loadTextureInternal(const std::string &filepath, T // 创建新纹理 try { - auto texture = makePtr(fullPath); + auto texture = shared(fullPath); if (!texture->isValid()) { E2D_LOG_ERROR("ResourceManager: failed to load texture: {}", filepath); return nullptr; @@ -273,7 +280,8 @@ Ptr ResourceManager::loadTextureInternal(const std::string &filepath, T evictTexturesIfNeeded(); // 计算纹理大小 - size_t textureSize = calculateTextureSize(texture->getWidth(), texture->getHeight(), texture->getFormat()); + size_t textureSize = calculateTextureSize( + texture->getWidth(), texture->getHeight(), texture->getFormat()); // 分配LRU节点 uint32_t lruIndex = allocateLRUNode(filepath); @@ -292,7 +300,8 @@ Ptr ResourceManager::loadTextureInternal(const std::string &filepath, T // 添加到LRU链表头部 moveToFront(lruIndex); - E2D_LOG_DEBUG("ResourceManager: loaded texture: {} ({} KB)", filepath, textureSize / 1024); + E2D_LOG_DEBUG("ResourceManager: loaded texture: {} ({} KB)", filepath, + textureSize / 1024); return texture; } catch (...) { E2D_LOG_ERROR("ResourceManager: exception loading texture: {}", filepath); @@ -304,24 +313,26 @@ TextureFormat ResourceManager::selectBestFormat(TextureFormat requested) const { if (requested != TextureFormat::Auto) { return requested; } - + // 自动选择最佳格式 // 检查支持的扩展 // 这里简化处理,实际应该查询 OpenGL 扩展 - + // 桌面平台优先 DXT // 移动平台优先 ETC2 或 ASTC - + // 默认返回 RGBA8 return TextureFormat::RGBA8; } -std::vector ResourceManager::compressTexture(const uint8_t* data, int width, int height, - int channels, TextureFormat format) { +std::vector ResourceManager::compressTexture(const uint8_t *data, + int width, int height, + int channels, + TextureFormat format) { // 纹理压缩实现 // 这里需要根据格式使用相应的压缩库 // 如:squish (DXT), etcpack (ETC2), astc-encoder (ASTC) - + // 目前返回原始数据 std::vector result(data, data + width * height * channels); return result; @@ -383,9 +394,9 @@ Ptr ResourceManager::getTexture(const std::string &key) const { auto it = textureCache_.find(key); if (it != textureCache_.end()) { - const_cast(this)->touchTexture(key); - const_cast(this)->textureHitCount_++; - const_cast(it->second.accessCount)++; + const_cast(this)->touchTexture(key); + const_cast(this)->textureHitCount_++; + const_cast(it->second.accessCount)++; return it->second.texture; } return nullptr; @@ -402,8 +413,9 @@ void ResourceManager::unloadTexture(const std::string &key) { auto it = textureCache_.find(key); if (it != textureCache_.end()) { // 从LRU链表中移除 - auto nodeIt = std::find_if(lruNodes_.begin(), lruNodes_.end(), - [&key](const LRUNode &node) { return node.valid && node.key == key; }); + auto nodeIt = std::find_if( + lruNodes_.begin(), lruNodes_.end(), + [&key](const LRUNode &node) { return node.valid && node.key == key; }); if (nodeIt != lruNodes_.end()) { removeFromList(static_cast(nodeIt - lruNodes_.begin()) + 1); freeLRUNode(static_cast(nodeIt - lruNodes_.begin()) + 1); @@ -453,7 +465,7 @@ Ptr ResourceManager::loadFont(const std::string &filepath, // 创建新字体图集 try { - auto font = makePtr(fullPath, fontSize, useSDF); + auto font = shared(fullPath, fontSize, useSDF); if (!font->getTexture() || !font->getTexture()->isValid()) { E2D_LOG_ERROR("ResourceManager: failed to load font: {}", filepath); return nullptr; @@ -648,7 +660,8 @@ size_t ResourceManager::getTextureCacheSize() const { // LRU 缓存管理 // ============================================================================ -void ResourceManager::setTextureCache(size_t maxCacheSize, size_t maxTextureCount, +void ResourceManager::setTextureCache(size_t maxCacheSize, + size_t maxTextureCount, float unloadInterval) { std::lock_guard lock(textureMutex_); maxCacheSize_ = maxCacheSize; @@ -694,7 +707,8 @@ void ResourceManager::update(float dt) { size_t targetSize = static_cast(maxCacheSize_ * 0.5f); while (totalTextureSize_ > targetSize && lruTail_ != 0) { std::string key = evictLRU(); - if (key.empty()) break; + if (key.empty()) + break; auto it = textureCache_.find(key); if (it != textureCache_.end()) { @@ -703,7 +717,8 @@ void ResourceManager::update(float dt) { E2D_LOG_DEBUG("ResourceManager: auto-evicted texture: {}", key); } } - E2D_LOG_INFO("ResourceManager: texture cache trimmed to {} MB", totalTextureSize_ / (1024 * 1024)); + E2D_LOG_INFO("ResourceManager: texture cache trimmed to {} MB", + totalTextureSize_ / (1024 * 1024)); } } } @@ -733,7 +748,8 @@ uint32_t ResourceManager::allocateLRUNode(const std::string &key) { } void ResourceManager::freeLRUNode(uint32_t index) { - if (index == 0 || index > lruNodes_.size()) return; + if (index == 0 || index > lruNodes_.size()) + return; LRUNode &node = lruNodes_[index - 1]; node.valid = false; @@ -744,7 +760,8 @@ void ResourceManager::freeLRUNode(uint32_t index) { } void ResourceManager::moveToFront(uint32_t index) { - if (index == 0 || index > lruNodes_.size() || index == lruHead_) return; + if (index == 0 || index > lruNodes_.size() || index == lruHead_) + return; removeFromList(index); @@ -763,7 +780,8 @@ void ResourceManager::moveToFront(uint32_t index) { } void ResourceManager::removeFromList(uint32_t index) { - if (index == 0 || index > lruNodes_.size()) return; + if (index == 0 || index > lruNodes_.size()) + return; LRUNode &node = lruNodes_[index - 1]; @@ -781,7 +799,8 @@ void ResourceManager::removeFromList(uint32_t index) { } std::string ResourceManager::evictLRU() { - if (lruTail_ == 0) return ""; + if (lruTail_ == 0) + return ""; uint32_t index = lruTail_; std::string key = lruNodes_[index - 1].key; @@ -794,8 +813,9 @@ std::string ResourceManager::evictLRU() { void ResourceManager::touchTexture(const std::string &key) { // 查找对应的LRU节点 - auto nodeIt = std::find_if(lruNodes_.begin(), lruNodes_.end(), - [&key](const LRUNode &node) { return node.valid && node.key == key; }); + auto nodeIt = std::find_if( + lruNodes_.begin(), lruNodes_.end(), + [&key](const LRUNode &node) { return node.valid && node.key == key; }); if (nodeIt != lruNodes_.end()) { uint32_t index = static_cast(nodeIt - lruNodes_.begin()) + 1; moveToFront(index); @@ -813,7 +833,8 @@ void ResourceManager::evictTexturesIfNeeded() { textureCache_.size() >= maxTextureCount_) && lruTail_ != 0) { std::string key = evictLRU(); - if (key.empty()) break; + if (key.empty()) + break; auto it = textureCache_.find(key); if (it != textureCache_.end()) { @@ -824,24 +845,25 @@ void ResourceManager::evictTexturesIfNeeded() { } } -size_t ResourceManager::calculateTextureSize(int width, int height, PixelFormat format) const { +size_t ResourceManager::calculateTextureSize(int width, int height, + PixelFormat format) const { int bytesPerPixel = 4; switch (format) { - case PixelFormat::R8: - bytesPerPixel = 1; - break; - case PixelFormat::RG8: - bytesPerPixel = 2; - break; - case PixelFormat::RGB8: - bytesPerPixel = 3; - break; - case PixelFormat::RGBA8: - bytesPerPixel = 4; - break; - default: - bytesPerPixel = 4; - break; + case PixelFormat::R8: + bytesPerPixel = 1; + break; + case PixelFormat::RG8: + bytesPerPixel = 2; + break; + case PixelFormat::RGB8: + bytesPerPixel = 3; + break; + case PixelFormat::RGBA8: + bytesPerPixel = 4; + break; + default: + bytesPerPixel = 4; + break; } return static_cast(width * height * bytesPerPixel); } @@ -864,7 +886,8 @@ std::string ResourceManager::loadTextFile(const std::string &filepath) { return loadTextFile(filepath, "UTF-8"); } -std::string ResourceManager::loadTextFile(const std::string &filepath, const std::string &encoding) { +std::string ResourceManager::loadTextFile(const std::string &filepath, + const std::string &encoding) { (void)encoding; // 目前只支持 UTF-8 std::lock_guard lock(textFileMutex_); @@ -888,13 +911,15 @@ std::string ResourceManager::loadTextFile(const std::string &filepath, const std #ifdef _WIN32 errno_t err = fopen_s(&file, resolvedPath.c_str(), "rb"); if (err != 0 || !file) { - E2D_LOG_ERROR("ResourceManager: failed to open text file: {}", resolvedPath); + E2D_LOG_ERROR("ResourceManager: failed to open text file: {}", + resolvedPath); return ""; } #else file = fopen(resolvedPath.c_str(), "rb"); if (!file) { - E2D_LOG_ERROR("ResourceManager: failed to open text file: {}", resolvedPath); + E2D_LOG_ERROR("ResourceManager: failed to open text file: {}", + resolvedPath); return ""; } #endif @@ -917,13 +942,15 @@ std::string ResourceManager::loadTextFile(const std::string &filepath, const std fclose(file); if (readSize != static_cast(fileSize)) { - E2D_LOG_ERROR("ResourceManager: failed to read text file: {}", resolvedPath); + E2D_LOG_ERROR("ResourceManager: failed to read text file: {}", + resolvedPath); return ""; } // 缓存内容 textFileCache_[filepath] = content; - E2D_LOG_DEBUG("ResourceManager: loaded text file: {} ({} bytes)", filepath, content.size()); + E2D_LOG_DEBUG("ResourceManager: loaded text file: {} ({} bytes)", filepath, + content.size()); return content; } @@ -989,13 +1016,15 @@ std::string ResourceManager::loadJsonFile(const std::string &filepath) { #ifdef _WIN32 errno_t err = fopen_s(&file, resolvedPath.c_str(), "rb"); if (err != 0 || !file) { - E2D_LOG_ERROR("ResourceManager: failed to open JSON file: {}", resolvedPath); + E2D_LOG_ERROR("ResourceManager: failed to open JSON file: {}", + resolvedPath); return ""; } #else file = fopen(resolvedPath.c_str(), "rb"); if (!file) { - E2D_LOG_ERROR("ResourceManager: failed to open JSON file: {}", resolvedPath); + E2D_LOG_ERROR("ResourceManager: failed to open JSON file: {}", + resolvedPath); return ""; } #endif @@ -1018,7 +1047,8 @@ std::string ResourceManager::loadJsonFile(const std::string &filepath) { fclose(file); if (readSize != static_cast(fileSize)) { - E2D_LOG_ERROR("ResourceManager: failed to read JSON file: {}", resolvedPath); + E2D_LOG_ERROR("ResourceManager: failed to read JSON file: {}", + resolvedPath); return ""; } @@ -1032,7 +1062,8 @@ std::string ResourceManager::loadJsonFile(const std::string &filepath) { // 缓存内容 jsonFileCache_[filepath] = content; - E2D_LOG_DEBUG("ResourceManager: loaded JSON file: {} ({} bytes)", filepath, content.size()); + E2D_LOG_DEBUG("ResourceManager: loaded JSON file: {} ({} bytes)", filepath, + content.size()); return content; } diff --git a/Extra2D/src/scene/scene.cpp b/Extra2D/src/scene/scene.cpp index adb6050..2a8bc80 100644 --- a/Extra2D/src/scene/scene.cpp +++ b/Extra2D/src/scene/scene.cpp @@ -5,7 +5,7 @@ namespace extra2d { -Scene::Scene() { defaultCamera_ = makePtr(); } +Scene::Scene() { defaultCamera_ = shared(); } void Scene::setCamera(Ptr camera) { camera_ = camera; } @@ -126,6 +126,6 @@ void Scene::collectRenderCommands(std::vector &commands, Node::collectRenderCommands(commands, parentZOrder); } -Ptr Scene::create() { return makePtr(); } +Ptr Scene::create() { return shared(); } } // namespace extra2d diff --git a/Extra2D/src/scene/shape_node.cpp b/Extra2D/src/scene/shape_node.cpp index fb46c41..78f5857 100644 --- a/Extra2D/src/scene/shape_node.cpp +++ b/Extra2D/src/scene/shape_node.cpp @@ -9,10 +9,10 @@ namespace extra2d { ShapeNode::ShapeNode() = default; -Ptr ShapeNode::create() { return makePtr(); } +Ptr ShapeNode::create() { return shared(); } Ptr ShapeNode::createPoint(const Vec2 &pos, const Color &color) { - auto node = makePtr(); + auto node = shared(); node->shapeType_ = ShapeType::Point; node->color_ = color; node->points_ = {pos}; @@ -21,7 +21,7 @@ Ptr ShapeNode::createPoint(const Vec2 &pos, const Color &color) { Ptr ShapeNode::createLine(const Vec2 &start, const Vec2 &end, const Color &color, float width) { - auto node = makePtr(); + auto node = shared(); node->shapeType_ = ShapeType::Line; node->color_ = color; node->lineWidth_ = width; @@ -31,7 +31,7 @@ Ptr ShapeNode::createLine(const Vec2 &start, const Vec2 &end, Ptr ShapeNode::createRect(const Rect &rect, const Color &color, float width) { - auto node = makePtr(); + auto node = shared(); node->shapeType_ = ShapeType::Rect; node->color_ = color; node->lineWidth_ = width; @@ -52,7 +52,7 @@ Ptr ShapeNode::createFilledRect(const Rect &rect, Ptr ShapeNode::createCircle(const Vec2 ¢er, float radius, const Color &color, int segments, float width) { - auto node = makePtr(); + auto node = shared(); node->shapeType_ = ShapeType::Circle; node->color_ = color; node->lineWidth_ = width; @@ -74,7 +74,7 @@ Ptr ShapeNode::createFilledCircle(const Vec2 ¢er, float radius, Ptr ShapeNode::createTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Color &color, float width) { - auto node = makePtr(); + auto node = shared(); node->shapeType_ = ShapeType::Triangle; node->color_ = color; node->lineWidth_ = width; @@ -93,7 +93,7 @@ Ptr ShapeNode::createFilledTriangle(const Vec2 &p1, const Vec2 &p2, Ptr ShapeNode::createPolygon(const std::vector &points, const Color &color, float width) { - auto node = makePtr(); + auto node = shared(); node->shapeType_ = ShapeType::Polygon; node->color_ = color; node->lineWidth_ = width; @@ -262,16 +262,16 @@ void ShapeNode::generateRenderCommand(std::vector &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; @@ -281,14 +281,14 @@ void ShapeNode::generateRenderCommand(std::vector &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; @@ -298,12 +298,12 @@ void ShapeNode::generateRenderCommand(std::vector &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; @@ -336,7 +336,8 @@ void ShapeNode::generateRenderCommand(std::vector &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; diff --git a/Extra2D/src/scene/sprite.cpp b/Extra2D/src/scene/sprite.cpp index 185f483..770a4d2 100644 --- a/Extra2D/src/scene/sprite.cpp +++ b/Extra2D/src/scene/sprite.cpp @@ -31,14 +31,14 @@ void Sprite::setFlipX(bool flip) { flipX_ = flip; } void Sprite::setFlipY(bool flip) { flipY_ = flip; } -Ptr Sprite::create() { return makePtr(); } +Ptr Sprite::create() { return shared(); } Ptr Sprite::create(Ptr texture) { - return makePtr(texture); + return shared(texture); } Ptr Sprite::create(Ptr texture, const Rect &rect) { - auto sprite = makePtr(texture); + auto sprite = shared(texture); sprite->setTextureRect(rect); return sprite; } diff --git a/Extra2D/src/utils/data.cpp b/Extra2D/src/utils/data.cpp index 52a300a..4cf5751 100644 --- a/Extra2D/src/utils/data.cpp +++ b/Extra2D/src/utils/data.cpp @@ -15,7 +15,7 @@ public: CSimpleIniA ini; }; -DataStore::DataStore() : impl_(makeUnique()) {} +DataStore::DataStore() : impl_(unique()) {} DataStore::~DataStore() { // 如果在事务中,尝试提交 @@ -203,9 +203,7 @@ std::string DataStore::getSaveDataPath(const std::string &path) const { return path; } -UserId DataStore::getCurrentUserId() { - return UserId(); -} +UserId DataStore::getCurrentUserId() { return UserId(); } #endif @@ -402,7 +400,8 @@ std::vector DataStore::getAllSections() const { return sections; } -std::vector DataStore::getAllKeys(const std::string §ion) const { +std::vector +DataStore::getAllKeys(const std::string §ion) const { std::vector keys; CSimpleIniA::TNamesDepend keyList; impl_->ini.GetAllKeys(section.c_str(), keyList); diff --git a/Extra2D/src/window/window.cpp b/Extra2D/src/window/window.cpp index d4d54ab..d03b28f 100644 --- a/Extra2D/src/window/window.cpp +++ b/Extra2D/src/window/window.cpp @@ -1,6 +1,7 @@ -#include #include #include +#include + #include #include @@ -8,10 +9,8 @@ namespace extra2d { Window::Window() - : sdlWindow_(nullptr), glContext_(nullptr), - width_(1280), height_(720), vsync_(true), shouldClose_(false), - fullscreen_(true) { -} + : sdlWindow_(nullptr), glContext_(nullptr), width_(1280), height_(720), + vsync_(true), shouldClose_(false), fullscreen_(true) {} Window::~Window() { destroy(); } @@ -31,7 +30,7 @@ bool Window::create(const WindowConfig &config) { return false; } - input_ = makeUnique(); + input_ = unique(); input_->init(); E2D_LOG_INFO("Window created: {}x{}", width_, height_); @@ -57,7 +56,7 @@ bool Window::initSDL(const WindowConfig &config) { SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); Uint32 windowFlags = SDL_WINDOW_OPENGL; - + if (config.fullscreen) { windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; } else { @@ -71,7 +70,7 @@ bool Window::initSDL(const WindowConfig &config) { config.centerWindow ? SDL_WINDOWPOS_CENTERED : SDL_WINDOWPOS_UNDEFINED, config.centerWindow ? SDL_WINDOWPOS_CENTERED : SDL_WINDOWPOS_UNDEFINED, width_, height_, windowFlags); - + if (!sdlWindow_) { E2D_LOG_ERROR("SDL_CreateWindow failed: {}", SDL_GetError()); SDL_Quit(); @@ -97,7 +96,8 @@ bool Window::initSDL(const WindowConfig &config) { return false; } - if (gladLoadGLES2Loader(reinterpret_cast(SDL_GL_GetProcAddress)) == 0) { + if (gladLoadGLES2Loader( + reinterpret_cast(SDL_GL_GetProcAddress)) == 0) { E2D_LOG_ERROR("gladLoadGLES2Loader failed"); SDL_GL_DeleteContext(glContext_); glContext_ = nullptr; @@ -110,8 +110,10 @@ bool Window::initSDL(const WindowConfig &config) { SDL_GL_SetSwapInterval(vsync_ ? 1 : 0); E2D_LOG_INFO("SDL2 + OpenGL ES 3.0 initialized successfully"); - E2D_LOG_INFO("OpenGL Version: {}", reinterpret_cast(glGetString(GL_VERSION))); - E2D_LOG_INFO("OpenGL Renderer: {}", reinterpret_cast(glGetString(GL_RENDERER))); + E2D_LOG_INFO("OpenGL Version: {}", + reinterpret_cast(glGetString(GL_VERSION))); + E2D_LOG_INFO("OpenGL Renderer: {}", + reinterpret_cast(glGetString(GL_RENDERER))); return true; } diff --git a/docs/API_Tutorial/01_Quick_Start.md b/docs/API_Tutorial/01_Quick_Start.md index 071b9a7..b8ab95f 100644 --- a/docs/API_Tutorial/01_Quick_Start.md +++ b/docs/API_Tutorial/01_Quick_Start.md @@ -105,7 +105,7 @@ int main(int argc, char **argv) } // 进入 Hello World 场景 - app.enterScene(makePtr()); + app.enterScene(shared()); // 运行应用 app.run(); diff --git a/docs/API_Tutorial/02_Scene_System.md b/docs/API_Tutorial/02_Scene_System.md index 02369d8..9ff6b57 100644 --- a/docs/API_Tutorial/02_Scene_System.md +++ b/docs/API_Tutorial/02_Scene_System.md @@ -96,7 +96,7 @@ void BaseScene::updateViewport() { setViewportSize(GAME_WIDTH, GAME_HEIGHT); // 创建并设置相机 - auto camera = extra2d::makePtr(); + auto camera = extra2d::shared(); camera->setViewport(0.0f, GAME_WIDTH, GAME_HEIGHT, 0.0f); setCamera(camera); } @@ -146,7 +146,7 @@ void GameScene::onEnter() { float screenHeight = GAME_HEIGHT; // 512.0f // 所有坐标都基于逻辑分辨率 - auto bird = extra2d::makePtr(); + auto bird = extra2d::shared(); bird->setPosition(extra2d::Vec2(screenWidth / 2.0f - 50.0f, screenHeight / 2.0f)); addChild(bird); } @@ -246,17 +246,17 @@ public: auto& scenes = app.scenes(); // 运行第一个场景 -scenes.runWithScene(makePtr()); +scenes.runWithScene(shared()); // 替换当前场景(无过渡) -scenes.replaceScene(makePtr()); +scenes.replaceScene(shared()); // 替换当前场景(有过渡效果) -scenes.replaceScene(makePtr(), TransitionType::Fade, 0.5f); +scenes.replaceScene(shared(), TransitionType::Fade, 0.5f); // 推入场景(保留当前场景) -scenes.pushScene(makePtr()); -scenes.pushScene(makePtr(), TransitionType::SlideLeft, 0.5f); +scenes.pushScene(shared()); +scenes.pushScene(shared(), TransitionType::SlideLeft, 0.5f); // 弹出场景(返回上一个场景) scenes.popScene(); @@ -319,7 +319,7 @@ scenes.end(); ## 场景生命周期 ``` -创建场景 (makePtr) +创建场景 (shared) ↓ 进入场景 (runWithScene/replaceScene/pushScene) ↓ @@ -452,7 +452,7 @@ private: switch (selectedIndex_) { case 0: - scenes.replaceScene(makePtr(), + scenes.replaceScene(shared(), TransitionType::Fade, 0.25f); break; case 1: diff --git a/docs/API_Tutorial/03_Node_System.md b/docs/API_Tutorial/03_Node_System.md index 7c33dbf..c16263d 100644 --- a/docs/API_Tutorial/03_Node_System.md +++ b/docs/API_Tutorial/03_Node_System.md @@ -170,7 +170,7 @@ panel->setPosition(Vec2(0, 256)); // 相对于父节点的中心 addChild(panel); // 添加子节点到 panel -auto scoreNumber = makePtr(); +auto scoreNumber = shared(); scoreNumber->setPosition(Vec2(95.0f, 10.0f)); // 相对于 panel 中心 panel->addChild(scoreNumber); @@ -384,7 +384,7 @@ parent->reorderChild(child, newZOrder); class Player : public Node { public: static Ptr create(Ptr texture) { - auto player = makePtr(); + auto player = shared(); if (player->init(texture)) { return player; } @@ -454,7 +454,7 @@ void GameOverLayer::onEnter() { initButtons(); // 创建向上移动的动画 - auto moveAction = extra2d::makePtr( + auto moveAction = extra2d::shared( 1.0f, extra2d::Vec2(0.0f, -screenHeight) ); runAction(moveAction); @@ -474,7 +474,7 @@ void GameOverLayer::initPanel(int score, float screenHeight) { addChild(panel); // 显示本局得分(相对于 panel 的本地坐标) - auto scoreNumber = extra2d::makePtr(); + auto scoreNumber = extra2d::shared(); scoreNumber->setLittleNumber(score); scoreNumber->setPosition(extra2d::Vec2(95.0f, 10.0f)); panel->addChild(scoreNumber); @@ -483,7 +483,7 @@ void GameOverLayer::initPanel(int score, float screenHeight) { static int bestScore = 0; if (score > bestScore) bestScore = score; - auto bestNumber = extra2d::makePtr(); + auto bestNumber = extra2d::shared(); bestNumber->setLittleNumber(bestScore); bestNumber->setPosition(extra2d::Vec2(95.0f, 50.0f)); panel->addChild(bestNumber); @@ -591,16 +591,16 @@ Extra2D 提供了丰富的动作类: ```cpp // 移动动画 -auto moveAction = makePtr(1.0f, Vec2(0.0f, -100.0f)); +auto moveAction = shared(1.0f, Vec2(0.0f, -100.0f)); node->runAction(moveAction); // 缩放动画 -auto scaleAction = makePtr(0.5f, 2.0f); +auto scaleAction = shared(0.5f, 2.0f); node->runAction(scaleAction); // 淡入淡出 -auto fadeOut = makePtr(0.3f); -auto fadeIn = makePtr(0.3f); +auto fadeOut = shared(0.3f); +auto fadeIn = shared(0.3f); node->runAction(fadeOut); ``` @@ -614,7 +614,7 @@ void GameOverLayer::onEnter() { Node::onEnter(); // 创建向上移动的动画 - auto moveAction = extra2d::makePtr( + auto moveAction = extra2d::shared( 1.0f, extra2d::Vec2(0.0f, -screenHeight)); // 设置动画完成回调 @@ -636,7 +636,7 @@ void GameOverLayer::onEnter() { ```cpp // 顺序执行:先移动,再缩放,最后淡出 -auto sequence = makePtr({ +auto sequence = shared({ new MoveTo(1.0f, Vec2(100, 100)), new ScaleTo(0.5f, 2.0f), new FadeOut(0.3f) @@ -644,21 +644,21 @@ auto sequence = makePtr({ node->runAction(sequence); // 并行执行:同时移动和旋转 -auto spawn = makePtr({ +auto spawn = shared({ new MoveTo(1.0f, Vec2(100, 100)), new RotateBy(1.0f, 360.0f) }); node->runAction(spawn); // 循环执行 -auto loop = makePtrrunAction(loop); ``` ### 动画进度回调 ```cpp -auto action = makePtr(2.0f, Vec2(100, 100)); +auto action = shared(2.0f, Vec2(100, 100)); action->setProgressCallback([](float progress) { // progress: 0.0 - 1.0 E2D_LOG_INFO("动画进度: {}%", progress * 100); diff --git a/docs/API_Tutorial/04_Resource_Management.md b/docs/API_Tutorial/04_Resource_Management.md index f614dfa..520c6e2 100644 --- a/docs/API_Tutorial/04_Resource_Management.md +++ b/docs/API_Tutorial/04_Resource_Management.md @@ -332,7 +332,7 @@ ResLoader::getKeyFrame(const std::string &name) { } const ImageInfo &info = it->second; - return extra2d::makePtr( + return extra2d::shared( atlasTexture_, extra2d::Rect(info.x, info.y, info.width, info.height)); } diff --git a/docs/API_Tutorial/05_Input_Handling.md b/docs/API_Tutorial/05_Input_Handling.md index b687646..69d2e98 100644 --- a/docs/API_Tutorial/05_Input_Handling.md +++ b/docs/API_Tutorial/05_Input_Handling.md @@ -165,7 +165,7 @@ void GameOverLayer::onUpdate(float dt) { if (input.isButtonPressed(extra2d::GamepadButton::A)) { ResLoader::playMusic(MusicType::Click); auto &app = extra2d::Application::instance(); - app.scenes().replaceScene(extra2d::makePtr(), + app.scenes().replaceScene(extra2d::shared(), extra2d::TransitionType::Fade, 0.5f); } @@ -173,7 +173,7 @@ void GameOverLayer::onUpdate(float dt) { if (input.isButtonPressed(extra2d::GamepadButton::B)) { ResLoader::playMusic(MusicType::Click); auto &app = extra2d::Application::instance(); - app.scenes().replaceScene(extra2d::makePtr(), + app.scenes().replaceScene(extra2d::shared(), extra2d::TransitionType::Fade, 0.5f); } } @@ -221,7 +221,7 @@ void GameOverLayer::onUpdate(float dt) { if (input.isButtonPressed(extra2d::GamepadButton::A)) { ResLoader::playMusic(MusicType::Click); auto &app = extra2d::Application::instance(); - app.scenes().replaceScene(extra2d::makePtr(), + app.scenes().replaceScene(extra2d::shared(), extra2d::TransitionType::Fade, 0.5f); } @@ -229,7 +229,7 @@ void GameOverLayer::onUpdate(float dt) { if (input.isButtonPressed(extra2d::GamepadButton::B)) { ResLoader::playMusic(MusicType::Click); auto &app = extra2d::Application::instance(); - app.scenes().replaceScene(extra2d::makePtr(), + app.scenes().replaceScene(extra2d::shared(), extra2d::TransitionType::Fade, 0.5f); } } diff --git a/docs/API_Tutorial/06_Collision_Detection.md b/docs/API_Tutorial/06_Collision_Detection.md index 9347d26..b378190 100644 --- a/docs/API_Tutorial/06_Collision_Detection.md +++ b/docs/API_Tutorial/06_Collision_Detection.md @@ -85,7 +85,7 @@ public: } void createBox(float x, float y) { - auto box = makePtr(50.0f, 50.0f, Color(0.3f, 0.7f, 1.0f, 0.8f)); + auto box = shared(50.0f, 50.0f, Color(0.3f, 0.7f, 1.0f, 0.8f)); box->setPosition(Vec2(x, y)); addChild(box); // 通过 addChild 管理节点生命周期 } diff --git a/docs/API_Tutorial/07_UI_System.md b/docs/API_Tutorial/07_UI_System.md index db65b4c..f3a7bff 100644 --- a/docs/API_Tutorial/07_UI_System.md +++ b/docs/API_Tutorial/07_UI_System.md @@ -231,7 +231,7 @@ void GameOverLayer::onEnter() { initButtons(); // 创建动画 - auto moveAction = extra2d::makePtr( + auto moveAction = extra2d::shared( 1.0f, extra2d::Vec2(0.0f, -screenHeight)); // 动画完成后启用按钮 diff --git a/docs/API_Tutorial/08_Audio_System.md b/docs/API_Tutorial/08_Audio_System.md index cc663af..a4e3bda 100644 --- a/docs/API_Tutorial/08_Audio_System.md +++ b/docs/API_Tutorial/08_Audio_System.md @@ -173,7 +173,7 @@ void Bird::jump() { restartBtn_->setOnClick([]() { ResLoader::playMusic(MusicType::Click); // 播放点击音效 auto &app = extra2d::Application::instance(); - app.scenes().replaceScene(extra2d::makePtr(), + app.scenes().replaceScene(extra2d::shared(), extra2d::TransitionType::Fade, 0.5f); }); ``` diff --git a/examples/collision_demo/main.cpp b/examples/collision_demo/main.cpp index 9b0c105..9dd368f 100644 --- a/examples/collision_demo/main.cpp +++ b/examples/collision_demo/main.cpp @@ -67,7 +67,7 @@ public: // 创建移动的中心方块 centerBox_ = - makePtr(80.0f, 80.0f, Color(0.2f, 0.6f, 1.0f, 0.8f)); + shared(80.0f, 80.0f, Color(0.2f, 0.6f, 1.0f, 0.8f)); centerBox_->setPosition(Vec2(centerX, centerY)); addChild(centerBox_); @@ -149,7 +149,8 @@ private: addChild(fpsText_); // 创建退出提示文本 - float screenHeight = static_cast(Application::instance().getConfig().height); + float screenHeight = + static_cast(Application::instance().getConfig().height); exitHintText_ = Text::create("按 + 键退出", infoFont_); exitHintText_->setPosition(50.0f, screenHeight - 50.0f); exitHintText_->setTextColor(Color(0.8f, 0.8f, 0.8f, 1.0f)); @@ -182,7 +183,7 @@ private: }; for (const auto &[pos, color] : positions) { - auto box = makePtr(70.0f, 70.0f, color); + auto box = shared(70.0f, 70.0f, color); box->setPosition(pos); addChild(box); boxes_.push_back(box); @@ -238,8 +239,7 @@ private: // 程序入口 // ============================================================================ -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { // 初始化日志系统 Logger::init(); Logger::setLevel(LogLevel::Debug); @@ -266,7 +266,7 @@ int main(int argc, char **argv) } // 进入场景 - app.enterScene(makePtr()); + app.enterScene(shared()); E2D_LOG_INFO("开始主循环..."); diff --git a/examples/hello_world/main.cpp b/examples/hello_world/main.cpp index 2d1fa7d..fb71f85 100644 --- a/examples/hello_world/main.cpp +++ b/examples/hello_world/main.cpp @@ -115,7 +115,7 @@ int main(int argc, char **argv) { } // 进入 Hello World 场景 - app.enterScene(makePtr()); + app.enterScene(shared()); E2D_LOG_INFO("开始主循环..."); diff --git a/scripts/create_project.py b/scripts/create_project.py index afdcb83..d5ec0e3 100644 --- a/scripts/create_project.py +++ b/scripts/create_project.py @@ -321,7 +321,7 @@ int main(int argc, char **argv) {{ }} // 进入主场景 - app.enterScene(makePtr()); + app.enterScene(shared()); E2D_LOG_INFO("开始主循环...");