Commit f5794f9eebaac3e70d4fc849641f30bc36913367

Sam Lantinga 2021-08-10T15:17:59

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

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d68d2c0..72f62d0 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 5029256..21b0733 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 d80f4d3..c67837e 100644
--- a/include/SDL_render.h
+++ b/include/SDL_render.h
@@ -481,6 +481,28 @@ 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.
  *
  * The pixel data must be in the pixel format of the texture. Use
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 1e7d8bb..40c0879 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 9fe7649..2c89497 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 25be260..99050cc 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 d9a2d15..87d14c2 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;