X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg%2Fvg_tex.h;h=a9c24c0a68cd4e27e58cc801c248a0b521c8e842;hb=72e9a469a490e537f2e12464c1b075eb8991a868;hp=742493b5720d8bfc4a873f864c9b209deb3ecee8;hpb=684ee6fcb34db7c58a7e845d33a19d8fb0cccfd3;p=fishladder.git diff --git a/vg/vg_tex.h b/vg/vg_tex.h index 742493b..a9c24c0 100644 --- a/vg/vg_tex.h +++ b/vg/vg_tex.h @@ -1,5 +1,23 @@ // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved +#define VG_TEXTURE_NO_MIP 0x1 +#define VG_TEXTURE_REPEAT 0x2 +#define VG_TEXTURE_CLAMP 0x4 +#define VG_TEXTURE_NEAREST 0x8 + +struct vg_tex2d +{ + const char *path; + u32 flags; + GLuint name; +}; + +static void vg_tex2d_bind( vg_tex2d *tex, u32 id ) +{ + glActiveTexture( GL_TEXTURE0 + id ); + glBindTexture( GL_TEXTURE_2D, tex->name ); +} + static inline void vg_tex2d_mipmap(void) { glGenerateMipmap( GL_TEXTURE_2D ); @@ -37,8 +55,7 @@ static inline void vg_tex2d_clamp(void) static GLuint vg_tex2d_rgba( const char *path ) { - int x,y,nc; - stbi_set_flip_vertically_on_load( 1 ); + int x,y; i64 length; u8 *src_data = vg_asset_read_s( path, &length ); @@ -49,7 +66,8 @@ static GLuint vg_tex2d_rgba( const char *path ) if( src_data ) { - u8 *tex_buffer = stbi_load_from_memory( src_data, length, &x, &y, &nc, 4 ); + u8 *tex_buffer = qoi_decode( src_data, length, &x, &y, 4 ); + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_buffer ); free( tex_buffer ); @@ -71,3 +89,42 @@ static GLuint vg_tex2d_rgba( const char *path ) return texture_name; } + +static void vg_tex2d_init( vg_tex2d *textures[], int num ) +{ + for( int i = 0; i < num; i ++ ) + { + vg_tex2d *tex = textures[i]; + tex->name = vg_tex2d_rgba( tex->path ); + if( !(tex->flags & VG_TEXTURE_NO_MIP) ) + vg_tex2d_mipmap(); + + if( tex->flags & VG_TEXTURE_NEAREST ) + { + if( tex->flags & VG_TEXTURE_NO_MIP ) + vg_error( "Invalid texture settings\n" ); + else + vg_tex2d_nearest(); + } + else + { + if( tex->flags & VG_TEXTURE_NO_MIP ) + vg_tex2d_linear(); + else + vg_tex2d_linear_mipmap(); + } + + if( tex->flags & VG_TEXTURE_CLAMP ) + vg_tex2d_clamp(); + else + vg_tex2d_repeat(); + } +} + +static void vg_tex2d_free( vg_tex2d *textures[], int num ) +{ + for( int i = 0; i < num; i ++ ) + { + glDeleteTextures( 1, &textures[i]->name ); + } +}