SDL: Add SDL_RenderGeometry based on SDL_RenderGeometryRaw

From cc37c38e30a8106ea8e82242a24dd41444a10abe Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Thu, 1 Apr 2021 09:55:00 +0200
Subject: [PATCH] Add SDL_RenderGeometry based on SDL_RenderGeometryRaw

---
 include/SDL_render.h              | 29 +++++++++++++++++++++++++++++
 src/dynapi/SDL_dynapi_overrides.h |  1 +
 src/dynapi/SDL_dynapi_procs.h     |  1 +
 src/render/SDL_render.c           | 19 +++++++++++++++++++
 4 files changed, 50 insertions(+)

diff --git a/include/SDL_render.h b/include/SDL_render.h
index 1022f9c96d..056ce39823 100644
--- a/include/SDL_render.h
+++ b/include/SDL_render.h
@@ -85,6 +85,16 @@ typedef struct SDL_RendererInfo
     int max_texture_height;     /**< The maximum texture height */
 } SDL_RendererInfo;
 
+/**
+ *  \brief Vertex structure
+ */
+typedef struct SDL_Vertex
+{
+    SDL_FPoint position;        /**< Vertex position, in SDL_Renderer coordinates  */
+    SDL_Color  color;           /**< Vertex color */
+    SDL_FPoint tex_coord;       /**< Normalized texture coordinates, if needed */
+} SDL_Vertex;
+
 /**
  * The scaling mode for a texture.
  */
@@ -1441,6 +1451,25 @@ extern DECLSPEC int SDLCALL SDL_RenderCopyExF(SDL_Renderer * renderer,
                                             const SDL_FPoint *center,
                                             const SDL_RendererFlip flip);
 
+/**
+ *  \brief Render a list of triangles, optionally using a texture and indices into the vertex array
+ *  Color and alpha modulation is done per vertex (SDL_SetTextureColorMod and SDL_SetTextureAlphaMod are ignored).
+ *
+ *  \param texture      (optional) The SDL texture to use.
+ *  \param vertices     Vertices.
+ *  \param num_vertices Number of vertices.
+ *  \param indices      (optional) An array of integer indices into the 'vertices' array, if NULL all vertices will be rendered in sequential order.
+ *  \param num_indices  Number of indices.
+ *
+ *  \sa SDL_Vertex
+ *
+ *  \return 0 on success, or -1 if the operation is not supported
+ */
+extern DECLSPEC int SDLCALL SDL_RenderGeometry(SDL_Renderer *renderer,
+                                               SDL_Texture *texture,
+                                               const SDL_Vertex *vertices, int num_vertices,
+                                               const int *indices, int num_indices);
+
 /**
  *  \brief Render a list of triangles, optionally using a texture and indices into the vertex arrays
  *  Color and alpha modulation is done per vertex (SDL_SetTextureColorMod and SDL_SetTextureAlphaMod are ignored).
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 6a849c4fd4..98cf07979f 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -819,3 +819,4 @@
 #define SDL_GetTextureUserData SDL_GetTextureUserData_REAL
 #define SDL_RenderGeometry SDL_RenderGeometry_REAL
 #define SDL_RenderGeometryRaw SDL_RenderGeometryRaw_REAL
+#define SDL_RenderGeometry SDL_RenderGeometry_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 88ecb994a5..524e80b7a0 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -884,3 +884,4 @@ SDL_DYNAPI_PROC(int,SDL_SetTextureUserData,(SDL_Texture *a, void *b),(a,b),retur
 SDL_DYNAPI_PROC(void*,SDL_GetTextureUserData,(SDL_Texture *a),(a),return)
 SDL_DYNAPI_PROC(int,SDL_RenderGeometry,(SDL_Renderer *a, SDL_Texture *b, SDL_Vertex *c, int d, int *e, int f),(a,b,c,d,e,f),return)
 SDL_DYNAPI_PROC(int,SDL_RenderGeometryRaw,(SDL_Renderer *a, SDL_Texture *b, const float *c, int d, const int *e, int f, const float *g, int h, int i, const void *j, int k, int l),(a,b,c,d,e,f,g,h,i,j,k,l),return)
+SDL_DYNAPI_PROC(int,SDL_RenderGeometry,(SDL_Renderer *a, SDL_Texture *b, const SDL_Vertex *c, int d, const int *e, int f),(a,b,c,d,e,f),return)
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index d8c49ba49c..d25fdd2c3e 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -3330,6 +3330,25 @@ SDL_RenderCopyExF(SDL_Renderer * renderer, SDL_Texture * texture,
     return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
 }
 
+
+#define SDL_OFFSETOF(_TYPE,_MEMBER)  ((size_t)&(((_TYPE*)0)->_MEMBER))
+int
+SDL_RenderGeometry(SDL_Renderer *renderer,
+                               SDL_Texture *texture,
+                               const SDL_Vertex *vertices, int num_vertices,
+                               const int *indices, int num_indices)
+{
+    const float *xy = (const float *)((const Uint8 *)vertices + SDL_OFFSETOF(SDL_Vertex, position));
+    int xy_stride = sizeof (SDL_Vertex);
+    const int *color = (const int *) ((const Uint8 *)vertices + SDL_OFFSETOF(SDL_Vertex, color));
+    int color_stride = sizeof (SDL_Vertex);
+    const float *uv = (const float *)((const Uint8 *)vertices + SDL_OFFSETOF(SDL_Vertex, tex_coord));
+    int uv_stride = sizeof (SDL_Vertex);
+    int size_indice = 4;
+
+    return SDL_RenderGeometryRaw(renderer, texture, xy, xy_stride, color, color_stride, uv, uv_stride, num_vertices, indices, num_indices, size_indice);
+}
+
 int
 SDL_RenderGeometryRaw(SDL_Renderer *renderer,
                                   SDL_Texture *texture,