CPrologEpilogVideo.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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), elapsedTimeMilliseconds(0)
  22. {
  23. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  24. addUsedEvents(LCLICK | TIME);
  25. pos = center(Rect(0, 0, 800, 600));
  26. updateShadow();
  27. auto audioData = CCS->videoh->getAudio(spe.prologVideo);
  28. videoSoundHandle = CCS->soundh->playSound(audioData, -1);
  29. CCS->videoh->open(spe.prologVideo);
  30. CCS->musich->playMusic(spe.prologMusic, true, true);
  31. voiceDurationMilliseconds = CCS->soundh->getSoundDurationMilliseconds(spe.prologVoice);
  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. const uint32_t speed = (voiceDurationMilliseconds == 0) ? 150 : (voiceDurationMilliseconds / (text->textSize.y));
  46. if(elapsedTimeMilliseconds > speed && text->textSize.y - 50 > positionCounter)
  47. {
  48. text->scrollTextBy(1);
  49. elapsedTimeMilliseconds -= speed;
  50. ++positionCounter;
  51. }
  52. else if(elapsedTimeMilliseconds > (voiceDurationMilliseconds == 0 ? 8000 : 3000) && voiceStopped) // pause after completed scrolling (longer for intros missing voice)
  53. clickPressed(GH.getCursorPosition());
  54. }
  55. void CPrologEpilogVideo::show(Canvas & to)
  56. {
  57. to.drawColor(pos, Colors::BLACK);
  58. //some videos are 800x600 in size while some are 800x400
  59. CCS->videoh->update(pos.x, pos.y + (CCS->videoh->size().y == 400 ? 100 : 0), to.getInternalSurface(), true, false);
  60. text->showAll(to); // blit text over video, if needed
  61. }
  62. void CPrologEpilogVideo::clickPressed(const Point & cursorPosition)
  63. {
  64. close();
  65. CCS->soundh->resetCallback(voiceSoundHandle); // reset callback to avoid memory corruption since 'this' will be destroyed
  66. CCS->soundh->stopSound(voiceSoundHandle);
  67. CCS->soundh->stopSound(videoSoundHandle);
  68. if(exitCb)
  69. exitCb();
  70. }