FontFile.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "common/FileSystem.h"
  2. #include "common/Logger.h"
  3. #include "FontFile.h"
  4. #include <Ogre.h>
  5. #include "common/TypeDefine.h"
  6. using namespace QGears;
  7. /*
  8. FontFile::FontFile(const Ogre::String& file):
  9. File(*GAMEFILESYSTEM, file)
  10. {
  11. }
  12. */
  13. FontFile::FontFile(File* pFile):
  14. File(pFile)
  15. {
  16. }
  17. FontFile::FontFile(File* pFile, const u32& offset, const u32& length):
  18. File(pFile, offset, length)
  19. {
  20. }
  21. FontFile::FontFile(u8* pBuffer, const u32& offset, const u32& length):
  22. File(pBuffer, offset, length)
  23. {
  24. }
  25. FontFile::~FontFile(void)
  26. {
  27. }
  28. Surface*
  29. FontFile::GetSurface(void)
  30. {
  31. int width = 512;
  32. int height = 4096;
  33. int chars = 0;
  34. int char_num = 6165;
  35. Surface* ret = CreateSurface(width, height);
  36. int x_16 = 0;
  37. int y_16 = 0;
  38. struct ClutColor
  39. {
  40. u8 r; /**< @brief red color in CLUT */
  41. u8 g; /**< @brief green color in CLUT */
  42. u8 b; /**< @brief blue color in CLUT */
  43. u8 a; /**< @brief alpha in CLUT */
  44. };
  45. ClutColor color;
  46. color.r = 0;
  47. color.g = 0;
  48. color.b = 0;
  49. color.a = 255;
  50. u8 data = 0;
  51. for (int chars = 0; chars < char_num; ++chars)
  52. {
  53. Surface* glyth = CreateSurface(16, 16);
  54. for (int y = 0; y < 16; ++y)
  55. {
  56. data = (y < 11) ? GetU8(0xE + chars * 22 + y * 2 + 0) : 0;
  57. //LOGGER->Log(LOGGER_INFO, "%x, %02x", y, data);
  58. int j = 0;
  59. for (int i = 7; i >= 0; --i)
  60. {
  61. color.r = (((data >> i) & 0x01) == 1) ? 0 : 255;
  62. color.g = (((data >> i) & 0x01) == 1) ? 0 : 255;
  63. color.b = (((data >> i) & 0x01) == 1) ? 0 : 255;
  64. color.a = (((data >> i) & 0x01) == 1) ? 255 : 255;
  65. memcpy(glyth->pixels.data() + 64 * y + j, &color, sizeof(ClutColor));
  66. j += 4;
  67. }
  68. data = (y < 11) ? GetU8(0xE + chars * 22 + y * 2 + 1) : 0;
  69. //LOGGER->Log(LOGGER_INFO, "%02x", data);
  70. for (int i = 7; i >= 0; --i)
  71. {
  72. color.r = (((data >> i) & 0x01) == 1) ? 0 : 255;
  73. color.g = (((data >> i) & 0x01) == 1) ? 0 : 255;
  74. color.b = (((data >> i) & 0x01) == 1) ? 0 : 255;
  75. color.a = (((data >> i) & 0x01) == 1) ? 255 : 255;
  76. memcpy(glyth->pixels.data() + 64 * y + j, &color, sizeof(ClutColor));
  77. j += 4;
  78. }
  79. }
  80. CopyToSurface(ret, x_16, y_16, glyth);
  81. delete glyth;
  82. x_16 += 16;
  83. if (x_16 == ret->width)
  84. {
  85. y_16 += 16;
  86. x_16 = 0;
  87. }
  88. }
  89. Ogre::TexturePtr ptex;
  90. Ogre::HardwarePixelBufferSharedPtr buffer;
  91. ptex = Ogre::TextureManager::getSingleton().createManual("DynaTex", "General", Ogre::TEX_TYPE_2D, ret->width, ret->height, 0, Ogre::PF_R8G8B8A8, Ogre::TU_STATIC);
  92. buffer = ptex->getBuffer(0, 0);
  93. buffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
  94. const Ogre::PixelBox& pb = buffer->getCurrentLock();
  95. for (auto y = 0; y < ret->height; ++y)
  96. {
  97. auto* data = static_cast<unsigned int*>(pb.data) + y * pb.rowPitch;
  98. for (auto x = 0; x < ret->width; ++x)
  99. {
  100. auto clut = ret->pixels[y * ret->width * 4 + x * 4 + 3] | (ret->pixels[y * ret->width * 4 + x * 4 + 2] << 8) | (ret->pixels[y * ret->width * 4 + x * 4 + 1] << 16) | (ret->pixels[y * ret->width * 4 + x * 4 + 0] << 24);
  101. data[x] = clut;
  102. }
  103. }
  104. Ogre::Image image;
  105. image.loadDynamicImage((Ogre::uchar*)pb.data, ret->width, ret->height, Ogre::PF_R8G8B8A8);
  106. image.save("font.png");
  107. buffer->unlock();
  108. Ogre::TextureManager::getSingleton().remove("DynaTex");
  109. return ret;
  110. }