CVideoHandler.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * CVideoHandler.h, 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. #pragma once
  11. #include "../lib/Rect.h"
  12. #include "../lib/filesystem/ResourcePath.h"
  13. struct SDL_Surface;
  14. struct SDL_Texture;
  15. class IVideoPlayer : boost::noncopyable
  16. {
  17. public:
  18. virtual bool open(const VideoPath & name, bool scale = false)=0; //true - succes
  19. virtual void close()=0;
  20. virtual bool nextFrame()=0;
  21. virtual void show(int x, int y, SDL_Surface *dst, bool update = true)=0;
  22. virtual void redraw(int x, int y, SDL_Surface *dst, bool update = true)=0; //reblits buffer
  23. virtual bool wait()=0;
  24. virtual int curFrame() const =0;
  25. virtual int frameCount() const =0;
  26. };
  27. class IMainVideoPlayer : public IVideoPlayer
  28. {
  29. public:
  30. virtual void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true, std::function<void()> restart = 0){}
  31. virtual bool openAndPlayVideo(const VideoPath & name, int x, int y, bool stopOnKey = false, bool scale = false)
  32. {
  33. return false;
  34. }
  35. };
  36. class CEmptyVideoPlayer : public IMainVideoPlayer
  37. {
  38. public:
  39. int curFrame() const override {return -1;};
  40. int frameCount() const override {return -1;};
  41. void redraw( int x, int y, SDL_Surface *dst, bool update = true ) override {};
  42. void show( int x, int y, SDL_Surface *dst, bool update = true ) override {};
  43. bool nextFrame() override {return false;};
  44. void close() override {};
  45. bool wait() override {return false;};
  46. bool open(const VideoPath & name, bool scale = false) override {return false;};
  47. };
  48. #ifndef DISABLE_VIDEO
  49. struct AVFormatContext;
  50. struct AVCodecContext;
  51. struct AVCodec;
  52. struct AVFrame;
  53. struct AVIOContext;
  54. VCMI_LIB_NAMESPACE_BEGIN
  55. class CInputStream;
  56. VCMI_LIB_NAMESPACE_END
  57. class CVideoPlayer : public IMainVideoPlayer
  58. {
  59. int stream; // stream index in video
  60. AVFormatContext *format;
  61. AVCodecContext *codecContext; // codec context for stream
  62. const AVCodec *codec;
  63. AVFrame *frame;
  64. struct SwsContext *sws;
  65. AVIOContext * context;
  66. VideoPath fname; //name of current video file (empty if idle)
  67. // Destination. Either overlay or dest.
  68. SDL_Texture *texture;
  69. SDL_Surface *dest;
  70. Rect destRect; // valid when dest is used
  71. Rect pos; // destination on screen
  72. /// video playback currnet progress, in seconds
  73. double frameTime;
  74. bool doLoop; // loop through video
  75. bool playVideo(int x, int y, bool stopOnKey);
  76. bool open(const VideoPath & fname, bool loop, bool useOverlay = false, bool scale = false);
  77. public:
  78. CVideoPlayer();
  79. ~CVideoPlayer();
  80. bool init();
  81. bool open(const VideoPath & fname, bool scale = false) override;
  82. void close() override;
  83. bool nextFrame() override; // display next frame
  84. void show(int x, int y, SDL_Surface *dst, bool update = true) override; //blit current frame
  85. void redraw(int x, int y, SDL_Surface *dst, bool update = true) override; //reblits buffer
  86. void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true, std::function<void()> onVideoRestart = nullptr) override; //moves to next frame if appropriate, and blits it or blits only if redraw parameter is set true
  87. // Opens video, calls playVideo, closes video; returns playVideo result (if whole video has been played)
  88. bool openAndPlayVideo(const VideoPath & name, int x, int y, bool stopOnKey = false, bool scale = false) override;
  89. //TODO:
  90. bool wait() override {return false;};
  91. int curFrame() const override {return -1;};
  92. int frameCount() const override {return -1;};
  93. // public to allow access from ffmpeg IO functions
  94. std::unique_ptr<CInputStream> data;
  95. };
  96. #endif