text.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <stdlib.h>
  4. #include "gfx/SDL_easy.h"
  5. #include "gfx/text.h"
  6. #include "gfx/font.h"
  7. bool __create_text(text_t *t, font_t *font, int x, int y, Colour colour, const char *text)
  8. {
  9. t->font = font;
  10. t->colour = colour;
  11. t->rect.x = x;
  12. t->rect.y = y;
  13. SDL_Colour col = SDL_GetColour(colour);
  14. SDL_Surface *surface = TTF_RenderUTF8_Blended(font->fnt, text, col);
  15. if (!surface)
  16. {
  17. return false;
  18. }
  19. t->tex = SDL_CreateTextureFromSurface(SDL_GetRenderer(SDL_GetWindow()), surface);
  20. SDL_FreeSurface(surface);
  21. if (!t->tex)
  22. {
  23. return false;
  24. }
  25. SDL_QueryTexture(t->tex, NULL, NULL, &t->rect.w, &t->rect.h);
  26. return true;
  27. }
  28. text_t *create_text(font_t *font, int x, int y, Colour colour, const char *text, ...)
  29. {
  30. text_t *t = calloc(1, sizeof(text_t));
  31. char full_text[0x100];
  32. va_list argv;
  33. va_start(argv, text);
  34. vsnprintf(full_text, sizeof(full_text), text, argv);
  35. va_end(argv);
  36. return __create_text(t, font, x, y, colour, full_text) ? t : NULL;
  37. }
  38. void enable_text_clip(text_t *text, int start_x, int end_x)
  39. {
  40. if (!text) return;
  41. text->clip.enabled = true;
  42. text->clip.start_x = start_x;
  43. text->clip.end_x = end_x;
  44. }
  45. void disable_text_clip(text_t *text)
  46. {
  47. if (!text) return;
  48. text->clip.enabled = false;
  49. }
  50. void draw_text(text_t *text)
  51. {
  52. if (!text) return;
  53. SDL_Rect clip = { 0, 0, text->rect.w, text->rect.h };
  54. SDL_Rect dest = { text->rect.x, text->rect.y, text->rect.w, text->rect.h };
  55. if (text->clip.enabled)
  56. {
  57. clip.x = text->clip.start_x;
  58. clip.w = text->clip.end_x;
  59. dest.w = dest.w < text->clip.end_x ? dest.w : text->clip.end_x;
  60. }
  61. SDL_RenderCopy(SDL_GetRenderer(SDL_GetWindow()), text->tex, &clip, &dest);
  62. }
  63. void draw_text_position(text_t *text, int x, int y)
  64. {
  65. if (!text) return;
  66. SDL_Rect clip = { 0, 0, text->rect.w, text->rect.h };
  67. SDL_Rect dest = { x, y, text->rect.w, text->rect.h };
  68. if (text->clip.enabled)
  69. {
  70. clip.x = text->clip.start_x;
  71. clip.w = text->clip.end_x;
  72. dest.w = dest.w < text->clip.end_x ? dest.w : text->clip.end_x;
  73. }
  74. SDL_RenderCopy(SDL_GetRenderer(SDL_GetWindow()), text->tex, &clip, &dest);
  75. }
  76. void draw_text_right_align(text_t *text, int end_x, int y)
  77. {
  78. if (!text) return;
  79. SDL_Rect pos = { end_x - text->rect.w, y, text->rect.w, text->rect.h };
  80. SDL_Rect clip = { 0, 0, text->rect.w, text->rect.h };
  81. if (text->clip.enabled)
  82. {
  83. clip.x = text->clip.start_x;
  84. clip.w = text->clip.end_x;
  85. }
  86. SDL_RenderCopy(SDL_GetRenderer(SDL_GetWindow()), text->tex, &clip, &pos);
  87. }
  88. void draw_text_scale(text_t *text, int w, int h)
  89. {
  90. if (!text) return;
  91. SDL_Rect pos = { text->rect.x, text->rect.y, w, h };
  92. SDL_Rect clip = { 0, 0, text->rect.w, text->rect.h };
  93. if (text->clip.enabled)
  94. {
  95. clip.x = text->clip.start_x;
  96. clip.w = text->clip.end_x;
  97. }
  98. SDL_RenderCopy(SDL_GetRenderer(SDL_GetWindow()), text->tex, &clip, &pos);
  99. }
  100. void draw_text_set(text_t *text, int x, int y, int w, int h)
  101. {
  102. if (!text) return;
  103. text->rect.x = x;
  104. text->rect.y = y;
  105. text->rect.w = w;
  106. text->rect.h = h;
  107. draw_text(text);
  108. }
  109. void position_text(text_t *text, int x, int y)
  110. {
  111. if (!text) return;
  112. text->rect.x = x;
  113. text->rect.y = y;
  114. }
  115. void set_text()
  116. {}
  117. void update_text(text_t *t, const char *text, ...)
  118. {
  119. if (!t) return;
  120. SDL_DestroyTexture(t->tex);
  121. t->tex = NULL;
  122. char full_text[0x100];
  123. va_list argv;
  124. va_start(argv, text);
  125. vsnprintf(full_text, sizeof(full_text), text, argv);
  126. va_end(argv);
  127. __create_text(t, t->font, t->rect.x, t->rect.y, t->colour, full_text);
  128. }
  129. void free_text(text_t *t)
  130. {
  131. if (!t)
  132. return;
  133. if (t->tex)
  134. SDL_DestroyTexture(t->tex);
  135. free(t);
  136. }