1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

use video player for spellbook animation

This commit is contained in:
Laserlicht 2024-11-29 21:58:20 +01:00
parent 7fdddee503
commit 8808c9538c
6 changed files with 16 additions and 73 deletions

View File

@ -14,10 +14,6 @@
class CEmptyVideoPlayer final : public IVideoPlayer
{
public:
void playSpellbookAnimation(const VideoPath & name, const Point & position) override
{
}
/// Load video from specified path
std::unique_ptr<IVideoInstance> open(const VideoPath & name, float scaleFactor) override
{

View File

@ -637,68 +637,6 @@ std::pair<std::unique_ptr<ui8 []>, si64> CAudioInstance::extractAudio(const Vide
return dat;
}
bool CVideoPlayer::openAndPlayVideoImpl(const VideoPath & name, const Point & position, bool useOverlay, bool stopOnKey)
{
CVideoInstance instance;
auto extractedAudio = getAudio(name);
int audioHandle = CCS->soundh->playSound(extractedAudio);
if (!instance.openInput(name))
return true;
instance.openVideo();
instance.prepareOutput(1, true);
auto lastTimePoint = boost::chrono::steady_clock::now();
while(instance.loadNextFrame())
{
if(stopOnKey)
{
GH.input().fetchEvents();
if(GH.input().ignoreEventsUntilInput())
{
CCS->soundh->stopSound(audioHandle);
return false;
}
}
SDL_Rect rect;
rect.x = position.x;
rect.y = position.y;
rect.w = instance.dimensions.x;
rect.h = instance.dimensions.y;
SDL_RenderFillRect(mainRenderer, &rect);
if(instance.textureYUV)
SDL_RenderCopy(mainRenderer, instance.textureYUV, nullptr, &rect);
else
SDL_RenderCopy(mainRenderer, instance.textureRGB, nullptr, &rect);
SDL_RenderPresent(mainRenderer);
// Framerate delay
double targetFrameTimeSeconds = instance.getCurrentFrameDuration();
auto targetFrameTime = boost::chrono::milliseconds(static_cast<int>(1000 * targetFrameTimeSeconds));
auto timePointAfterPresent = boost::chrono::steady_clock::now();
auto timeSpentBusy = boost::chrono::duration_cast<boost::chrono::milliseconds>(timePointAfterPresent - lastTimePoint);
if(targetFrameTime > timeSpentBusy)
boost::this_thread::sleep_for(targetFrameTime - timeSpentBusy);
lastTimePoint = boost::chrono::steady_clock::now();
}
return true;
}
void CVideoPlayer::playSpellbookAnimation(const VideoPath & name, const Point & position)
{
openAndPlayVideoImpl(name, position * GH.screenHandler().getScalingFactor(), false, false);
}
std::unique_ptr<IVideoInstance> CVideoPlayer::open(const VideoPath & name, float scaleFactor)
{
auto result = std::make_unique<CVideoInstance>();

View File

@ -103,11 +103,9 @@ public:
class CVideoPlayer final : public IVideoPlayer
{
bool openAndPlayVideoImpl(const VideoPath & name, const Point & position, bool useOverlay, bool stopOnKey);
void openVideoFile(CVideoInstance & state, const VideoPath & fname);
public:
void playSpellbookAnimation(const VideoPath & name, const Point & position) final;
std::unique_ptr<IVideoInstance> open(const VideoPath & name, float scaleFactor) final;
std::pair<std::unique_ptr<ui8[]>, si64> getAudio(const VideoPath & videoToOpen) final;
};

View File

@ -45,9 +45,6 @@ public:
class IVideoPlayer : boost::noncopyable
{
public:
/// Plays video on top of the screen, returns only after playback is over
virtual void playSpellbookAnimation(const VideoPath & name, const Point & position) = 0;
/// Load video from specified path. Returns nullptr on failure
virtual std::unique_ptr<IVideoInstance> open(const VideoPath & name, float scaleFactor) = 0;

View File

@ -30,6 +30,7 @@
#include "../widgets/CTextInput.h"
#include "../widgets/TextControls.h"
#include "../widgets/Buttons.h"
#include "../widgets/VideoWidget.h"
#include "../adventureMap/AdventureMapInterface.h"
#include "../render/AssetGenerator.h"
@ -395,6 +396,8 @@ void CSpellWindow::fRcornerb()
void CSpellWindow::show(Canvas & to)
{
if(video)
video->show(to);
statusBar->show(to);
}
@ -493,14 +496,22 @@ void CSpellWindow::setCurrentPage(int value)
void CSpellWindow::turnPageLeft()
{
OBJECT_CONSTRUCTION;
if(settings["video"]["spellbookAnimation"].Bool() && !isBigSpellbook)
CCS->videoh->playSpellbookAnimation(VideoPath::builtin("PGTRNLFT.SMK"), pos.topLeft() + Point(13, 14));
video = std::make_shared<VideoWidgetOnce>(Point(13, 14), VideoPath::builtin("PGTRNLFT.SMK"), false, [this](){
video.reset();
redraw();
});
}
void CSpellWindow::turnPageRight()
{
OBJECT_CONSTRUCTION;
if(settings["video"]["spellbookAnimation"].Bool() && !isBigSpellbook)
CCS->videoh->playSpellbookAnimation(VideoPath::builtin("PGTRNRGH.SMK"), pos.topLeft() + Point(13, 14));
video = std::make_shared<VideoWidgetOnce>(Point(13, 14), VideoPath::builtin("PGTRNRGH.SMK"), false, [this](){
video.reset();
redraw();
});
}
void CSpellWindow::keyPressed(EShortcut key)

View File

@ -28,6 +28,7 @@ class CSpellWindow;
class CTextInput;
class TransparentFilledRectangle;
class CToggleButton;
class VideoWidgetOnce;
/// The spell window
class CSpellWindow : public CWindowObject
@ -86,6 +87,8 @@ class CSpellWindow : public CWindowObject
std::shared_ptr<CToggleButton> showAllSpells;
std::shared_ptr<CLabel> showAllSpellsDescription;
std::shared_ptr<VideoWidgetOnce> video;
bool isBigSpellbook;
int spellsPerPage;
int offL;