CPrologEpilogVideo.cpp 2.7 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 "../CMusicHandler.h"
  14. #include "../CVideoHandler.h"
  15. #include "../gui/WindowHandler.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/FramerateManager.h"
  18. #include "../widgets/TextControls.h"
  19. #include "../render/Canvas.h"
  20. CPrologEpilogVideo::CPrologEpilogVideo(CampaignScenarioPrologEpilog _spe, std::function<void()> callback)
  21. : CWindowObject(BORDERED), spe(_spe), positionCounter(0), voiceSoundHandle(-1), videoSoundHandle(-1), exitCb(callback), elapsedTime(0)
  22. {
  23. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  24. addUsedEvents(LCLICK);
  25. pos = center(Rect(0, 0, 800, 600));
  26. updateShadow();
  27. auto audioData = CCS->videoh->getAudio(spe.prologVideo);
  28. videoSoundHandle = CCS->soundh->playSound(audioData);
  29. CCS->videoh->open(spe.prologVideo);
  30. CCS->musich->playMusic(spe.prologMusic, true, true);
  31. voiceDurationMilliseconds = CCS->soundh->getSoundDuration(spe.prologVoice) * 1000.0;
  32. voiceSoundHandle = CCS->soundh->playSound(spe.prologVoice);
  33. auto onVoiceStop = [this]()
  34. {
  35. voiceStopped = true;
  36. elapsedTimeMilliseconds = 0;
  37. };
  38. CCS->soundh->setCallback(voiceSoundHandle, onVoiceStop);
  39. text = std::make_shared<CMultiLineLabel>(Rect(100, 500, 600, 100), EFonts::FONT_BIG, ETextAlignment::CENTER, Colors::METALLIC_GOLD, spe.prologText.toString());
  40. text->scrollTextTo(-50); // beginning of text in the vertical middle of black area
  41. }
  42. void CPrologEpilogVideo::tick(uint32_t msPassed)
  43. {
  44. elapsedTimeMilliseconds += msPassed;
  45. }
  46. void CPrologEpilogVideo::show(Canvas & to)
  47. {
  48. to.drawColor(pos, Colors::BLACK);
  49. //some videos are 800x600 in size while some are 800x400
  50. CCS->videoh->update(pos.x, pos.y + (CCS->videoh->size().y == 400 ? 100 : 0), to.getInternalSurface(), true, false);
  51. const double speed = (voiceDurationMilliseconds == 0) ? 100 : (voiceDurationMilliseconds / (text->textSize.y));
  52. if(elapsedTimeMilliseconds > speed && text->textSize.y - 50 > positionCounter)
  53. {
  54. text->scrollTextBy(1);
  55. elapsedTimeMilliseconds -= speed;
  56. ++positionCounter;
  57. }
  58. else
  59. {
  60. text->showAll(to); // blit text over video, if needed
  61. if(elapsedTime > (voiceDuration == 0.0 ? 6000 : 3000) && voiceStopped) // pause after completed scrolling (longer for intros missing voice)
  62. clickPressed(GH.getCursorPosition());
  63. }
  64. }
  65. void CPrologEpilogVideo::clickPressed(const Point & cursorPosition)
  66. {
  67. close();
  68. CCS->soundh->stopSound(voiceSoundHandle);
  69. CCS->soundh->stopSound(videoSoundHandle);
  70. exitCb();
  71. }