2
0

CVideoHandler.h 3.5 KB

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