Files
SunnyLand/src/engine/core/game_app.h
T

82 lines
2.1 KiB
C++
Raw Normal View History

2025-05-15 20:33:20 +08:00
#pragma once
2025-05-16 16:45:16 +08:00
#include <memory>
2025-05-15 20:33:20 +08:00
// 前向声明, 减少头文件的依赖,增加编译速度
struct SDL_Window;
struct SDL_Renderer;
2025-05-19 16:22:09 +08:00
namespace engine::resource {
class ResourceManager;
}
2025-05-21 10:04:05 +08:00
namespace engine::render {
class Renderer;
class Camera;
}
2025-05-22 11:15:25 +08:00
namespace engine::input {
class InputManager;
}
2025-05-16 16:45:16 +08:00
namespace engine::core { // 命名空间的最佳实践:与文件路径一致
class Time;
2025-05-21 10:10:09 +08:00
class Config;
2025-05-15 20:33:20 +08:00
/**
* @brief 主游戏应用程序类,初始化SDL,管理游戏循环。
*/
2025-05-16 16:45:16 +08:00
class GameApp final { // final 表示该类不能被继承
2025-05-15 20:33:20 +08:00
private:
SDL_Window* window_ = nullptr;
SDL_Renderer* sdl_renderer_ = nullptr;
bool is_running_ = false;
2025-05-16 16:45:16 +08:00
// 引擎组件
std::unique_ptr<engine::core::Time> time_;
2025-05-19 16:22:09 +08:00
std::unique_ptr<engine::resource::ResourceManager> resource_manager_;
2025-05-21 10:04:05 +08:00
std::unique_ptr<engine::render::Renderer> renderer_;
std::unique_ptr<engine::render::Camera> camera_;
2025-05-21 10:10:09 +08:00
std::unique_ptr<engine::core::Config> config_;
2025-05-22 11:15:25 +08:00
std::unique_ptr<engine::input::InputManager> input_manager_;
2025-05-15 20:33:20 +08:00
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:
2025-05-16 16:45:16 +08:00
[[nodiscard]] bool init(); // nodiscard 表示该函数返回值不应该被忽略
2025-05-15 20:33:20 +08:00
void handleEvents();
void update(float delta_time);
void render();
void close();
2025-05-19 16:22:09 +08:00
// 各模块的初始化/创建函数,在init()中调用
2025-05-21 10:10:09 +08:00
[[nodiscard]] bool initConfig();
2025-05-21 10:04:05 +08:00
[[nodiscard]] bool initSDL();
[[nodiscard]] bool initTime();
[[nodiscard]] bool initResourceManager();
[[nodiscard]] bool initRenderer();
[[nodiscard]] bool initCamera();
2025-05-22 11:15:25 +08:00
[[nodiscard]] bool initInputManager();
2025-05-19 16:22:09 +08:00
// 测试用函数
void testResourceManager();
2025-05-21 10:04:05 +08:00
void testRenderer();
void testCamera();
2025-05-22 11:15:25 +08:00
void testInputManager();
2025-05-24 11:00:25 +08:00
void testGameObject();
2025-05-15 20:33:20 +08:00
};
} // namespace engine::core