From f46978fc3114d9247ff47d5c374c0000c6657dcc Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Mon, 23 Feb 2026 03:27:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(hello=5Fworld):=20=E6=B7=BB=E5=8A=A0SDL2?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E5=92=8C=E7=AA=97=E5=8F=A3=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor(window): 重构窗口模块文件结构并移动按键枚举 docs: 更新main.cpp实现完整的Hello World示例 --- Extra2D/include/extra2d/extra2d.h | 13 ++--- .../{src => include/extra2d}/window/keys.h | 0 Extra2D/include/extra2d/window/window.h | 56 +++++++++---------- examples/hello_world/main.cpp | 45 ++++++++++++++- examples/hello_world/xmake.lua | 9 +++ 5 files changed, 86 insertions(+), 37 deletions(-) rename Extra2D/{src => include/extra2d}/window/keys.h (100%) diff --git a/Extra2D/include/extra2d/extra2d.h b/Extra2D/include/extra2d/extra2d.h index c5871f4..11b7702 100644 --- a/Extra2D/include/extra2d/extra2d.h +++ b/Extra2D/include/extra2d/extra2d.h @@ -8,16 +8,14 @@ #include #include #include -#include #include -// Platform -#include -#include -#include +// Window +#include +#include +#include // Window - SDL2 + OpenGL -#include // Render (OpenGL 4.5) #include @@ -42,15 +40,14 @@ #include #include #include -#include // Audio #include #include // Utils -#include #include +#include #include // Spatial diff --git a/Extra2D/src/window/keys.h b/Extra2D/include/extra2d/window/keys.h similarity index 100% rename from Extra2D/src/window/keys.h rename to Extra2D/include/extra2d/window/keys.h diff --git a/Extra2D/include/extra2d/window/window.h b/Extra2D/include/extra2d/window/window.h index 6236a3b..0cafe2c 100644 --- a/Extra2D/include/extra2d/window/window.h +++ b/Extra2D/include/extra2d/window/window.h @@ -9,47 +9,47 @@ namespace extra2d { struct WindowConfig { - std::string title = "Extra2D Application"; - int width = 1280; - int height = 720; - bool fullscreen = true; - bool resizable = false; - bool centerWindow = true; + std::string title = "Extra2D Application"; + int width = 1280; + int height = 720; + bool fullscreen = false; + bool resizable = true; + bool centerWindow = true; }; class Window { public: - using OnEvent = std::function; + using OnEvent = std::function; - Window(); - ~Window(); + Window(); + ~Window(); - bool create(const WindowConfig& config); - void destroy(); + bool create(const WindowConfig &config); + void destroy(); - void poll(); - bool shouldClose() const; - void close(); + void poll(); + bool shouldClose() const; + void close(); - void setTitle(const std::string& title); - void setSize(int width, int height); - void setFullscreen(bool fullscreen); + void setTitle(const std::string &title); + void setSize(int width, int height); + void setFullscreen(bool fullscreen); - int width() const { return width_; } - int height() const { return height_; } - bool fullscreen() const { return fullscreen_; } + int width() const { return width_; } + int height() const { return height_; } + bool fullscreen() const { return fullscreen_; } - void onEvent(OnEvent cb) { onEvent_ = cb; } + void onEvent(OnEvent cb) { onEvent_ = cb; } - SDL_Window* native() const { return sdlWindow_; } + SDL_Window *native() const { return sdlWindow_; } private: - SDL_Window* sdlWindow_ = nullptr; - int width_ = 1280; - int height_ = 720; - bool fullscreen_ = true; - bool shouldClose_ = false; - OnEvent onEvent_; + SDL_Window *sdlWindow_ = nullptr; + int width_ = 1280; + int height_ = 720; + bool fullscreen_ = true; + bool shouldClose_ = false; + OnEvent onEvent_; }; } // namespace extra2d diff --git a/examples/hello_world/main.cpp b/examples/hello_world/main.cpp index d0202d3..b5aea5b 100644 --- a/examples/hello_world/main.cpp +++ b/examples/hello_world/main.cpp @@ -6,4 +6,47 @@ using namespace extra2d; // Hello World 示例 // ============================================================================ -int main(int \ No newline at end of file +int main(int argc, char **argv) { + E2D_INFO(CAT_APP, "========================"); + E2D_INFO(CAT_APP, "Extra2D Hello World Demo"); + E2D_INFO(CAT_APP, "========================"); + + // 获取应用实例并初始化 + auto &app = Application::instance(); + + if (!app.init()) { + E2D_ERROR(CAT_APP, "应用初始化失败!"); + return -1; + } + + // 获取事件服务并订阅键盘事件 + auto eventService = ServiceLocator::instance().get(); + if (eventService) { + // 订阅 ESC 键退出 + eventService->on(EventType::KeyPressed, [&app](Event &e) { + auto &keyEvent = std::get(e.data); + if (keyEvent.key == static_cast(Key::Escape)) { + E2D_INFO(CAT_INPUT, "ESC 键按下,退出应用"); + app.quit(); + } + }); + + // 订阅手柄按钮退出 + eventService->on(EventType::GamepadButtonPressed, [&app](Event &e) { + auto &btnEvent = std::get(e.data); + if (btnEvent.button == static_cast(Gamepad::Start)) { + E2D_INFO(CAT_INPUT, "START 按钮按下,退出应用"); + app.quit(); + } + }); + } + + E2D_INFO(CAT_APP, "开始主循环..."); + + // 运行应用 + app.run(); + + E2D_INFO(CAT_APP, "应用结束"); + + return 0; +} diff --git a/examples/hello_world/xmake.lua b/examples/hello_world/xmake.lua index 8998521..22d45ae 100644 --- a/examples/hello_world/xmake.lua +++ b/examples/hello_world/xmake.lua @@ -6,12 +6,21 @@ -- 获取当前脚本所在目录(示例根目录) local example_dir = os.scriptdir() +-- 添加依赖包 +if is_plat("mingw") then + add_requires("glm", "libsdl2", "libsdl2_mixer") +end + -- 可执行文件目标 target("hello_world") set_kind("binary") add_files("main.cpp") add_includedirs("../../Extra2D/include") add_deps("extra2d") + + if is_plat("mingw") then + add_packages("glm", "libsdl2", "libsdl2_mixer") + end -- 使用与主项目相同的平台配置 if is_plat("switch") then