a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit ffc5a9b5c93c58d9655534f127683582560e6b9c
parent 8efa94bbe3e439a488a6c5d22e28f4ab29fbf3c0
Author: amin <dev@aminmesbah.com>
Date:   Mon,  1 Apr 2019 07:25:12 +0000

Remove allocation from glmth

FossilOrigin-Name: c87035127f26429a0a7a2ddf4b58df1f4eceae83b2b322341e6b95f8a745a0ce
Diffstat:
Msrc/glmth.c | 6++----
Msrc/glmth.h | 5++++-
Msrc/shader.c | 14+++++++++++---
3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/src/glmth.c b/src/glmth.c @@ -20,16 +20,14 @@ void glmth_m4_print(m4 m) } -f32 *glmth_m4_valueptr(m4 m) +void glmth_m4_valueptr(m4 m, f32* out_valueptr) { - f32 *values = malloc(sizeof(m4)); for (u8 v = 0; v < 16; ++v) { u8 row = v / 4; u8 col = v % 4; - values[v] = m.E[row][col]; + out_valueptr[v] = m.E[row][col]; } - return values; } diff --git a/src/glmth.h b/src/glmth.h @@ -97,7 +97,10 @@ typedef struct m4 glmth_m4_init_id(); void glmth_m4_print(m4 m); -f32 *glmth_m4_valueptr(m4 m); + +// out_valueptr must be a buffer of 16 f32s +void glmth_m4_valueptr(m4 m, f32* out_valueptr); + bool glmth_m4m4_eq(m4 mat1, m4 mat2); m4 glmth_m4m4_m(m4 mat1, m4 mat2); v4 glmth_m4v4_m(m4 m, v4 v); diff --git a/src/shader.c b/src/shader.c @@ -144,9 +144,17 @@ void shader_setm4(struct Shader *s, char *name, m4 *mat) { if (s) { - f32* valueptr = glmth_m4_valueptr(*mat); - glUniformMatrix4fv(glGetUniformLocation(s->program, name), 1, GL_TRUE, valueptr); - free(valueptr); + f32* valueptr = malloc(sizeof(m4)); + if (valueptr) + { + glmth_m4_valueptr(*mat, valueptr); + glUniformMatrix4fv(glGetUniformLocation(s->program, name), 1, GL_TRUE, valueptr); + free(valueptr); + } + else + { + printf("Error: Allocation failed\n"); + } } else {