1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-15 20:03:15 +02:00

code review

This commit is contained in:
Laserlicht
2023-10-08 20:27:56 +02:00
committed by GitHub
parent a1f6e49f87
commit 89a39fcfc0
7 changed files with 17 additions and 23 deletions

View File

@@ -146,7 +146,7 @@ Mix_Chunk *CSoundHandler::GetSoundChunk(std::pair<std::unique_ptr<ui8 []>, si64>
{ {
try try
{ {
std::vector<ui8> startBytes = std::vector<ui8>(data.first.get(), data.first.get() + 100); std::vector<ui8> startBytes = std::vector<ui8>(data.first.get(), data.first.get() + std::min((si64)100, data.second));
if (cache && soundChunksRaw.find(startBytes) != soundChunksRaw.end()) if (cache && soundChunksRaw.find(startBytes) != soundChunksRaw.end())
return soundChunksRaw[startBytes].first; return soundChunksRaw[startBytes].first;
@@ -223,10 +223,8 @@ int CSoundHandler::playSound(const AudioPath & sound, int repeats, bool cache)
int CSoundHandler::playSound(std::pair<std::unique_ptr<ui8 []>, si64> & data, int repeats, bool cache) int CSoundHandler::playSound(std::pair<std::unique_ptr<ui8 []>, si64> & data, int repeats, bool cache)
{ {
int channel; int channel = -1;
Mix_Chunk *chunk = GetSoundChunk(data, cache); if (Mix_Chunk *chunk = GetSoundChunk(data, cache))
if (chunk)
{ {
channel = Mix_PlayChannel(-1, chunk, repeats); channel = Mix_PlayChannel(-1, chunk, repeats);
if (channel == -1) if (channel == -1)
@@ -240,9 +238,6 @@ int CSoundHandler::playSound(std::pair<std::unique_ptr<ui8 []>, si64> & data, in
else else
initCallback(channel, [chunk](){ Mix_FreeChunk(chunk);}); initCallback(channel, [chunk](){ Mix_FreeChunk(chunk);});
} }
else
channel = -1;
return channel; return channel;
} }

View File

@@ -492,7 +492,7 @@ std::pair<std::unique_ptr<ui8 []>, si64> CVideoPlayer::getAudio(const VideoPath
// Find the first audio stream // Find the first audio stream
int streamAudio = -1; int streamAudio = -1;
for(ui32 i=0; i<formatAudio->nb_streams; i++) for(ui32 i = 0; i < formatAudio->nb_streams; i++)
{ {
if (formatAudio->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) if (formatAudio->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
{ {
@@ -513,8 +513,7 @@ std::pair<std::unique_ptr<ui8 []>, si64> CVideoPlayer::getAudio(const VideoPath
// Get a pointer to the codec context for the audio stream // Get a pointer to the codec context for the audio stream
if (streamAudio > -1) if (streamAudio > -1)
{ {
int ret = 0; int ret = avcodec_parameters_to_context(codecContextAudio, formatAudio->streams[streamAudio]->codecpar);
ret = avcodec_parameters_to_context(codecContextAudio, formatAudio->streams[streamAudio]->codecpar);
if (ret < 0) if (ret < 0)
{ {
//We cannot get codec from parameters //We cannot get codec from parameters
@@ -549,7 +548,7 @@ std::pair<std::unique_ptr<ui8 []>, si64> CVideoPlayer::getAudio(const VideoPath
rc = avcodec_receive_frame(codecContextAudio, frameAudio); rc = avcodec_receive_frame(codecContextAudio, frameAudio);
int bytesToRead = (frameAudio->nb_samples * 2 * (formatAudio->streams[streamAudio]->codecpar->bits_per_coded_sample / 8)); int bytesToRead = (frameAudio->nb_samples * 2 * (formatAudio->streams[streamAudio]->codecpar->bits_per_coded_sample / 8));
if (rc >= 0) if (rc >= 0)
for (int s = 0; s < bytesToRead; s+=sizeof(ui8)) for (int s = 0; s < bytesToRead; s += sizeof(ui8))
{ {
ui8 value; ui8 value;
memcpy(&value, &frameAudio->data[0][s], sizeof(ui8)); memcpy(&value, &frameAudio->data[0][s], sizeof(ui8));
@@ -583,7 +582,7 @@ std::pair<std::unique_ptr<ui8 []>, si64> CVideoPlayer::getAudio(const VideoPath
wav.bitsPerSample = formatAudio->streams[streamAudio]->codecpar->bits_per_coded_sample; wav.bitsPerSample = formatAudio->streams[streamAudio]->codecpar->bits_per_coded_sample;
auto wavPtr = reinterpret_cast<ui8*>(&wav); auto wavPtr = reinterpret_cast<ui8*>(&wav);
dat = std::pair<std::unique_ptr<ui8 []>, si64>(std::make_pair(std::make_unique<ui8[]>(samples.size() + sizeof(wav_hdr)), samples.size() + sizeof(wav_hdr))); dat = std::make_pair(std::make_unique<ui8[]>(samples.size() + sizeof(wav_hdr)), samples.size() + sizeof(wav_hdr));
std::copy(wavPtr, wavPtr + sizeof(wav_hdr), dat.first.get()); std::copy(wavPtr, wavPtr + sizeof(wav_hdr), dat.first.get());
std::copy(samples.begin(), samples.end(), dat.first.get() + sizeof(wav_hdr)); std::copy(samples.begin(), samples.end(), dat.first.get() + sizeof(wav_hdr));

View File

@@ -36,7 +36,7 @@ public:
{ {
return false; return false;
} }
virtual std::pair<std::unique_ptr<ui8 []>, si64> getAudio(const VideoPath & videoToOpen) { return std::pair<std::unique_ptr<ui8 []>, si64>(std::make_pair(std::make_unique<ui8[]>(0), 0)); }; virtual std::pair<std::unique_ptr<ui8 []>, si64> getAudio(const VideoPath & videoToOpen) { return std::make_pair(std::make_unique<ui8[]>(0), 0); };
}; };
class CEmptyVideoPlayer : public IMainVideoPlayer class CEmptyVideoPlayer : public IMainVideoPlayer

View File

@@ -215,7 +215,7 @@ void CHighScoreScreen::buttonExitClick()
} }
CHighScoreInputScreen::CHighScoreInputScreen(bool won, HighScoreCalculation calc) CHighScoreInputScreen::CHighScoreInputScreen(bool won, HighScoreCalculation calc)
: CWindowObject(BORDERED), won(won), calc(calc) : CWindowObject(BORDERED), won(won), calc(calc), videoSoundHandle(-1)
{ {
addUsedEvents(LCLICK | KEYBOARD); addUsedEvents(LCLICK | KEYBOARD);
@@ -296,7 +296,7 @@ void CHighScoreInputScreen::show(Canvas & to)
CCS->videoh->close(); CCS->videoh->close();
video = "HSLOOP.SMK"; video = "HSLOOP.SMK";
auto audioData = CCS->videoh->getAudio(VideoPath::builtin(video)); auto audioData = CCS->videoh->getAudio(VideoPath::builtin(video));
sound = CCS->soundh->playSound(audioData); videoSoundHandle = CCS->soundh->playSound(audioData);
CCS->videoh->open(VideoPath::builtin(video)); CCS->videoh->open(VideoPath::builtin(video));
} }
else else
@@ -310,7 +310,7 @@ void CHighScoreInputScreen::show(Canvas & to)
void CHighScoreInputScreen::activate() void CHighScoreInputScreen::activate()
{ {
auto audioData = CCS->videoh->getAudio(VideoPath::builtin(video)); auto audioData = CCS->videoh->getAudio(VideoPath::builtin(video));
sound = CCS->soundh->playSound(audioData); videoSoundHandle = CCS->soundh->playSound(audioData);
if(!CCS->videoh->open(VideoPath::builtin(video))) if(!CCS->videoh->open(VideoPath::builtin(video)))
{ {
if(!won) if(!won)
@@ -324,7 +324,7 @@ void CHighScoreInputScreen::activate()
void CHighScoreInputScreen::deactivate() void CHighScoreInputScreen::deactivate()
{ {
CCS->videoh->close(); CCS->videoh->close();
CCS->soundh->stopSound(sound); CCS->soundh->stopSound(videoSoundHandle);
CIntObject::deactivate(); CIntObject::deactivate();
} }

View File

@@ -95,7 +95,7 @@ class CHighScoreInputScreen : public CWindowObject
std::shared_ptr<TransparentFilledRectangle> background; std::shared_ptr<TransparentFilledRectangle> background;
std::string video; std::string video;
int sound; int videoSoundHandle;
bool won; bool won;
HighScoreCalculation calc; HighScoreCalculation calc;
public: public:

View File

@@ -20,7 +20,7 @@
CPrologEpilogVideo::CPrologEpilogVideo(CampaignScenarioPrologEpilog _spe, std::function<void()> callback) CPrologEpilogVideo::CPrologEpilogVideo(CampaignScenarioPrologEpilog _spe, std::function<void()> callback)
: CWindowObject(BORDERED), spe(_spe), positionCounter(0), voiceSoundHandle(-1), exitCb(callback) : CWindowObject(BORDERED), spe(_spe), positionCounter(0), voiceSoundHandle(-1), videoSoundHandle(-1), exitCb(callback)
{ {
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE; OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
addUsedEvents(LCLICK); addUsedEvents(LCLICK);
@@ -28,7 +28,7 @@ CPrologEpilogVideo::CPrologEpilogVideo(CampaignScenarioPrologEpilog _spe, std::f
updateShadow(); updateShadow();
auto audioData = CCS->videoh->getAudio(spe.prologVideo); auto audioData = CCS->videoh->getAudio(spe.prologVideo);
sound = CCS->soundh->playSound(audioData); videoSoundHandle = CCS->soundh->playSound(audioData);
CCS->videoh->open(spe.prologVideo); CCS->videoh->open(spe.prologVideo);
CCS->musich->playMusic(spe.prologMusic, true, true); CCS->musich->playMusic(spe.prologMusic, true, true);
voiceSoundHandle = CCS->soundh->playSound(spe.prologVoice); voiceSoundHandle = CCS->soundh->playSound(spe.prologVoice);
@@ -64,7 +64,7 @@ void CPrologEpilogVideo::show(Canvas & to)
void CPrologEpilogVideo::clickPressed(const Point & cursorPosition) void CPrologEpilogVideo::clickPressed(const Point & cursorPosition)
{ {
close(); close();
CCS->soundh->stopSound(sound);
CCS->soundh->stopSound(voiceSoundHandle); CCS->soundh->stopSound(voiceSoundHandle);
CCS->soundh->stopSound(videoSoundHandle);
exitCb(); exitCb();
} }

View File

@@ -19,11 +19,11 @@ class CPrologEpilogVideo : public CWindowObject
CampaignScenarioPrologEpilog spe; CampaignScenarioPrologEpilog spe;
int positionCounter; int positionCounter;
int voiceSoundHandle; int voiceSoundHandle;
int videoSoundHandle;
std::function<void()> exitCb; std::function<void()> exitCb;
std::shared_ptr<CMultiLineLabel> text; std::shared_ptr<CMultiLineLabel> text;
int sound = 0;
bool voiceStopped = false; bool voiceStopped = false;
public: public: