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