SWF_Load.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "../renderer/Font.h"
  23. #pragma warning(disable: 4355) // 'this' : used in base member initializer list
  24. #define BSWF_VERSION 16 // bumped to 16 for storing atlas image dimensions for unbuffered loads
  25. #define BSWF_MAGIC ( ( 'B' << 24 ) | ( 'S' << 16 ) | ( 'W' << 8 ) | BSWF_VERSION )
  26. /*
  27. ===================
  28. idSWF::LoadSWF
  29. ===================
  30. */
  31. bool idSWF::LoadSWF( const char * fullpath ) {
  32. idFile * rawfile = fileSystem->OpenFileRead( fullpath );
  33. if ( rawfile == NULL ) {
  34. idLib::Printf( "SWF File not found %s\n", fullpath );
  35. return false;
  36. }
  37. swfHeader_t header;
  38. rawfile->Read( &header, sizeof( header ) );
  39. if ( header.W != 'W' || header.S != 'S' ) {
  40. idLib::Warning( "Wrong signature bytes" );
  41. delete rawfile;
  42. return false;
  43. }
  44. if ( header.version > 9 ) {
  45. idLib::Warning( "Unsupported version %d", header.version );
  46. delete rawfile;
  47. return false;
  48. }
  49. bool compressed;
  50. if ( header.compression == 'F' ) {
  51. compressed = false;
  52. } else if ( header.compression == 'C' ) {
  53. compressed = true;
  54. } else {
  55. idLib::Warning( "Unsupported compression type %c", header.compression );
  56. delete rawfile;
  57. return false;
  58. }
  59. idSwap::Little( header.fileLength );
  60. // header.fileLength somewhat annoyingly includes the size of the header
  61. uint32 fileLength2 = header.fileLength - (uint32)sizeof( swfHeader_t );
  62. // slurp the raw file into a giant array, which is somewhat atrocious when loading from the preload since it's already an idFile_Memory
  63. byte * fileData = (byte *)Mem_Alloc( fileLength2, TAG_SWF );
  64. size_t fileSize = rawfile->Read( fileData, fileLength2 );
  65. delete rawfile;
  66. if ( compressed ) {
  67. byte * uncompressed = (byte *)Mem_Alloc( fileLength2, TAG_SWF );
  68. if ( !Inflate( fileData, (int)fileSize, uncompressed, fileLength2 ) ) {
  69. idLib::Warning( "Inflate error" );
  70. Mem_Free( uncompressed );
  71. return false;
  72. }
  73. Mem_Free( fileData );
  74. fileData = uncompressed;
  75. }
  76. idSWFBitStream bitstream( fileData, fileLength2, false );
  77. swfRect_t frameSize;
  78. bitstream.ReadRect( frameSize );
  79. if ( !frameSize.tl.Compare( vec2_zero ) ) {
  80. idLib::Warning( "Invalid frameSize top left" );
  81. Mem_Free( fileData );
  82. return false;
  83. }
  84. frameWidth = frameSize.br.x;
  85. frameHeight = frameSize.br.y;
  86. frameRate = bitstream.ReadU16();
  87. // parse everything
  88. mainsprite->Load( bitstream, true );
  89. // now that all images have been loaded, write out the combined image
  90. idStr atlasFileName = "generated/";
  91. atlasFileName += fullpath;
  92. atlasFileName.SetFileExtension( ".tga" );
  93. WriteSwfImageAtlas( atlasFileName );
  94. Mem_Free( fileData );
  95. return true;
  96. }
  97. /*
  98. ===================
  99. idSWF::LoadBinary
  100. ===================
  101. */
  102. bool idSWF::LoadBinary( const char * bfilename, ID_TIME_T sourceTime ) {
  103. idFile * f = fileSystem->OpenFileReadMemory( bfilename );
  104. if ( f == NULL || f->Length() <= 0 ) {
  105. return false;
  106. }
  107. uint32 magic = 0;
  108. ID_TIME_T btimestamp = 0;
  109. f->ReadBig( magic );
  110. f->ReadBig( btimestamp );
  111. if ( magic != BSWF_MAGIC || ( !fileSystem->InProductionMode() && sourceTime != btimestamp ) ) {
  112. delete f;
  113. return false;
  114. }
  115. f->ReadBig( frameWidth );
  116. f->ReadBig( frameHeight );
  117. f->ReadBig( frameRate );
  118. if ( mouseX == -1 ) {
  119. mouseX = ( frameWidth / 2 );
  120. }
  121. if ( mouseY == -1 ) {
  122. mouseY = ( frameHeight / 2 );
  123. }
  124. mainsprite->Read( f );
  125. int num = 0;
  126. f->ReadBig( num );
  127. dictionary.SetNum( num );
  128. for ( int i = 0; i < dictionary.Num(); i++ ) {
  129. f->ReadBig( dictionary[i].type );
  130. switch ( dictionary[i].type ) {
  131. case SWF_DICT_IMAGE: {
  132. idStr imageName;
  133. f->ReadString( imageName );
  134. if ( imageName[0] == '.' ) {
  135. // internal image in the atlas
  136. dictionary[i].material = NULL;
  137. } else {
  138. dictionary[i].material = declManager->FindMaterial( imageName );
  139. }
  140. for ( int j = 0 ; j < 2 ; j++ ) {
  141. f->ReadBig( dictionary[i].imageSize[j] );
  142. f->ReadBig( dictionary[i].imageAtlasOffset[j] );
  143. }
  144. for ( int j = 0 ; j < 4 ; j++ ) {
  145. f->ReadBig( dictionary[i].channelScale[j] );
  146. }
  147. break;
  148. }
  149. case SWF_DICT_MORPH:
  150. case SWF_DICT_SHAPE: {
  151. dictionary[i].shape = new (TAG_SWF) idSWFShape;
  152. idSWFShape * shape = dictionary[i].shape;
  153. f->ReadBig( shape->startBounds.tl );
  154. f->ReadBig( shape->startBounds.br );
  155. f->ReadBig( shape->endBounds.tl );
  156. f->ReadBig( shape->endBounds.br );
  157. f->ReadBig( num ); shape->fillDraws.SetNum( num );
  158. for ( int d = 0; d < shape->fillDraws.Num(); d++ ) {
  159. idSWFShapeDrawFill & fillDraw = shape->fillDraws[d];
  160. f->ReadBig( fillDraw.style.type );
  161. f->ReadBig( fillDraw.style.subType );
  162. f->Read( &fillDraw.style.startColor, 4 );
  163. f->Read( &fillDraw.style.endColor, 4 );
  164. f->ReadBigArray( (float *)&fillDraw.style.startMatrix, 6 );
  165. f->ReadBigArray( (float *)&fillDraw.style.endMatrix, 6 );
  166. f->ReadBig( fillDraw.style.gradient.numGradients );
  167. for ( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) {
  168. f->ReadBig( fillDraw.style.gradient.gradientRecords[g].startRatio );
  169. f->ReadBig( fillDraw.style.gradient.gradientRecords[g].endRatio );
  170. f->Read( &fillDraw.style.gradient.gradientRecords[g].startColor, 4 );
  171. f->Read( &fillDraw.style.gradient.gradientRecords[g].endColor, 4 );
  172. }
  173. f->ReadBig( fillDraw.style.focalPoint );
  174. f->ReadBig( fillDraw.style.bitmapID );
  175. f->ReadBig( num ); fillDraw.startVerts.SetNum( num );
  176. f->ReadBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() );
  177. f->ReadBig( num ); fillDraw.endVerts.SetNum( num );
  178. f->ReadBigArray( fillDraw.endVerts.Ptr(), fillDraw.endVerts.Num() );
  179. f->ReadBig( num ); fillDraw.indices.SetNum( num );
  180. f->ReadBigArray( fillDraw.indices.Ptr(), fillDraw.indices.Num() );
  181. }
  182. f->ReadBig( num ); shape->lineDraws.SetNum( num );
  183. for ( int d = 0; d < shape->lineDraws.Num(); d++ ) {
  184. idSWFShapeDrawLine & lineDraw = shape->lineDraws[d];
  185. f->ReadBig( lineDraw.style.startWidth );
  186. f->ReadBig( lineDraw.style.endWidth );
  187. f->Read( &lineDraw.style.startColor, 4 );
  188. f->Read( &lineDraw.style.endColor, 4 );
  189. f->ReadBig( num ); lineDraw.startVerts.SetNum( num );
  190. f->ReadBigArray( lineDraw.startVerts.Ptr(), lineDraw.startVerts.Num() );
  191. f->ReadBig( num ); lineDraw.endVerts.SetNum( num );
  192. f->ReadBigArray( lineDraw.endVerts.Ptr(), lineDraw.endVerts.Num() );
  193. f->ReadBig( num ); lineDraw.indices.SetNum( num );
  194. f->ReadBigArray( lineDraw.indices.Ptr(), lineDraw.indices.Num() );
  195. }
  196. break;
  197. }
  198. case SWF_DICT_SPRITE: {
  199. dictionary[i].sprite = new (TAG_SWF) idSWFSprite( this );
  200. dictionary[i].sprite->Read( f );
  201. break;
  202. }
  203. case SWF_DICT_FONT: {
  204. dictionary[i].font = new (TAG_SWF) idSWFFont;
  205. idSWFFont * font = dictionary[i].font;
  206. idStr fontName;
  207. f->ReadString( fontName );
  208. font->fontID = renderSystem->RegisterFont( fontName );
  209. f->ReadBig( font->ascent );
  210. f->ReadBig( font->descent );
  211. f->ReadBig( font->leading );
  212. f->ReadBig( num ); font->glyphs.SetNum( num );
  213. for ( int g = 0; g < font->glyphs.Num(); g++ ) {
  214. f->ReadBig( font->glyphs[g].code );
  215. f->ReadBig( font->glyphs[g].advance );
  216. f->ReadBig( num ); font->glyphs[g].verts.SetNum( num );
  217. f->ReadBigArray( font->glyphs[g].verts.Ptr(), font->glyphs[g].verts.Num() );
  218. f->ReadBig( num ); font->glyphs[g].indices.SetNum( num );
  219. f->ReadBigArray( font->glyphs[g].indices.Ptr(), font->glyphs[g].indices.Num() );
  220. }
  221. break;
  222. }
  223. case SWF_DICT_TEXT: {
  224. dictionary[i].text = new (TAG_SWF) idSWFText;
  225. idSWFText * text = dictionary[i].text;
  226. f->ReadBig( text->bounds.tl );
  227. f->ReadBig( text->bounds.br );
  228. f->ReadBigArray( (float *)&text->matrix, 6 );
  229. f->ReadBig( num ); text->textRecords.SetNum( num );
  230. for ( int t = 0; t < text->textRecords.Num(); t++ ) {
  231. idSWFTextRecord & textRecord = text->textRecords[t];
  232. f->ReadBig( textRecord.fontID );
  233. f->Read( &textRecord.color, 4 );
  234. f->ReadBig( textRecord.xOffset );
  235. f->ReadBig( textRecord.yOffset );
  236. f->ReadBig( textRecord.textHeight );
  237. f->ReadBig( textRecord.firstGlyph );
  238. f->ReadBig( textRecord.numGlyphs );
  239. }
  240. f->ReadBig( num ); text->glyphs.SetNum( num );
  241. for ( int g = 0; g < text->glyphs.Num(); g++ ) {
  242. f->ReadBig( text->glyphs[g].index );
  243. f->ReadBig( text->glyphs[g].advance );
  244. }
  245. break;
  246. }
  247. case SWF_DICT_EDITTEXT: {
  248. dictionary[i].edittext = new (TAG_SWF) idSWFEditText;
  249. idSWFEditText * edittext = dictionary[i].edittext;
  250. f->ReadBig( edittext->bounds.tl );
  251. f->ReadBig( edittext->bounds.br );
  252. f->ReadBig( edittext->flags );
  253. f->ReadBig( edittext->fontID );
  254. f->ReadBig( edittext->fontHeight );
  255. f->Read( &edittext->color, 4 );
  256. f->ReadBig( edittext->maxLength );
  257. f->ReadBig( edittext->align );
  258. f->ReadBig( edittext->leftMargin );
  259. f->ReadBig( edittext->rightMargin );
  260. f->ReadBig( edittext->indent );
  261. f->ReadBig( edittext->leading );
  262. f->ReadString( edittext->variable );
  263. f->ReadString( edittext->initialText );
  264. break;
  265. }
  266. }
  267. }
  268. delete f;
  269. return true;
  270. }
  271. /*
  272. ===================
  273. idSWF::WriteBinary
  274. ===================
  275. */
  276. void idSWF::WriteBinary( const char * bfilename ) {
  277. idFileLocal file( fileSystem->OpenFileWrite( bfilename, "fs_basepath" ) );
  278. if ( file == NULL ) {
  279. return;
  280. }
  281. file->WriteBig( BSWF_MAGIC );
  282. file->WriteBig( timestamp );
  283. file->WriteBig( frameWidth );
  284. file->WriteBig( frameHeight );
  285. file->WriteBig( frameRate );
  286. mainsprite->Write( file );
  287. file->WriteBig( dictionary.Num() );
  288. for ( int i = 0; i < dictionary.Num(); i++ ) {
  289. file->WriteBig( dictionary[i].type );
  290. switch ( dictionary[i].type ) {
  291. case SWF_DICT_IMAGE: {
  292. if ( dictionary[i].material ) {
  293. file->WriteString( dictionary[i].material->GetName() );
  294. } else {
  295. file->WriteString( "." );
  296. }
  297. for ( int j = 0 ; j < 2 ; j++ ) {
  298. file->WriteBig( dictionary[i].imageSize[j] );
  299. file->WriteBig( dictionary[i].imageAtlasOffset[j] );
  300. }
  301. for ( int j = 0 ; j < 4 ; j++ ) {
  302. file->WriteBig( dictionary[i].channelScale[j] );
  303. }
  304. break;
  305. }
  306. case SWF_DICT_MORPH:
  307. case SWF_DICT_SHAPE: {
  308. idSWFShape * shape = dictionary[i].shape;
  309. file->WriteBig( shape->startBounds.tl );
  310. file->WriteBig( shape->startBounds.br );
  311. file->WriteBig( shape->endBounds.tl );
  312. file->WriteBig( shape->endBounds.br );
  313. file->WriteBig( shape->fillDraws.Num() );
  314. for ( int d = 0; d < shape->fillDraws.Num(); d++ ) {
  315. idSWFShapeDrawFill & fillDraw = shape->fillDraws[d];
  316. file->WriteBig( fillDraw.style.type );
  317. file->WriteBig( fillDraw.style.subType );
  318. file->Write( &fillDraw.style.startColor, 4 );
  319. file->Write( &fillDraw.style.endColor, 4 );
  320. file->WriteBigArray( (float *)&fillDraw.style.startMatrix, 6 );
  321. file->WriteBigArray( (float *)&fillDraw.style.endMatrix, 6 );
  322. file->WriteBig( fillDraw.style.gradient.numGradients );
  323. for ( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) {
  324. file->WriteBig( fillDraw.style.gradient.gradientRecords[g].startRatio );
  325. file->WriteBig( fillDraw.style.gradient.gradientRecords[g].endRatio );
  326. file->Write( &fillDraw.style.gradient.gradientRecords[g].startColor, 4 );
  327. file->Write( &fillDraw.style.gradient.gradientRecords[g].endColor, 4 );
  328. }
  329. file->WriteBig( fillDraw.style.focalPoint );
  330. file->WriteBig( fillDraw.style.bitmapID );
  331. file->WriteBig( fillDraw.startVerts.Num() );
  332. file->WriteBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() );
  333. file->WriteBig( fillDraw.endVerts.Num() );
  334. file->WriteBigArray( fillDraw.endVerts.Ptr(), fillDraw.endVerts.Num() );
  335. file->WriteBig( fillDraw.indices.Num() );
  336. file->WriteBigArray( fillDraw.indices.Ptr(), fillDraw.indices.Num() );
  337. }
  338. file->WriteBig( shape->lineDraws.Num() );
  339. for ( int d = 0; d < shape->lineDraws.Num(); d++ ) {
  340. idSWFShapeDrawLine & lineDraw = shape->lineDraws[d];
  341. file->WriteBig( lineDraw.style.startWidth );
  342. file->WriteBig( lineDraw.style.endWidth );
  343. file->Write( &lineDraw.style.startColor, 4 );
  344. file->Write( &lineDraw.style.endColor, 4 );
  345. file->WriteBig( lineDraw.startVerts.Num() );
  346. file->WriteBigArray( lineDraw.startVerts.Ptr(), lineDraw.startVerts.Num() );
  347. file->WriteBig( lineDraw.endVerts.Num() );
  348. file->WriteBigArray( lineDraw.endVerts.Ptr(), lineDraw.endVerts.Num() );
  349. file->WriteBig( lineDraw.indices.Num() );
  350. file->WriteBigArray( lineDraw.indices.Ptr(), lineDraw.indices.Num() );
  351. }
  352. break;
  353. }
  354. case SWF_DICT_SPRITE: {
  355. dictionary[i].sprite->Write( file );
  356. break;
  357. }
  358. case SWF_DICT_FONT: {
  359. idSWFFont * font = dictionary[i].font;
  360. file->WriteString( font->fontID->GetName() );
  361. file->WriteBig( font->ascent );
  362. file->WriteBig( font->descent );
  363. file->WriteBig( font->leading );
  364. file->WriteBig( font->glyphs.Num() );
  365. for ( int g = 0; g < font->glyphs.Num(); g++ ) {
  366. file->WriteBig( font->glyphs[g].code );
  367. file->WriteBig( font->glyphs[g].advance );
  368. file->WriteBig( font->glyphs[g].verts.Num() );
  369. file->WriteBigArray( font->glyphs[g].verts.Ptr(), font->glyphs[g].verts.Num() );
  370. file->WriteBig( font->glyphs[g].indices.Num() );
  371. file->WriteBigArray( font->glyphs[g].indices.Ptr(), font->glyphs[g].indices.Num() );
  372. }
  373. break;
  374. }
  375. case SWF_DICT_TEXT: {
  376. idSWFText * text = dictionary[i].text;
  377. file->WriteBig( text->bounds.tl );
  378. file->WriteBig( text->bounds.br );
  379. file->WriteBigArray( (float *)&text->matrix, 6 );
  380. file->WriteBig( text->textRecords.Num() );
  381. for ( int t = 0; t < text->textRecords.Num(); t++ ) {
  382. idSWFTextRecord & textRecord = text->textRecords[t];
  383. file->WriteBig( textRecord.fontID );
  384. file->Write( &textRecord.color, 4 );
  385. file->WriteBig( textRecord.xOffset );
  386. file->WriteBig( textRecord.yOffset );
  387. file->WriteBig( textRecord.textHeight );
  388. file->WriteBig( textRecord.firstGlyph );
  389. file->WriteBig( textRecord.numGlyphs );
  390. }
  391. file->WriteBig( text->glyphs.Num() );
  392. for ( int g = 0; g < text->glyphs.Num(); g++ ) {
  393. file->WriteBig( text->glyphs[g].index );
  394. file->WriteBig( text->glyphs[g].advance );
  395. }
  396. break;
  397. }
  398. case SWF_DICT_EDITTEXT: {
  399. idSWFEditText * edittext = dictionary[i].edittext;
  400. file->WriteBig( edittext->bounds.tl );
  401. file->WriteBig( edittext->bounds.br );
  402. file->WriteBig( edittext->flags );
  403. file->WriteBig( edittext->fontID );
  404. file->WriteBig( edittext->fontHeight );
  405. file->Write( &edittext->color, 4 );
  406. file->WriteBig( edittext->maxLength );
  407. file->WriteBig( edittext->align );
  408. file->WriteBig( edittext->leftMargin );
  409. file->WriteBig( edittext->rightMargin );
  410. file->WriteBig( edittext->indent );
  411. file->WriteBig( edittext->leading );
  412. file->WriteString( edittext->variable );
  413. file->WriteString( edittext->initialText );
  414. break;
  415. }
  416. }
  417. }
  418. }