diff --git a/Frostbite2D/include/frostbite2D/core/application.h b/Frostbite2D/include/frostbite2D/core/application.h index 976d249..2765a25 100644 --- a/Frostbite2D/include/frostbite2D/core/application.h +++ b/Frostbite2D/include/frostbite2D/core/application.h @@ -12,11 +12,27 @@ namespace frostbite2D { * 仅包含应用级别的配置项,模块配置由各模块自行管理 */ struct AppConfig { + /** + * @brief 应用名称 + */ std::string appName = "frostbite2D App"; + /** + * @brief 应用版本号 + */ std::string appVersion = "1.0.0"; + /** + * @brief 组织名称 + */ std::string organization = "frostbite"; + /** + * @brief 窗口配置 + */ WindowConfig windowConfig; + + /** + * @brief 目标平台 + */ PlatformType targetPlatform = PlatformType::Auto; /** @@ -166,11 +182,6 @@ private: */ void destroyAllModules(); - /** - * @brief 注册核心服务 - */ - void registerCoreServices(); - /** * @brief 主循环 */ diff --git a/Frostbite2D/src/frostbite2D/core/application.cpp b/Frostbite2D/src/frostbite2D/core/application.cpp index 0a8c9df..8b3ce13 100644 --- a/Frostbite2D/src/frostbite2D/core/application.cpp +++ b/Frostbite2D/src/frostbite2D/core/application.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace frostbite2D { Application &Application::get() { @@ -45,13 +46,16 @@ bool Application::init(const AppConfig &config) { switchInit(); #endif + // 使用SDL2创建窗口 this->window_ = new Window(); + // 创建窗口会使用窗口配置 if (!window_->create(config.windowConfig)) { SDL_Log("Failed to create window"); return false; } + // 初始化核心模块 if (!initCoreModules()) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize core modules"); shutdown(); @@ -96,7 +100,20 @@ Application::~Application() { } 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(); @@ -105,11 +122,12 @@ bool Application::initCoreModules() { 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() { - SDL_Log("Registering core services..."); + return true; } void Application::setupAllModules() { diff --git a/Frostbite2D/src/frostbite2D/graphics/renderer.cpp b/Frostbite2D/src/frostbite2D/graphics/renderer.cpp index 88f1bf4..a96162f 100644 --- a/Frostbite2D/src/frostbite2D/graphics/renderer.cpp +++ b/Frostbite2D/src/frostbite2D/graphics/renderer.cpp @@ -26,6 +26,12 @@ bool Renderer::init() { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize batch system"); return false; } + + //初始化着色器管理器 + if (!shaderManager_.init("shaders")) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize shader manager"); + return false; + } glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); diff --git a/assets/icons/README.md b/Game/assets/icons/README.md similarity index 100% rename from assets/icons/README.md rename to Game/assets/icons/README.md diff --git a/assets/icons/app.ico b/Game/assets/icons/app.ico similarity index 100% rename from assets/icons/app.ico rename to Game/assets/icons/app.ico diff --git a/assets/icons/icon.bmp b/Game/assets/icons/icon.bmp similarity index 100% rename from assets/icons/icon.bmp rename to Game/assets/icons/icon.bmp diff --git a/shaders/colored_quad.frag b/Game/shaders/colored_quad.frag similarity index 100% rename from shaders/colored_quad.frag rename to Game/shaders/colored_quad.frag diff --git a/shaders/colored_quad.vert b/Game/shaders/colored_quad.vert similarity index 100% rename from shaders/colored_quad.vert rename to Game/shaders/colored_quad.vert diff --git a/shaders/sprite.frag b/Game/shaders/sprite.frag similarity index 100% rename from shaders/sprite.frag rename to Game/shaders/sprite.frag diff --git a/shaders/sprite.vert b/Game/shaders/sprite.vert similarity index 100% rename from shaders/sprite.vert rename to Game/shaders/sprite.vert diff --git a/Frostbite2D/src/main.cpp b/Game/src/main.cpp similarity index 75% rename from Frostbite2D/src/main.cpp rename to Game/src/main.cpp index 78f8dc5..f4a4cf0 100644 --- a/Frostbite2D/src/main.cpp +++ b/Game/src/main.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include using namespace frostbite2D; @@ -23,15 +22,7 @@ int main(int argc, char **argv) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize application!"); 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..."); app.run(); diff --git a/platform/linux.lua b/platform/linux.lua index 53dba30..6616fab 100644 --- a/platform/linux.lua +++ b/platform/linux.lua @@ -8,13 +8,16 @@ target("Frostbite2D") add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c")) 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("glm") -- 复制着色器文件到输出目录 after_build(function (target) -- 复制 shaders 目录 - local shaders_dir = path.join(os.projectdir(), "shaders") + local shaders_dir = path.join(os.projectdir(), "Game/shaders") local output_dir = target:targetdir() local target_shaders_dir = path.join(output_dir, "shaders") diff --git a/platform/mingw.lua b/platform/mingw.lua index 27e3353..ec2151b 100644 --- a/platform/mingw.lua +++ b/platform/mingw.lua @@ -10,13 +10,16 @@ target("Frostbite2D") add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c")) 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("glm") -- 复制着色器文件到输出目录 after_build(function (target) -- 复制 shaders 目录 - local shaders_dir = path.join(os.projectdir(), "shaders") + local shaders_dir = path.join(os.projectdir(), "Game/shaders") local output_dir = target:targetdir() local target_shaders_dir = path.join(output_dir, "shaders") diff --git a/platform/switch.lua b/platform/switch.lua index 83d1d85..7c32ed5 100644 --- a/platform/switch.lua +++ b/platform/switch.lua @@ -4,6 +4,9 @@ target("Frostbite2D") 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_files(path.join(os.projectdir(), "Game/src/**.cpp")) + add_includedirs(path.join(os.projectdir(), "Game/include")) -- 检查 DEVKITPRO 环境变量(Windows 上使用 C:/devkitPro) local devkitPro = os.getenv("DEVKITPRO") or "L:/Switch/devkitPro" @@ -42,7 +45,9 @@ target("Frostbite2D") {public = true}) add_syslinks("nx", "m") + -- 构建后生成 NRO 文件 + -- 复制着色器文件到输出目录 after_build(function (target) local elf_file = target:targetfile() local output_dir = path.directory(elf_file) @@ -63,5 +68,24 @@ target("Frostbite2D") end print("Generated NRO: " .. nro_file) 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) target_end() diff --git a/platform/windows.lua b/platform/windows.lua deleted file mode 100644 index 35f4527..0000000 --- a/platform/windows.lua +++ /dev/null @@ -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() \ No newline at end of file diff --git a/resources/app.rc b/resources/app.rc deleted file mode 100644 index 1eba335..0000000 --- a/resources/app.rc +++ /dev/null @@ -1,3 +0,0 @@ -#include - -1 ICON "assets/icons/app.ico" \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index e6235b3..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "SDL_log.h" -#include -#include -#include -#include -#include -#include - -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; -} \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index 18cd70c..9c3c411 100644 --- a/xmake.lua +++ b/xmake.lua @@ -10,7 +10,10 @@ add_rules("mode.debug", "mode.release") local host_plat = os.host() 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