util_tex_load.c (1269B)
1 ////////////////////////////////////////////////////////////////// 2 // util_tex_load.c 3 4 #define STB_IMAGE_IMPLEMENTATION 5 #define STB_IMAGE_STATIC 6 #include "../external/stb_image.h" 7 8 RV_GLOBAL rv_texture_t* rv_tex_load_from_file(rv_arena* arena, rv_str8 filename, bool32 vertical_flip_on_load) 9 { 10 rv_temp_arena scratch = rv_scratch_begin(0, 0); 11 rv_str8 f = rv_read_file(scratch.arena, filename); 12 rv_texture_t* res = rv_tex_load_from_memory(arena, f, vertical_flip_on_load); 13 rv_scratch_end(scratch); 14 15 return res; 16 } 17 18 RV_GLOBAL rv_texture_t* rv_tex_load_from_memory(rv_arena* arena, rv_str8 memory, bool32 vertical_flip_on_load) 19 { 20 rv_texture_t* res = rv_push(arena, rv_texture_t); 21 22 s32 num_comps, width, height; 23 stbi_set_flip_vertically_on_load(vertical_flip_on_load); 24 void* data = stbi_load_from_memory((const stbi_uc*)memory.str, memory.len, &width, &height, &num_comps, STBI_rgb_alpha); 25 26 if (data) { 27 res->size = rv_v2(width, height); 28 res->data = rv_mem_copy(rv_arena_push(arena, 4 * width * height, 8), data, 4 * width * height); 29 stbi_image_free(data); 30 } else { 31 // set default texture... 32 rv_unreachable(); 33 rv_get_default_texture(&res->data, &res->size); 34 } 35 36 return res; 37 }