Browse Source

code review

Laserlicht 2 years ago
parent
commit
89a39fcfc0

+ 3 - 8
client/CMusicHandler.cpp

@@ -146,7 +146,7 @@ Mix_Chunk *CSoundHandler::GetSoundChunk(std::pair<std::unique_ptr<ui8 []>, si64>
 {
 	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())
 			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 channel;
-	Mix_Chunk *chunk = GetSoundChunk(data, cache);
-
-	if (chunk)
+	int channel = -1;
+	if (Mix_Chunk *chunk = GetSoundChunk(data, cache))
 	{
 		channel = Mix_PlayChannel(-1, chunk, repeats);
 		if (channel == -1)
@@ -240,9 +238,6 @@ int CSoundHandler::playSound(std::pair<std::unique_ptr<ui8 []>, si64> & data, in
 		else
 			initCallback(channel, [chunk](){ Mix_FreeChunk(chunk);});
 	}
-	else
-		channel = -1;
-
 	return channel;
 }
 

+ 4 - 5
client/CVideoHandler.cpp

@@ -492,7 +492,7 @@ std::pair<std::unique_ptr<ui8 []>, si64> CVideoPlayer::getAudio(const VideoPath
 
 	// Find the first audio stream
 	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)
 		{
@@ -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
 	if (streamAudio > -1)
 	{
-		int ret = 0;
-		ret = avcodec_parameters_to_context(codecContextAudio, formatAudio->streams[streamAudio]->codecpar);
+		int ret = avcodec_parameters_to_context(codecContextAudio, formatAudio->streams[streamAudio]->codecpar);
 		if (ret < 0)
 		{
 			//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);
 			int bytesToRead = (frameAudio->nb_samples * 2 * (formatAudio->streams[streamAudio]->codecpar->bits_per_coded_sample / 8));
 			if (rc >= 0)
-				for (int s = 0; s < bytesToRead; s+=sizeof(ui8))
+				for (int s = 0; s < bytesToRead; s += sizeof(ui8))
 				{
 					ui8 value;
 					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;
 	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(samples.begin(), samples.end(), dat.first.get() + sizeof(wav_hdr));
 

+ 1 - 1
client/CVideoHandler.h

@@ -36,7 +36,7 @@ public:
 	{
 		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

+ 4 - 4
client/mainmenu/CHighScoreScreen.cpp

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

+ 1 - 1
client/mainmenu/CHighScoreScreen.h

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

+ 3 - 3
client/mainmenu/CPrologEpilogVideo.cpp

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

+ 1 - 1
client/mainmenu/CPrologEpilogVideo.h

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