CVideoHandler.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #pragma once
  2. #include "CSndHandler.h"
  3. struct SDL_Surface;
  4. class IVideoPlayer
  5. {
  6. public:
  7. virtual bool open(std::string name)=0; //true - succes
  8. virtual void close()=0;
  9. virtual bool nextFrame()=0;
  10. virtual void show(int x, int y, SDL_Surface *dst, bool update = true)=0;
  11. virtual void redraw(int x, int y, SDL_Surface *dst, bool update = true)=0; //reblits buffer
  12. virtual bool wait()=0;
  13. virtual int curFrame() const =0;
  14. virtual int frameCount() const =0;
  15. };
  16. class IMainVideoPlayer : public IVideoPlayer
  17. {
  18. public:
  19. std::string fname; //name of current video file (empty if idle)
  20. virtual void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true){}
  21. virtual bool openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey = false)
  22. {
  23. return false;
  24. }
  25. };
  26. class CEmptyVideoPlayer : public IMainVideoPlayer
  27. {
  28. public:
  29. virtual int curFrame() const {return -1;};
  30. virtual int frameCount() const {return -1;};
  31. virtual void redraw( int x, int y, SDL_Surface *dst, bool update = true ) {};
  32. virtual void show( int x, int y, SDL_Surface *dst, bool update = true ) {};
  33. virtual bool nextFrame() {return false;};
  34. virtual void close() {};
  35. virtual bool wait() {return false;};
  36. virtual bool open( std::string name ) {return false;};
  37. };
  38. #ifdef _WIN32
  39. #define WIN32_LEAN_AND_MEAN //excludes rarely used stuff from windows headers - delete this line if something is missing
  40. #include <windows.h>
  41. #pragma pack(push,1)
  42. struct BINK_STRUCT
  43. {
  44. si32 width;
  45. si32 height;
  46. si32 frameCount;
  47. si32 currentFrame;
  48. si32 lastFrame;
  49. si32 FPSMul;
  50. si32 FPSDiv;
  51. si32 unknown0;
  52. ui8 flags;
  53. ui8 unknown1[260];
  54. si32 CurPlane; // current plane
  55. void *plane0; // posi32er to plane 0
  56. void *plane1; // posi32er to plane 1
  57. si32 unknown2;
  58. si32 unknown3;
  59. si32 yWidth; // Y plane width
  60. si32 yHeight; // Y plane height
  61. si32 uvWidth; // U&V plane width
  62. si32 uvHeight; // U&V plane height
  63. };
  64. #pragma pack(pop)
  65. typedef BINK_STRUCT* HBINK;
  66. class DLLHandler
  67. {
  68. public:
  69. std::string name;
  70. HINSTANCE dll;
  71. void Instantiate(const char *filename);
  72. const char *GetLibExtension();
  73. void *FindAddress(const char *symbol);
  74. DLLHandler();
  75. virtual ~DLLHandler(); //d-tor
  76. };
  77. typedef void*(__stdcall* BinkSetSoundSystem)(void * soundfun, void*);
  78. typedef HBINK(__stdcall* BinkOpen)(HANDLE bikfile, int flags);
  79. typedef void(__stdcall* BinkClose)(HBINK);
  80. //typedef si32(__stdcall* BinkGetPalette)(HBINK);
  81. typedef void(__stdcall* BinkNextFrame)(HBINK);
  82. typedef void(__stdcall* BinkDoFrame)(HBINK);
  83. typedef ui8(__stdcall* BinkWait)(HBINK);
  84. typedef si32(__stdcall* BinkCopyToBuffer)(HBINK, void* buffer, int stride, int height, int x, int y, int mode);
  85. class CBIKHandler : public DLLHandler, public IVideoPlayer
  86. {
  87. void allocBuffer(int Bpp = 0);
  88. void freeBuffer();
  89. public:
  90. HANDLE hBinkFile;
  91. HBINK hBink;
  92. char * buffer;
  93. int bufferSize;
  94. BinkSetSoundSystem binkSetSoundSystem;
  95. BinkOpen binkOpen;
  96. //BinkGetPalette getPalette;
  97. BinkNextFrame binkNextFrame;
  98. BinkDoFrame binkDoFrame;
  99. BinkCopyToBuffer binkCopyToBuffer;
  100. BinkWait binkWait;
  101. BinkClose binkClose;
  102. CBIKHandler();
  103. bool open(std::string name);
  104. void close();
  105. bool nextFrame();
  106. void show(int x, int y, SDL_Surface *dst, bool update = true);
  107. void redraw(int x, int y, SDL_Surface *dst, bool update = true); //reblits buffer
  108. bool wait();
  109. int curFrame() const;
  110. int frameCount() const;
  111. };
  112. //////////SMK Player ///////////////////////////////////////////////////////
  113. struct SmackStruct
  114. {
  115. si32 version;
  116. si32 width;
  117. si32 height;
  118. si32 frameCount;
  119. si32 mspf;
  120. ui8 unk1[88];
  121. ui8 palette[776];
  122. si32 currentFrame; // Starting with 0
  123. ui8 unk[56];
  124. ui32 fileHandle; // exact type is HANDLE in windows.h
  125. };
  126. // defines function pointer types
  127. typedef SmackStruct* (__stdcall* SmackOpen)(void* , ui32, si32 );
  128. typedef int (__stdcall* SmackDoFrame)( SmackStruct * );
  129. typedef void (__stdcall * SmackGoto )(SmackStruct *, int frameNumber);
  130. typedef void (__stdcall* SmackNextFrame)(SmackStruct*);
  131. typedef void (__stdcall* SmackClose)(SmackStruct*);
  132. typedef void (__stdcall* SmackToBuffer) (SmackStruct*, int, int, int, int, char *, ui32);
  133. typedef bool (__stdcall* SmackWait)(SmackStruct*);
  134. typedef void (__stdcall* SmackSoundOnOff) (SmackStruct*, bool);
  135. typedef int (__stdcall* SmackVolumePan)(SmackStruct *, int SmackTrack, int volume, int pan);
  136. class CSmackPlayer: public DLLHandler, public IVideoPlayer
  137. {
  138. public:
  139. SmackOpen ptrSmackOpen;
  140. SmackDoFrame ptrSmackDoFrame;
  141. SmackToBuffer ptrSmackToBuffer;
  142. SmackNextFrame ptrSmackNextFrame;
  143. SmackWait ptrSmackWait;
  144. SmackSoundOnOff ptrSmackSoundOnOff;
  145. SmackClose ptrSmackClose;
  146. SmackVolumePan ptrVolumePan;
  147. char *buffer, *buf;
  148. SmackStruct* data;
  149. CSmackPlayer();
  150. ~CSmackPlayer();
  151. bool open(std::string name);
  152. void close();
  153. bool nextFrame();
  154. void show(int x, int y, SDL_Surface *dst, bool update = true);
  155. void redraw(int x, int y, SDL_Surface *dst, bool update = true); //reblits buffer
  156. bool wait();
  157. int curFrame() const;
  158. int frameCount() const;
  159. };
  160. class CVidHandler;
  161. class CVideoPlayer : public IMainVideoPlayer
  162. {
  163. private:
  164. CVidHandler vidh; //.vid file handling
  165. CSmackPlayer smkPlayer; //for .SMK
  166. CBIKHandler bikPlayer; //for .BIK
  167. IVideoPlayer *current; //points to bik or smk player, appropriate to type of currently played video
  168. bool first; //are we about to display the first frame (blocks update)
  169. public:
  170. CVideoPlayer(); //c-tor
  171. ~CVideoPlayer(); //d-tor
  172. bool open(std::string name);
  173. void close();
  174. bool nextFrame(); //move animation to the next frame
  175. void show(int x, int y, SDL_Surface *dst, bool update = true); //blit current frame
  176. void redraw(int x, int y, SDL_Surface *dst, bool update = true); //reblits buffer
  177. 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 paremeter is set true
  178. bool wait(); //true if we should wait before displaying next frame (for keeping FPS)
  179. int curFrame() const; //current frame number <1, framecount>
  180. int frameCount() const;
  181. bool openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey = false); //opens video, calls playVideo, closes video; returns playVideo result (if whole video has been played)
  182. bool playVideo(int x, int y, SDL_Surface *dst, bool stopOnKey = false); //plays whole opened video; returns: true when whole video has been shown, false when it has been interrupted
  183. };
  184. #define VIDEO_TAVERN "TAVERN.BIK"
  185. #define VIDEO_WIN "WIN3.BIK"
  186. #define VIDEO_LOSE_BATTLE_START "LBSTART.BIK"
  187. #define VIDEO_LOSE_BATTLE_LOOP "LBLOOP.BIK"
  188. #define VIDEO_RETREAT_START "RTSTART.BIK"
  189. #define VIDEO_RETREAT_LOOP "RTLOOP.BIK"
  190. #define VIDEO_SURRENDER "SURRENDER.BIK"
  191. #else
  192. #include "CSndHandler.h"
  193. #include <SDL.h>
  194. #include <SDL_video.h>
  195. #if SDL_VERSION_ATLEAST(1,3,0)
  196. #include <SDL_compat.h>
  197. #endif
  198. typedef struct AVFormatContext AVFormatContext;
  199. typedef struct AVCodecContext AVCodecContext;
  200. typedef struct AVCodec AVCodec;
  201. typedef struct AVFrame AVFrame;
  202. struct SwsContext;
  203. class CVideoPlayer : public IMainVideoPlayer
  204. {
  205. private:
  206. int stream; // stream index in video
  207. AVFormatContext *format;
  208. AVCodecContext *codecContext; // codec context for stream
  209. AVCodec *codec;
  210. AVFrame *frame;
  211. struct SwsContext *sws;
  212. // Destination. Either overlay or dest.
  213. SDL_Overlay *overlay;
  214. SDL_Surface *dest;
  215. SDL_Rect destRect; // valid when dest is used
  216. SDL_Rect pos; // destination on screen
  217. CVidHandler vidh;
  218. int refreshWait; // Wait several refresh before updating the image
  219. int refreshCount;
  220. bool doLoop; // loop through video
  221. bool playVideo(int x, int y, SDL_Surface *dst, bool stopOnKey);
  222. bool open(std::string fname, bool loop, bool useOverlay = false);
  223. public:
  224. CVideoPlayer();
  225. ~CVideoPlayer();
  226. bool init();
  227. bool open(std::string fname);
  228. void close();
  229. bool nextFrame(); // display next frame
  230. void show(int x, int y, SDL_Surface *dst, bool update = true); //blit current frame
  231. void redraw(int x, int y, SDL_Surface *dst, bool update = true); //reblits buffer
  232. 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
  233. // Opens video, calls playVideo, closes video; returns playVideo result (if whole video has been played)
  234. bool openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey = false);
  235. //TODO:
  236. bool wait(){return false;};
  237. int curFrame() const {return -1;};
  238. int frameCount() const {return -1;};
  239. const char *data; // video buffer
  240. int length; // video size
  241. ui32 offset; // current data offset
  242. };
  243. #define VIDEO_TAVERN "tavern.mjpg"
  244. #define VIDEO_WIN "win3.mjpg"
  245. #define VIDEO_LOSE_BATTLE_START "lbstart.mjpg"
  246. #define VIDEO_LOSE_BATTLE_LOOP "lbloop.mjpg"
  247. #define VIDEO_RETREAT_START "rtstart.mjpg"
  248. #define VIDEO_RETREAT_LOOP "rtloop.mjpg"
  249. #define VIDEO_SURRENDER "surrender.mjpg"
  250. #endif