fonts.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #ifdef _arch_dreamcast
  2. #include "GL/glkos.h"
  3. #include "GL/glu.h"
  4. #include "GL/glext.h"
  5. #include "kos.h"
  6. #else
  7. #include "GL/gl.h"
  8. #endif
  9. #include "stdio.h"
  10. #include "dirent.h"
  11. #include "string.h"
  12. #include "../../include/base.h"
  13. const unsigned char font6x8_ascii[128][6] = {
  14. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x00
  15. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x01
  16. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x02
  17. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x03
  18. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x04
  19. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x05
  20. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x06
  21. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x07
  22. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x08
  23. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x09
  24. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x0A
  25. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x0B
  26. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x0C
  27. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x0D
  28. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x0E
  29. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x0F
  30. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x10
  31. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x11
  32. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x12
  33. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x13
  34. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x14
  35. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x15
  36. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x16
  37. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x17
  38. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x18
  39. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x19
  40. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x1A
  41. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x1B
  42. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x1C
  43. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x1D
  44. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x1E
  45. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x1F
  46. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x20 (space)
  47. {0x00, 0x00, 0x00, 0x2F, 0x00, 0x00}, // 0x21 (!)
  48. {0x00, 0x00, 0x03, 0x00, 0x03, 0x00}, // 0x22 (")
  49. {0x00, 0x14, 0x3E, 0x14, 0x3E, 0x14}, // 0x23 (#)
  50. {0x00, 0x04, 0x2A, 0x7F, 0x2A, 0x10}, // 0x24 ($)
  51. {0x00, 0x26, 0x10, 0x08, 0x04, 0x32}, // 0x25 (%)
  52. {0x00, 0x1A, 0x25, 0x29, 0x10, 0x28}, // 0x26 (&)
  53. {0x00, 0x00, 0x00, 0x03, 0x00, 0x00}, // 0x27 (')
  54. {0x00, 0x00, 0x3E, 0x41, 0x41, 0x00}, // 0x28 (()
  55. {0x00, 0x00, 0x41, 0x41, 0x3E, 0x00}, // 0x29 ())
  56. {0x00, 0x28, 0x18, 0x0E, 0x18, 0x28}, // 0x2A (*)
  57. {0x00, 0x08, 0x08, 0x3E, 0x08, 0x08}, // 0x2B (+)
  58. {0x00, 0x00, 0x20, 0x10, 0x00, 0x00}, // 0x2C (,)
  59. {0x00, 0x08, 0x08, 0x08, 0x08, 0x08}, // 0x2D (-)
  60. {0x00, 0x00, 0x00, 0x10, 0x00, 0x00}, // 0x2E (.)
  61. {0x00, 0x20, 0x10, 0x08, 0x04, 0x02}, // 0x2F (/)
  62. {0x00, 0x00, 0x1E, 0x21, 0x21, 0x1E}, // 0x30 (0)
  63. {0x00, 0x00, 0x00, 0x22, 0x3F, 0x20}, // 0x31 (1)
  64. {0x00, 0x00, 0x22, 0x31, 0x29, 0x26}, // 0x32 (2)
  65. {0x00, 0x00, 0x12, 0x21, 0x25, 0x1A}, // 0x33 (3)
  66. {0x00, 0x00, 0x0C, 0x0A, 0x3F, 0x08}, // 0x34 (4)
  67. {0x00, 0x00, 0x17, 0x25, 0x25, 0x19}, // 0x35 (5)
  68. {0x00, 0x00, 0x1C, 0x26, 0x25, 0x18}, // 0x36 (6)
  69. {0x00, 0x00, 0x01, 0x39, 0x05, 0x03}, // 0x37 (7)
  70. {0x00, 0x00, 0x1A, 0x25, 0x25, 0x1A}, // 0x38 (8)
  71. {0x00, 0x00, 0x06, 0x09, 0x29, 0x1E}, // 0x39 (9)
  72. {0x00, 0x00, 0x00, 0x14, 0x00, 0x00}, // 0x3A (:)
  73. {0x00, 0x00, 0x20, 0x14, 0x00, 0x00}, // 0x3B (;)
  74. {0x00, 0x00, 0x08, 0x14, 0x22, 0x00}, // 0x3C (<)
  75. {0x00, 0x00, 0x14, 0x14, 0x14, 0x00}, // 0x3D (=)
  76. {0x00, 0x00, 0x22, 0x14, 0x08, 0x00}, // 0x3E (>)
  77. {0x00, 0x02, 0x01, 0x29, 0x06, 0x00}, // 0x3F (?)
  78. {0x00, 0x1E, 0x21, 0x2D, 0x2E, 0x00}, // 0x40 (@)
  79. {0x00, 0x30, 0x0C, 0x0B, 0x0C, 0x30}, // 0x41 (A)
  80. {0x00, 0x00, 0x3F, 0x25, 0x25, 0x1A}, // 0x42 (B)
  81. {0x00, 0x00, 0x1E, 0x21, 0x21, 0x12}, // 0x43 (C)
  82. {0x00, 0x00, 0x3F, 0x21, 0x21, 0x1E}, // 0x44 (D)
  83. {0x00, 0x00, 0x3F, 0x25, 0x25, 0x25}, // 0x45 (E)
  84. {0x00, 0x00, 0x3F, 0x05, 0x05, 0x05}, // 0x46 (F)
  85. {0x00, 0x00, 0x1E, 0x21, 0x29, 0x1A}, // 0x47 (G)
  86. {0x00, 0x00, 0x3F, 0x04, 0x04, 0x3F}, // 0x48 (H)
  87. {0x00, 0x00, 0x21, 0x3F, 0x21, 0x00}, // 0x49 (I)
  88. {0x00, 0x18, 0x20, 0x21, 0x1F, 0x00}, // 0x4A (J)
  89. {0x00, 0x00, 0x3F, 0x0C, 0x12, 0x21}, // 0x4B (K)
  90. {0x00, 0x00, 0x3F, 0x20, 0x20, 0x20}, // 0x4C (L)
  91. {0x00, 0x3F, 0x06, 0x08, 0x06, 0x3F}, // 0x4D (M)
  92. {0x00, 0x00, 0x3F, 0x06, 0x18, 0x3F}, // 0x4E (N)
  93. {0x00, 0x00, 0x1E, 0x21, 0x21, 0x1E}, // 0x4F (O)
  94. {0x00, 0x00, 0x3F, 0x09, 0x09, 0x06}, // 0x50 (P)
  95. {0x00, 0x1E, 0x21, 0x21, 0x5E, 0x00}, // 0x51 (Q)
  96. {0x00, 0x00, 0x3F, 0x09, 0x19, 0x26}, // 0x52 (R)
  97. {0x00, 0x00, 0x12, 0x25, 0x29, 0x12}, // 0x53 (S)
  98. {0x00, 0x01, 0x01, 0x3F, 0x01, 0x01}, // 0x54 (T)
  99. {0x00, 0x1F, 0x20, 0x20, 0x20, 0x1F}, // 0x55 (U)
  100. {0x00, 0x0F, 0x10, 0x20, 0x10, 0x0F}, // 0x56 (V)
  101. {0x00, 0x1F, 0x20, 0x1C, 0x20, 0x1F}, // 0x57 (W)
  102. {0x00, 0x21, 0x12, 0x0C, 0x12, 0x21}, // 0x58 (X)
  103. {0x00, 0x03, 0x04, 0x38, 0x04, 0x03}, // 0x59 (Y)
  104. {0x00, 0x00, 0x31, 0x29, 0x25, 0x23}, // 0x5A (Z)
  105. {0x00, 0x00, 0x3F, 0x21, 0x21, 0x00}, // 0x5B ([)
  106. {0x00, 0x02, 0x04, 0x08, 0x10, 0x20}, // 0x5C (\)
  107. {0x00, 0x00, 0x21, 0x21, 0x3F, 0x00}, // 0x5D (])
  108. {0x00, 0x04, 0x02, 0x01, 0x02, 0x04}, // 0x5E (^)
  109. {0x00, 0x40, 0x40, 0x40, 0x40, 0x40}, // 0x5F (_)
  110. {0x00, 0x00, 0x00, 0x01, 0x02, 0x00}, // 0x60 (`)
  111. {0x00, 0x18, 0x24, 0x24, 0x18, 0x20}, // 0x61 (a)
  112. {0x00, 0x00, 0x3F, 0x24, 0x24, 0x18}, // 0x62 (b)
  113. {0x00, 0x00, 0x18, 0x24, 0x24, 0x00}, // 0x63 (c)
  114. {0x00, 0x00, 0x18, 0x24, 0x24, 0x3F}, // 0x64 (d)
  115. {0x00, 0x00, 0x1C, 0x2A, 0x2A, 0x0C}, // 0x65 (e)
  116. {0x00, 0x04, 0x04, 0x3E, 0x05, 0x05}, // 0x66 (f)
  117. {0x00, 0x00, 0x0C, 0x52, 0x52, 0x3E}, // 0x67 (g)
  118. {0x00, 0x00, 0x3F, 0x04, 0x04, 0x38}, // 0x68 (h)
  119. {0x00, 0x00, 0x24, 0x3D, 0x20, 0x00}, // 0x69 (i)
  120. {0x00, 0x20, 0x40, 0x44, 0x3D, 0x00}, // 0x6A (j)
  121. {0x00, 0x00, 0x3F, 0x08, 0x14, 0x22}, // 0x6B (k)
  122. {0x00, 0x00, 0x1F, 0x20, 0x20, 0x10}, // 0x6C (l)
  123. {0x00, 0x3E, 0x04, 0x18, 0x04, 0x3E}, // 0x6D (m)
  124. {0x00, 0x00, 0x3C, 0x02, 0x02, 0x3C}, // 0x6E (n)
  125. {0x00, 0x00, 0x1C, 0x22, 0x22, 0x1C}, // 0x6F (o)
  126. {0x00, 0x00, 0x7E, 0x12, 0x12, 0x0C}, // 0x70 (p)
  127. {0x00, 0x00, 0x0C, 0x12, 0x12, 0x7E}, // 0x71 (q)
  128. {0x00, 0x00, 0x04, 0x38, 0x04, 0x04}, // 0x72 (r)
  129. {0x00, 0x00, 0x04, 0x2A, 0x2A, 0x10}, // 0x73 (s)
  130. {0x00, 0x00, 0x04, 0x1E, 0x24, 0x10}, // 0x74 (t)
  131. {0x00, 0x1C, 0x20, 0x20, 0x20, 0x1C}, // 0x75 (u)
  132. {0x00, 0x0C, 0x10, 0x20, 0x10, 0x0C}, // 0x76 (v)
  133. {0x00, 0x1C, 0x20, 0x18, 0x20, 0x1C}, // 0x77 (w)
  134. {0x00, 0x22, 0x14, 0x08, 0x14, 0x22}, // 0x78 (x)
  135. {0x00, 0x04, 0x48, 0x30, 0x08, 0x04}, // 0x79 (y)
  136. {0x00, 0x00, 0x24, 0x34, 0x2C, 0x24}, // 0x7A (z)
  137. {0x00, 0x08, 0x08, 0x36, 0x41, 0x41}, // 0x7B ({)
  138. {0x00, 0x00, 0x00, 0x7F, 0x00, 0x00}, // 0x7C (|)
  139. {0x00, 0x41, 0x41, 0x36, 0x08, 0x08}, // 0x7D (})
  140. {0x00, 0x08, 0x04, 0x08, 0x10, 0x08}, // 0x7E (~)
  141. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 0x7F
  142. };
  143. void DrawText(const char* text, float x, float y, float scale, Color color) {
  144. if (!text) return;
  145. glPushMatrix();
  146. glTranslatef(x, y, 0.0f);
  147. glScalef(scale, scale, 1.0f);
  148. glColor4f(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f);
  149. glDisable(GL_TEXTURE_2D);
  150. const float charWidth = 6.0f;
  151. const float charHeight = 8.0f;
  152. const float lineHeight = charHeight * 1.0f;
  153. float startX = 0.0f;
  154. float currentY = 0.0f;
  155. for (const char* c = text; *c; c++) {
  156. if (*c == '\n') {
  157. glPopMatrix();
  158. glPushMatrix();
  159. glTranslatef(x, y - currentY - lineHeight, 0.0f);
  160. glScalef(scale, scale, 1.0f);
  161. currentY -= lineHeight;
  162. startX = 0.0f;
  163. continue;
  164. }
  165. if (*c < 0 || *c >= 128) {
  166. continue;
  167. }
  168. const unsigned char* bitmap = font6x8_ascii[(int)*c];
  169. for (int row = 0; row < charHeight; row++) {
  170. for (int col = 0; col < charWidth; col++) {
  171. if (bitmap[col] & (1 << row)) {
  172. glBegin(GL_QUADS);
  173. glVertex2f(startX + col, currentY - row);
  174. glVertex2f(startX + col + 1, currentY - row);
  175. glVertex2f(startX + col + 1, currentY - row + 1);
  176. glVertex2f(startX + col, currentY - row + 1);
  177. glEnd();
  178. }
  179. }
  180. }
  181. startX += charWidth + 1;
  182. }
  183. glPopMatrix();
  184. }
  185. #include "../../include/textures.h"
  186. Texture fontTextures[256];
  187. void LoadFont(char* pathToFolder) {
  188. DIR *fontFolder;
  189. struct dirent *entry;
  190. printf("%s\n", "loading font");
  191. fontFolder = opendir(pathToFolder);
  192. if (fontFolder == NULL) {
  193. perror("directory cant be opened");
  194. return;
  195. }
  196. int currentSymbol = 0;
  197. while ((entry = readdir(fontFolder)) != NULL) {
  198. if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
  199. continue;
  200. }
  201. char fullPath[512];
  202. strcpy(fullPath, pathToFolder);
  203. if (fullPath[strlen(fullPath)-1] != '/') {
  204. strcat(fullPath, "/");
  205. }
  206. strcat(fullPath, entry->d_name);
  207. printf("%s\n", fullPath);
  208. fontTextures[currentSymbol] = LoadTexture(fullPath);
  209. currentSymbol++;
  210. }
  211. closedir(fontFolder);
  212. return;
  213. }
  214. void DrawTextWithFont(unsigned char* text, int x, int y, float fontSize, Color color) {
  215. unsigned int currentSymbol = 0;
  216. unsigned int currentX = x;
  217. while (text[currentSymbol] != '\0') {
  218. DrawTexture(fontTextures[text[currentSymbol]], currentX, y, fontSize, color);
  219. currentX = currentX + fontTextures[text[currentSymbol]].width*fontSize;
  220. currentSymbol++;
  221. }
  222. }
  223. void UnloadFont() {
  224. int currentTexture = 0;
  225. while (currentTexture < 256) {
  226. UnloadTexture(fontTextures[currentTexture]);
  227. currentTexture++;
  228. }
  229. }