Clone of Akilla's ac2d @ https://github.com/deregtd/AC2D

cPortal.cpp 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "stdafx.h"
  2. #include "cPortal.h"
  3. #include "cByteStream.h"
  4. /* Portal Types:
  5. 01 - Type 1 models
  6. 02 - Type 2 models (Modelgroups)
  7. 03 - Animations
  8. 04 - Palettes
  9. 05 - Texture lookups
  10. 06 - Textures
  11. 08 - Texture Lookups
  12. 0A - Sounds
  13. 0D - Dungeon Parts
  14. 0E - UI text
  15. 0F - palette lookups?
  16. 13 - Worldinfo
  17. 22 - String lists
  18. 25 - More string lists
  19. 27 - Magic Strings
  20. 31 - Character creation strings
  21. */
  22. PFNGLCOMPRESSEDTEXIMAGE2DARBPROC glCompressedTexImage2DARB;
  23. cPortal::cPortal()
  24. {
  25. m_tfPortal = new cTurbineFile();
  26. m_tfPortal->LoadFile("client_portal.dat");
  27. TCHAR szEXEPathname[_MAX_PATH];
  28. GetModuleFileName(NULL, szEXEPathname, _MAX_PATH);
  29. *(strrchr(szEXEPathname, '\\')+1) = 0;
  30. strcat(szEXEPathname, "client_highres.dat");
  31. FILE *ftest = fopen(szEXEPathname,"rb");
  32. if (ftest)
  33. {
  34. fclose(ftest);
  35. m_tfPortalHighRes = new cTurbineFile();
  36. m_tfPortalHighRes->LoadFile("client_highres.dat");
  37. }
  38. else
  39. m_tfPortalHighRes = NULL;
  40. }
  41. cPortal::~cPortal()
  42. {
  43. if (m_tfPortal)
  44. delete m_tfPortal;
  45. if (m_tfPortalHighRes)
  46. delete m_tfPortalHighRes;
  47. }
  48. cPortalFile * cPortal::OpenEntry( DWORD dwID )
  49. {
  50. cPortalFile *tp = NULL;
  51. if (m_tfPortalHighRes)
  52. tp = m_tfPortalHighRes->OpenEntry(dwID);
  53. if (tp)
  54. return tp;
  55. else return m_tfPortal->OpenEntry(dwID);
  56. }
  57. int cPortal::HighestPowerOfTwo(DWORD In)
  58. {
  59. int tp = -1;
  60. for (int i=0;i<32;i++)
  61. {
  62. if (In & 1) tp = i;
  63. In >>= 1;
  64. }
  65. return tp;
  66. }
  67. DWORD cPortal::FindTexturePalette(DWORD Texture, std::vector<stPaletteSwap> *vPaletteSwaps, float fTransTex, float fTransPix)
  68. {
  69. //TODO: Implement the transparency!
  70. cPortalFile *pfTex = OpenEntry(Texture);
  71. if (!pfTex)
  72. return 0;
  73. //redirect if it can find it
  74. if ((Texture & 0xFF000000) == 0x05000000)
  75. {
  76. DWORD NumIDs = *((DWORD *) (pfTex->data + 9));
  77. for (DWORD i=13; i<=13 + NumIDs*4; i+=4)
  78. {
  79. DWORD tp = FindGraphic(*((DWORD *) (pfTex->data + i)), vPaletteSwaps, fTransTex, fTransPix);
  80. if (tp)
  81. {
  82. m_mGraphicEdgeCache[Texture] = m_mGraphicEdgeCache[*((DWORD *) (pfTex->data + i))];
  83. return tp;
  84. }
  85. }
  86. }
  87. return 0;
  88. }
  89. DWORD cPortal::FindGraphic(DWORD ID, std::vector<stPaletteSwap> *vPaletteSwaps, float fTransTex, float fTransPix)
  90. {
  91. //Pull it out of the cache, if possible...
  92. QWORD Hash = ID | ((QWORD) ID << 32);
  93. int num = 0;
  94. if (vPaletteSwaps)
  95. {
  96. for (std::vector<stPaletteSwap>::iterator i = vPaletteSwaps->begin(); i != vPaletteSwaps->end(); i++)
  97. {
  98. Hash += num*((QWORD) i->newPalette << 40);
  99. Hash += num*((QWORD) i->length << 32);
  100. Hash += num*((QWORD) i->offset << 24);
  101. Hash += num++;
  102. }
  103. }
  104. Hash += (QWORD) (*((DWORD *) &fTransTex))*72;
  105. Hash += (QWORD) (*((DWORD *) &fTransPix))*9727;
  106. if (m_mGraphicCache.find(Hash) != m_mGraphicCache.end())
  107. return m_mGraphicCache[Hash];
  108. //Open object and make sure it exists...
  109. cPortalFile *pfUI = OpenEntry(ID);
  110. if (!pfUI)
  111. return 0;
  112. cByteStream pBS(pfUI->data, pfUI->length);
  113. pBS.ReadBegin();
  114. //Load and parse image
  115. DWORD dwPicID = pBS.ReadDWORD();
  116. DWORD unk = pBS.ReadDWORD();
  117. DWORD sizeX = pBS.ReadDWORD();
  118. DWORD sizeY = pBS.ReadDWORD();
  119. DWORD format = pBS.ReadDWORD();
  120. DWORD length = pBS.ReadDWORD();
  121. if (format == 0x1F4) //JPEG
  122. return 0;
  123. //If it's a palettized image, form the palette and transparencies
  124. DWORD palette = 0;
  125. DWORD *newPalette = 0;
  126. cPortalFile *pfPal = 0;
  127. bool newPalUsed = false;
  128. if ((format == 0x65) || (format == 0x29))
  129. {
  130. //Form the palette we're going to use
  131. memcpy(&palette, pfUI->data+24+length, sizeof(DWORD));
  132. pfPal = OpenEntry(palette);
  133. DWORD palentries = *((DWORD *) (pfPal->data+4));
  134. //Copy a new palette if we have to, otherwise just pull from memory
  135. newPalUsed = true;
  136. newPalette = new DWORD[palentries];
  137. memcpy(newPalette, pfPal->data+8, palentries*sizeof(DWORD));
  138. //Perform paletteswaps
  139. if (vPaletteSwaps)
  140. {
  141. for (std::vector<stPaletteSwap>::iterator i = vPaletteSwaps->begin(); i != vPaletteSwaps->end(); i++)
  142. {
  143. cPortalFile *pfPalNew = m_Portal->OpenEntry(0x04000000 | i->newPalette);
  144. if (!pfPalNew)
  145. return false;
  146. if (format == 0x29) //256 entries
  147. memcpy(&newPalette[i->offset], pfPalNew->data+8+(i->offset*sizeof(DWORD)), (i->length*sizeof(DWORD)));
  148. else //2048 entries
  149. memcpy(&newPalette[i->offset*8], pfPalNew->data+8+(i->offset*sizeof(DWORD))*8, (i->length*sizeof(DWORD))*8);
  150. }
  151. }
  152. //Overall Transparency
  153. DWORD tpf = ((DWORD) (0xFF * (1.0f-fTransTex))) << 24;
  154. for (DWORD i=0;i<palentries;i++)
  155. newPalette[i] = (newPalette[i] & 0x00FFFFFF) | tpf;
  156. //Color key 0 (0-7 for 2048-entry palettes, cuz turbine is gay)
  157. tpf = ((DWORD) (0xFF * (1.0f-fTransPix))) << 24;
  158. if (format == 0x29)
  159. {
  160. newPalette[0] = (newPalette[0] & 0x00FFFFFF) | tpf;
  161. }
  162. else
  163. {
  164. for (int l=0;l<8;l++)
  165. newPalette[l] = (newPalette[l] & 0x00FFFFFF) | tpf;
  166. }
  167. }
  168. //Figure out size of actual texture to store (must be powers of 2)
  169. int powx = HighestPowerOfTwo(sizeX), powy = HighestPowerOfTwo(sizeY);
  170. DWORD sizeX2 = 1 << powx;
  171. while (sizeX2 < sizeX)
  172. sizeX2 <<= 1;
  173. DWORD sizeY2 = 1 << powy;
  174. while (sizeY2 < sizeY)
  175. sizeY2 <<= 1;
  176. //Disable DXT textures for termserv
  177. //if (format >= 0x31545844) return 0;
  178. //Generate the OpenGL texture
  179. GLuint dwid = 0;
  180. glGenTextures( 1, &dwid );
  181. glBindTexture(GL_TEXTURE_2D, dwid);
  182. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  183. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  184. if ((sizeX2 == sizeX) && (sizeY2 == sizeY) && (format != 0x65) && (format != 0x29))
  185. {
  186. //If it's just immediately loadable without any processing, do it!
  187. if (format == 0x31545844)
  188. glCompressedTexImage2DARB(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, sizeX2,sizeY2, 0, length, pfUI->data+24);
  189. else if (format == 0x35545844)
  190. glCompressedTexImage2DARB(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, sizeX2,sizeY2, 0, length, pfUI->data+24);
  191. else if (format == 0x15)
  192. glTexImage2D(GL_TEXTURE_2D, 0, 4, sizeX2,sizeY2, 0, GL_BGRA, GL_UNSIGNED_BYTE, pfUI->data+24);
  193. else if (format == 0xF3)
  194. glTexImage2D(GL_TEXTURE_2D, 0, 3, sizeX2,sizeY2, 0, GL_RGB, GL_UNSIGNED_BYTE, pfUI->data+24);
  195. else if (format == 0x14)
  196. glTexImage2D(GL_TEXTURE_2D, 0, 3, sizeX2,sizeY2, 0, GL_BGR, GL_UNSIGNED_BYTE, pfUI->data+24);
  197. else if ((format == 0x1C) || (format == 0xF4))
  198. glTexImage2D(GL_TEXTURE_2D, 0, 1, sizeX2,sizeY2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pfUI->data+24);
  199. else if ((format == 0x17) || (format == 0x1A)) //16-bit grayscale?
  200. glTexImage2D(GL_TEXTURE_2D, 0, 1, sizeX2,sizeY2, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, pfUI->data+24);
  201. else
  202. return NULL;
  203. }
  204. else
  205. {
  206. //Padding will be required, unfortunately...
  207. BYTE *tex = new BYTE[sizeX2*sizeY2*4];
  208. ZeroMemory(tex, sizeX2*sizeY2*4);
  209. for (DWORD y=0;y<sizeY2;y++)
  210. {
  211. for (DWORD x=0;x<sizeX2;x++)
  212. {
  213. if ((x < sizeX) && (y < sizeY))
  214. {
  215. //Image formats...
  216. switch (format)
  217. {
  218. case 0x14:
  219. case 0xF3: //RGB (24-bit color)
  220. *((DWORD *) &tex[4*sizeX2*y + x*4]) = *((DWORD *) (pfUI->data+24+(3*(sizeX*y+x)))) | 0xFF000000;
  221. break;
  222. case 0x15: //RGBA (32-bit color)
  223. *((DWORD *) &tex[4*sizeX2*y + x*4]) = *((DWORD *) (pfUI->data+24+(4*(sizeX*y+x))));
  224. break;
  225. case 0x65: //2-byte indexed, palette ID follows encoded data
  226. {
  227. WORD index = *((WORD *)(pfUI->data+24+(2*(sizeX*y+x))));
  228. *((DWORD *) &tex[4*sizeX2*y + x*4]) = newPalette[index]/* & 0x7FFFFFFF*/;
  229. }
  230. break;
  231. case 0x29: //1-byte indexed, palette ID follows encoded data
  232. {
  233. BYTE index = *((BYTE *)(pfUI->data+24+(sizeX*y+x)));
  234. *((DWORD *) &tex[4*sizeX2*y + x*4]) = newPalette[index]/* & 0x7FFFFFFF*/;
  235. }
  236. break;
  237. case 0x1C: //1-byte grayscale
  238. case 0xF4:
  239. {
  240. DWORD index = *((BYTE *)(pfUI->data+24+(sizeX*y+x)));
  241. *((DWORD *) &tex[4*sizeX2*y + x*4]) = index | (index << 8) | (index << 16) | 0xFF000000;
  242. }
  243. break;
  244. default:
  245. {
  246. return NULL;
  247. }
  248. }
  249. }
  250. else
  251. *((DWORD *) &tex[4*sizeX2*y + x*4]) = 0x00000000;
  252. }
  253. }
  254. glTexImage2D(GL_TEXTURE_2D, 0, 4, sizeX2,sizeY2, 0, GL_BGRA, GL_UNSIGNED_BYTE, tex);
  255. delete []tex;
  256. }
  257. //Store info to our caches
  258. POINTf tpoint( (float) sizeX/sizeX2, (float) sizeY/sizeY2 );
  259. m_mGraphicEdgeCache[ID] = tpoint;
  260. m_mGraphicCache[Hash] = dwid;
  261. //Clean up
  262. if (newPalUsed)
  263. delete []newPalette;
  264. return dwid;
  265. }
  266. POINTf cPortal::GetGraphicEdges(DWORD ID)
  267. {
  268. if (m_mGraphicEdgeCache.find(ID) == m_mGraphicEdgeCache.end())
  269. {
  270. POINTf tpzero( 0, 0 );
  271. return tpzero;
  272. }
  273. return m_mGraphicEdgeCache[ID];
  274. }
  275. DWORD cPortal::GetPoolSize()
  276. {
  277. DWORD size = 0;
  278. if (m_tfPortalHighRes) size += m_tfPortalHighRes->GetPoolSize();
  279. size += m_tfPortal->GetPoolSize();
  280. return size;
  281. }