修改Tom总流程,完善动画机的状态设置以及游戏逻辑;在core/platform新增win和板端的鼠标点击/触控的逻辑代码/驱动;测试游戏内容。

This commit is contained in:
HP
2026-06-08 12:53:38 +08:00
parent 24fe01af46
commit 8785368913
11 changed files with 671 additions and 270 deletions
+7
View File
@@ -4,10 +4,17 @@ set_source_files_properties(${TOM_ATLAS_HEADER} PROPERTIES GENERATED TRUE)
add_executable(${TOM_GAME_TARGET}
Main.cpp
src/app/TomGameApp.cpp
src/audio/VoiceEffect.cpp
src/audio/VoicePlayer.cpp
src/audio/VoiceRecorder.cpp
${TOM_ATLAS_HEADER}
)
target_include_directories(${TOM_GAME_TARGET} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/src/app
${CMAKE_CURRENT_SOURCE_DIR}/src/audio
${CMAKE_CURRENT_SOURCE_DIR}/generated
)
+28 -49
View File
@@ -1,4 +1,3 @@
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <cstdlib>
@@ -6,12 +5,12 @@
#include <iostream>
#include <thread>
#include "Color.h"
#include "DefaultHardware.h"
#include "Display.h"
#include "DrawContext.h"
#include "TimeSource.h"
#include "Timer.h"
#include "tom_atlas.h"
#include "app/TomGameApp.h"
#ifdef USE_FRAMEBUFFER
#include "FBDisplay.h"
@@ -108,25 +107,6 @@ namespace
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
}
}
static const RenderData::SpriteRegion& SelectTomFrame(
uint32_t animation_time_ms,
const RenderData::SpriteRegion* const* speaking_frames,
size_t speaking_frame_count)
{
const uint32_t phase = (animation_time_ms / 1000u) % 6u;
if (phase < 2u)
{
return TomAtlas::tom_stand;
}
if (phase < 3u)
{
return TomAtlas::tom_listhen;
}
const size_t frame_index = (animation_time_ms / 120u) % speaking_frame_count;
return *speaking_frames[frame_index];
}
}
int main(int argc, char* argv[])
@@ -145,28 +125,37 @@ int main(int argc, char* argv[])
return -1;
}
const RenderData::SpriteRegion* speaking_frames[] = {
&TomAtlas::tom_say1,
&TomAtlas::tom_say2,
&TomAtlas::tom_say3,
&TomAtlas::tom_say4,
&TomAtlas::tom_say3,
&TomAtlas::tom_say2
};
const size_t speaking_frame_count = sizeof(speaking_frames) / sizeof(speaking_frames[0]);
Platform::DefaultAudioInput audioInput;
Platform::DefaultAudioOutput audioOutput;
Platform::DefaultButtonInput buttonInput;
Platform::DefaultPointerInput pointerInput;
if (!buttonInput.init())
{
std::cerr << "[WARN] Button input init failed; physical key trigger is disabled." << std::endl;
}
if (!pointerInput.init("", ScreenWidth, ScreenHeight))
{
std::cerr << "[WARN] Pointer input init failed; on-screen button trigger is disabled." << std::endl;
}
Core::DrawContext ctx(ScreenWidth, ScreenHeight);
Core::Timer timer(options.target_fps);
Platform::SteadyTimeSource time_source;
Game::TomGameApp app(
ScreenWidth,
ScreenHeight,
&audioInput,
&audioOutput,
&buttonInput,
&pointerInput);
std::cout << "[INFO] Tom game started. Target FPS: " << timer.target_fps() << std::endl;
bool is_running = true;
uint32_t animation_time_ms = 0;
while (is_running)
{
timer.begin_frame(time_source.get_time_ms());
animation_time_ms += timer.fixed_delta_ms();
bool should_quit = false;
display->poll_events(should_quit);
@@ -175,26 +164,16 @@ int main(int argc, char* argv[])
is_running = false;
}
ctx.clear(RenderData::Color(18, 18, 24, 255));
ctx.draw_sprite_region(0, 0, TomAtlas::background);
const RenderData::SpriteRegion& tom = SelectTomFrame(
animation_time_ms,
speaking_frames,
speaking_frame_count);
const int32_t tom_x = (ScreenWidth - tom.width) / 2;
const int32_t tom_y = std::max(0, ScreenHeight - tom.height - 72);
ctx.draw_sprite_region(tom_x, tom_y, tom);
const int32_t button_x = (ScreenWidth - TomAtlas::ui_record.width) / 2;
const int32_t button_y = ScreenHeight - TomAtlas::ui_record.height - 16;
ctx.draw_sprite_region(button_x, button_y, TomAtlas::ui_record);
app.update(timer.fixed_delta_ms());
app.draw(ctx);
ctx.present(display);
SleepRemainingFrameTime(timer, time_source);
}
pointerInput.shutdown();
buttonInput.shutdown();
audioInput.shutdown();
audioOutput.shutdown();
display->shutdown();
delete display;
return 0;
+175 -165
View File
@@ -3,56 +3,67 @@
#include "AudioInput.h"
#include "AudioOutput.h"
#include "ButtonInput.h"
#include "FrameBuffer.h"
#include "SpriteRasterizer.h"
#include "Image.h"
#include "Color.h"
#include "DrawContext.h"
#include "PointerInput.h"
#include "SpriteRegion.h"
#include "tom_atlas.h"
#include <algorithm>
#include <cstddef>
#include <iostream>
namespace
{
const uint32_t DefaultMaxRecordMs = 5000u;
const uint32_t SpeakingFrameMs = 90u;
const int32_t TomBottomPadding = 72;
const int32_t ButtonBottomPadding = 16;
const RenderData::SpriteRegion* const SpeakingFrames[] = {
&TomAtlas::tom_say1,
&TomAtlas::tom_say2,
&TomAtlas::tom_say3,
&TomAtlas::tom_say4,
&TomAtlas::tom_say3,
&TomAtlas::tom_say2
};
static const RenderData::SpriteRegion& SelectSpeakingFrame(uint32_t animationMs)
{
const size_t frameCount = sizeof(SpeakingFrames) / sizeof(SpeakingFrames[0]);
const size_t frameIndex = (animationMs / SpeakingFrameMs) % frameCount;
return *SpeakingFrames[frameIndex];
}
}
namespace Game
{
TomGameApp::TomGameApp(
Core::FrameBuffer* frameBuffer,
Rasterizer::SpriteRasterizer* spriteRasterizer,
int32_t screenWidth,
int32_t screenHeight,
Platform::IAudioInput* audioInput,
Platform::IAudioOutput* audioOutput,
Platform::IButtonInput* buttonInput) :
frameBuffer(frameBuffer),
spriteRasterizer(spriteRasterizer),
audioInput(audioInput),
audioOutput(audioOutput),
buttonInput(buttonInput),
state(TomGameState::Idle),
screenWidth(frameBuffer ? frameBuffer->get_width() : 0),
screenHeight(frameBuffer ? frameBuffer->get_height() : 0),
tomX(0),
tomY(0),
buttonX(0),
buttonY(0),
audioSampleRate(16000),
audioChannels(1),
pitchFactor(1.45f),
outputGain(1.15f)
Platform::IButtonInput* buttonInput,
Platform::IPointerInput* pointerInput)
: audioInput(audioInput),
audioOutput(audioOutput),
buttonInput(buttonInput),
pointerInput(pointerInput),
state(TomGameState::Idle),
screenWidth(screenWidth),
screenHeight(screenHeight),
buttonX(0),
buttonY(0),
audioSampleRate(16000),
audioChannels(1),
maxRecordMs(DefaultMaxRecordMs),
recordingElapsedMs(0),
speakingAnimationMs(0),
pitchFactor(1.45f),
outputGain(1.15f)
{
recorder.configure(audioSampleRate, audioChannels);
}
void TomGameApp::set_assets(const TomGameAssets& assets)
{
this->assets = assets;
speakingFrames.clear();
if (assets.tomSay1 != nullptr) speakingFrames.push_back(RenderData::Sprite(assets.tomSay1));
if (assets.tomSay2 != nullptr) speakingFrames.push_back(RenderData::Sprite(assets.tomSay2));
if (assets.tomSay3 != nullptr) speakingFrames.push_back(RenderData::Sprite(assets.tomSay3));
if (assets.tomSay4 != nullptr) speakingFrames.push_back(RenderData::Sprite(assets.tomSay4));
if (assets.tomSay3 != nullptr) speakingFrames.push_back(RenderData::Sprite(assets.tomSay3));
if (assets.tomSay2 != nullptr) speakingFrames.push_back(RenderData::Sprite(assets.tomSay2));
speakingAnimator.set_frames(speakingFrames);
speakingAnimator.set_frame_time(0.08f);
speakingAnimator.set_loop(true);
speakingAnimator.stop();
calculate_layout();
configure_audio(audioSampleRate, audioChannels);
calculate_button_layout();
}
void TomGameApp::configure_audio(uint32_t sampleRate, uint32_t channels)
@@ -66,142 +77,142 @@ namespace Game
audioChannels = channels;
}
recorder.configure(audioSampleRate, audioChannels);
recorder.configure(
audioSampleRate,
audioChannels,
static_cast<float>(maxRecordMs) / 1000.0f,
0.0f,
static_cast<float>(maxRecordMs) / 1000.0f,
0.0f);
}
void TomGameApp::update(float deltaTime, bool buttonPressed)
void TomGameApp::update(uint32_t deltaMs)
{
bool trigger = buttonPressed;
if (buttonInput != nullptr)
{
buttonInput->update();
trigger = trigger || buttonInput->was_pressed();
}
bool recordTrigger = false;
update_input(recordTrigger);
switch (state)
{
case TomGameState::Idle:
if (trigger)
if (recordTrigger)
{
start_recording();
}
break;
case TomGameState::Recording:
if (audioInput != nullptr)
{
recorder.update(*audioInput, 512);
}
if (trigger || recorder.is_finished())
{
finish_recording();
}
break;
case TomGameState::Processing:
process_voice();
update_recording(deltaMs);
break;
case TomGameState::Speaking:
speakingAnimator.update(deltaTime);
if (audioOutput == nullptr)
{
back_to_idle();
break;
}
player.update(*audioOutput, 1024);
if (player.is_finished())
{
back_to_idle();
}
update_speaking(deltaMs);
break;
}
}
void TomGameApp::draw()
void TomGameApp::draw(Core::DrawContext& ctx)
{
if (spriteRasterizer == nullptr)
ctx.clear_color(RenderData::Color(18, 18, 24, 255));
ctx.draw_sprite_region(0, 0, TomAtlas::background);
const RenderData::SpriteRegion* tom = &TomAtlas::tom_stand;
if (state == TomGameState::Recording)
{
return;
tom = &TomAtlas::tom_listhen;
}
else if (state == TomGameState::Speaking)
{
tom = &SelectSpeakingFrame(speakingAnimationMs);
}
if (assets.background != nullptr)
const int32_t tomX = (screenWidth - tom->width) / 2;
int32_t tomY = screenHeight - tom->height - TomBottomPadding;
if (tomY < 0)
{
spriteRasterizer->DrawImage(*assets.background, 0, 0);
tomY = 0;
}
const RenderData::Sprite tomSprite = get_current_tom_sprite();
if (tomSprite.is_valid())
{
spriteRasterizer->DrawSprite(tomSprite, tomX, tomY);
}
if (assets.button != nullptr)
{
spriteRasterizer->DrawImage(*assets.button, buttonX, buttonY);
}
ctx.draw_sprite_region(tomX, tomY, *tom);
ctx.draw_sprite_region(buttonX, buttonY, TomAtlas::ui_record);
}
bool TomGameApp::is_ready() const
void TomGameApp::update_input(bool& recordTrigger)
{
return frameBuffer != nullptr &&
spriteRasterizer != nullptr &&
assets.background != nullptr &&
assets.tomStand != nullptr &&
assets.tomListen != nullptr &&
assets.button != nullptr &&
speakingFrames.size() == 6;
}
recordTrigger = false;
bool TomGameApp::is_button_hit(int32_t x, int32_t y) const
{
if (assets.button == nullptr)
if (buttonInput != nullptr)
{
return false;
buttonInput->update();
recordTrigger = recordTrigger || buttonInput->was_pressed();
}
return x >= buttonX &&
x < buttonX + assets.button->get_width() &&
y >= buttonY &&
y < buttonY + assets.button->get_height();
}
void TomGameApp::calculate_layout()
{
screenWidth = frameBuffer ? frameBuffer->get_width() : 0;
screenHeight = frameBuffer ? frameBuffer->get_height() : 0;
if (assets.button != nullptr)
if (pointerInput != nullptr)
{
buttonX = (screenWidth - assets.button->get_width()) / 2;
buttonY = screenHeight - assets.button->get_height() - 16;
}
const RenderData::Sprite tomSprite = get_current_tom_sprite();
if (tomSprite.is_valid())
{
tomX = (screenWidth - tomSprite.width) / 2;
tomY = screenHeight - tomSprite.height - 72;
if (tomY < 0)
pointerInput->update();
if (pointerInput->was_pressed() &&
is_record_button_hit(pointerInput->get_x(), pointerInput->get_y()))
{
tomY = 0;
recordTrigger = true;
}
}
}
void TomGameApp::start_recording()
{
if (audioInput != nullptr && !audioInput->is_open())
if (audioInput == nullptr)
{
audioInput->init("default", audioSampleRate, audioChannels);
std::cerr << "[WARN] Audio input is unavailable." << std::endl;
return;
}
if (audioOutput != nullptr && audioOutput->is_open())
{
audioOutput->shutdown();
}
if (!audioInput->is_open() &&
!audioInput->init("default", audioSampleRate, audioChannels))
{
std::cerr << "[WARN] Audio input init failed." << std::endl;
return;
}
audioSampleRate = audioInput->get_sample_rate();
audioChannels = audioInput->get_channels();
player.stop();
recorder.configure(audioSampleRate, audioChannels);
recorder.configure(
audioSampleRate,
audioChannels,
static_cast<float>(maxRecordMs) / 1000.0f,
0.0f,
static_cast<float>(maxRecordMs) / 1000.0f,
0.0f);
recorder.start();
recordingElapsedMs = 0;
speakingAnimationMs = 0;
state = TomGameState::Recording;
calculate_layout();
}
void TomGameApp::update_recording(uint32_t deltaMs)
{
recordingElapsedMs += deltaMs;
if (audioInput != nullptr)
{
uint32_t requestCount = (audioSampleRate * audioChannels * deltaMs) / 1000u;
if (requestCount < audioChannels)
{
requestCount = audioChannels;
}
requestCount -= requestCount % audioChannels;
recorder.update(*audioInput, static_cast<int>(std::min<uint32_t>(requestCount, 2048u)));
}
if (recordingElapsedMs >= maxRecordMs || recorder.is_finished())
{
finish_recording();
}
}
void TomGameApp::finish_recording()
@@ -211,11 +222,11 @@ namespace Game
recorder.stop();
}
state = TomGameState::Processing;
}
if (audioInput != nullptr && audioInput->is_open())
{
audioInput->shutdown();
}
void TomGameApp::process_voice()
{
std::vector<int16_t> samples = recorder.get_samples();
samples = VoiceEffect::trim_silence(samples, 0.02f, audioChannels);
samples = VoiceEffect::pitch_up(samples, pitchFactor, audioChannels);
@@ -226,7 +237,7 @@ namespace Game
void TomGameApp::start_speaking(const std::vector<int16_t>& samples)
{
if (samples.empty())
if (samples.empty() || audioOutput == nullptr)
{
back_to_idle();
return;
@@ -234,47 +245,46 @@ namespace Game
player.set_voice(samples, audioSampleRate, audioChannels);
player.play();
speakingAnimator.reset();
speakingAnimator.play();
speakingAnimationMs = 0;
state = TomGameState::Speaking;
calculate_layout();
}
void TomGameApp::update_speaking(uint32_t deltaMs)
{
speakingAnimationMs += deltaMs;
if (audioOutput == nullptr)
{
back_to_idle();
return;
}
player.update(*audioOutput, 1024);
if (player.is_finished())
{
back_to_idle();
}
}
void TomGameApp::back_to_idle()
{
player.stop();
speakingAnimator.stop();
recordingElapsedMs = 0;
speakingAnimationMs = 0;
state = TomGameState::Idle;
calculate_layout();
}
void TomGameApp::draw_sprite_centered(const RenderData::Sprite& sprite, int32_t centerX, int32_t y)
void TomGameApp::calculate_button_layout()
{
if (spriteRasterizer == nullptr || !sprite.is_valid())
{
return;
}
spriteRasterizer->DrawSprite(sprite, centerX - sprite.width / 2, y);
buttonX = (screenWidth - TomAtlas::ui_record.width) / 2;
buttonY = screenHeight - TomAtlas::ui_record.height - ButtonBottomPadding;
}
const RenderData::Sprite TomGameApp::get_current_tom_sprite() const
bool TomGameApp::is_record_button_hit(int32_t x, int32_t y) const
{
if (state == TomGameState::Recording && assets.tomListen != nullptr)
{
return RenderData::Sprite(assets.tomListen);
}
if (state == TomGameState::Speaking)
{
const RenderData::Sprite* sprite = speakingAnimator.get_current_sprite();
if (sprite != nullptr)
{
return *sprite;
}
}
return RenderData::Sprite(assets.tomStand);
return x >= buttonX &&
x < buttonX + TomAtlas::ui_record.width &&
y >= buttonY &&
y < buttonY + TomAtlas::ui_record.height;
}
}
+19 -56
View File
@@ -1,24 +1,13 @@
#pragma once
#include <cstdint>
#include <vector>
#include "../audio/VoicePlayer.h"
#include "../audio/VoiceRecorder.h"
#include "../components/SpriteAnimator.h"
#include "Sprite.h"
namespace Core
{
class FrameBuffer;
}
namespace Rasterizer
{
class SpriteRasterizer;
}
namespace RenderData
{
class Image;
class DrawContext;
}
namespace Platform
@@ -26,6 +15,7 @@ namespace Platform
class IAudioInput;
class IAudioOutput;
class IButtonInput;
class IPointerInput;
}
namespace Game
@@ -34,85 +24,58 @@ namespace Game
{
Idle,
Recording,
Processing,
Speaking
};
struct TomGameAssets
{
const RenderData::Image* background;
const RenderData::Image* tomStand;
const RenderData::Image* tomListen;
const RenderData::Image* tomSay1;
const RenderData::Image* tomSay2;
const RenderData::Image* tomSay3;
const RenderData::Image* tomSay4;
const RenderData::Image* button;
TomGameAssets() :
background(nullptr),
tomStand(nullptr),
tomListen(nullptr),
tomSay1(nullptr),
tomSay2(nullptr),
tomSay3(nullptr),
tomSay4(nullptr),
button(nullptr)
{}
};
class TomGameApp
{
private:
Core::FrameBuffer* frameBuffer;
Rasterizer::SpriteRasterizer* spriteRasterizer;
Platform::IAudioInput* audioInput;
Platform::IAudioOutput* audioOutput;
Platform::IButtonInput* buttonInput;
Platform::IPointerInput* pointerInput;
TomGameAssets assets;
TomGameState state;
VoiceRecorder recorder;
VoicePlayer player;
SpriteAnimator speakingAnimator;
std::vector<RenderData::Sprite> speakingFrames;
int32_t screenWidth;
int32_t screenHeight;
int32_t tomX;
int32_t tomY;
int32_t buttonX;
int32_t buttonY;
uint32_t audioSampleRate;
uint32_t audioChannels;
uint32_t maxRecordMs;
uint32_t recordingElapsedMs;
uint32_t speakingAnimationMs;
float pitchFactor;
float outputGain;
void calculate_layout();
void update_input(bool& recordTrigger);
void start_recording();
void update_recording(uint32_t deltaMs);
void finish_recording();
void process_voice();
void start_speaking(const std::vector<int16_t>& samples);
void update_speaking(uint32_t deltaMs);
void back_to_idle();
void draw_sprite_centered(const RenderData::Sprite& sprite, int32_t centerX, int32_t y);
const RenderData::Sprite get_current_tom_sprite() const;
void calculate_button_layout();
bool is_record_button_hit(int32_t x, int32_t y) const;
public:
TomGameApp(
Core::FrameBuffer* frameBuffer,
Rasterizer::SpriteRasterizer* spriteRasterizer,
int32_t screenWidth,
int32_t screenHeight,
Platform::IAudioInput* audioInput,
Platform::IAudioOutput* audioOutput,
Platform::IButtonInput* buttonInput = nullptr);
Platform::IButtonInput* buttonInput,
Platform::IPointerInput* pointerInput);
void set_assets(const TomGameAssets& assets);
void configure_audio(uint32_t sampleRate = 16000, uint32_t channels = 1);
void update(float deltaTime, bool buttonPressed = false);
void draw();
void update(uint32_t deltaMs);
void draw(Core::DrawContext& ctx);
bool is_ready() const;
bool is_button_hit(int32_t x, int32_t y) const;
TomGameState get_state() const { return state; }
float get_record_volume() const { return recorder.get_last_volume(); }
float get_play_progress() const { return player.get_progress(); }