X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=src%2Fvg%2Fvg_tex.h;h=e92e5f152582258af9b0c73d59713a68f1be8764;hb=367883958336d1c04c8a304af6119b21f0f2f15a;hp=4c14540ca431a4b5404534dc5d44f27938e04467;hpb=6cfa3e0895e42f702276e97c85ad371f3512c67d;p=vg.git diff --git a/src/vg/vg_tex.h b/src/vg/vg_tex.h index 4c14540..e92e5f1 100644 --- a/src/vg/vg_tex.h +++ b/src/vg/vg_tex.h @@ -1,9 +1,21 @@ -// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved +/* 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 + +#define QOI_IMPLEMENTATION +#define QOI_MALLOC vg_alloc +#define QOI_FREE vg_free + +#include "phoboslab/qoi.h" struct vg_tex2d { @@ -19,6 +31,12 @@ struct vg_sprite 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 ); } @@ -42,7 +60,8 @@ static inline void vg_tex2d_nearest(void) static inline void vg_tex2d_linear_mipmap(void) { - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + GL_LINEAR_MIPMAP_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); } @@ -72,10 +91,11 @@ static GLuint vg_tex2d_rgba( const char *path ) 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 ); + 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 ); + vg_free( tex_buffer ); + vg_free( src_data ); } else { @@ -88,7 +108,8 @@ static GLuint vg_tex2d_rgba( const char *path ) }; 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 ); + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, + 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_err ); } return texture_name; @@ -96,7 +117,7 @@ static GLuint vg_tex2d_rgba( const char *path ) 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 ); @@ -122,6 +143,8 @@ static void vg_tex2d_init( vg_tex2d *textures[], int num ) vg_tex2d_clamp(); else vg_tex2d_repeat(); + + tex->flags |= VG_TEXTURE_ALLOCATED_INTERNAL; } } @@ -132,3 +155,5 @@ static void vg_tex2d_free( vg_tex2d *textures[], int num ) glDeleteTextures( 1, &textures[i]->name ); } } + +#endif /* VG_TEX_H */