FONT.CPP 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <io.h>
  4. #include "debug4g.h"
  5. #include "error.h"
  6. #include "font.h"
  7. #include "names.h"
  8. #include "misc.h" // remove before release, used for FileSave
  9. FONTDATA testFont = {
  10. { "FNT", kFontVersion },
  11. "7777777",
  12. 2,
  13. 5,
  14. 10,
  15. kFontTech,
  16. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  17. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  18. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  19. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
  20. 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
  21. 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
  22. 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
  23. 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, -1
  24. },
  25. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  27. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  30. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  33. }
  34. };
  35. Font::Font(void)
  36. {
  37. hFont = NULL;
  38. pFont = NULL;
  39. }
  40. //Font::~Font()
  41. //{
  42. // hFont = NULL;
  43. // pFont = NULL;
  44. //}
  45. void Font::Unload( void )
  46. {
  47. if ((hFont == NULL) && (pFont != NULL))
  48. {
  49. // forces deallocation in the event the font was created
  50. // internally and not loaded as a resource via Lookup()
  51. // this code should go away once all fonts are resources
  52. dprintf("Unloading font %s\n",pFont->name);
  53. Resource::Free( pFont );
  54. pFont = NULL;
  55. }
  56. }
  57. void Font::Load( char *zName )
  58. {
  59. dassert( zName != NULL );
  60. hFont = gSysRes.Lookup( zName, ".fnt" );
  61. #if 0
  62. if (hFont != NULL) {
  63. pFont = (FONTDATA *)gSysRes.Lock( hFont );
  64. dprintf("Font: %s Version: %d\n", pFont->name, pFont->header.version);
  65. dprintf("charSpace: %d lineSpace: %d wordSpace: %d\n",
  66. pFont->charSpace, pFont->lineSpace, pFont->wordSpace);
  67. dprintf("firstTile: %d\n", pFont->firstTile);
  68. gSysRes.Unlock( hFont );
  69. pFont = NULL;
  70. }
  71. #endif
  72. if (hFont == NULL) {
  73. pFont = (FONTDATA *)Resource::Alloc( sizeof(FONTDATA) );
  74. if (pFont == NULL)
  75. ThrowError("Error allocating FNT resource", ES_ERROR );
  76. *pFont = testFont; // memcpy( pFont, testFont, sizeof(FONTDATA) );
  77. // manually generate a width table until we have font tools
  78. for (int i = 0; i < SCHAR_MAX; i++ )
  79. {
  80. if (pFont->tileOffset[i] == -1)
  81. pFont->tileWidth[i] = pFont->wordSpace;
  82. else
  83. pFont->tileWidth[i] = tilesizx[ pFont->firstTile + pFont->tileOffset[i] ];
  84. }
  85. // create and null terminate the internal font name
  86. strncpy(pFont->name, zName, kFontNameSize);
  87. *(pFont->name + kFontNameSize) = '\0';
  88. // save the file until we have font tools
  89. char zFilename[_MAX_PATH];
  90. strcpy(zFilename,zName);
  91. strcat(zFilename,".fnt");
  92. FileSave(zFilename, pFont, sizeof(FONTDATA) );
  93. //dprintf("Error creating resource handle for %s.fnt\n", zName );
  94. //ThrowError("Error reading FNT resource", ES_ERROR );
  95. }
  96. }
  97. int Font::DrawText( int nDestTile, unsigned justFlags, int x, int y, char *zText )
  98. {
  99. char *p;
  100. short nWidth;
  101. if (hFont != NULL)
  102. pFont = (FONTDATA *)gSysRes.Lock( hFont );
  103. for (p = zText, nWidth = 0; *p != '\0'; p++)
  104. {
  105. nWidth += pFont->tileWidth[ (*p) & 0x7F ] + pFont->charSpace;
  106. }
  107. nWidth -= pFont->charSpace;
  108. switch (justFlags)
  109. {
  110. // case kFontJustLeft:
  111. // break;
  112. case kFontJustRight:
  113. x -= nWidth;
  114. break;
  115. case kFontJustCenter:
  116. x -= nWidth / 2;
  117. break;
  118. }
  119. for (p = zText; *p != '\0'; p++)
  120. {
  121. int cIndex = (*p) & 0x7F;
  122. int nTile = pFont->firstTile + pFont->tileOffset[ cIndex ];
  123. // handle drawing to the screen or an allocated tile
  124. if (nDestTile == -1) {
  125. if (pFont->tileOffset[ cIndex ] == -1) {
  126. x += pFont->wordSpace;
  127. } else {
  128. // overwritesprite(x, y, (short)nTile, 0, 0, 0);
  129. x += pFont->tileWidth[ cIndex ] + pFont->charSpace;
  130. }
  131. } else {
  132. if (pFont->tileOffset[ cIndex ] == -1) {
  133. x += pFont->wordSpace;
  134. } else {
  135. (void)y;
  136. // copytilepiece(nTile, 0, 0, tilesizx[nTile], tilesizy[nTile],
  137. // nDestTile, x, y);
  138. x += pFont->tileWidth[ cIndex ] + pFont->charSpace;
  139. }
  140. }
  141. }
  142. if (hFont != NULL) {
  143. gSysRes.Unlock( hFont );
  144. pFont = NULL;
  145. }
  146. return 0;
  147. }
  148. int Font::Print( int x, int y, char *zFormat, ... )
  149. {
  150. int len;
  151. va_list argptr;
  152. char buf[ kFontMaxText + 1 ];
  153. // print variable argument string into buf for output
  154. va_start( argptr, zFormat );
  155. len = vsprintf( buf, zFormat, argptr );
  156. va_end( argptr );
  157. if (len >= kFontMaxText) { // buf overwritten? return error.
  158. ThrowError("Message length exceeded buffer length!", ES_ERROR );
  159. }
  160. else if (len >= 0) { // no vsprintf error? print buf.
  161. DrawText( -1, kFontJustLeft, x, y, buf );
  162. }
  163. return len;
  164. }
  165. int Font::Center( int x, int y, char *zFormat, ... )
  166. {
  167. int len;
  168. va_list argptr;
  169. char buf[ kFontMaxText + 1 ];
  170. // print variable argument string into buf for output
  171. va_start( argptr, zFormat );
  172. len = vsprintf( buf, zFormat, argptr );
  173. va_end( argptr );
  174. if (len >= kFontMaxText) { // buf overwritten? return error.
  175. ThrowError("Message length exceeded buffer length!", ES_ERROR );
  176. }
  177. else if (len >= 0) { // no vsprintf error? print buf.
  178. DrawText( -1, kFontJustCenter, x, y, buf );
  179. }
  180. return len;
  181. }
  182. int Font::Draw( int nTile, int x, int y, char *zFormat, ... )
  183. {
  184. int len;
  185. va_list argptr;
  186. char buf[ kFontMaxText + 1 ];
  187. // print variable argument string into buf for output
  188. va_start( argptr, zFormat );
  189. len = vsprintf( buf, zFormat, argptr );
  190. va_end( argptr );
  191. if (len >= kFontMaxText) { // buf overwritten? return error.
  192. ThrowError("Message length exceeded buffer length!", ES_ERROR );
  193. }
  194. else if (len >= 0) { // no vsprintf error? print buf.
  195. DrawText( nTile, kFontJustLeft, x, y, buf );
  196. }
  197. return len;
  198. }