CVideoHandler.h 9.1 KB

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