feat(hello_world): 添加SDL2依赖和窗口事件处理

refactor(window): 重构窗口模块文件结构并移动按键枚举

docs: 更新main.cpp实现完整的Hello World示例
This commit is contained in:
ChestnutYueyue 2026-02-23 03:27:26 +08:00
parent 60191063d2
commit f46978fc31
5 changed files with 86 additions and 37 deletions

View File

@ -8,16 +8,14 @@
#include <extra2d/core/math_types.h>
#include <extra2d/core/module.h>
#include <extra2d/core/registry.h>
#include <extra2d/core/string.h>
#include <extra2d/core/types.h>
// Platform
#include <extra2d/platform/glfw/glfw_window.h>
#include <extra2d/platform/keys.h>
#include <extra2d/platform/window_module.h>
// Window
#include <extra2d/window/keys.h>
#include <extra2d/window/window.h>
#include <extra2d/window/event_converter.h>
// Window - SDL2 + OpenGL
#include <extra2d/window/window.h>
// Render (OpenGL 4.5)
#include <extra2d/render/buffer.h>
@ -42,15 +40,14 @@
#include <extra2d/event/event.h>
#include <extra2d/event/event_dispatcher.h>
#include <extra2d/event/event_queue.h>
#include <extra2d/event/input_codes.h>
// Audio
#include <extra2d/audio/audio_engine.h>
#include <extra2d/audio/sound.h>
// Utils
#include <extra2d/utils/data.h>
#include <extra2d/utils/random.h>
#include <extra2d/utils/save_store.h>
#include <extra2d/utils/timer.h>
// Spatial

View File

@ -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<void(const SDL_Event&)>;
using OnEvent = std::function<void(const SDL_Event &)>;
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

View File

@ -6,4 +6,47 @@ using namespace extra2d;
// Hello World 示例
// ============================================================================
int main(int
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<IEventService>();
if (eventService) {
// 订阅 ESC 键退出
eventService->on(EventType::KeyPressed, [&app](Event &e) {
auto &keyEvent = std::get<KeyEvent>(e.data);
if (keyEvent.key == static_cast<i32>(Key::Escape)) {
E2D_INFO(CAT_INPUT, "ESC 键按下,退出应用");
app.quit();
}
});
// 订阅手柄按钮退出
eventService->on(EventType::GamepadButtonPressed, [&app](Event &e) {
auto &btnEvent = std::get<GamepadButtonEvent>(e.data);
if (btnEvent.button == static_cast<i32>(Gamepad::Start)) {
E2D_INFO(CAT_INPUT, "START 按钮按下,退出应用");
app.quit();
}
});
}
E2D_INFO(CAT_APP, "开始主循环...");
// 运行应用
app.run();
E2D_INFO(CAT_APP, "应用结束");
return 0;
}

View File

@ -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