CPrologEpilogVideo.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "../media/IMusicPlayer.h"
  13. #include "../media/ISoundPlayer.h"
  14. #include "../GameEngine.h"
  15. #include "../widgets/TextControls.h"
  16. #include "../widgets/VideoWidget.h"
  17. #include "../widgets/Images.h"
  18. #include "../render/Canvas.h"
  19. CPrologEpilogVideo::CPrologEpilogVideo(CampaignScenarioPrologEpilog _spe, std::function<void()> callback)
  20. : CWindowObject(BORDERED), spe(_spe), positionCounter(0), voiceSoundHandle(-1), videoSoundHandle(-1), exitCb(callback), elapsedTimeMilliseconds(0)
  21. {
  22. OBJECT_CONSTRUCTION;
  23. addUsedEvents(LCLICK | TIME);
  24. pos = center(Rect(0, 0, 800, 600));
  25. backgroundAroundMenu = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(-pos.x, -pos.y, ENGINE->screenDimensions().x, ENGINE->screenDimensions().y));
  26. //TODO: remove hardcoded paths. Some of campaigns video actually consist from 2 parts
  27. // however, currently our campaigns format expects only a single video file
  28. static const std::map<VideoPath, VideoPath> pairedVideoFiles = {
  29. { VideoPath::builtin("EVIL2AP1"), VideoPath::builtin("EVIL2AP2") },
  30. { VideoPath::builtin("H3ABdb4"), VideoPath::builtin("H3ABdb4b") },
  31. { VideoPath::builtin("H3x2_RNe1"), VideoPath::builtin("H3x2_RNe2") },
  32. };
  33. if (pairedVideoFiles.count(spe.prologVideo))
  34. videoPlayer = std::make_shared<VideoWidget>(Point(0, 0), spe.prologVideo, pairedVideoFiles.at(spe.prologVideo), true);
  35. else
  36. videoPlayer = std::make_shared<VideoWidget>(Point(0, 0), spe.prologVideo, true);
  37. //some videos are 800x600 in size while some are 800x400
  38. if (videoPlayer->pos.h == 400)
  39. videoPlayer->moveBy(Point(0, 100));
  40. ENGINE->music().setVolume(ENGINE->music().getVolume() / 2); // background volume is too loud by default
  41. ENGINE->music().playMusic(spe.prologMusic, true, true);
  42. voiceDurationMilliseconds = ENGINE->sound().getSoundDurationMilliseconds(spe.prologVoice);
  43. voiceSoundHandle = ENGINE->sound().playSound(spe.prologVoice);
  44. auto onVoiceStop = [this]()
  45. {
  46. voiceStopped = true;
  47. elapsedTimeMilliseconds = 0;
  48. };
  49. ENGINE->sound().setCallback(voiceSoundHandle, onVoiceStop);
  50. text = std::make_shared<CMultiLineLabel>(Rect(100, 500, 600, 100), EFonts::FONT_BIG, ETextAlignment::CENTER, Colors::METALLIC_GOLD, spe.prologText.toString());
  51. if(text->getLines().size() == 3)
  52. text->scrollTextTo(-25); // beginning of text in the vertical middle of black area
  53. else if(text->getLines().size() > 3)
  54. text->scrollTextTo(-50); // beginning of text in the vertical middle of black area
  55. }
  56. void CPrologEpilogVideo::tick(uint32_t msPassed)
  57. {
  58. elapsedTimeMilliseconds += msPassed;
  59. const uint32_t speed = (voiceDurationMilliseconds == 0) ? 150 : (voiceDurationMilliseconds / (text->textSize.y));
  60. if(elapsedTimeMilliseconds > speed && text->textSize.y - 50 > positionCounter)
  61. {
  62. text->scrollTextBy(1);
  63. elapsedTimeMilliseconds -= speed;
  64. ++positionCounter;
  65. }
  66. else if(elapsedTimeMilliseconds > (voiceDurationMilliseconds == 0 ? 8000 : 3000) && voiceStopped) // pause after completed scrolling (longer for intros missing voice)
  67. clickPressed(ENGINE->getCursorPosition());
  68. }
  69. void CPrologEpilogVideo::show(Canvas & to)
  70. {
  71. to.drawColor(pos, Colors::BLACK);
  72. videoPlayer->show(to);
  73. text->showAll(to); // blit text over video, if needed
  74. }
  75. void CPrologEpilogVideo::clickPressed(const Point & cursorPosition)
  76. {
  77. ENGINE->music().setVolume(ENGINE->music().getVolume() * 2); // restore background volume
  78. close();
  79. ENGINE->sound().resetCallback(voiceSoundHandle); // reset callback to avoid memory corruption since 'this' will be destroyed
  80. ENGINE->sound().stopSound(voiceSoundHandle);
  81. ENGINE->sound().stopSound(videoSoundHandle);
  82. if(exitCb)
  83. exitCb();
  84. }