1
0

Mesh.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef SCUFENG_SHAPE_H_
  2. #define SCUFENG_SHAPE_H_
  3. #include <stdbool.h>
  4. #include <stddef.h>
  5. #include "cglm/types.h"
  6. #include "scufcont/DString.h"
  7. #include "scufeng/engine.h"
  8. #define SEMesh_string_c_str(mesh_ptr) SCDString_c_str(&mesh_ptr->string.text)
  9. typedef enum {
  10. SE_TRIANGLES,
  11. SE_TRIANGLE_STRIP,
  12. SE_TRIANGLE_FAN,
  13. SE_LINES,
  14. SE_LINE_STRIP
  15. } SEDrawMode;
  16. typedef struct {
  17. vec3 pos;
  18. vec3 scale;
  19. vec3 rotation;
  20. } SETransform;
  21. typedef struct {
  22. float pos[3];
  23. float tex_coords[2];
  24. } SEVertex;
  25. typedef enum {
  26. SE_MESH_COLOR,
  27. SE_MESH_TEXTURE,
  28. SE_MESH_STRING
  29. } SEMeshType;
  30. typedef struct {
  31. unsigned int id;
  32. float w;
  33. float h;
  34. } SEMeshTexture;
  35. typedef struct {
  36. SEMeshTexture texture;
  37. SCDString text;
  38. vec3 color;
  39. } SEMeshString;
  40. typedef struct {
  41. unsigned int vao;
  42. unsigned int vbo;
  43. size_t verts_n;
  44. int draw_mode;
  45. SETransform transform;
  46. SEMeshType type;
  47. union {
  48. SEMeshString string;
  49. SEMeshTexture texture;
  50. vec4 color;
  51. };
  52. } SEMesh;
  53. void SEMesh_free(SEMesh *const mesh);
  54. void SEMesh_set_draw_mode(SEMesh *mesh, const SEDrawMode mode);
  55. void SEMesh_render(const SEMesh *const mesh);
  56. void SEMesh_color_set(SEMesh *const mesh, const vec4 color);
  57. void SEMesh_rect_dims_get(const SEMesh *const mesh, vec2 dims);
  58. void SEMesh_scale_set_to_texture_size(SEMesh *const mesh);
  59. /**
  60. Set x or y to 0 to automatically calculate that value based on the
  61. w/h ratio of the rect's texture. Setting both to 0 will result in
  62. scale being { 0, 0 }.
  63. */
  64. void SEMesh_rect_textured_scale_set(SEMesh *const mesh, float x, float y);
  65. void SEMesh_textured_replace(
  66. SEMesh *const mesh,
  67. const unsigned int id,
  68. const float w,
  69. const float h
  70. );
  71. void SEMesh_line_segment_coords_set(SEMesh *const mesh, const vec2 a, const vec2 b);
  72. SEMesh SEMesh_line_segment_color_alloc(const vec2 a, const vec2 b, const vec4 color);
  73. void SEMesh_bbox_render_2d(const SEMesh *const mesh);
  74. void SEMesh_bbox_get_2d(
  75. const SEMesh *const mesh,
  76. float *const left,
  77. float *const top,
  78. float *const right,
  79. float *const bottom
  80. );
  81. /**
  82. @param bbox Returns the bbox in order: { left, top, right, bottom }.
  83. */
  84. void SEMesh_bbox_get_2d_v(const SEMesh *const mesh, vec4 bbox);
  85. bool SEMesh_rect_is_pos_inside(const SEMesh *const mesh, const float x, const float y);
  86. SEMesh SEMesh_rect_color_alloc(vec4 color);
  87. /**
  88. @return SE_OUT_OF_MEMORY
  89. @return SE_SUCCESS
  90. */
  91. SEResult SEMesh_string_alloc(
  92. SEMesh *const mesh,
  93. const char *text,
  94. const size_t text_len,
  95. const vec3 color
  96. );
  97. /**
  98. @return SE_OUT_OF_MEMORY
  99. @return SE_SUCCESS
  100. */
  101. SEResult SEMesh_string_text_replace(
  102. SEMesh *const mesh,
  103. const char *text,
  104. const size_t text_len,
  105. const vec3 color
  106. );
  107. void SEMesh_render_multiple_2d(SEMesh *const *const meshes, const size_t meshes_n);
  108. #endif // SCUFENG_SHAPE_H_