X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=src%2Fvg%2Fvg_tex.h;h=0896b75de57bb021b936f7fe3f1f48e1d48e476c;hb=3dd767bb10e6fee9cbffeb185d1a9685810c17b5;hp=33e074ef59b6736f8f45186028e8a4d78a8d6c56;hpb=95c9a820474d4974ee394f2fcfae82fbfa367dbd;p=vg.git diff --git a/src/vg/vg_tex.h b/src/vg/vg_tex.h index 33e074e..0896b75 100644 --- a/src/vg/vg_tex.h +++ b/src/vg/vg_tex.h @@ -1,9 +1,32 @@ /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ +#ifndef VG_TEX_H +#define VG_TEX_H + +#include "vg/vg.h" +#include "vg/vg_log.h" #define VG_TEXTURE_NO_MIP 0x1 #define VG_TEXTURE_REPEAT 0x2 #define VG_TEXTURE_CLAMP 0x4 #define VG_TEXTURE_NEAREST 0x8 +#define VG_TEXTURE_ALLOCATED_INTERNAL 0x10 + +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" struct vg_tex2d { @@ -17,8 +40,14 @@ struct vg_sprite v4f uv_xywh; }; -static void vg_tex2d_bind( vg_tex2d *tex, u32 id ) +VG_STATIC void vg_tex2d_bind( vg_tex2d *tex, u32 id ) { + if( !(tex->flags & VG_TEXTURE_ALLOCATED_INTERNAL) ) + { + vg_error( "Tried to use '%s' while unloaded!\n", tex->path ); + return; + } + glActiveTexture( GL_TEXTURE0 + id ); glBindTexture( GL_TEXTURE_2D, tex->name ); } @@ -59,47 +88,75 @@ static inline void vg_tex2d_clamp(void) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); } -static GLuint vg_tex2d_rgba( const char *path ) +VG_STATIC GLuint vg_tex2d_new(void) { - i64 length; - u8 *src_data = vg_asset_read_s( path, &length ); - GLuint texture_name; glGenTextures( 1, &texture_name ); glBindTexture( GL_TEXTURE_2D, texture_name ); - - if( src_data ) - { - qoi_desc info; - u8 *tex_buffer = qoi_decode( src_data, length, &info, 4 ); - - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, info.width, info.height, - 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_buffer ); - - free( tex_buffer ); - free( src_data ); - } - else - { - u32 tex_err[4] = - { - 0xffff00ff, - 0xff000000, - 0xff000000, - 0xffff00ff - }; - + + 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 ) + { + vg_tex2d_qoi( file, size, path ); + } + else + { vg_error( "Loading texture failed (%s)\n", path ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, - 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_err ); - } - + vg_tex2d_set_error(); + } + return texture_name; } -static void vg_tex2d_init( vg_tex2d *textures[], int num ) +VG_STATIC void vg_tex2d_init( vg_tex2d *textures[], int num ) { - for( int i = 0; i < num; i ++ ) + for( int i=0; iname = vg_tex2d_rgba( tex->path ); @@ -125,13 +182,17 @@ static void vg_tex2d_init( vg_tex2d *textures[], int num ) vg_tex2d_clamp(); else vg_tex2d_repeat(); + + tex->flags |= VG_TEXTURE_ALLOCATED_INTERNAL; } } -static void vg_tex2d_free( vg_tex2d *textures[], int num ) +VG_STATIC void vg_tex2d_free( vg_tex2d *textures[], int num ) { for( int i = 0; i < num; i ++ ) { glDeleteTextures( 1, &textures[i]->name ); } } + +#endif /* VG_TEX_H */