diff --git a/src/libtrx/gfx/gl/buffer.c b/src/libtrx/gfx/gl/buffer.c index aed161a43..f2aa5538a 100644 --- a/src/libtrx/gfx/gl/buffer.c +++ b/src/libtrx/gfx/gl/buffer.c @@ -6,22 +6,27 @@ void GFX_GL_Buffer_Init(GFX_GL_BUFFER *buf, GLenum target) { - assert(buf); + assert(buf != NULL); buf->target = target; glGenBuffers(1, &buf->id); GFX_GL_CheckError(); + buf->initialized = true; } void GFX_GL_Buffer_Close(GFX_GL_BUFFER *buf) { - assert(buf); - glDeleteBuffers(1, &buf->id); - GFX_GL_CheckError(); + assert(buf != NULL); + if (buf->initialized) { + glDeleteBuffers(1, &buf->id); + GFX_GL_CheckError(); + } + buf->initialized = false; } void GFX_GL_Buffer_Bind(GFX_GL_BUFFER *buf) { - assert(buf); + assert(buf != NULL); + assert(buf->initialized); glBindBuffer(buf->target, buf->id); GFX_GL_CheckError(); } @@ -29,7 +34,8 @@ void GFX_GL_Buffer_Bind(GFX_GL_BUFFER *buf) void GFX_GL_Buffer_Data( GFX_GL_BUFFER *buf, GLsizei size, const void *data, GLenum usage) { - assert(buf); + assert(buf != NULL); + assert(buf->initialized); glBufferData(buf->target, size, data, usage); GFX_GL_CheckError(); } @@ -37,14 +43,16 @@ void GFX_GL_Buffer_Data( void GFX_GL_Buffer_SubData( GFX_GL_BUFFER *buf, GLsizei offset, GLsizei size, const void *data) { - assert(buf); + assert(buf != NULL); + assert(buf->initialized); glBufferSubData(buf->target, offset, size, data); GFX_GL_CheckError(); } void *GFX_GL_Buffer_Map(GFX_GL_BUFFER *buf, GLenum access) { - assert(buf); + assert(buf != NULL); + assert(buf->initialized); void *ret = glMapBuffer(buf->target, access); GFX_GL_CheckError(); return ret; @@ -52,14 +60,16 @@ void *GFX_GL_Buffer_Map(GFX_GL_BUFFER *buf, GLenum access) void GFX_GL_Buffer_Unmap(GFX_GL_BUFFER *buf) { - assert(buf); + assert(buf != NULL); + assert(buf->initialized); glUnmapBuffer(buf->target); GFX_GL_CheckError(); } GLint GFX_GL_Buffer_Parameter(GFX_GL_BUFFER *buf, GLenum pname) { - assert(buf); + assert(buf != NULL); + assert(buf->initialized); GLint params = 0; glGetBufferParameteriv(buf->target, pname, ¶ms); GFX_GL_CheckError(); diff --git a/src/libtrx/gfx/gl/program.c b/src/libtrx/gfx/gl/program.c index a3e65ab92..c45c182bf 100644 --- a/src/libtrx/gfx/gl/program.c +++ b/src/libtrx/gfx/gl/program.c @@ -24,6 +24,7 @@ bool GFX_GL_Program_Init(GFX_GL_PROGRAM *program) void GFX_GL_Program_Close(GFX_GL_PROGRAM *program) { + assert(program != NULL); if (program->id) { glDeleteProgram(program->id); GFX_GL_CheckError(); @@ -33,6 +34,7 @@ void GFX_GL_Program_Close(GFX_GL_PROGRAM *program) void GFX_GL_Program_Bind(GFX_GL_PROGRAM *program) { + assert(program != NULL); glUseProgram(program->id); GFX_GL_CheckError(); } @@ -40,6 +42,8 @@ void GFX_GL_Program_Bind(GFX_GL_PROGRAM *program) char *GFX_GL_Program_PreprocessShader( const char *content, GLenum type, GFX_GL_BACKEND backend) { + assert(content != NULL); + const char *version_ogl21 = "#version 120\n" "#extension GL_ARB_explicit_attrib_location: enable\n" @@ -85,6 +89,9 @@ char *GFX_GL_Program_PreprocessShader( void GFX_GL_Program_AttachShader( GFX_GL_PROGRAM *program, GLenum type, const char *path) { + assert(program != NULL); + assert(path != NULL); + GLuint shader_id = glCreateShader(type); GFX_GL_CheckError(); if (!shader_id) { @@ -137,6 +144,7 @@ void GFX_GL_Program_AttachShader( void GFX_GL_Program_Link(GFX_GL_PROGRAM *program) { + assert(program != NULL); glLinkProgram(program->id); GFX_GL_CheckError(); @@ -160,12 +168,14 @@ void GFX_GL_Program_Link(GFX_GL_PROGRAM *program) void GFX_GL_Program_FragmentData(GFX_GL_PROGRAM *program, const char *name) { + assert(program != NULL); glBindFragDataLocation(program->id, 0, name); GFX_GL_CheckError(); } GLint GFX_GL_Program_UniformLocation(GFX_GL_PROGRAM *program, const char *name) { + assert(program != NULL); GLint location = glGetUniformLocation(program->id, name); GFX_GL_CheckError(); if (location == -1) { @@ -177,6 +187,7 @@ GLint GFX_GL_Program_UniformLocation(GFX_GL_PROGRAM *program, const char *name) void GFX_GL_Program_Uniform3f( GFX_GL_PROGRAM *program, GLint loc, GLfloat v0, GLfloat v1, GLfloat v2) { + assert(program != NULL); glUniform3f(loc, v0, v1, v2); GFX_GL_CheckError(); } @@ -185,12 +196,14 @@ void GFX_GL_Program_Uniform4f( GFX_GL_PROGRAM *program, GLint loc, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) { + assert(program != NULL); glUniform4f(loc, v0, v1, v2, v3); GFX_GL_CheckError(); } void GFX_GL_Program_Uniform1i(GFX_GL_PROGRAM *program, GLint loc, GLint v0) { + assert(program != NULL); glUniform1i(loc, v0); GFX_GL_CheckError(); } @@ -199,6 +212,7 @@ void GFX_GL_Program_UniformMatrix4fv( GFX_GL_PROGRAM *program, GLint loc, GLsizei count, GLboolean transpose, const GLfloat *value) { + assert(program != NULL); glUniformMatrix4fv(loc, count, transpose, value); GFX_GL_CheckError(); } diff --git a/src/libtrx/gfx/gl/sampler.c b/src/libtrx/gfx/gl/sampler.c index cb76df850..1735693d2 100644 --- a/src/libtrx/gfx/gl/sampler.c +++ b/src/libtrx/gfx/gl/sampler.c @@ -2,20 +2,30 @@ #include "gfx/gl/utils.h" +#include + void GFX_GL_Sampler_Init(GFX_GL_SAMPLER *sampler) { + assert(sampler != NULL); glGenSamplers(1, &sampler->id); GFX_GL_CheckError(); + sampler->initialized = true; } void GFX_GL_Sampler_Close(GFX_GL_SAMPLER *sampler) { - glDeleteSamplers(1, &sampler->id); - GFX_GL_CheckError(); + assert(sampler != NULL); + if (sampler->initialized) { + glDeleteSamplers(1, &sampler->id); + GFX_GL_CheckError(); + } + sampler->initialized = false; } void GFX_GL_Sampler_Bind(GFX_GL_SAMPLER *sampler, GLuint unit) { + assert(sampler != NULL); + assert(sampler->initialized); glBindSampler(unit, sampler->id); GFX_GL_CheckError(); } @@ -23,6 +33,8 @@ void GFX_GL_Sampler_Bind(GFX_GL_SAMPLER *sampler, GLuint unit) void GFX_GL_Sampler_Parameteri( GFX_GL_SAMPLER *sampler, GLenum pname, GLint param) { + assert(sampler != NULL); + assert(sampler->initialized); glSamplerParameteri(sampler->id, pname, param); GFX_GL_CheckError(); } @@ -30,6 +42,8 @@ void GFX_GL_Sampler_Parameteri( void GFX_GL_Sampler_Parameterf( GFX_GL_SAMPLER *sampler, GLenum pname, GLfloat param) { + assert(sampler != NULL); + assert(sampler->initialized); glSamplerParameterf(sampler->id, pname, param); GFX_GL_CheckError(); } diff --git a/src/libtrx/gfx/gl/texture.c b/src/libtrx/gfx/gl/texture.c index f5f067dcd..d0659389e 100644 --- a/src/libtrx/gfx/gl/texture.c +++ b/src/libtrx/gfx/gl/texture.c @@ -27,18 +27,23 @@ void GFX_GL_Texture_Init(GFX_GL_TEXTURE *texture, GLenum target) texture->target = target; glGenTextures(1, &texture->id); GFX_GL_CheckError(); + texture->initialized = true; } void GFX_GL_Texture_Close(GFX_GL_TEXTURE *texture) { assert(texture != NULL); - glDeleteTextures(1, &texture->id); - GFX_GL_CheckError(); + if (texture->initialized) { + glDeleteTextures(1, &texture->id); + GFX_GL_CheckError(); + } + texture->initialized = false; } void GFX_GL_Texture_Bind(GFX_GL_TEXTURE *texture) { assert(texture != NULL); + assert(texture->initialized); glBindTexture(texture->target, texture->id); GFX_GL_CheckError(); } @@ -48,6 +53,7 @@ void GFX_GL_Texture_Load( GLint internal_format, GLint format) { assert(texture != NULL); + assert(texture->initialized); GFX_GL_Texture_Bind(texture); @@ -65,6 +71,7 @@ void GFX_GL_Texture_Load( void GFX_GL_Texture_LoadFromBackBuffer(GFX_GL_TEXTURE *const texture) { assert(texture != NULL); + assert(texture->initialized); GFX_GL_Texture_Bind(texture); diff --git a/src/libtrx/gfx/gl/vertex_array.c b/src/libtrx/gfx/gl/vertex_array.c index 6f8903968..461ca531d 100644 --- a/src/libtrx/gfx/gl/vertex_array.c +++ b/src/libtrx/gfx/gl/vertex_array.c @@ -7,21 +7,26 @@ void GFX_GL_VertexArray_Init(GFX_GL_VERTEX_ARRAY *array) { - assert(array); + assert(array != NULL); glGenVertexArrays(1, &array->id); GFX_GL_CheckError(); + array->initialized = true; } void GFX_GL_VertexArray_Close(GFX_GL_VERTEX_ARRAY *array) { - assert(array); - glDeleteVertexArrays(1, &array->id); - GFX_GL_CheckError(); + assert(array != NULL); + if (array->initialized) { + glDeleteVertexArrays(1, &array->id); + GFX_GL_CheckError(); + } + array->initialized = false; } void GFX_GL_VertexArray_Bind(GFX_GL_VERTEX_ARRAY *array) { - assert(array); + assert(array != NULL); + assert(array->initialized); glBindVertexArray(array->id); GFX_GL_CheckError(); } @@ -30,7 +35,8 @@ void GFX_GL_VertexArray_Attribute( GFX_GL_VERTEX_ARRAY *array, GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLsizei offset) { - assert(array); + assert(array != NULL); + assert(array->initialized); glEnableVertexAttribArray(index); GFX_GL_CheckError(); diff --git a/src/libtrx/include/libtrx/gfx/gl/buffer.h b/src/libtrx/include/libtrx/gfx/gl/buffer.h index 99f8f68c2..d3711cd74 100644 --- a/src/libtrx/include/libtrx/gfx/gl/buffer.h +++ b/src/libtrx/include/libtrx/gfx/gl/buffer.h @@ -2,7 +2,10 @@ #include "gl_core_3_3.h" +#include + typedef struct { + bool initialized; GLuint id; GLenum target; } GFX_GL_BUFFER; diff --git a/src/libtrx/include/libtrx/gfx/gl/program.h b/src/libtrx/include/libtrx/gfx/gl/program.h index 427a8bb4d..0ab22dae7 100644 --- a/src/libtrx/include/libtrx/gfx/gl/program.h +++ b/src/libtrx/include/libtrx/gfx/gl/program.h @@ -6,6 +6,7 @@ #include typedef struct { + bool initialized; GLuint id; } GFX_GL_PROGRAM; diff --git a/src/libtrx/include/libtrx/gfx/gl/sampler.h b/src/libtrx/include/libtrx/gfx/gl/sampler.h index b1bae2ae1..e45e6c29a 100644 --- a/src/libtrx/include/libtrx/gfx/gl/sampler.h +++ b/src/libtrx/include/libtrx/gfx/gl/sampler.h @@ -2,7 +2,10 @@ #include "../gl/gl_core_3_3.h" +#include + typedef struct { + bool initialized; GLuint id; } GFX_GL_SAMPLER; diff --git a/src/libtrx/include/libtrx/gfx/gl/texture.h b/src/libtrx/include/libtrx/gfx/gl/texture.h index 96b7bc34e..99d7b1543 100644 --- a/src/libtrx/include/libtrx/gfx/gl/texture.h +++ b/src/libtrx/include/libtrx/gfx/gl/texture.h @@ -2,7 +2,10 @@ #include "../gl/gl_core_3_3.h" +#include + typedef struct { + bool initialized; GLuint id; GLenum target; } GFX_GL_TEXTURE; diff --git a/src/libtrx/include/libtrx/gfx/gl/vertex_array.h b/src/libtrx/include/libtrx/gfx/gl/vertex_array.h index 01615f1fc..e25b26481 100644 --- a/src/libtrx/include/libtrx/gfx/gl/vertex_array.h +++ b/src/libtrx/include/libtrx/gfx/gl/vertex_array.h @@ -2,7 +2,10 @@ #include "../gl/gl_core_3_3.h" +#include + typedef struct { + bool initialized; GLuint id; } GFX_GL_VERTEX_ARRAY;