CVideoHandler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. struct SDL_Surface;
  3. class IVideoPlayer
  4. {
  5. public:
  6. virtual bool open(std::string name)=0; //true - succes
  7. virtual void close()=0;
  8. virtual bool nextFrame()=0;
  9. virtual void show(int x, int y, SDL_Surface *dst, bool update = true)=0;
  10. virtual void redraw(int x, int y, SDL_Surface *dst, bool update = true)=0; //reblits buffer
  11. virtual bool wait()=0;
  12. virtual int curFrame() const =0;
  13. virtual int frameCount() const =0;
  14. };
  15. class IMainVideoPlayer : public IVideoPlayer
  16. {
  17. public:
  18. std::string fname; //name of current video file (empty if idle)
  19. virtual void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true){}
  20. virtual bool openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey = false)
  21. {
  22. return false;
  23. }
  24. };
  25. class CEmptyVideoPlayer : public IMainVideoPlayer
  26. {
  27. public:
  28. virtual int curFrame() const {return -1;};
  29. virtual int frameCount() const {return -1;};
  30. virtual void redraw( int x, int y, SDL_Surface *dst, bool update = true ) {};
  31. virtual void show( int x, int y, SDL_Surface *dst, bool update = true ) {};
  32. virtual bool nextFrame() {return false;};
  33. virtual void close() {};
  34. virtual bool wait() {return false;};
  35. virtual bool open( std::string name ) {return false;};
  36. };
  37. #ifndef DISABLE_VIDEO
  38. #include "../lib/filesystem/CInputStream.h"
  39. #include <SDL.h>
  40. #include <SDL_video.h>
  41. #if SDL_VERSION_ATLEAST(1,3,0) && !SDL_VERSION_ATLEAST(2,0,0)
  42. #include <SDL_compat.h>
  43. #endif
  44. extern "C" {
  45. #include <libavformat/avformat.h>
  46. #include <libswscale/swscale.h>
  47. }
  48. class CVideoPlayer : public IMainVideoPlayer
  49. {
  50. int stream; // stream index in video
  51. AVFormatContext *format;
  52. AVCodecContext *codecContext; // codec context for stream
  53. AVCodec *codec;
  54. AVFrame *frame;
  55. struct SwsContext *sws;
  56. AVIOContext * context;
  57. // Destination. Either overlay or dest.
  58. #ifdef VCMI_SDL1
  59. SDL_Overlay * overlay;
  60. #else
  61. SDL_Texture *texture;
  62. #endif
  63. SDL_Surface *dest;
  64. SDL_Rect destRect; // valid when dest is used
  65. SDL_Rect pos; // destination on screen
  66. int refreshWait; // Wait several refresh before updating the image
  67. int refreshCount;
  68. bool doLoop; // loop through video
  69. bool playVideo(int x, int y, SDL_Surface *dst, bool stopOnKey);
  70. bool open(std::string fname, bool loop, bool useOverlay = false);
  71. public:
  72. CVideoPlayer();
  73. ~CVideoPlayer();
  74. bool init();
  75. bool open(std::string fname);
  76. void close();
  77. bool nextFrame(); // display next frame
  78. void show(int x, int y, SDL_Surface *dst, bool update = true); //blit current frame
  79. void redraw(int x, int y, SDL_Surface *dst, bool update = true); //reblits buffer
  80. void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true); //moves to next frame if appropriate, and blits it or blits only if redraw parameter is set true
  81. // Opens video, calls playVideo, closes video; returns playVideo result (if whole video has been played)
  82. bool openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey = false);
  83. //TODO:
  84. bool wait(){return false;};
  85. int curFrame() const {return -1;};
  86. int frameCount() const {return -1;};
  87. // public to allow access from ffmpeg IO functions
  88. std::unique_ptr<CInputStream> data;
  89. };
  90. #endif