SDL: Added SDL_SetTextureUserData() and SDL_GetTextureUserData() to associate a user-specified pointer with an SDL texture

From f5794f9eebaac3e70d4fc849641f30bc36913367 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 10 Aug 2021 15:17:59 -0700
Subject: [PATCH] Added SDL_SetTextureUserData() and SDL_GetTextureUserData()
 to associate a user-specified pointer with an SDL texture

---
 CMakeLists.txt                    |  2 +-
 configure.ac                      |  2 +-
 include/SDL_render.h              | 22 ++++++++++++++++++++++
 src/dynapi/SDL_dynapi_overrides.h |  2 ++
 src/dynapi/SDL_dynapi_procs.h     |  2 ++
 src/render/SDL_render.c           | 17 +++++++++++++++++
 src/render/SDL_sysrender.h        |  1 +
 7 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d68d2c0a9..72f62d007 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -52,7 +52,7 @@ include(${SDL2_SOURCE_DIR}/cmake/sdlchecks.cmake)
 set(SDL_MAJOR_VERSION 2)
 set(SDL_MINOR_VERSION 0)
 set(SDL_MICRO_VERSION 17)
-set(SDL_INTERFACE_AGE 1)
+set(SDL_INTERFACE_AGE 0)
 set(SDL_BINARY_AGE 17)
 set(SDL_VERSION "${SDL_MAJOR_VERSION}.${SDL_MINOR_VERSION}.${SDL_MICRO_VERSION}")
 # the following should match the versions in Xcode project file:
diff --git a/configure.ac b/configure.ac
index 50292569d..21b0733de 100644
--- a/configure.ac
+++ b/configure.ac
@@ -23,7 +23,7 @@ dnl Set various version strings - taken gratefully from the GTk sources
 SDL_MAJOR_VERSION=2
 SDL_MINOR_VERSION=0
 SDL_MICRO_VERSION=17
-SDL_INTERFACE_AGE=1
+SDL_INTERFACE_AGE=0
 SDL_BINARY_AGE=17
 SDL_VERSION=$SDL_MAJOR_VERSION.$SDL_MINOR_VERSION.$SDL_MICRO_VERSION
 
diff --git a/include/SDL_render.h b/include/SDL_render.h
index d80f4d3f5..c67837e98 100644
--- a/include/SDL_render.h
+++ b/include/SDL_render.h
@@ -480,6 +480,28 @@ extern DECLSPEC int SDLCALL SDL_SetTextureScaleMode(SDL_Texture * texture,
 extern DECLSPEC int SDLCALL SDL_GetTextureScaleMode(SDL_Texture * texture,
                                                     SDL_ScaleMode *scaleMode);
 
+/**
+ * Associate a user-specified pointer with a texture.
+ *
+ * \param texture the texture to update.
+ * \param userdata the pointer to associate with the texture.
+ * \returns 0 on success, or -1 if the texture is not valid.
+ *
+ * \sa SDL_GetTextureUserData
+ */
+extern DECLSPEC int SDLCALL SDL_SetTextureUserData(SDL_Texture * texture,
+                                                   void *userdata);
+
+/**
+ * Get the user-specified pointer associated with a texture
+ *
+ * \param texture the texture to query.
+ * \return the pointer associated with the texture, or NULL if the texture is not valid.
+ *
+ * \sa SDL_SetTextureUserData
+ */
+extern DECLSPEC void * SDLCALL SDL_GetTextureUserData(SDL_Texture * texture);
+
 /**
  * Update the given texture rectangle with new pixel data.
  *
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 1e7d8bb45..40c087934 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -815,3 +815,5 @@
 #define SDL_GameControllerSendEffect SDL_GameControllerSendEffect_REAL
 #define SDL_JoystickSendEffect SDL_JoystickSendEffect_REAL
 #define SDL_GameControllerGetSensorDataRate SDL_GameControllerGetSensorDataRate_REAL
+#define SDL_SetTextureUserData SDL_SetTextureUserData_REAL
+#define SDL_GetTextureUserData SDL_GetTextureUserData_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 9fe764949..2c894979a 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -880,3 +880,5 @@ SDL_DYNAPI_PROC(int,SDL_FlashWindow,(SDL_Window *a, SDL_FlashOperation b),(a,b),
 SDL_DYNAPI_PROC(int,SDL_GameControllerSendEffect,(SDL_GameController *a, const void *b, int c),(a,b,c),return)
 SDL_DYNAPI_PROC(int,SDL_JoystickSendEffect,(SDL_Joystick *a, const void *b, int c),(a,b,c),return)
 SDL_DYNAPI_PROC(float,SDL_GameControllerGetSensorDataRate,(SDL_GameController *a, SDL_SensorType b),(a,b),return)
+SDL_DYNAPI_PROC(int,SDL_SetTextureUserData,(SDL_Texture *a, void *b),(a,b),return)
+SDL_DYNAPI_PROC(void*,SDL_GetTextureUserData,(SDL_Texture *a),(a),return)
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index 25be260a1..99050ccb9 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -1486,6 +1486,23 @@ SDL_GetTextureScaleMode(SDL_Texture * texture, SDL_ScaleMode *scaleMode)
     return 0;
 }
 
+int
+SDL_SetTextureUserData(SDL_Texture * texture, void *userdata)
+{
+    CHECK_TEXTURE_MAGIC(texture, -1);
+
+    texture->userdata = userdata;
+    return 0;
+}
+
+void *
+SDL_GetTextureUserData(SDL_Texture * texture)
+{
+    CHECK_TEXTURE_MAGIC(texture, NULL);
+
+    return texture->userdata;
+}
+
 #if SDL_HAVE_YUV
 static int
 SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
diff --git a/src/render/SDL_sysrender.h b/src/render/SDL_sysrender.h
index d9a2d155c..87d14c259 100644
--- a/src/render/SDL_sysrender.h
+++ b/src/render/SDL_sysrender.h
@@ -58,6 +58,7 @@ struct SDL_Texture
     Uint32 last_command_generation; /* last command queue generation this texture was in. */
 
     void *driverdata;           /**< Driver specific texture representation */
+    void *userdata;
 
     SDL_Texture *prev;
     SDL_Texture *next;