CPrologEpilogVideo.cpp 3.6 KB

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