Implements SDL_GL_BindTexture and SDL_GL_UnbindTexture (#1576)
authorGabriel Jacobo <gabomdq@gmail.com>
Mon, 03 Sep 2012 11:16:12 -0300
changeset 6414 df50b0d6c1c3
parent 6413 701a0c0d70d0
child 6415 c3893f1bb5f5
Implements SDL_GL_BindTexture and SDL_GL_UnbindTexture (#1576)
include/SDL_render.h
src/render/SDL_render.c
src/render/SDL_sysrender.h
src/render/opengl/SDL_render_gl.c
src/render/opengles/SDL_render_gles.c
src/render/opengles2/SDL_render_gles2.c
--- a/include/SDL_render.h	Sun Sep 02 19:37:36 2012 -0400
+++ b/include/SDL_render.h	Mon Sep 03 11:16:12 2012 -0300
@@ -672,6 +672,28 @@
 extern DECLSPEC void SDLCALL SDL_DestroyRenderer(SDL_Renderer * renderer);
 
 
+/**
+ *  \brief Bind the texture to the current OpenGL/ES/ES2 context for use with
+ *         OpenGL instructions.
+ *
+ *  \param texture  The SDL texture to bind
+ *  \param texw     A pointer to a float that will be filled with the texture width
+ *  \param texh     A pointer to a float that will be filled with the texture height
+ *
+ *  \return 0 on success, or -1 if the operation is not supported
+ */
+extern DECLSPEC int SDLCALL SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh);
+
+/**
+ *  \brief Unbind a texture from the current OpenGL/ES/ES2 context.
+ *
+ *  \param texture  The SDL texture to unbind
+ *
+ *  \return 0 on success, or -1 if the operation is not supported
+ */
+extern DECLSPEC int SDLCALL SDL_GL_UnbindTexture(SDL_Texture *texture);
+
+
 /* Ends C function definitions when using C++ */
 #ifdef __cplusplus
 /* *INDENT-OFF* */
--- a/src/render/SDL_render.c	Sun Sep 02 19:37:36 2012 -0400
+++ b/src/render/SDL_render.c	Mon Sep 03 11:16:12 2012 -0300
@@ -1382,4 +1382,32 @@
     renderer->DestroyRenderer(renderer);
 }
 
+int SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh)
+{
+    SDL_Renderer *renderer;
+
+    CHECK_TEXTURE_MAGIC(texture, );
+    renderer = texture->renderer;
+    if (renderer && renderer->GL_BindTexture) {
+        return renderer->GL_BindTexture(renderer, texture, texw, texh);
+    }
+
+    SDL_Unsupported();
+    return -1;
+}
+
+int SDL_GL_UnbindTexture(SDL_Texture *texture)
+{
+    SDL_Renderer *renderer;
+
+    CHECK_TEXTURE_MAGIC(texture, );
+    renderer = texture->renderer;
+    if (renderer && renderer->GL_UnbindTexture) {
+        return renderer->GL_UnbindTexture(renderer, texture);
+    }
+
+    SDL_Unsupported();
+    return -1;
+}
+
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/render/SDL_sysrender.h	Sun Sep 02 19:37:36 2012 -0400
+++ b/src/render/SDL_sysrender.h	Mon Sep 03 11:16:12 2012 -0300
@@ -98,6 +98,9 @@
 
     void (*DestroyRenderer) (SDL_Renderer * renderer);
 
+    int (*GL_BindTexture) (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
+    int (*GL_UnbindTexture) (SDL_Renderer * renderer, SDL_Texture *texture);
+
     /* The current renderer info */
     SDL_RendererInfo info;
 
--- a/src/render/opengl/SDL_render_gl.c	Sun Sep 02 19:37:36 2012 -0400
+++ b/src/render/opengl/SDL_render_gl.c	Mon Sep 03 11:16:12 2012 -0300
@@ -73,7 +73,8 @@
 static void GL_RenderPresent(SDL_Renderer * renderer);
 static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
 static void GL_DestroyRenderer(SDL_Renderer * renderer);
-
+static int GL_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
+static int GL_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
 
 SDL_RenderDriver GL_RenderDriver = {
     GL_CreateRenderer,
@@ -322,6 +323,8 @@
     renderer->RenderPresent = GL_RenderPresent;
     renderer->DestroyTexture = GL_DestroyTexture;
     renderer->DestroyRenderer = GL_DestroyRenderer;
+    renderer->GL_BindTexture = GL_BindTexture;
+    renderer->GL_UnbindTexture = GL_UnbindTexture;
     renderer->info = GL_RenderDriver.info;
     renderer->info.flags = SDL_RENDERER_ACCELERATED;
     renderer->driverdata = data;
@@ -1228,6 +1231,49 @@
     SDL_free(renderer);
 }
 
+static int GL_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh) {
+    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
+    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
+    GL_ActivateRenderer(renderer);
+
+    data->glEnable(texturedata->type);
+    if (texturedata->yuv) {
+        data->glActiveTextureARB(GL_TEXTURE2_ARB);
+        data->glBindTexture(texturedata->type, texturedata->vtexture);
+
+        data->glActiveTextureARB(GL_TEXTURE1_ARB);
+        data->glBindTexture(texturedata->type, texturedata->utexture);
+
+        data->glActiveTextureARB(GL_TEXTURE0_ARB);
+    }
+    data->glBindTexture(texturedata->type, texturedata->texture);
+
+    if(texw) *texw = (float)texturedata->texw;
+    if(texh) *texh = (float)texturedata->texh;
+
+    return 0;
+}
+
+static int GL_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture) {
+    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
+    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
+    GL_ActivateRenderer(renderer);
+
+    if (texturedata->yuv) {
+        data->glActiveTextureARB(GL_TEXTURE2_ARB);
+        data->glDisable(texturedata->type);
+
+        data->glActiveTextureARB(GL_TEXTURE1_ARB);
+        data->glDisable(texturedata->type);
+
+        data->glActiveTextureARB(GL_TEXTURE0_ARB);
+    }
+    
+    data->glDisable(texturedata->type);
+
+    return 0;
+}
+
 #endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
 
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/render/opengles/SDL_render_gles.c	Sun Sep 02 19:37:36 2012 -0400
+++ b/src/render/opengles/SDL_render_gles.c	Mon Sep 03 11:16:12 2012 -0300
@@ -78,6 +78,8 @@
 static void GLES_DestroyTexture(SDL_Renderer * renderer,
                                 SDL_Texture * texture);
 static void GLES_DestroyRenderer(SDL_Renderer * renderer);
+static int GLES_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
+static int GLES_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
 
 typedef struct GLES_FBOList GLES_FBOList;
 
@@ -312,6 +314,8 @@
     renderer->RenderPresent = GLES_RenderPresent;
     renderer->DestroyTexture = GLES_DestroyTexture;
     renderer->DestroyRenderer = GLES_DestroyRenderer;
+    renderer->GL_BindTexture = GLES_BindTexture;
+    renderer->GL_UnbindTexture = GLES_UnbindTexture;
     renderer->info = GLES_RenderDriver.info;
     renderer->info.flags = SDL_RENDERER_ACCELERATED;
     renderer->driverdata = data;
@@ -1105,6 +1109,30 @@
     SDL_free(renderer);
 }
 
+static int GLES_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh) {
+    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
+    GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
+    GLES_ActivateRenderer(renderer);
+
+    data->glEnable(GL_TEXTURE_2D);
+    data->glBindTexture(texturedata->type, texturedata->texture);
+
+    if(texw) *texw = (float)texturedata->texw;
+    if(texh) *texh = (float)texturedata->texh;
+
+    return 0;
+}
+
+static int GLES_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture) {
+    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
+    GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
+    GLES_ActivateRenderer(renderer);
+    data->glDisable(texturedata->type);
+
+    return 0;
+}
+
+
 #endif /* SDL_VIDEO_RENDER_OGL_ES && !SDL_RENDER_DISABLED */
 
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/render/opengles2/SDL_render_gles2.c	Sun Sep 02 19:37:36 2012 -0400
+++ b/src/render/opengles2/SDL_render_gles2.c	Mon Sep 03 11:16:12 2012 -0300
@@ -1564,6 +1564,39 @@
     SDL_GL_SwapWindow(renderer->window);
 }
 
+
+/*************************************************************************************************
+ * Bind/unbinding of textures
+ *************************************************************************************************/
+static int GLES2_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
+static int GLES2_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
+
+static int GLES2_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh) {
+    GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata;
+    GLES2_TextureData *texturedata = (GLES2_TextureData *)texture->driverdata;
+    GLES2_ActivateRenderer(renderer);
+
+    data->glActiveTexture(GL_TEXTURE0);
+    data->glBindTexture(texturedata->texture_type, texturedata->texture);
+
+    if(texw) *texw = 1.0;
+    if(texh) *texh = 1.0;
+
+    return 0;
+}
+
+static int GLES2_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture) {
+    GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata;
+    GLES2_TextureData *texturedata = (GLES2_TextureData *)texture->driverdata;
+    GLES2_ActivateRenderer(renderer);
+
+    data->glActiveTexture(GL_TEXTURE0);
+    data->glDisable(texturedata->texture_type);
+
+    return 0;
+}
+
+
 /*************************************************************************************************
  * Renderer instantiation                                                                        *
  *************************************************************************************************/
@@ -1712,6 +1745,8 @@
     renderer->RenderPresent       = &GLES2_RenderPresent;
     renderer->DestroyTexture      = &GLES2_DestroyTexture;
     renderer->DestroyRenderer     = &GLES2_DestroyRenderer;
+    renderer->GL_BindTexture      = &GLES2_BindTexture;
+    renderer->GL_UnbindTexture    = &GLES2_UnbindTexture;
 
     GLES2_ResetState(renderer);