util_idraw2d.h (4187B)
1 ////////////////////////////////////////////////////////////////// 2 // util_idraw2d.h 3 // 4 // TODO(Samdal): 5 // Implement line drawing 6 // Implement circle drawing 7 8 ////////////////////////////////////////////////////////////////// 9 // types 10 11 typedef struct { 12 rv_arena* permanent_arena; 13 rv_arena* arena; 14 15 // gpu objects 16 rv_vbo_t* vbo; 17 rv_ibo_t* ibo; 18 rv_pipeline_t* pip; 19 20 rv_command_list_node_t* default_binds; 21 22 rv_uniform_t* u_resolution; 23 rv_uniform_t* u_tex; 24 rv_uniform_t* u_omit_tex; 25 rv_vec2 u_resolution_last; 26 rv_texture_t* u_tex_last; 27 f32 u_omit_tex_last; 28 29 // state 30 rv_bump_cache_ctx_t vertex; 31 rv_bump_cache_ctx_t index; 32 rv_command_list_t commands; 33 rv_command_t* last_commited_command; 34 rv_texture_t* last_tex; 35 36 rv_command_list_node_t* custom_binds_stack; 37 rv_command_list_node_t* last_bind; 38 } rv_idraw2d_ctx; 39 40 ////////////////////////////////////////////////////////////////// 41 // init 42 43 RV_GLOBAL void rvi2d_create(rv_idraw2d_ctx* ctx, rv_arena* arena, rv_command_list_t* create_commands); 44 RV_GLOBAL void rvi2d_destroy(rv_idraw2d_ctx* ctx, rv_arena* arena, rv_command_list_t* destroy_commands); 45 46 ////////////////////////////////////////////////////////////////// 47 // draw 48 49 RV_GLOBAL rv_command_t* rvi2d_draw_and_reset(rv_idraw2d_ctx* ctx, rv_arena* arena, rv_command_list_t* render_commands); 50 51 ////////////////////////////////////////////////////////////////// 52 // core update functions 53 // 54 // NOTE(Samdal): 55 // rvi2d_camera, rvi2d_tex and rvi2d_custom_bind(_clear) invalidates instancing 56 // 57 // NOTE(Samdal): 58 // fixed vertex layout ( you can add more binds if you want ): 59 // layout(location = 0) in vec2 a_pos; (bind index 0) 60 // layout(location = 1) in vec2 a_uv; (bind index 0) 61 // layout(location = 2) in vec4 a_color; (bind index 0) 62 // 63 // NOTE(Samdal): 64 // These functions return the first command if there was a break in the instancing. 65 // Otherwise they return NULL. 66 67 RV_GLOBAL rv_command_t* rvi2d_break(rv_idraw2d_ctx* ctx); 68 RV_GLOBAL rv_command_t* rvi2d_camera(rv_idraw2d_ctx* ctx, rv_vec2 screen_size); 69 RV_GLOBAL rv_command_t* rvi2d_tex(rv_idraw2d_ctx* ctx, rv_texture_t* tex); 70 RV_GLOBAL rv_command_t* rvi2d_tex_ex(rv_idraw2d_ctx* ctx, rv_texture_t* tex, bool32 is_red_channel); 71 RV_GLOBAL rv_command_t* rvi2d_custom_binds_push(rv_idraw2d_ctx* ctx, rv_command_list_t* copy_binds); 72 RV_GLOBAL rv_command_t* rvi2d_custom_binds_pop(rv_idraw2d_ctx* ctx); 73 RV_GLOBAL void rvi2d_triangle(rv_idraw2d_ctx* ctx, 74 rv_vec2 pos_a, rv_vec2 pos_b, rv_vec2 pos_c, 75 rv_vec2 uv_a, rv_vec2 uv_b, rv_vec2 uv_c, 76 rv_color_t color); 77 RV_GLOBAL void rvi2d_quad(rv_idraw2d_ctx* ctx, 78 rv_vec2 pos_tl, rv_vec2 pos_tr, rv_vec2 pos_bl, rv_vec2 pos_br, 79 rv_vec2 uv_tl, rv_vec2 uv_tr, rv_vec2 uv_bl, rv_vec2 uv_br, 80 rv_color_t color); 81 82 ////////////////////////////////////////////////////////////////// 83 // helpers functions 84 85 #define rvi2d_triangle_no_uv(ctx, pos_a, pos_b, pos_c, color) \ 86 rvi2d_triangle(ctx, pos_a, pos_b, pos_c, rv_v2(0,0), rv_v2(0,0), rv_v2(0,0), color) 87 #define rvi2d_quad_default_uv(ctx, pos_tl, pos_tr, pos_bl, pos_br, color)\ 88 rvi2d_quad(ctx, pos_tl, pos_tr, pos_bl, pos_br,\ 89 rv_v2(1,0), rv_v2(1,1), rv_v2(0,0), rv_v2(0,1),\ 90 color) 91 #define rvi2d_quad_yflipped_uv(ctx, pos_tl, pos_tr, pos_bl, pos_br, color)\ 92 rvi2d_quad(ctx, pos_tl, pos_tr, pos_bl, pos_br,\ 93 rv_v2(0,0), rv_v2(0,1), rv_v2(1,0), rv_v2(1,1),\ 94 color) 95 96 RV_GLOBAL void rvi2d_rect(rv_idraw2d_ctx* ctx, rv_rect rect, rv_color_t color); 97 RV_GLOBAL void rvi2d_line(rv_idraw2d_ctx* ctx, rv_vec2 a, rv_vec2 b, f32 thickness, rv_color_t color); 98 RV_GLOBAL void rvi2d_circle(rv_idraw2d_ctx* ctx, rv_vec2 center, f32 radius, s32 segments); 99 100 // remember to unbind font texture once you're done 101 RV_GLOBAL rv_command_t* rvi2d_text(rv_idraw2d_ctx* ctx, rv_ascii_font_t* font, rv_vec2 bl_first, rv_str8 str, rv_color_t color);