text.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef SCUFENG_TEXT_H_
  2. #define SCUFENG_TEXT_H_
  3. #include <stdint.h>
  4. #include "cglm/types.h"
  5. #include "scufeng/engine.h"
  6. #include "scufeng/Mesh.h"
  7. #include "scufcont/DString.h"
  8. #include "scufcont/DArray.h"
  9. #define SETextInput_pos(ti) (ti.bg_mesh.transform.pos)
  10. #define SETextInput_scale(ti) (ti.bg_mesh.transform.scale)
  11. typedef struct {
  12. unsigned int textures[128];
  13. ivec2 sizes[128];
  14. ivec2 bearings[128];
  15. unsigned int advances[128];
  16. int size_x_max;
  17. int size_y_max;
  18. int bearing_y_max;
  19. } SEFont;
  20. SCDARRAY_HEADER(SEFont, SEFont)
  21. typedef struct {
  22. bool active;
  23. size_t cursor;
  24. SEMesh cursor_mesh;
  25. SEMesh bg_mesh;
  26. SCDString text;
  27. vec3 text_color;
  28. } SETextInput;
  29. SEResult se_font_load(
  30. const char *path,
  31. const unsigned int pixel_w,
  32. const unsigned int pixel_h,
  33. size_t *const font_id
  34. );
  35. void se_font_set(const size_t font_id);
  36. SEFont *se_font_current_get();
  37. size_t se_font_current_get_id();
  38. /**
  39. @param x Left edge of text.
  40. @param y Bottom of text. For example, the bottom edge of a '_' character is at y.
  41. @param h Height scale. Values above font.size_y_max scale up, values below it scale down. Set to 0 or font.size_y_max for no scaling.
  42. */
  43. int se_text_render(
  44. const char *text,
  45. unsigned int x,
  46. unsigned int y,
  47. unsigned int h,
  48. const vec3 color
  49. );
  50. SETextInput SETextInput_alloc(
  51. const char *text,
  52. const size_t text_len,
  53. vec3 color,
  54. vec4 bg_color
  55. );
  56. void SETextInput_free(SETextInput *const ti);
  57. void SETextInput_scale_set(SETextInput *const ti, const vec2 dims);
  58. void SETextInput_pos_set(SETextInput *const ti, const vec2 pos);
  59. void SETextInput_color_set(SETextInput *const ti, const vec3 color);
  60. void SETextInput_bg_color_set(SETextInput *const ti, const vec4 color);
  61. SCDString *SETextInput_string_get_ref(SETextInput *const ti);
  62. void SETextInput_deactivate();
  63. void SETextInput_activate(SETextInput *const ti);
  64. void SETextInput_bbox_get(
  65. const SETextInput *const ti,
  66. float *const left,
  67. float *const top,
  68. float *const right,
  69. float *const bottom
  70. );
  71. void SETextInput_bbox_get_v(const SETextInput *const ti, vec4 bbox);
  72. SEResult SETextInput_text_copy(const SETextInput *const ti, SCDString *const dst);
  73. const char *SETextInput_c_str(const SETextInput *const ti);
  74. SEResult SETextInput_char_insert_at_cursor(SETextInput *const ti, const char ch);
  75. void SETextInput_char_remove_at_cursor(SETextInput *const ti);
  76. bool SETextInput_is_pos_inside(const SETextInput *const ti, const float x, const float y);
  77. void SETextInput_render_2d(SETextInput *const ti);
  78. void SETextInput_render_multiple_2d(SETextInput *const *const tis, const size_t ti_count);
  79. #endif // SCUFENG_TEXT_H_