CVideoHandler.h 8.1 KB

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