CPrologEpilogVideo.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "../gui/WindowHandler.h"
  16. #include "../gui/CGuiHandler.h"
  17. //#include "../gui/FramerateManager.h"
  18. #include "../widgets/TextControls.h"
  19. #include "../widgets/VideoWidget.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. //TODO: remove hardcoded paths. Some of campaigns video actually consist from 2 parts
  29. // however, currently our campaigns format expects only a single video file
  30. static const std::map<VideoPath, VideoPath> pairedVideoFiles = {
  31. { VideoPath::builtin("EVIL2AP1"), VideoPath::builtin("EVIL2AP2") },
  32. { VideoPath::builtin("H3ABdb4"), VideoPath::builtin("H3ABdb4b") },
  33. { VideoPath::builtin("H3x2_RNe1"), VideoPath::builtin("H3x2_RNe2") },
  34. };
  35. if (pairedVideoFiles.count(spe.prologVideo))
  36. videoPlayer = std::make_shared<VideoWidget>(Point(0, 0), spe.prologVideo, pairedVideoFiles.at(spe.prologVideo), true);
  37. else
  38. videoPlayer = std::make_shared<VideoWidget>(Point(0, 0), spe.prologVideo, true);
  39. //some videos are 800x600 in size while some are 800x400
  40. if (videoPlayer->pos.h == 400)
  41. videoPlayer->moveBy(Point(0, 100));
  42. CCS->musich->playMusic(spe.prologMusic, true, true);
  43. voiceDurationMilliseconds = CCS->soundh->getSoundDurationMilliseconds(spe.prologVoice);
  44. voiceSoundHandle = CCS->soundh->playSound(spe.prologVoice);
  45. auto onVoiceStop = [this]()
  46. {
  47. voiceStopped = true;
  48. elapsedTimeMilliseconds = 0;
  49. };
  50. CCS->soundh->setCallback(voiceSoundHandle, onVoiceStop);
  51. text = std::make_shared<CMultiLineLabel>(Rect(100, 500, 600, 100), EFonts::FONT_BIG, ETextAlignment::CENTER, Colors::METALLIC_GOLD, spe.prologText.toString());
  52. text->scrollTextTo(-50); // beginning of text in the vertical middle of black area
  53. }
  54. void CPrologEpilogVideo::tick(uint32_t msPassed)
  55. {
  56. elapsedTimeMilliseconds += msPassed;
  57. const uint32_t speed = (voiceDurationMilliseconds == 0) ? 150 : (voiceDurationMilliseconds / (text->textSize.y));
  58. if(elapsedTimeMilliseconds > speed && text->textSize.y - 50 > positionCounter)
  59. {
  60. text->scrollTextBy(1);
  61. elapsedTimeMilliseconds -= speed;
  62. ++positionCounter;
  63. }
  64. else if(elapsedTimeMilliseconds > (voiceDurationMilliseconds == 0 ? 8000 : 3000) && voiceStopped) // pause after completed scrolling (longer for intros missing voice)
  65. clickPressed(GH.getCursorPosition());
  66. }
  67. void CPrologEpilogVideo::show(Canvas & to)
  68. {
  69. to.drawColor(pos, Colors::BLACK);
  70. videoPlayer->show(to);
  71. text->showAll(to); // blit text over video, if needed
  72. }
  73. void CPrologEpilogVideo::clickPressed(const Point & cursorPosition)
  74. {
  75. close();
  76. CCS->soundh->resetCallback(voiceSoundHandle); // reset callback to avoid memory corruption since 'this' will be destroyed
  77. CCS->soundh->stopSound(voiceSoundHandle);
  78. CCS->soundh->stopSound(videoSoundHandle);
  79. if(exitCb)
  80. exitCb();
  81. }