SWF.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __SWF_H__
  21. #define __SWF_H__
  22. #include "SWF_Enums.h"
  23. #include "SWF_Types.h"
  24. #include "SWF_Bitstream.h"
  25. #include "SWF_ScriptVar.h"
  26. #include "SWF_Sprites.h"
  27. #include "SWF_ScriptObject.h"
  28. #include "SWF_ParmList.h"
  29. #include "SWF_ScriptFunction.h"
  30. #include "SWF_SpriteInstance.h"
  31. #include "SWF_ShapeParser.h"
  32. #include "SWF_TextInstance.h"
  33. class idSWFDictionaryEntry {
  34. public:
  35. idSWFDictionaryEntry();
  36. ~idSWFDictionaryEntry();
  37. idSWFDictionaryEntry & operator=( idSWFDictionaryEntry & other );
  38. swfDictType_t type;
  39. const idMaterial * material;
  40. idSWFShape * shape;
  41. idSWFSprite * sprite;
  42. idSWFFont * font;
  43. idSWFText * text;
  44. idSWFEditText * edittext;
  45. idVec2i imageSize;
  46. idVec2i imageAtlasOffset;
  47. // the compressed images are normalize to reduce compression artifacts,
  48. // color must be scaled down by this
  49. idVec4 channelScale;
  50. };
  51. struct purgableSwfImage_t {
  52. purgableSwfImage_t() { image = NULL; swfFrameNum = 0; }
  53. idImage * image;
  54. unsigned swfFrameNum;
  55. };
  56. /*
  57. ================================================
  58. This class handles loading and rendering SWF files
  59. ================================================
  60. */
  61. class idSWF {
  62. public:
  63. idSWF( const char * filename, idSoundWorld * soundWorld = NULL );
  64. ~idSWF();
  65. bool IsLoaded() { return ( frameRate > 0 ); }
  66. bool IsActive() { return isActive; }
  67. void Activate( bool b );
  68. const char * GetName() { return filename; }
  69. void Pause() { mainspriteInstance->Stop(); paused = true; }
  70. void Resume() { mainspriteInstance->Play(); paused = false; }
  71. bool IsPaused() { return paused; }
  72. void SetPausedRender( bool valid ) { pausedRender = valid; }
  73. bool GetPausedRender() { return pausedRender; }
  74. void Render( idRenderSystem * gui, int time = 0, bool isSplitscreen = false );
  75. bool HandleEvent( const sysEvent_t * event );
  76. bool InhibitControl();
  77. void ForceInhibitControl( bool val ) { inhibitControl = val; }
  78. void SetGlobal( const char * name, const idSWFScriptVar & value ) { globals->Set( name, value ); }
  79. void SetGlobalNative( const char * name, idSWFScriptNativeVariable * native ) { globals->SetNative( name, native ); }
  80. idSWFScriptVar GetGlobal( const char * name ) { return globals->Get( name ); }
  81. idSWFScriptObject & GetRootObject() { assert( mainspriteInstance->GetScriptObject() != NULL ); return *( mainspriteInstance->GetScriptObject() ); }
  82. void Invoke( const char * functionName, const idSWFParmList & parms );
  83. void Invoke( const char * functionName, const idSWFParmList & parms, idSWFScriptVar & scriptVar );
  84. void Invoke( const char * functionName, const idSWFParmList & parms, bool & functionExists );
  85. int PlaySound( const char * sound, int channel = SCHANNEL_ANY, bool blocking = false );
  86. void StopSound( int channel = SCHANNEL_ANY );
  87. float GetFrameWidth() const { return frameWidth; }
  88. float GetFrameHeight() const { return frameHeight; }
  89. int GetMouseX() { return mouseX; }
  90. int GetMouseY() { return mouseY; }
  91. bool UseCircleForAccept();
  92. void SetSWFScale( float scale ) { swfScale = scale; }
  93. void SetForceNonPCGetPlatform() { forceNonPCPlatform = true; }
  94. idRandom2 & GetRandom() { return random; }
  95. int GetPlatform();
  96. //----------------------------------
  97. // SWF_Dictionary.cpp
  98. //----------------------------------
  99. idSWFDictionaryEntry * AddDictionaryEntry( int characterID, swfDictType_t type );
  100. idSWFDictionaryEntry * FindDictionaryEntry( int characterID, swfDictType_t type );
  101. idSWFDictionaryEntry * FindDictionaryEntry( int characterID );
  102. idSWFDictionaryEntry * GetDictionaryEntry( int index ) { return &dictionary[ index ]; }
  103. int GetNumDictionaryEntry() { return dictionary.Num(); }
  104. idSWFScriptObject * HitTest( idSWFSpriteInstance * spriteInstance, const swfRenderState_t & renderState, int x, int y, idSWFScriptObject * parentObject );
  105. private:
  106. idStr filename;
  107. ID_TIME_T timestamp;
  108. float frameWidth;
  109. float frameHeight;
  110. uint16 frameRate;
  111. float renderBorder;
  112. float swfScale;
  113. idVec2 scaleToVirtual;
  114. int lastRenderTime;
  115. bool isActive;
  116. bool inhibitControl;
  117. bool useInhibtControl;
  118. // certain screens need to be rendered when the pause menu is up so if this flag is
  119. // set on the gui we will allow it to render at a paused state;
  120. bool pausedRender;
  121. bool mouseEnabled;
  122. bool useMouse;
  123. bool blackbars;
  124. bool crop;
  125. bool paused;
  126. bool hasHitObject;
  127. bool forceNonPCPlatform;
  128. idRandom2 random;
  129. static int mouseX; // mouse x coord for all flash files
  130. static int mouseY; // mouse y coord for all flash files
  131. static bool isMouseInClientArea;
  132. idSWFScriptObject * mouseObject;
  133. idSWFScriptObject * hoverObject;
  134. idSWFSprite * mainsprite;
  135. idSWFSpriteInstance * mainspriteInstance;
  136. idSWFScriptObject * globals;
  137. idSWFScriptObject * shortcutKeys;
  138. idSoundWorld * soundWorld;
  139. const idMaterial * atlasMaterial;
  140. idBlockAlloc< idSWFSpriteInstance, 16 > spriteInstanceAllocator;
  141. idBlockAlloc< idSWFTextInstance, 16 > textInstanceAllocator;
  142. #define SWF_NATIVE_FUNCTION_SWF_DECLARE( x ) \
  143. class idSWFScriptFunction_##x : public idSWFScriptFunction_Nested< idSWF > { \
  144. public: \
  145. idSWFScriptVar Call( idSWFScriptObject * thisObject, const idSWFParmList & parms ); \
  146. } scriptFunction_##x;
  147. SWF_NATIVE_FUNCTION_SWF_DECLARE( shortcutKeys_clear );
  148. SWF_NATIVE_FUNCTION_SWF_DECLARE( deactivate );
  149. SWF_NATIVE_FUNCTION_SWF_DECLARE( inhibitControl );
  150. SWF_NATIVE_FUNCTION_SWF_DECLARE( useInhibit );
  151. SWF_NATIVE_FUNCTION_SWF_DECLARE( precacheSound );
  152. SWF_NATIVE_FUNCTION_SWF_DECLARE( playSound );
  153. SWF_NATIVE_FUNCTION_SWF_DECLARE( stopSounds );
  154. SWF_NATIVE_FUNCTION_SWF_DECLARE( getPlatform );
  155. SWF_NATIVE_FUNCTION_SWF_DECLARE( getTruePlatform );
  156. SWF_NATIVE_FUNCTION_SWF_DECLARE( getLocalString );
  157. SWF_NATIVE_FUNCTION_SWF_DECLARE( swapPS3Buttons );
  158. SWF_NATIVE_FUNCTION_SWF_DECLARE( getCVarInteger );
  159. SWF_NATIVE_FUNCTION_SWF_DECLARE( setCVarInteger );
  160. SWF_NATIVE_FUNCTION_SWF_DECLARE( strReplace );
  161. SWF_NATIVE_FUNCTION_SWF_DECLARE( acos );
  162. SWF_NATIVE_FUNCTION_SWF_DECLARE( cos );
  163. SWF_NATIVE_FUNCTION_SWF_DECLARE( sin );
  164. SWF_NATIVE_FUNCTION_SWF_DECLARE( round );
  165. SWF_NATIVE_FUNCTION_SWF_DECLARE( pow );
  166. SWF_NATIVE_FUNCTION_SWF_DECLARE( sqrt );
  167. SWF_NATIVE_FUNCTION_SWF_DECLARE( abs );
  168. SWF_NATIVE_FUNCTION_SWF_DECLARE( rand );
  169. SWF_NATIVE_FUNCTION_SWF_DECLARE( floor );
  170. SWF_NATIVE_FUNCTION_SWF_DECLARE( ceil );
  171. SWF_NATIVE_FUNCTION_SWF_DECLARE( toUpper );
  172. SWF_NATIVE_VAR_DECLARE_NESTED_READONLY( platform, idSWFScriptFunction_getPlatform, Call( object, idSWFParmList() ) );
  173. SWF_NATIVE_VAR_DECLARE_NESTED( blackbars, idSWF );
  174. SWF_NATIVE_VAR_DECLARE_NESTED( crop, idSWF );
  175. class idSWFScriptFunction_Object : public idSWFScriptFunction {
  176. public:
  177. idSWFScriptVar Call( idSWFScriptObject * thisObject, const idSWFParmList & parms ) { return idSWFScriptVar(); }
  178. void AddRef() { }
  179. void Release() { }
  180. idSWFScriptObject * GetPrototype() { return &object; }
  181. void SetPrototype( idSWFScriptObject * _object ) { assert( false ); }
  182. idSWFScriptObject object;
  183. } scriptFunction_Object;
  184. idList< idSWFDictionaryEntry, TAG_SWF > dictionary;
  185. struct keyButtonImages_t {
  186. keyButtonImages_t() {
  187. key = "";
  188. xbImage = "";
  189. psImage = "";
  190. width = 0;
  191. height = 0;
  192. baseline = 0;
  193. }
  194. keyButtonImages_t( const char* _key, const char* _xbImage, const char* _psImage, int w, int h, int _baseline ) {
  195. key = _key;
  196. xbImage = _xbImage;
  197. psImage = _psImage;
  198. width = w;
  199. height = h;
  200. baseline = _baseline;
  201. }
  202. const char * key;
  203. const char * xbImage;
  204. const char * psImage;
  205. int width;
  206. int height;
  207. int baseline;
  208. };
  209. idList< keyButtonImages_t, TAG_SWF > tooltipButtonImage;
  210. struct tooltipIcon_t {
  211. tooltipIcon_t() {
  212. startIndex = -1;
  213. endIndex = -1;
  214. material = NULL;
  215. imageWidth = 0;
  216. imageHeight = 0;
  217. baseline = 0;
  218. };
  219. int startIndex;
  220. int endIndex;
  221. const idMaterial * material;
  222. short imageWidth;
  223. short imageHeight;
  224. int baseline;
  225. };
  226. idList< tooltipIcon_t, TAG_SWF > tooltipIconList;
  227. const idMaterial * guiSolid;
  228. const idMaterial * guiCursor_arrow;
  229. const idMaterial * guiCursor_hand;
  230. const idMaterial * white;
  231. private:
  232. friend class idSWFSprite;
  233. friend class idSWFSpriteInstance;
  234. bool LoadSWF( const char * fullpath );
  235. bool LoadBinary( const char * bfilename, ID_TIME_T sourceTime );
  236. void WriteBinary( const char * bfilename );
  237. //----------------------------------
  238. // SWF_Shapes.cpp
  239. //----------------------------------
  240. void DefineShape( idSWFBitStream & bitstream );
  241. void DefineShape2( idSWFBitStream & bitstream );
  242. void DefineShape3( idSWFBitStream & bitstream );
  243. void DefineShape4( idSWFBitStream & bitstream );
  244. void DefineMorphShape( idSWFBitStream & bitstream );
  245. //----------------------------------
  246. // SWF_Sprites.cpp
  247. //----------------------------------
  248. void DefineSprite( idSWFBitStream & bitstream );
  249. //----------------------------------
  250. // SWF_Sounds.cpp
  251. //----------------------------------
  252. void DefineSound( idSWFBitStream & bitstream );
  253. //----------------------------------
  254. // SWF_Render.cpp
  255. //----------------------------------
  256. void DrawStretchPic( float x, float y, float w, float h, float s1, float t1, float s2, float t2, const idMaterial *material );
  257. void DrawStretchPic( const idVec4 & topLeft, const idVec4 & topRight, const idVec4 & bottomRight, const idVec4 & bottomLeft, const idMaterial * material );
  258. void RenderSprite( idRenderSystem * gui, idSWFSpriteInstance * sprite, const swfRenderState_t & renderState, int time, bool isSplitscreen = false );
  259. void RenderMask( idRenderSystem * gui, const swfDisplayEntry_t * mask, const swfRenderState_t & renderState, const int stencilMode );
  260. void RenderShape( idRenderSystem * gui, const idSWFShape * shape, const swfRenderState_t & renderState );
  261. void RenderMorphShape( idRenderSystem * gui, const idSWFShape* shape, const swfRenderState_t & renderState );
  262. void DrawEditCursor( idRenderSystem * gui, float x, float y, float w, float h, const swfMatrix_t & matrix );
  263. void DrawLine( idRenderSystem * gui, const idVec2 & p1, const idVec2 & p2, float width, const swfMatrix_t & matrix );
  264. void RenderEditText( idRenderSystem * gui, idSWFTextInstance * textInstance, const swfRenderState_t & renderState, int time, bool isSplitscreen = false );
  265. uint64 GLStateForRenderState( const swfRenderState_t & renderState );
  266. void FindTooltipIcons( idStr * text );
  267. //----------------------------------
  268. // SWF_Image.cpp
  269. //----------------------------------
  270. class idDecompressJPEG {
  271. public:
  272. idDecompressJPEG();
  273. ~idDecompressJPEG();
  274. byte * Load( const byte * input, int inputSize, int & width, int & height );
  275. private:
  276. void * vinfo;
  277. };
  278. idDecompressJPEG jpeg;
  279. void LoadImage( int characterID, const byte * imageData, int width, int height );
  280. void JPEGTables( idSWFBitStream & bitstream );
  281. void DefineBits( idSWFBitStream & bitstream );
  282. void DefineBitsJPEG2( idSWFBitStream & bitstream );
  283. void DefineBitsJPEG3( idSWFBitStream & bitstream );
  284. void DefineBitsLossless( idSWFBitStream & bitstream );
  285. void DefineBitsLossless2( idSWFBitStream & bitstream );
  286. // per-swf image atlas
  287. struct imageToPack_t {
  288. int characterID;
  289. idVec2i trueSize; // in texels
  290. byte * imageData; // trueSize.x * trueSize.y * 4
  291. idVec2i allocSize; // in DXT tiles, includes a border texel and rounding up to DXT blocks
  292. };
  293. class idSortBlocks : public idSort_Quick< imageToPack_t, idSortBlocks > {
  294. public:
  295. int Compare( const imageToPack_t & a, const imageToPack_t & b ) const {
  296. return ( b.allocSize.x * b.allocSize.y ) - ( a.allocSize.x * a.allocSize.y );
  297. }
  298. };
  299. idList<imageToPack_t, TAG_SWF> packImages; // only used during creation
  300. void WriteSwfImageAtlas( const char *filename );
  301. //----------------------------------
  302. // SWF_Text.cpp
  303. //----------------------------------
  304. void DefineFont2( idSWFBitStream & bitstream );
  305. void DefineFont3( idSWFBitStream & bitstream );
  306. void DefineTextX( idSWFBitStream & bitstream, bool rgba );
  307. void DefineText( idSWFBitStream & bitstream );
  308. void DefineText2( idSWFBitStream & bitstream );
  309. void DefineEditText( idSWFBitStream & bitstream );
  310. //----------------------------------
  311. // SWF_Zlib.cpp
  312. //----------------------------------
  313. bool Inflate( const byte * input, int inputSize, byte * output, int outputSize );
  314. public:
  315. //----------------------------------
  316. // SWF_Names.cpp
  317. //----------------------------------
  318. static const char * GetTagName( swfTag_t tag );
  319. static const char * GetActionName( swfAction_t action );
  320. };
  321. #endif // !__SWF_H__