CPrologEpilogVideo.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * CPrologEpilogVideo.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CPrologEpilogVideo.h"
  12. #include "../CGameInfo.h"
  13. #include "../media/IMusicPlayer.h"
  14. #include "../media/ISoundPlayer.h"
  15. #include "../media/IVideoPlayer.h"
  16. #include "../gui/WindowHandler.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../gui/FramerateManager.h"
  19. #include "../widgets/TextControls.h"
  20. #include "../render/Canvas.h"
  21. CPrologEpilogVideo::CPrologEpilogVideo(CampaignScenarioPrologEpilog _spe, std::function<void()> callback)
  22. : CWindowObject(BORDERED), spe(_spe), positionCounter(0), voiceSoundHandle(-1), videoSoundHandle(-1), exitCb(callback), elapsedTimeMilliseconds(0)
  23. {
  24. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  25. addUsedEvents(LCLICK | TIME);
  26. pos = center(Rect(0, 0, 800, 600));
  27. updateShadow();
  28. auto audioData = CCS->videoh->getAudio(spe.prologVideo);
  29. videoSoundHandle = CCS->soundh->playSound(audioData, -1);
  30. CCS->videoh->open(spe.prologVideo);
  31. CCS->musich->playMusic(spe.prologMusic, true, true);
  32. voiceDurationMilliseconds = CCS->soundh->getSoundDurationMilliseconds(spe.prologVoice);
  33. voiceSoundHandle = CCS->soundh->playSound(spe.prologVoice);
  34. auto onVoiceStop = [this]()
  35. {
  36. voiceStopped = true;
  37. elapsedTimeMilliseconds = 0;
  38. };
  39. CCS->soundh->setCallback(voiceSoundHandle, onVoiceStop);
  40. text = std::make_shared<CMultiLineLabel>(Rect(100, 500, 600, 100), EFonts::FONT_BIG, ETextAlignment::CENTER, Colors::METALLIC_GOLD, spe.prologText.toString());
  41. text->scrollTextTo(-50); // beginning of text in the vertical middle of black area
  42. }
  43. void CPrologEpilogVideo::tick(uint32_t msPassed)
  44. {
  45. elapsedTimeMilliseconds += msPassed;
  46. const uint32_t speed = (voiceDurationMilliseconds == 0) ? 150 : (voiceDurationMilliseconds / (text->textSize.y));
  47. if(elapsedTimeMilliseconds > speed && text->textSize.y - 50 > positionCounter)
  48. {
  49. text->scrollTextBy(1);
  50. elapsedTimeMilliseconds -= speed;
  51. ++positionCounter;
  52. }
  53. else if(elapsedTimeMilliseconds > (voiceDurationMilliseconds == 0 ? 8000 : 3000) && voiceStopped) // pause after completed scrolling (longer for intros missing voice)
  54. clickPressed(GH.getCursorPosition());
  55. }
  56. void CPrologEpilogVideo::show(Canvas & to)
  57. {
  58. to.drawColor(pos, Colors::BLACK);
  59. //some videos are 800x600 in size while some are 800x400
  60. CCS->videoh->update(pos.x, pos.y + (CCS->videoh->size().y == 400 ? 100 : 0), to.getInternalSurface(), true, false);
  61. text->showAll(to); // blit text over video, if needed
  62. }
  63. void CPrologEpilogVideo::clickPressed(const Point & cursorPosition)
  64. {
  65. close();
  66. CCS->soundh->resetCallback(voiceSoundHandle); // reset callback to avoid memory corruption since 'this' will be destroyed
  67. CCS->soundh->stopSound(voiceSoundHandle);
  68. CCS->soundh->stopSound(videoSoundHandle);
  69. if(exitCb)
  70. exitCb();
  71. }