refactor: 重构项目结构和资源管理

- 将主程序代码和资源文件移动到Game目录下
- 更新构建脚本以适配新的目录结构
- 重构应用初始化流程,移除冗余代码
- 更新着色器文件路径和资源管理逻辑
- 删除废弃的windows.lua构建脚本
- 优化Switch平台构建配置
This commit is contained in:
Lenheart 2026-03-16 02:45:52 +08:00
parent f8c2c26cdc
commit 092a28c30e
18 changed files with 82 additions and 181 deletions

View File

@ -12,11 +12,27 @@ namespace frostbite2D {
* *
*/ */
struct AppConfig { struct AppConfig {
/**
* @brief
*/
std::string appName = "frostbite2D App"; std::string appName = "frostbite2D App";
/**
* @brief
*/
std::string appVersion = "1.0.0"; std::string appVersion = "1.0.0";
/**
* @brief
*/
std::string organization = "frostbite"; std::string organization = "frostbite";
/**
* @brief
*/
WindowConfig windowConfig; WindowConfig windowConfig;
/**
* @brief
*/
PlatformType targetPlatform = PlatformType::Auto; PlatformType targetPlatform = PlatformType::Auto;
/** /**
@ -166,11 +182,6 @@ private:
*/ */
void destroyAllModules(); void destroyAllModules();
/**
* @brief
*/
void registerCoreServices();
/** /**
* @brief * @brief
*/ */

View File

@ -4,6 +4,7 @@
#include <frostbite2D/graphics/renderer.h> #include <frostbite2D/graphics/renderer.h>
#include <frostbite2D/platform/switch.h> #include <frostbite2D/platform/switch.h>
#include <frostbite2D/types/type_math.h> #include <frostbite2D/types/type_math.h>
#include <frostbite2D/utils/asset.h>
namespace frostbite2D { namespace frostbite2D {
Application &Application::get() { Application &Application::get() {
@ -45,13 +46,16 @@ bool Application::init(const AppConfig &config) {
switchInit(); switchInit();
#endif #endif
// 使用SDL2创建窗口
this->window_ = new Window(); this->window_ = new Window();
// 创建窗口会使用窗口配置
if (!window_->create(config.windowConfig)) { if (!window_->create(config.windowConfig)) {
SDL_Log("Failed to create window"); SDL_Log("Failed to create window");
return false; return false;
} }
// 初始化核心模块
if (!initCoreModules()) { if (!initCoreModules()) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize core modules"); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize core modules");
shutdown(); shutdown();
@ -96,7 +100,20 @@ Application::~Application() {
} }
bool Application::initCoreModules() { bool Application::initCoreModules() {
registerCoreServices();
// 初始化资产管理器
auto &asset = Asset::get();
// 平台相关 switch平台不可以获取当前工作目录
#ifndef __SWITCH__
// 获取程序工作目录
std::string workingDir = SDL_GetBasePath();
asset.setWorkingDirectory(workingDir);
SDL_Log("Asset working directory: %s", workingDir.c_str());
#else
asset.setWorkingDirectory("/switch/Frostbite2D/" + config_.appName);
SDL_Log("Asset working directory: %s", asset.getWorkingDirectory().c_str());
#endif
// 初始化渲染器 // 初始化渲染器
renderer_ = &Renderer::get(); renderer_ = &Renderer::get();
@ -105,11 +122,12 @@ bool Application::initCoreModules() {
return false; return false;
} }
return true; // 设置窗口清除颜色和视口
} renderer_->setClearColor(0.0f, 0.0f, 0.0f);
renderer_->setViewport(0, 0, config_.windowConfig.width,
config_.windowConfig.height);
void Application::registerCoreServices() { return true;
SDL_Log("Registering core services...");
} }
void Application::setupAllModules() { void Application::setupAllModules() {

View File

@ -26,6 +26,12 @@ bool Renderer::init() {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize batch system"); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize batch system");
return false; return false;
} }
//初始化着色器管理器
if (!shaderManager_.init("shaders")) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize shader manager");
return false;
}
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -4,7 +4,6 @@
#include <frostbite2D/core/window.h> #include <frostbite2D/core/window.h>
#include <frostbite2D/graphics/renderer.h> #include <frostbite2D/graphics/renderer.h>
#include <frostbite2D/graphics/texture.h> #include <frostbite2D/graphics/texture.h>
#include <frostbite2D/utils/asset.h>
#include <glad/glad.h> #include <glad/glad.h>
using namespace frostbite2D; using namespace frostbite2D;
@ -23,15 +22,7 @@ int main(int argc, char **argv) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize application!"); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize application!");
return -1; return -1;
} }
auto& renderer = Renderer::get();
renderer.setClearColor(0.2f, 0.3f, 1.0f);
renderer.setViewport(0, 0, 800, 600);
auto& asset = Asset::get();
asset.setWorkingDirectory("assets");
SDL_Log("Application initialized successfully");
SDL_Log("Starting main loop..."); SDL_Log("Starting main loop...");
app.run(); app.run();

View File

@ -8,13 +8,16 @@ target("Frostbite2D")
add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c")) add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c"))
add_includedirs(path.join(os.projectdir(), "Frostbite2D/include")) add_includedirs(path.join(os.projectdir(), "Frostbite2D/include"))
add_files(path.join(os.projectdir(), "Game/src/**.cpp"))
add_includedirs(path.join(os.projectdir(), "Game/include"))
add_packages("libsdl2") add_packages("libsdl2")
add_packages("glm") add_packages("glm")
-- 复制着色器文件到输出目录 -- 复制着色器文件到输出目录
after_build(function (target) after_build(function (target)
-- 复制 shaders 目录 -- 复制 shaders 目录
local shaders_dir = path.join(os.projectdir(), "shaders") local shaders_dir = path.join(os.projectdir(), "Game/shaders")
local output_dir = target:targetdir() local output_dir = target:targetdir()
local target_shaders_dir = path.join(output_dir, "shaders") local target_shaders_dir = path.join(output_dir, "shaders")

View File

@ -10,13 +10,16 @@ target("Frostbite2D")
add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c")) add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c"))
add_includedirs(path.join(os.projectdir(), "Frostbite2D/include")) add_includedirs(path.join(os.projectdir(), "Frostbite2D/include"))
add_files(path.join(os.projectdir(), "Game/src/**.cpp"))
add_includedirs(path.join(os.projectdir(), "Game/include"))
add_packages("libsdl2") add_packages("libsdl2")
add_packages("glm") add_packages("glm")
-- 复制着色器文件到输出目录 -- 复制着色器文件到输出目录
after_build(function (target) after_build(function (target)
-- 复制 shaders 目录 -- 复制 shaders 目录
local shaders_dir = path.join(os.projectdir(), "shaders") local shaders_dir = path.join(os.projectdir(), "Game/shaders")
local output_dir = target:targetdir() local output_dir = target:targetdir()
local target_shaders_dir = path.join(output_dir, "shaders") local target_shaders_dir = path.join(output_dir, "shaders")

View File

@ -4,6 +4,9 @@ target("Frostbite2D")
add_files(path.join(os.projectdir(), "Frostbite2D/src/**.cpp")) add_files(path.join(os.projectdir(), "Frostbite2D/src/**.cpp"))
add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c")) add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c"))
add_includedirs(path.join(os.projectdir(), "Frostbite2D/include")) add_includedirs(path.join(os.projectdir(), "Frostbite2D/include"))
add_files(path.join(os.projectdir(), "Game/src/**.cpp"))
add_includedirs(path.join(os.projectdir(), "Game/include"))
-- 检查 DEVKITPRO 环境变量Windows 上使用 C:/devkitPro -- 检查 DEVKITPRO 环境变量Windows 上使用 C:/devkitPro
local devkitPro = os.getenv("DEVKITPRO") or "L:/Switch/devkitPro" local devkitPro = os.getenv("DEVKITPRO") or "L:/Switch/devkitPro"
@ -42,7 +45,9 @@ target("Frostbite2D")
{public = true}) {public = true})
add_syslinks("nx", "m") add_syslinks("nx", "m")
-- 构建后生成 NRO 文件 -- 构建后生成 NRO 文件
-- 复制着色器文件到输出目录
after_build(function (target) after_build(function (target)
local elf_file = target:targetfile() local elf_file = target:targetfile()
local output_dir = path.directory(elf_file) local output_dir = path.directory(elf_file)
@ -63,5 +68,24 @@ target("Frostbite2D")
end end
print("Generated NRO: " .. nro_file) print("Generated NRO: " .. nro_file)
end end
-- 复制 shaders 目录
local shaders_dir = path.join(os.projectdir(), "Game/shaders")
local target_shaders_dir = path.join(output_dir, "shaders")
if os.isdir(shaders_dir) then
-- 确保目标目录存在
if not os.isdir(target_shaders_dir) then
os.mkdir(target_shaders_dir)
end
-- 复制所有着色器文件
for _, file in ipairs(os.files(path.join(shaders_dir, "*.*"))) do
local filename = path.filename(file)
local target_file = path.join(target_shaders_dir, filename)
os.cp(file, target_file)
print("Copy shader: " .. filename)
end
end
end) end)
target_end() target_end()

View File

@ -1,108 +0,0 @@
-- MinGW 编译配置
set_toolchains("mingw")
add_requires("libsdl2", {configs = {shared = true}})
add_requires("glm")
-- 创建自定义规则来编译 .rc 文件
rule("windres_compile")
set_extensions(".rc")
on_build(function (target)
local windres = "windres"
local rc_file = target:sourcefiles()[1]
local obj_file = target:targetfile()
local arch = is_arch("x86_64") and "pe-x86-64" or "pe-i386"
os.execv(windres, {"-i", rc_file, "-o", obj_file, "-O", "coff", "-F", arch})
end)
rule_end()
target("Frostbite2D")
set_kind("binary")
add_files(path.join(os.projectdir(), "Frostbite2D/src/**.cpp"))
add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c"))
add_includedirs(path.join(os.projectdir(), "Frostbite2D/include"))
add_includedirs(path.join(os.projectdir(), "Frostbite2D/include/json"))
-- 添加资源文件(如果存在)
local rc_file = path.join(os.projectdir(), "resources/app.rc")
local ico_file = path.join(os.projectdir(), "assets/icons/app.ico")
if os.isfile(rc_file) and os.isfile(ico_file) then
-- 添加 .rc 文件并应用自定义规则
add_files(rc_file, {rule = "windres_compile"})
end
add_packages("libsdl2")
add_packages("glm")
-- 复制着色器文件到输出目录
after_build(function (target)
-- 复制 shaders 目录
local shaders_dir = path.join(os.projectdir(), "shaders")
local output_dir = target:targetdir()
local target_shaders_dir = path.join(output_dir, "shaders")
if os.isdir(shaders_dir) then
-- 确保目标目录存在
if not os.isdir(target_shaders_dir) then
os.mkdir(target_shaders_dir)
end
-- 复制所有着色器文件
for _, file in ipairs(os.files(path.join(shaders_dir, "*.*"))) do
local filename = path.filename(file)
local target_file = path.join(target_shaders_dir, filename)
os.cp(file, target_file)
end
end
-- 复制图标文件到输出目录
local icons_dir = path.join(os.projectdir(), "assets/icons")
local target_icons_dir = path.join(output_dir, "assets/icons")
if os.isdir(icons_dir) then
if not os.isdir(target_icons_dir) then
os.mkdir(target_icons_dir)
end
for _, file in ipairs(os.files(path.join(icons_dir, "*.*"))) do
local filename = path.filename(file)
local target_file = path.join(target_icons_dir, filename)
os.cp(file, target_file)
print("Copy icon: " .. filename)
end
end
-- 复制 SDL2 DLL
local sdl2_lib = target:pkg("libsdl2")
if sdl2_lib then
local libfiles = sdl2_lib:get("libfiles")
if libfiles then
for _, libfile in ipairs(libfiles) do
-- 查找 DLL 文件
if libfile:endswith(".dll") then
local target_dll = path.join(output_dir, path.filename(libfile))
os.cp(libfile, target_dll)
print("Copy DLL: " .. path.filename(libfile))
end
end
end
end
-- 尝试从 xmake 包目录复制 SDL2.dll
local sdl2_dll_paths = {
path.join(os.getenv("USERPROFILE") or "", ".xmake/packages/l/libsdl2/**/bin/SDL2.dll"),
path.join(os.getenv("USERPROFILE") or "", ".xmake/packages/l/libsdl2/**/lib/SDL2.dll"),
}
for _, dll_pattern in ipairs(sdl2_dll_paths) do
local dll_files = os.files(dll_pattern)
for _, dll_file in ipairs(dll_files) do
local target_dll = path.join(output_dir, "SDL2.dll")
if not os.isfile(target_dll) then
os.cp(dll_file, target_dll)
print("Copy SDL2.dll from: " .. dll_file)
end
end
end
end)
target_end()

View File

@ -1,3 +0,0 @@
#include <windows.h>
1 ICON "assets/icons/app.ico"

View File

@ -1,47 +0,0 @@
#include "SDL_log.h"
#include <SDL2/SDL.h>
#include <frostbite2D/core/application.h>
#include <frostbite2D/core/window.h>
#include <frostbite2D/graphics/renderer.h>
#include <frostbite2D/utils/asset.h>
#include <glad/glad.h>
using namespace frostbite2D;
int main(int argc, char **argv) {
AppConfig AppConfig::createDefault();
config.appName = "Frostbite2D Test App";
config.appVersion = "1.0.0";
config.windowConfig.width = 800;
config.windowConfig.height = 600;
config.windowConfig.title = "Frostbite2D - Renderer Test";
Application& app = Application::get();
if (!app.init(config)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize application!");
return -1;
}
auto& renderer = Renderer::get();
if (!renderer.init()) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize renderer!");
app.shutdown();
return -1;
}
renderer.setClearColor(0.2f, 0.3f, 1.0f);
auto& asset = Asset::get();
asset.setWorkingDirectory("assets");
SDL_Log("Application initialized successfully");
SDL_Log("Starting main loop...");
app.run();
app.shutdown();
SDL_Log("Application exited normally");
return 0;
}

View File

@ -10,7 +10,10 @@ add_rules("mode.debug", "mode.release")
local host_plat = os.host() local host_plat = os.host()
local target_plat = get_config("plat") or host_plat local target_plat = get_config("plat") or host_plat
local supported_plats = {mingw = true, windows = true, linux = true, macosx = true, switch = true} if target_plat == "windows" then
target_plat = "mingw"
end
local supported_plats = {mingw = true, linux = true, macosx = true, switch = true}
-- 自动选择平台 -- 自动选择平台
if not supported_plats[target_plat] then if not supported_plats[target_plat] then