X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=src%2Fvg%2Fvg_tex.h;h=0896b75de57bb021b936f7fe3f1f48e1d48e476c;hb=e997a6b7afe1fe2d7ee0abab2bfb5525417240b2;hp=7a434507ca7cb53541eb643c1a108521b1c03721;hpb=5071899d6840f99825d0eaab4132a0bf27646938;p=vg.git diff --git a/src/vg/vg_tex.h b/src/vg/vg_tex.h index 7a43450..0896b75 100644 --- a/src/vg/vg_tex.h +++ b/src/vg/vg_tex.h @@ -11,9 +11,20 @@ #define VG_TEXTURE_NEAREST 0x8 #define VG_TEXTURE_ALLOCATED_INTERNAL 0x10 -/* TODO: Update this implementation */ +VG_STATIC void *vg_qoi_malloc( size_t size ) +{ + return vg_linear_alloc( vg_mem.scratch, size ); +} + +VG_STATIC void vg_qoi_free( void *ptr ) +{ + +} + #define QOI_IMPLEMENTATION #define QOI_NO_STDIO +#define QOI_MALLOC(sz) vg_qoi_malloc( sz ) +#define QOI_FREE(p) vg_qoi_free( p ) #include "phoboslab/qoi.h" @@ -77,53 +88,67 @@ static inline void vg_tex2d_clamp(void) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); } -VG_STATIC GLuint vg_tex2d_rgba( const char *path ) +VG_STATIC GLuint vg_tex2d_new(void) { GLuint texture_name; glGenTextures( 1, &texture_name ); glBindTexture( GL_TEXTURE_2D, texture_name ); + return texture_name; +} + +VG_STATIC void vg_tex2d_set_error(void) +{ + u32 tex_err[4] = + { + 0xffff00ff, + 0xff000000, + 0xff000000, + 0xffff00ff + }; + + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, + 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_err ); +} + +VG_STATIC void vg_tex2d_qoi( void *mem, u32 size, const char *name ) +{ + qoi_desc info; + u8 *tex_buffer = qoi_decode( mem, size, &info, 4 ); + + if( tex_buffer ) + { + vg_info( "Texture decoded: [%u %u] %s\n", + info.width, info.height, name ); + + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, info.width, info.height, + 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_buffer ); + + QOI_FREE(tex_buffer); + } + else + { + vg_error( "File size: %u\n", size ); + vg_tex2d_set_error(); + } +} + +VG_STATIC GLuint vg_tex2d_rgba( const char *path ) +{ + GLuint texture_name = vg_tex2d_new(); + vg_linear_clear( vg_mem.scratch ); u32 size; void *file = vg_file_read( vg_mem.scratch, path, &size ); if( file ) { - qoi_desc info; - u8 *tex_buffer = qoi_decode( file, size, &info, 4 ); - - if( tex_buffer ) - { - vg_info( "Texture decoded: [%u %u] %s\n", - info.width, info.height, path ); - - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, info.width, info.height, - 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_buffer ); - - /* TODO: pass through linear_alloc function */ - QOI_FREE(tex_buffer); - } - else - { - vg_error( "File size: %u\n", size ); - goto temp_error; - } + vg_tex2d_qoi( file, size, path ); } else { -temp_error: vg_error( "Loading texture failed (%s)\n", path ); - - u32 tex_err[4] = - { - 0xffff00ff, - 0xff000000, - 0xff000000, - 0xffff00ff - }; - - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, - 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_err ); + vg_tex2d_set_error(); } return texture_name;