feat(hello_world): 添加SDL2依赖和窗口事件处理
refactor(window): 重构窗口模块文件结构并移动按键枚举 docs: 更新main.cpp实现完整的Hello World示例
This commit is contained in:
parent
60191063d2
commit
f46978fc31
|
|
@ -8,16 +8,14 @@
|
||||||
#include <extra2d/core/math_types.h>
|
#include <extra2d/core/math_types.h>
|
||||||
#include <extra2d/core/module.h>
|
#include <extra2d/core/module.h>
|
||||||
#include <extra2d/core/registry.h>
|
#include <extra2d/core/registry.h>
|
||||||
#include <extra2d/core/string.h>
|
|
||||||
#include <extra2d/core/types.h>
|
#include <extra2d/core/types.h>
|
||||||
|
|
||||||
// Platform
|
// Window
|
||||||
#include <extra2d/platform/glfw/glfw_window.h>
|
#include <extra2d/window/keys.h>
|
||||||
#include <extra2d/platform/keys.h>
|
#include <extra2d/window/window.h>
|
||||||
#include <extra2d/platform/window_module.h>
|
#include <extra2d/window/event_converter.h>
|
||||||
|
|
||||||
// Window - SDL2 + OpenGL
|
// Window - SDL2 + OpenGL
|
||||||
#include <extra2d/window/window.h>
|
|
||||||
|
|
||||||
// Render (OpenGL 4.5)
|
// Render (OpenGL 4.5)
|
||||||
#include <extra2d/render/buffer.h>
|
#include <extra2d/render/buffer.h>
|
||||||
|
|
@ -42,15 +40,14 @@
|
||||||
#include <extra2d/event/event.h>
|
#include <extra2d/event/event.h>
|
||||||
#include <extra2d/event/event_dispatcher.h>
|
#include <extra2d/event/event_dispatcher.h>
|
||||||
#include <extra2d/event/event_queue.h>
|
#include <extra2d/event/event_queue.h>
|
||||||
#include <extra2d/event/input_codes.h>
|
|
||||||
|
|
||||||
// Audio
|
// Audio
|
||||||
#include <extra2d/audio/audio_engine.h>
|
#include <extra2d/audio/audio_engine.h>
|
||||||
#include <extra2d/audio/sound.h>
|
#include <extra2d/audio/sound.h>
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
#include <extra2d/utils/data.h>
|
|
||||||
#include <extra2d/utils/random.h>
|
#include <extra2d/utils/random.h>
|
||||||
|
#include <extra2d/utils/save_store.h>
|
||||||
#include <extra2d/utils/timer.h>
|
#include <extra2d/utils/timer.h>
|
||||||
|
|
||||||
// Spatial
|
// Spatial
|
||||||
|
|
|
||||||
|
|
@ -9,47 +9,47 @@
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
struct WindowConfig {
|
struct WindowConfig {
|
||||||
std::string title = "Extra2D Application";
|
std::string title = "Extra2D Application";
|
||||||
int width = 1280;
|
int width = 1280;
|
||||||
int height = 720;
|
int height = 720;
|
||||||
bool fullscreen = true;
|
bool fullscreen = false;
|
||||||
bool resizable = false;
|
bool resizable = true;
|
||||||
bool centerWindow = true;
|
bool centerWindow = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Window {
|
class Window {
|
||||||
public:
|
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);
|
bool create(const WindowConfig &config);
|
||||||
void destroy();
|
void destroy();
|
||||||
|
|
||||||
void poll();
|
void poll();
|
||||||
bool shouldClose() const;
|
bool shouldClose() const;
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
void setTitle(const std::string& title);
|
void setTitle(const std::string &title);
|
||||||
void setSize(int width, int height);
|
void setSize(int width, int height);
|
||||||
void setFullscreen(bool fullscreen);
|
void setFullscreen(bool fullscreen);
|
||||||
|
|
||||||
int width() const { return width_; }
|
int width() const { return width_; }
|
||||||
int height() const { return height_; }
|
int height() const { return height_; }
|
||||||
bool fullscreen() const { return fullscreen_; }
|
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:
|
private:
|
||||||
SDL_Window* sdlWindow_ = nullptr;
|
SDL_Window *sdlWindow_ = nullptr;
|
||||||
int width_ = 1280;
|
int width_ = 1280;
|
||||||
int height_ = 720;
|
int height_ = 720;
|
||||||
bool fullscreen_ = true;
|
bool fullscreen_ = true;
|
||||||
bool shouldClose_ = false;
|
bool shouldClose_ = false;
|
||||||
OnEvent onEvent_;
|
OnEvent onEvent_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace extra2d
|
} // namespace extra2d
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,47 @@ using namespace extra2d;
|
||||||
// Hello World 示例
|
// 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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,21 @@
|
||||||
-- 获取当前脚本所在目录(示例根目录)
|
-- 获取当前脚本所在目录(示例根目录)
|
||||||
local example_dir = os.scriptdir()
|
local example_dir = os.scriptdir()
|
||||||
|
|
||||||
|
-- 添加依赖包
|
||||||
|
if is_plat("mingw") then
|
||||||
|
add_requires("glm", "libsdl2", "libsdl2_mixer")
|
||||||
|
end
|
||||||
|
|
||||||
-- 可执行文件目标
|
-- 可执行文件目标
|
||||||
target("hello_world")
|
target("hello_world")
|
||||||
set_kind("binary")
|
set_kind("binary")
|
||||||
add_files("main.cpp")
|
add_files("main.cpp")
|
||||||
add_includedirs("../../Extra2D/include")
|
add_includedirs("../../Extra2D/include")
|
||||||
add_deps("extra2d")
|
add_deps("extra2d")
|
||||||
|
|
||||||
|
if is_plat("mingw") then
|
||||||
|
add_packages("glm", "libsdl2", "libsdl2_mixer")
|
||||||
|
end
|
||||||
|
|
||||||
-- 使用与主项目相同的平台配置
|
-- 使用与主项目相同的平台配置
|
||||||
if is_plat("switch") then
|
if is_plat("switch") then
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue