zori_caption.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "zori.h"
  2. #include "zori_caption.h"
  3. #include "monolog.h"
  4. struct zori_caption *
  5. zori_caption_set(struct zori_caption * caption, const zori_string * text) {
  6. if (caption) {
  7. if (caption->text) {
  8. ustr_free(caption->text);
  9. caption->text = NULL;
  10. }
  11. if (text) {
  12. caption->text = ustr_dup(text);
  13. if (!caption->text) {
  14. LOG_ERROR("Out of memory in caption setup.");
  15. }
  16. }
  17. }
  18. return caption;
  19. }
  20. struct zori_caption *
  21. zori_caption_set_cstr(struct zori_caption * caption, const char * cstr) {
  22. const USTR * ustr;
  23. USTR_INFO info;
  24. ustr = ustr_refcstr(&info, cstr);
  25. return zori_caption_set(caption, ustr);
  26. }
  27. struct zori_caption *
  28. zori_caption_init(struct zori_caption * caption, const char * cstr) {
  29. caption->text = NULL;
  30. return zori_caption_set_cstr(caption, cstr);
  31. }
  32. void
  33. zori_caption_draw(
  34. const struct zori_caption * caption,
  35. const zori_rebox * box,
  36. const struct zori_style * style) {
  37. if (caption->text) {
  38. zori_font * font = style->text.font;
  39. zori_color color = style->text.color;
  40. float x = box->at.x;
  41. float y = box->at.y;
  42. float w = box->size.x;
  43. if (style->text.flags & ZORI_FONT_ALIGN_CENTRE) {
  44. x = x + (w / 2);
  45. }
  46. if (style->text.flags & ZORI_FONT_ALIGN_RIGHT) {
  47. x = x + w;
  48. }
  49. al_draw_multiline_ustr(font, color, x, y, w, -1, style->text.flags,
  50. caption->text);
  51. }
  52. }
  53. void zori_caption_done(struct zori_caption * caption) {
  54. zori_caption_set(caption, NULL);
  55. }