完成窗口创建
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
#include "game_app.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
namespace engine::core {
|
||||
|
||||
GameApp::GameApp() = default;
|
||||
|
||||
GameApp::~GameApp() {
|
||||
if (is_running_) {
|
||||
spdlog::warn("GameApp 被销毁时没有显式关闭。现在关闭。 ...");
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
void GameApp::run() {
|
||||
if (!init()) {
|
||||
spdlog::error("初始化失败,无法运行游戏。");
|
||||
return;
|
||||
}
|
||||
|
||||
while (is_running_) {
|
||||
float delta_time = 0.01f; // 每帧的时间间隔(临时设定)
|
||||
handleEvents();
|
||||
update(delta_time);
|
||||
render();
|
||||
}
|
||||
|
||||
close();
|
||||
}
|
||||
|
||||
bool GameApp::init() {
|
||||
spdlog::trace("初始化 GameApp ...");
|
||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
|
||||
spdlog::error("SDL 初始化失败! SDL错误: {}", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
window_ = SDL_CreateWindow("SunnyLand", 1280, 720, SDL_WINDOW_RESIZABLE);
|
||||
if (window_ == nullptr) {
|
||||
spdlog::error("无法创建窗口! SDL错误: {}", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
sdl_renderer_ = SDL_CreateRenderer(window_, nullptr);
|
||||
if (sdl_renderer_ == nullptr) {
|
||||
spdlog::error("无法创建渲染器! SDL错误: {}", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
is_running_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void GameApp::handleEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_EVENT_QUIT) {
|
||||
is_running_ = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameApp::update(float /* delta_time */) {
|
||||
// 游戏逻辑更新,暂时为空
|
||||
}
|
||||
|
||||
void GameApp::render() {
|
||||
// 渲染代码,暂时为空
|
||||
}
|
||||
|
||||
void GameApp::close() {
|
||||
spdlog::trace("关闭 GameApp ...");
|
||||
if (sdl_renderer_ != nullptr) {
|
||||
SDL_DestroyRenderer(sdl_renderer_);
|
||||
sdl_renderer_ = nullptr;
|
||||
}
|
||||
if (window_ != nullptr) {
|
||||
SDL_DestroyWindow(window_);
|
||||
window_ = nullptr;
|
||||
}
|
||||
SDL_Quit();
|
||||
is_running_ = false;
|
||||
}
|
||||
|
||||
} // namespace engine::core
|
||||
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
// 前向声明, 减少头文件的依赖,增加编译速度
|
||||
struct SDL_Window;
|
||||
struct SDL_Renderer;
|
||||
|
||||
namespace engine::core {
|
||||
|
||||
/**
|
||||
* @brief 主游戏应用程序类,初始化SDL,管理游戏循环。
|
||||
*/
|
||||
class GameApp final {
|
||||
private:
|
||||
SDL_Window* window_ = nullptr;
|
||||
SDL_Renderer* sdl_renderer_ = nullptr;
|
||||
bool is_running_ = false;
|
||||
|
||||
public:
|
||||
GameApp();
|
||||
~GameApp();
|
||||
|
||||
/**
|
||||
* @brief 运行游戏应用程序,其中会调用init(),然后进入主循环,离开循环后自动调用close()。
|
||||
*/
|
||||
void run();
|
||||
|
||||
// 禁止拷贝和移动
|
||||
GameApp(const GameApp&) = delete;
|
||||
GameApp& operator=(const GameApp&) = delete;
|
||||
GameApp(GameApp&&) = delete;
|
||||
GameApp& operator=(GameApp&&) = delete;
|
||||
|
||||
private:
|
||||
[[nodiscard]] bool init();
|
||||
void handleEvents();
|
||||
void update(float delta_time);
|
||||
void render();
|
||||
void close();
|
||||
};
|
||||
|
||||
} // namespace engine::core
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "engine/core/game_app.h"
|
||||
|
||||
int main(int /* argc */, char* /* argv */[]) {
|
||||
engine::core::GameApp app;
|
||||
app.run();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user