(gl_common.c) Refactors

This commit is contained in:
twinaphex 2015-07-12 04:11:26 +02:00
parent 6bb0d87d87
commit a687ca54d8

View File

@ -175,7 +175,9 @@ static INLINE bool realloc_checked(void **ptr, size_t size)
bool gl_coord_array_add(gfx_coord_array_t *ca, const gfx_coords_t *coords, unsigned count)
{
size_t base_size, offset;
bool success = false;
count = min(count, coords->vertices);
if (ca->coords.vertices + count >= ca->allocated)
@ -183,10 +185,10 @@ bool gl_coord_array_add(gfx_coord_array_t *ca, const gfx_coords_t *coords, unsig
unsigned alloc_size = next_pow2(ca->coords.vertices + count);
size_t base_size = sizeof(GLfloat) * alloc_size;
bool vert_ok = realloc_checked((void**)&ca->coords.vertex, 2 * base_size);
bool color_ok = realloc_checked((void**)&ca->coords.color, 4 * base_size);
bool tex_ok = realloc_checked((void**)&ca->coords.tex_coord, 2 * base_size);
bool lut_ok = realloc_checked((void**)&ca->coords.lut_tex_coord, 2 * base_size);
bool vert_ok = realloc_checked((void**)&ca->coords.vertex, 2 * base_size);
bool color_ok = realloc_checked((void**)&ca->coords.color, 4 * base_size);
bool tex_ok = realloc_checked((void**)&ca->coords.tex_coord, 2 * base_size);
bool lut_ok = realloc_checked((void**)&ca->coords.lut_tex_coord, 2 * base_size);
if (vert_ok && color_ok && tex_ok && lut_ok)
{
@ -197,23 +199,24 @@ bool gl_coord_array_add(gfx_coord_array_t *ca, const gfx_coords_t *coords, unsig
else
success = true;
if (success)
if (!success)
{
size_t base_size = count * sizeof(GLfloat);
size_t offset = ca->coords.vertices;
/* XXX: i wish we used interlaced arrays so we could call memcpy only once */
memcpy(ca->coords.vertex + offset * 2, coords->vertex, base_size * 2);
memcpy(ca->coords.color + offset * 4, coords->color, base_size * 4);
memcpy(ca->coords.tex_coord + offset * 2, coords->tex_coord, base_size * 2);
memcpy(ca->coords.lut_tex_coord + offset * 2, coords->lut_tex_coord, base_size * 2);
ca->coords.vertices += count;
}
else
RARCH_WARN("Allocation failed.");
return false;
}
return success;
base_size = count * sizeof(GLfloat);
offset = ca->coords.vertices;
/* XXX: i wish we used interlaced arrays so we could call memcpy only once */
memcpy(ca->coords.vertex + offset * 2, coords->vertex, base_size * 2);
memcpy(ca->coords.color + offset * 4, coords->color, base_size * 4);
memcpy(ca->coords.tex_coord + offset * 2, coords->tex_coord, base_size * 2);
memcpy(ca->coords.lut_tex_coord + offset * 2, coords->lut_tex_coord, base_size * 2);
ca->coords.vertices += count;
return true;
}
void gl_coord_array_free(gfx_coord_array_t *ca)
@ -221,14 +224,18 @@ void gl_coord_array_free(gfx_coord_array_t *ca)
if (!ca->allocated)
return;
free(ca->coords.vertex);
free(ca->coords.color);
free(ca->coords.tex_coord);
free(ca->coords.lut_tex_coord);
if (ca->coords.vertex)
free(ca->coords.vertex);
if (ca->coords.color)
free(ca->coords.color);
if (ca->coords.tex_coord)
free(ca->coords.tex_coord);
if (ca->coords.lut_tex_coord)
free(ca->coords.lut_tex_coord);
ca->coords.vertex = NULL;
ca->coords.color = NULL;
ca->coords.tex_coord = NULL;
ca->coords.vertex = NULL;
ca->coords.color = NULL;
ca->coords.tex_coord = NULL;
ca->coords.lut_tex_coord = NULL;
ca->coords.vertices = 0;