| 3276 | } |
| 3277 | |
| 3278 | void lf_image_render(vec2s pos, LfColor color, LfTexture tex, LfColor border_color, float border_width, float corner_radius) { |
| 3279 | if(!state.renderer_render) return; |
| 3280 | if(item_should_cull((LfAABB){.pos = pos, .size = (vec2s){tex.width, tex.height}})) { |
| 3281 | return; |
| 3282 | } |
| 3283 | if(state.render.tex_count - 1 >= MAX_TEX_COUNT_BATCH - 1) { |
| 3284 | renderer_flush(); |
| 3285 | renderer_begin(); |
| 3286 | } |
| 3287 | // Offsetting the postion, so that pos is the top left of the rendered object |
| 3288 | vec2s pos_initial = pos; |
| 3289 | pos = (vec2s){pos.x + tex.width / 2.0f, pos.y + tex.height / 2.0f}; |
| 3290 | |
| 3291 | if(state.image_color_stack.a != 0.0) { |
| 3292 | color = state.image_color_stack; |
| 3293 | } |
| 3294 | // Initializing texture coords data |
| 3295 | vec2s texcoords[4] = { |
| 3296 | (vec2s){0.0f, 0.0f}, |
| 3297 | (vec2s){1.0f, 0.0f}, |
| 3298 | (vec2s){1.0f, 1.0f}, |
| 3299 | (vec2s){0.0f, 1.0f}, |
| 3300 | }; |
| 3301 | // Retrieving the texture index of the rendered texture |
| 3302 | float tex_index = -1.0f; |
| 3303 | for(uint32_t i = 0; i < state.render.tex_count; i++) { |
| 3304 | if(tex.id == state.render.textures[i].id) { |
| 3305 | tex_index = i; |
| 3306 | break; |
| 3307 | } |
| 3308 | } |
| 3309 | if(tex_index == -1.0f) { |
| 3310 | tex_index = (float)state.render.tex_index; |
| 3311 | state.render.textures[state.render.tex_count++] = tex; |
| 3312 | state.render.tex_index++; |
| 3313 | } |
| 3314 | // Calculating the transform |
| 3315 | mat4 translate = GLM_MAT4_IDENTITY_INIT; |
| 3316 | mat4 scale = GLM_MAT4_IDENTITY_INIT; |
| 3317 | mat4 transform = GLM_MAT4_IDENTITY_INIT; |
| 3318 | vec3s pos_xyz = (vec3s){pos.x, pos.y, 0.0f}; |
| 3319 | vec3 tex_size; |
| 3320 | tex_size[0] = tex.width; |
| 3321 | tex_size[1] = tex.height; |
| 3322 | tex_size[2] = 0; |
| 3323 | glm_translate_make(translate, pos_xyz.raw); |
| 3324 | glm_scale_make(scale, tex_size); |
| 3325 | glm_mat4_mul(translate,scale,transform); |
| 3326 | |
| 3327 | // Adding the vertices to the batch renderer |
| 3328 | for(uint32_t i = 0; i < 4; i++) { |
| 3329 | if(state.render.vert_count >= MAX_RENDER_BATCH) { |
| 3330 | renderer_flush(); |
| 3331 | renderer_begin(); |
| 3332 | } |
| 3333 | vec4 result; |
| 3334 | glm_mat4_mulv(transform, state.render.vert_pos[i].raw, result); |
| 3335 | memcpy(state.render.verts[state.render.vert_count].pos, result, sizeof(vec2)); |
no test coverage detected