这是一次长久的提交:

1. 应用界面增加了返回主页的按钮
2. 修复了gif渲染内存泄漏的严重bug
3. 将PetDao当中的cJSON API替换为cpp_json,完美通过测试
4. 整合已经实现的各种上层建筑,实现了一个宠物对话基本业务应用,用于样品测试展示用
5. 重构了音频播放类,使其更modern,更加便于移植和拓展
This commit is contained in:
Misaki
2025-10-16 11:36:45 +08:00
parent 801138631e
commit ba5e47bc77
38 changed files with 2487 additions and 2008 deletions
+14 -11
View File
@@ -3,10 +3,13 @@
//
#pragma once
#include "PetInterface.h"
// 用于回调函数传递回调类型,以区分回调的是宠物动作还是宠物阶段
enum class PetType : uint8_t {Action, Stage};
// 宠物音频播放观察者类,继承自宠物观察者
class PetAudioStrategy : public PetObserver, public std::enable_shared_from_this<PetAudioStrategy> {
public:
using AudioCallback = std::function<void(const std::string&)>;
using AudioCallback = std::function<void(PetType petType, const std::string&)>;
/**
* 构造时候就将“事件→音频”两张表填好
* @param actionAudios 动作→音频
@@ -66,7 +69,7 @@ public:
if (!audio_callback) return;
auto it = action_audio.find(action);
if (it != action_audio.end()) {
audio_callback(it->second); // 播放对应音频
audio_callback(PetType::Action, it->second); // 播放对应音频
}
}
/**
@@ -79,7 +82,7 @@ public:
if (!audio_callback) return;
auto it = stage_audio.find(newStage); // 注意:播“新阶段”的音频
if (it != stage_audio.end()) {
audio_callback(it->second);
audio_callback(PetType::Stage, it->second);
}
}
private:
@@ -95,7 +98,7 @@ private:
// 渲染器观察者类,继承自PetObserver
class PetRendererStrategy : public PetObserver, public std::enable_shared_from_this<PetRendererStrategy> {
public:
using RenderCallback = std::function<void(const std::string&)>;
using RenderCallback = std::function<void(PetType petType, const std::string&)>;
/**
* 构造函数,可以传入动作到模型路径和阶段到模型路径的映射表
* @param actionModels 动作→模型路径映射
@@ -120,7 +123,7 @@ public:
}
~PetRendererStrategy() override {
// 自动取消注册
if (auto subject = pet_subject.lock()) {
if (const auto subject = pet_subject.lock()) {
subject->removeObserver(shared_from_this());
}
}
@@ -148,11 +151,11 @@ public:
* 宠物动作时触发[Observer接口实现]
* @param action 动作类型
*/
void onPetAction(PetActionType action) override {
void onPetAction(const PetActionType action) override {
if (!render_callback) return;
auto it = action_models.find(action);
const auto it = action_models.find(action);
if (it != action_models.end()) {
render_callback(it->second); // 渲染对应动作模型
render_callback(PetType::Action, it->second); // 渲染对应动作模型
}
}
/**
@@ -160,11 +163,11 @@ public:
* @param oldStage 旧阶段
* @param newStage 新阶段
*/
void onPetStageChange(PetStageType oldStage, PetStageType newStage) override {
void onPetStageChange(PetStageType oldStage, const PetStageType newStage) override {
if (!render_callback) return;
auto it = stage_models.find(newStage); // 注意:渲染"新阶段"的模型
const auto it = stage_models.find(newStage); // 注意:渲染"新阶段"的模型,实际上并没有使用到旧阶段,但还是保留,留一手
if (it != stage_models.end()) {
render_callback(it->second);
render_callback(PetType::Stage, it->second);
}
}
/**