CPrologEpilogVideo.cpp 4.0 KB

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