2015-07-12 03:30:04 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2017-01-22 12:40:32 +00:00
|
|
|
* Copyright (C) 2011-2017 - Daniel De Matteis
|
2017-12-12 07:55:31 +00:00
|
|
|
*
|
2015-07-12 03:30:04 +00:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <retro_inline.h>
|
2017-06-28 02:41:38 +00:00
|
|
|
#include <retro_math.h>
|
2015-07-12 03:30:04 +00:00
|
|
|
|
2016-03-05 06:27:48 +00:00
|
|
|
#include "video_coord_array.h"
|
2015-07-12 03:30:04 +00:00
|
|
|
|
|
|
|
static INLINE bool realloc_checked(void **ptr, size_t size)
|
|
|
|
{
|
2016-02-14 17:09:12 +00:00
|
|
|
void *nptr = NULL;
|
2015-07-12 03:30:04 +00:00
|
|
|
|
|
|
|
if (*ptr)
|
|
|
|
nptr = realloc(*ptr, size);
|
|
|
|
else
|
|
|
|
nptr = malloc(size);
|
|
|
|
|
|
|
|
if (nptr)
|
|
|
|
*ptr = nptr;
|
|
|
|
|
|
|
|
return *ptr == nptr;
|
|
|
|
}
|
|
|
|
|
2016-05-10 00:32:49 +00:00
|
|
|
static bool video_coord_array_resize(video_coord_array_t *ca,
|
2016-03-20 16:46:53 +00:00
|
|
|
unsigned cap)
|
2015-07-12 03:30:04 +00:00
|
|
|
{
|
2016-03-20 16:46:53 +00:00
|
|
|
size_t base_size = sizeof(float) * cap;
|
2015-07-12 03:30:04 +00:00
|
|
|
|
2016-03-20 16:46:53 +00:00
|
|
|
if (!realloc_checked((void**)&ca->coords.vertex,
|
2016-03-05 08:43:15 +00:00
|
|
|
2 * base_size))
|
2016-03-20 16:46:53 +00:00
|
|
|
return false;
|
|
|
|
if (!realloc_checked((void**)&ca->coords.color,
|
2016-03-05 08:43:15 +00:00
|
|
|
4 * base_size))
|
2016-03-20 16:46:53 +00:00
|
|
|
return false;
|
|
|
|
if (!realloc_checked((void**)&ca->coords.tex_coord,
|
2016-03-05 08:43:15 +00:00
|
|
|
2 * base_size))
|
2016-03-20 16:46:53 +00:00
|
|
|
return false;
|
|
|
|
if (!realloc_checked((void**)&ca->coords.lut_tex_coord,
|
2016-03-05 08:43:15 +00:00
|
|
|
2 * base_size))
|
2016-03-20 16:46:53 +00:00
|
|
|
return false;
|
2015-07-12 03:30:04 +00:00
|
|
|
|
2016-03-20 16:46:53 +00:00
|
|
|
ca->allocated = cap;
|
2015-07-12 03:30:04 +00:00
|
|
|
|
2016-03-05 08:43:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-10 00:32:49 +00:00
|
|
|
bool video_coord_array_append(video_coord_array_t *ca,
|
|
|
|
const video_coords_t *coords, unsigned count)
|
2016-03-05 08:43:15 +00:00
|
|
|
{
|
|
|
|
size_t base_size, offset;
|
|
|
|
count = MIN(count, coords->vertices);
|
|
|
|
|
2016-03-20 16:46:53 +00:00
|
|
|
if (ca->coords.vertices + count >= ca->allocated)
|
|
|
|
{
|
|
|
|
unsigned cap = next_pow2(ca->coords.vertices + count);
|
2016-05-10 00:32:49 +00:00
|
|
|
if (!video_coord_array_resize(ca, cap))
|
2016-03-20 16:46:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-07-12 03:30:04 +00:00
|
|
|
|
|
|
|
base_size = count * sizeof(float);
|
|
|
|
offset = ca->coords.vertices;
|
|
|
|
|
2017-10-04 04:04:17 +00:00
|
|
|
/* XXX: I wish we used interlaced arrays so
|
2016-02-14 17:09:12 +00:00
|
|
|
* 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);
|
2015-07-12 03:30:04 +00:00
|
|
|
|
|
|
|
ca->coords.vertices += count;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-10 00:32:49 +00:00
|
|
|
void video_coord_array_free(video_coord_array_t *ca)
|
2015-07-12 03:30:04 +00:00
|
|
|
{
|
|
|
|
if (!ca->allocated)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ca->coords.vertex)
|
|
|
|
free(ca->coords.vertex);
|
2016-03-05 06:27:48 +00:00
|
|
|
ca->coords.vertex = NULL;
|
|
|
|
|
2015-07-12 03:30:04 +00:00
|
|
|
if (ca->coords.color)
|
|
|
|
free(ca->coords.color);
|
2016-03-05 06:27:48 +00:00
|
|
|
ca->coords.color = NULL;
|
|
|
|
|
2015-07-12 03:30:04 +00:00
|
|
|
if (ca->coords.tex_coord)
|
|
|
|
free(ca->coords.tex_coord);
|
2016-03-05 06:27:48 +00:00
|
|
|
ca->coords.tex_coord = NULL;
|
|
|
|
|
2015-07-12 03:30:04 +00:00
|
|
|
if (ca->coords.lut_tex_coord)
|
|
|
|
free(ca->coords.lut_tex_coord);
|
|
|
|
ca->coords.lut_tex_coord = NULL;
|
|
|
|
|
2016-03-05 06:27:48 +00:00
|
|
|
ca->coords.vertices = 0;
|
|
|
|
ca->allocated = 0;
|
2015-07-12 03:30:04 +00:00
|
|
|
}
|