CVideoHandler.h 3.6 KB

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