1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00

code review + pause handling

This commit is contained in:
Laserlicht 2024-10-30 00:35:50 +01:00
parent a68522b370
commit bb73a35412
7 changed files with 54 additions and 7 deletions

View File

@ -240,6 +240,18 @@ void CSoundHandler::stopSound(int handler)
Mix_HaltChannel(handler);
}
void CSoundHandler::pauseSound(int handler)
{
if(isInitialized() && handler != -1)
Mix_Pause(handler);
}
void CSoundHandler::resumeSound(int handler)
{
if(isInitialized() && handler != -1)
Mix_Resume(handler);
}
ui32 CSoundHandler::getVolume() const
{
return volume;

View File

@ -67,6 +67,8 @@ public:
int playSound(std::pair<std::unique_ptr<ui8[]>, si64> & data, int repeats = 0, bool cache = false) final;
int playSoundFromSet(std::vector<soundBase::soundID> & sound_vec) final;
void stopSound(int handler) final;
void pauseSound(int handler) final;
void resumeSound(int handler) final;
void setCallback(int channel, std::function<void()> function) final;
void resetCallback(int channel) final;

View File

@ -391,10 +391,10 @@ void CVideoInstance::tick(uint32_t msPassed)
if(videoEnded())
throw std::runtime_error("Video already ended!");
if(startTime == std::chrono::high_resolution_clock::time_point())
startTime = std::chrono::high_resolution_clock::now();
if(startTime == std::chrono::steady_clock::time_point())
startTime = std::chrono::steady_clock::now();
auto nowTime = std::chrono::high_resolution_clock::now();
auto nowTime = std::chrono::steady_clock::now();
double difference = std::chrono::duration_cast<std::chrono::milliseconds>(nowTime - startTime).count() / 1000.0;
int frameskipCounter = 0;
@ -407,6 +407,22 @@ void CVideoInstance::tick(uint32_t msPassed)
loadNextFrame();
}
void CVideoInstance::activate()
{
if(deactivationStartTime != std::chrono::steady_clock::time_point())
{
auto pauseDuration = std::chrono::steady_clock::now() - deactivationStartTime;
startTime += pauseDuration;
deactivationStartTime = std::chrono::steady_clock::time_point();
}
}
void CVideoInstance::deactivate()
{
deactivationStartTime = std::chrono::steady_clock::now();
}
struct FFMpegFormatDescription
{
uint8_t sampleSizeBytes;

View File

@ -78,7 +78,8 @@ class CVideoInstance final : public IVideoInstance, public FFMpegStream
Point dimensions;
/// video playback start time point
std::chrono::high_resolution_clock::time_point startTime;
std::chrono::steady_clock::time_point startTime;
std::chrono::steady_clock::time_point deactivationStartTime;
void prepareOutput(float scaleFactor, bool useTextureOutput);
@ -96,6 +97,8 @@ public:
void show(const Point & position, Canvas & canvas) final;
void tick(uint32_t msPassed) final;
void activate() final;
void deactivate() final;
};
class CVideoPlayer final : public IVideoPlayer

View File

@ -22,6 +22,8 @@ public:
virtual int playSound(std::pair<std::unique_ptr<ui8[]>, si64> & data, int repeats = 0, bool cache = false) = 0;
virtual int playSoundFromSet(std::vector<soundBase::soundID> & sound_vec) = 0;
virtual void stopSound(int handler) = 0;
virtual void pauseSound(int handler) = 0;
virtual void resumeSound(int handler) = 0;
virtual ui32 getVolume() const = 0;
virtual void setVolume(ui32 percent) = 0;

View File

@ -35,6 +35,10 @@ public:
/// Advances video playback by specified duration
virtual void tick(uint32_t msPassed) = 0;
/// activate or deactivate video
virtual void activate() = 0;
virtual void deactivate() = 0;
virtual ~IVideoInstance() = default;
};

View File

@ -50,7 +50,8 @@ void VideoWidgetBase::playVideo(const VideoPath & fileToPlay)
{
pos.w = videoInstance->size().x;
pos.h = videoInstance->size().y;
subTitle = std::make_unique<CMultiLineLabel>(Rect(0, (pos.h / 5) * 4, pos.w, pos.h / 5), EFonts::FONT_HIGH_SCORE, ETextAlignment::CENTER, Colors::WHITE);
if(!subTitleData.isNull())
subTitle = std::make_unique<CMultiLineLabel>(Rect(0, (pos.h / 5) * 4, pos.w, pos.h / 5), EFonts::FONT_HIGH_SCORE, ETextAlignment::CENTER, Colors::WHITE);
}
if (playAudio)
@ -121,13 +122,20 @@ std::string VideoWidgetBase::getSubTitleLine(double timestamp)
void VideoWidgetBase::activate()
{
CIntObject::activate();
startAudio();
if(audioHandle != -1)
CCS->soundh->resumeSound(audioHandle);
else
startAudio();
if(videoInstance)
videoInstance->activate();
}
void VideoWidgetBase::deactivate()
{
CIntObject::deactivate();
stopAudio();
CCS->soundh->pauseSound(audioHandle);
if(videoInstance)
videoInstance->deactivate();
}
void VideoWidgetBase::showAll(Canvas & to)