完善模块间依赖
This commit is contained in:
+1
-1
@@ -12,7 +12,7 @@
|
|||||||
"target_fps": 60
|
"target_fps": 60
|
||||||
},
|
},
|
||||||
"audio": {
|
"audio": {
|
||||||
"music_volume": 0.5,
|
"music_volume": 0.2,
|
||||||
"sound_volume": 0.5
|
"sound_volume": 0.5
|
||||||
},
|
},
|
||||||
"input_mappings": {
|
"input_mappings": {
|
||||||
|
|||||||
@@ -11,7 +11,6 @@
|
|||||||
#include "../input/input_manager.h"
|
#include "../input/input_manager.h"
|
||||||
#include "../physics/physics_engine.h"
|
#include "../physics/physics_engine.h"
|
||||||
#include "../scene/scene_manager.h"
|
#include "../scene/scene_manager.h"
|
||||||
#include "../../game/scene/title_scene.h"
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
@@ -47,8 +46,18 @@ void GameApp::run() {
|
|||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GameApp::registerSceneSetup(std::function<void(engine::scene::SceneManager &)> func)
|
||||||
|
{
|
||||||
|
scene_setup_func_ = std::move(func);
|
||||||
|
spdlog::trace("已注册场景设置函数。");
|
||||||
|
}
|
||||||
|
|
||||||
bool GameApp::init() {
|
bool GameApp::init() {
|
||||||
spdlog::trace("初始化 GameApp ...");
|
spdlog::trace("初始化 GameApp ...");
|
||||||
|
if (!scene_setup_func_) {
|
||||||
|
spdlog::error("未注册场景设置函数,无法初始化 GameApp。");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (!initConfig()) return false;
|
if (!initConfig()) return false;
|
||||||
if (!initSDL()) return false;
|
if (!initSDL()) return false;
|
||||||
if (!initTime()) return false;
|
if (!initTime()) return false;
|
||||||
@@ -64,9 +73,8 @@ bool GameApp::init() {
|
|||||||
if (!initContext()) return false;
|
if (!initContext()) return false;
|
||||||
if (!initSceneManager()) return false;
|
if (!initSceneManager()) return false;
|
||||||
|
|
||||||
// 创建第一个场景并压入栈
|
// 调用场景设置函数 (创建第一个场景并压入栈)
|
||||||
auto scene = std::make_unique<game::scene::TitleScene>(*context_, *scene_manager_);
|
scene_setup_func_(*scene_manager_);
|
||||||
scene_manager_->requestPushScene(std::move(scene));
|
|
||||||
|
|
||||||
is_running_ = true;
|
is_running_ = true;
|
||||||
spdlog::trace("GameApp 初始化成功。");
|
spdlog::trace("GameApp 初始化成功。");
|
||||||
@@ -191,6 +199,8 @@ bool GameApp::initAudioPlayer()
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
audio_player_ = std::make_unique<engine::audio::AudioPlayer>(resource_manager_.get());
|
audio_player_ = std::make_unique<engine::audio::AudioPlayer>(resource_manager_.get());
|
||||||
|
audio_player_->setMusicVolume(config_->music_volume_); // 设置背景音乐音量
|
||||||
|
audio_player_->setSoundVolume(config_->sound_volume_); // 设置音效音量
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
spdlog::error("初始化音频播放器失败: {}", e.what());
|
spdlog::error("初始化音频播放器失败: {}", e.what());
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
// 前向声明, 减少头文件的依赖,增加编译速度
|
// 前向声明, 减少头文件的依赖,增加编译速度
|
||||||
struct SDL_Window;
|
struct SDL_Window;
|
||||||
@@ -46,6 +47,9 @@ private:
|
|||||||
SDL_Renderer* sdl_renderer_ = nullptr;
|
SDL_Renderer* sdl_renderer_ = nullptr;
|
||||||
bool is_running_ = false;
|
bool is_running_ = false;
|
||||||
|
|
||||||
|
/// @brief 游戏场景设置函数,用于在运行游戏前设置初始场景 (GameApp不再决定初始场景是什么)
|
||||||
|
std::function<void(engine::scene::SceneManager&)> scene_setup_func_;
|
||||||
|
|
||||||
// 引擎组件
|
// 引擎组件
|
||||||
std::unique_ptr<engine::core::Time> time_;
|
std::unique_ptr<engine::core::Time> time_;
|
||||||
std::unique_ptr<engine::resource::ResourceManager> resource_manager_;
|
std::unique_ptr<engine::resource::ResourceManager> resource_manager_;
|
||||||
@@ -69,6 +73,13 @@ public:
|
|||||||
*/
|
*/
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 注册用于设置初始游戏场景的函数。
|
||||||
|
* 这个函数将在 SceneManager 初始化后被调用。
|
||||||
|
* @param func 一个接收 SceneManager 引用的函数对象。
|
||||||
|
*/
|
||||||
|
void registerSceneSetup(std::function<void(engine::scene::SceneManager&)> func);
|
||||||
|
|
||||||
// 禁止拷贝和移动
|
// 禁止拷贝和移动
|
||||||
GameApp(const GameApp&) = delete;
|
GameApp(const GameApp&) = delete;
|
||||||
GameApp& operator=(const GameApp&) = delete;
|
GameApp& operator=(const GameApp&) = delete;
|
||||||
|
|||||||
@@ -69,10 +69,6 @@ void TitleScene::createUI() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置音量
|
|
||||||
context_.getAudioPlayer().setMusicVolume(0.2f); // 设置背景音乐音量为20%
|
|
||||||
context_.getAudioPlayer().setSoundVolume(0.5f); // 设置音效音量为50%
|
|
||||||
|
|
||||||
// 设置背景音乐
|
// 设置背景音乐
|
||||||
context_.getAudioPlayer().playMusic("assets/audio/platformer_level03_loop.ogg");
|
context_.getAudioPlayer().playMusic("assets/audio/platformer_level03_loop.ogg");
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,20 @@
|
|||||||
#include "engine/core/game_app.h"
|
#include "engine/core/game_app.h"
|
||||||
|
#include "engine/scene/scene_manager.h"
|
||||||
|
#include "game/scene/title_scene.h"
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
void setupInitialScene(engine::scene::SceneManager& scene_manager) {
|
||||||
|
// GameApp在调用run方法之前,先创建并设置初始场景
|
||||||
|
auto title_scene = std::make_unique<game::scene::TitleScene>(scene_manager.getContext(), scene_manager);
|
||||||
|
scene_manager.requestPushScene(std::move(title_scene));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int /* argc */, char* /* argv */[]) {
|
int main(int /* argc */, char* /* argv */[]) {
|
||||||
spdlog::set_level(spdlog::level::off);
|
spdlog::set_level(spdlog::level::off);
|
||||||
|
|
||||||
engine::core::GameApp app;
|
engine::core::GameApp app;
|
||||||
|
app.registerSceneSetup(setupInitialScene);
|
||||||
app.run();
|
app.run();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user