diff --git a/CMakeLists.txt b/CMakeLists.txt index 331de81..e15ad9a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,9 +15,7 @@ add_subdirectory(third_party/SDL3) set(SOURCE_FILES src/main.cpp src/core/Game.cpp - src/core/Game.h src/ui/Render.cpp - src/ui/Render.h ) # 添加可执行文件 diff --git a/README.md b/README.md index 9fa8b73..bc7971f 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ ninja ChessGame/ ├── src/ │ ├── core/ # 核心游戏逻辑 -│ │ ├── Game.h/cpp # 游戏主循环和状态管理 +│ │ ├── Game.h/cpp # 游戏核心管理 │ │ ├── Board.h/cpp # 棋盘逻辑和规则实现 │ │ ├── Piece.h/cpp # 棋子基类和具体棋子实现 │ │ └── Rules.h/cpp # 游戏规则验证 @@ -31,7 +31,7 @@ ChessGame/ │ │ ├── Renderer.h/cpp # 渲染器(SDL3) │ │ ├── UIComponents.h/cpp │ │ └── MenuSystem.h/cpp -│ ├── input/ # ← 新增:输入处理模块(键盘、鼠标等) +│ ├── input/ # ← 输入处理模块(键盘、鼠标等) │ │ ├── InputManager.h/cpp │ ├── utils/ # 工具类 │ │ ├── Logger.h/cpp @@ -47,4 +47,4 @@ ChessGame/ ``` ## 一些想说的 -这是我第一次用cpp写的项目,想写个比较完整的出来,但是遇到了一堆问题,只能说`路漫漫其修远兮,吾将上下而求索`,如果你有好的建议也是可以提出来的 \ No newline at end of file +这是我第一次用cpp写的项目,想写个比较完整的出来,但是遇到了一堆问题,只能说**路漫漫其修远兮,吾将上下而求索**,如果你有好的建议也是可以提出来的 \ No newline at end of file diff --git a/src/core/Game.cpp b/src/core/Game.cpp index c6387cb..77b2714 100644 --- a/src/core/Game.cpp +++ b/src/core/Game.cpp @@ -2,10 +2,7 @@ #include "Game.h" -Game::Game(int g_width, int g_heith) - : Width(g_width), - Height(g_heith), - m_render(std::make_unique()) +Game::Game() { } @@ -22,9 +19,7 @@ void Game::cleanup() { bool Game::initialize() { - if (!m_render->initialize()) { - return false; - } + @@ -35,6 +30,4 @@ bool Game::initialize() { } -void Game::render() { - m_render->Renderhello(); -} + diff --git a/src/core/Game.h b/src/core/Game.h index 093ad73..cfbc102 100644 --- a/src/core/Game.h +++ b/src/core/Game.h @@ -3,22 +3,21 @@ #include #include //#include "Board.h" -#include "ui/Render.h" + class Game { private: - std::unique_ptr m_render; - int Width; - int Height; + + public: - Game(int g_width, int g_heith); + Game(); ~Game(); void cleanup(); bool initialize(); - void render(); + }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 6201d70..a5d5bc2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,11 +2,12 @@ #include #include #include "core/Game.h" - +#include "ui/Render.h" //使用SDL3的appstate进行隔离,避免全局变量 struct AppState { - std::unique_ptr game; + std::unique_ptr g_game; + std::unique_ptr g_renderer; }; @@ -18,25 +19,27 @@ static const int HEIGHT = 900; // 1. 应用程序初始化回调 SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { // 设置应用元数据[8](@ref) - SDL_SetAppMetadata("孢子棋", "1.0.0", "com.zhenyan121.sporechess"); + SDL_SetAppMetadata("孢子棋", "1.0.0", "com.zhenyan121.sporebgconid"); // 初始化SDL视频子系统[6,7](@ref) if (!SDL_Init(SDL_INIT_VIDEO)) { SDL_Log("SDL初始化失败: %s", SDL_GetError()); return SDL_APP_FAILURE; } + // 创建游戏实例 auto state = new AppState(); - state->game = std::make_unique(WIDTH, HEIGHT); + state->g_game = std::make_unique(); + state->g_renderer = std::make_unique(WIDTH, HEIGHT); - if (!state->game->initialize()) { + if (!state->g_game->initialize() || !state->g_renderer->initialize()) { SDL_Log("游戏初始化失败!"); delete state; return SDL_APP_FAILURE; } - + *appstate = state; return SDL_APP_CONTINUE; } @@ -55,12 +58,12 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { // 3. 主循环迭代回调(每帧调用) SDL_AppResult SDL_AppIterate(void *appstate) { auto s = static_cast(appstate); - if (s->game) { //防止空指针 + if (s) { //防止空指针 // 更新游戏逻辑 // 渲染帧 - s->game->render(); + s->g_renderer->render(); } return SDL_APP_CONTINUE; } diff --git a/src/ui/Render.cpp b/src/ui/Render.cpp index bb781b8..38c58c7 100644 --- a/src/ui/Render.cpp +++ b/src/ui/Render.cpp @@ -50,4 +50,9 @@ void Renderer::Renderhello() { SDL_RenderClear(m_renderer); SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 255); SDL_RenderPresent(m_renderer); +} + + +void Renderer::render() { + Renderhello(); } \ No newline at end of file diff --git a/src/ui/Render.h b/src/ui/Render.h index 4307aee..61e60d7 100644 --- a/src/ui/Render.h +++ b/src/ui/Render.h @@ -13,6 +13,7 @@ public: Renderer(int WIDTH, int HEIGHT); Renderer(); ~Renderer(); + void render(); bool initialize(); void Renderhello(); };