TdbFile.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include "../../common/Logger.h"
  2. #include "TdbFile.h"
  3. TdbFile::TdbFile(const Ogre::String& file):
  4. LzsFile(file)
  5. {
  6. }
  7. TdbFile::TdbFile(File* pFile):
  8. LzsFile(pFile)
  9. {
  10. }
  11. TdbFile::TdbFile(File* pFile, const u32 offset, const u32 length):
  12. LzsFile(pFile, offset, length)
  13. {
  14. }
  15. TdbFile::TdbFile(u8* pBuffer, const u32 offset, const u32 length):
  16. LzsFile(pBuffer, offset, length)
  17. {
  18. }
  19. TdbFile::~TdbFile()
  20. {
  21. }
  22. void
  23. TdbFile::CreateTexture( const Ogre::PixelBox& pb, const int face_id, const int start_x, const int start_y )
  24. {
  25. typedef std::vector< u8 > Face;
  26. std::vector< Face > face_row;
  27. Face face;
  28. // face 0
  29. face.push_back( 0x7e ); face.push_back( 0x00 ); face.push_back( 0x01 ); face.push_back( 0x02 ); face.push_back( 0x03 ); face.push_back( 0x04 ); face.push_back( 0x07 );
  30. face.push_back( 0x7e ); face.push_back( 0x05 ); face.push_back( 0x06 );
  31. face_row.push_back( face );
  32. face.clear();
  33. // face 1
  34. face.push_back( 0x7e ); face.push_back( 0x08 ); face.push_back( 0x09 ); face.push_back( 0x0a ); face.push_back( 0x0b ); face.push_back( 0x0c ); face.push_back( 0x0f );
  35. face.push_back( 0x7e ); face.push_back( 0x0d ); face.push_back( 0x0e );
  36. face_row.push_back( face );
  37. face.clear();
  38. // face 2
  39. face.push_back( 0x7e ); face.push_back( 0x10 ); face.push_back( 0x11 ); face.push_back( 0x12 ); face.push_back( 0x13 ); face.push_back( 0x14 ); face.push_back( 0x17 );
  40. face.push_back( 0x7e ); face.push_back( 0x15 ); face.push_back( 0x16 );
  41. face_row.push_back( face );
  42. face.clear();
  43. // face 3
  44. face.push_back(0x7e); face.push_back(0x18); face.push_back(0x19); face.push_back(0x1a); face.push_back(0x1b); face.push_back(0x1c); face.push_back(0x1f);
  45. face.push_back(0x7e); face.push_back(0x1d); face.push_back(0x1e);
  46. face_row.push_back( face );
  47. face.clear();
  48. // face 4
  49. face_row.push_back( face );
  50. face.clear();
  51. // face 5
  52. face_row.push_back( face );
  53. face.clear();
  54. // face 6
  55. face.push_back(0x7e); face.push_back(0x30); face.push_back(0x31); face.push_back(0x32); face.push_back(0x33); face.push_back(0x34); face.push_back(0x17);
  56. face.push_back(0x7e); face.push_back(0x35); face.push_back(0x36);
  57. face_row.push_back( face );
  58. face.clear();
  59. // face 7
  60. face_row.push_back( face );
  61. face.clear();
  62. // face 8
  63. face_row.push_back( face );
  64. face.clear();
  65. // face 9
  66. face_row.push_back( face );
  67. face.clear();
  68. // face a
  69. face_row.push_back( face );
  70. face.clear();
  71. // face b
  72. face_row.push_back( face );
  73. face.clear();
  74. // face c
  75. face.push_back(0x7e); face.push_back(0x54); face.push_back(0x7e); face.push_back(0x7e); face.push_back(0x7e); face.push_back(0x7e); face.push_back(0x7e);
  76. face.push_back(0x7e); face.push_back(0x55); face.push_back(0x7e);
  77. face_row.push_back( face );
  78. face.clear();
  79. // face d
  80. face.push_back(0x7e); face.push_back(0x56); face.push_back(0x7e); face.push_back(0x7e); face.push_back(0x7e); face.push_back(0x7e); face.push_back(0x7e);
  81. face.push_back(0x7e); face.push_back(0x57); face.push_back(0x7e);
  82. face_row.push_back( face );
  83. face.clear();
  84. // face e
  85. face_row.push_back( face );
  86. face.clear();
  87. // face f
  88. face_row.push_back( face );
  89. face.clear();
  90. // face 10
  91. face_row.push_back( face );
  92. face.clear();
  93. // face 11
  94. face.push_back(0x7e); face.push_back(0x5e); face.push_back(0x7e); face.push_back(0x7e); face.push_back(0x7e); face.push_back(0x7e); face.push_back(0x7e);
  95. face.push_back(0x7e); face.push_back(0x5f); face.push_back(0x7e);
  96. face_row.push_back( face );
  97. face.clear();
  98. // face 12
  99. face.push_back( 0x7e ); face.push_back( 0x60 ); face.push_back( 0x7e ); face.push_back( 0x7e ); face.push_back( 0x7e ); face.push_back( 0x7e ); face.push_back( 0x7e );
  100. face.push_back( 0x7e ); face.push_back( 0x61 ); face.push_back( 0x7e );
  101. face_row.push_back( face );
  102. face.clear();
  103. // face 13
  104. face_row.push_back( face );
  105. face.clear();
  106. // face 14
  107. face_row.push_back( face );
  108. face.clear();
  109. // face 15
  110. face_row.push_back( face );
  111. face.clear();
  112. // face 16
  113. face_row.push_back( face );
  114. face.clear();
  115. // face 17
  116. face_row.push_back( face );
  117. face.clear();
  118. // face 18
  119. face_row.push_back( face );
  120. face.clear();
  121. // face 19
  122. face_row.push_back( face );
  123. face.clear();
  124. // face 1a
  125. face_row.push_back( face );
  126. face.clear();
  127. // face 1b
  128. face_row.push_back( face );
  129. face.clear();
  130. // face 1c
  131. face_row.push_back( face );
  132. face.clear();
  133. // face 1d
  134. face.push_back(0x7e); face.push_back(0x76); face.push_back(0x7e); face.push_back(0x7e); face.push_back(0x7e); face.push_back(0x7e); face.push_back(0x7e);
  135. face.push_back(0x7e); face.push_back(0x77); face.push_back(0x7e);
  136. face_row.push_back( face );
  137. face.clear();
  138. /*
  139. // eye and mouth by face id
  140. 7E202122232427 7E2627 4
  141. 7E28292A2B2C2F 7E2D2E 5
  142. 7E38393A3B3C3F 7E3D3E 7
  143. 7E404142434447 7E4546 8
  144. 7E48494A4B4C4F 7E4D4E 9
  145. 7E507E7E7E7E7E 7E517E a
  146. 7E527E7E7E7E7E 7E537E b
  147. 7E587E7E7E7E7E 7E597E e
  148. 7E5A7E7E7E7E7E 7E5B7E f
  149. 7E5C7E7E7E7E7E 7E5D7E 10
  150. 7E627E7E7E7E7E 7E637E 13
  151. 7E647E7E7E7E7E 7E657E 14
  152. 7E667E7E7E7E7E 7E677E 15
  153. 7E687E7E7E7E7E 7E697E 16
  154. 7E6A7E7E7E7E7E 7E6B7E 17
  155. 7E6C7E7E7E7E7E 7E6D7E 18
  156. 7E6E7E7E7E7E7E 7E6F7E 19
  157. 7E707E7E7E7E7E 7E717E 1a
  158. 7E727E7E7E7E7E 7E737E 1b
  159. 7E747E7E7E7E7E 7E757E 1c
  160. 7E787E7E7E7E7E 7E797E 1e
  161. 7E7A7E7E7E7E7E 7E7B7E 1f
  162. 7E7C7E7E7E7E7E 7E7D7E 20
  163. */
  164. if( face_id >= face_row.size() || face_row[ face_id ].size() == 0 )
  165. {
  166. LOGGER->Log( "[ERROR] There is no data for face id: 0x" + HexToString( face_id, 2, '0' ) );
  167. return;
  168. }
  169. int offset_to_clut = GetU32LE( 0xc ) + face_id * 32;
  170. int start_x_new = start_x;
  171. int start_y_new = start_y;
  172. // eyes should be created twice
  173. bool second_eye = false;
  174. for( int i = 1; i < face_row[ face_id ].size(); ++i )
  175. {
  176. int offset_to_image = GetU32LE( 0x8 ) + face_row[ face_id ][ i ] * 512;
  177. if( i == 7 ) // mouth starts here. mouth always starts from 0xa0
  178. {
  179. start_x_new = start_x;
  180. start_y_new += 160;
  181. }
  182. for( u32 y = 0; y < 32; ++y )
  183. {
  184. u32* data = static_cast< u32* >( pb.data ) + ( y + start_y_new ) * pb.rowPitch;
  185. for( u32 x = 0; x < 16; ++x )
  186. {
  187. u8 pixel = GetU8( offset_to_image + y * 16 + x );
  188. u16 clut1 = GetU16LE( offset_to_clut + ( pixel & 0xf ) * 2 );
  189. u16 clut2 = GetU16LE( offset_to_clut + ( pixel >> 4 ) * 2 );
  190. u32 ogre_pixel1 = ( ( ( clut1 & 0x1f ) * 255 / 31 ) << 0x18 ) | ( ( ( ( clut1 >> 5 ) & 0x1f ) * 255 / 31 ) << 0x10 ) | ( ( ( ( clut1 >> 10 ) & 0x1f ) * 255 / 31 ) << 0x8 ) | 0;
  191. u8 stp = ( clut1 & 0x8000 ) >> 15;
  192. if( stp == 0 && ( ogre_pixel1 & 0xffffff00 ) == 0 )
  193. {
  194. ogre_pixel1 |= 0;
  195. }
  196. else if( stp == 0 && ( ogre_pixel1 & 0xffffff00) != 0 )
  197. {
  198. ogre_pixel1 |= 255;
  199. }
  200. else if( stp == 1 && ( ogre_pixel1 & 0xffffff00 ) == 0 )
  201. {
  202. ogre_pixel1 |= 255;
  203. }
  204. else if( stp == 1 && ( ogre_pixel1 & 0xffffff00 ) != 0 )
  205. {
  206. ogre_pixel1 |= 255 / 4;
  207. }
  208. u32 ogre_pixel2 = ( ( ( clut2 & 0x1f ) * 255 / 31 ) << 0x18 ) | ( ( ( ( clut2 >> 5 ) & 0x1f ) * 255 / 31 ) << 0x10 ) | ( ( ( ( clut2 >> 10 ) & 0x1f ) * 255 / 31 ) << 0x8 ) | 0;
  209. stp = ( clut2 & 0x8000 ) >> 15;
  210. if( stp == 0 && ( ogre_pixel2 & 0xffffff00 ) == 0 )
  211. {
  212. ogre_pixel2 |= 0;
  213. }
  214. else if( stp == 0 && ( ogre_pixel2 & 0xffffff00 ) != 0 )
  215. {
  216. ogre_pixel2 |= 255;
  217. }
  218. else if( stp == 1 && ( ogre_pixel2 & 0xffffff00 ) == 0 )
  219. {
  220. ogre_pixel2 |= 255;
  221. }
  222. else if( stp == 1 && ( ogre_pixel2 & 0xffffff00 ) != 0 )
  223. {
  224. ogre_pixel2 |= 255 / 4;
  225. }
  226. data[ start_x_new + x * 2 + 0 ] = ogre_pixel1;
  227. data[ start_x_new + x * 2 + 1 ] = ogre_pixel2;
  228. }
  229. }
  230. if( second_eye == false && i < 7 )
  231. {
  232. --i;
  233. second_eye = true;
  234. }
  235. else
  236. {
  237. second_eye = false;
  238. }
  239. start_x_new += 32;
  240. }
  241. }