Implements SDL_GL_BindTexture and SDL_GL_UnbindTexture (#1576)
authorGabriel Jacobo <gabomdq@gmail.com>
Mon, 03 Sep 2012 11:16:12 -0300
changeset 6414df50b0d6c1c3
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
     1.1 --- a/include/SDL_render.h	Sun Sep 02 19:37:36 2012 -0400
     1.2 +++ b/include/SDL_render.h	Mon Sep 03 11:16:12 2012 -0300
     1.3 @@ -672,6 +672,28 @@
     1.4  extern DECLSPEC void SDLCALL SDL_DestroyRenderer(SDL_Renderer * renderer);
     1.5  
     1.6  
     1.7 +/**
     1.8 + *  \brief Bind the texture to the current OpenGL/ES/ES2 context for use with
     1.9 + *         OpenGL instructions.
    1.10 + *
    1.11 + *  \param texture  The SDL texture to bind
    1.12 + *  \param texw     A pointer to a float that will be filled with the texture width
    1.13 + *  \param texh     A pointer to a float that will be filled with the texture height
    1.14 + *
    1.15 + *  \return 0 on success, or -1 if the operation is not supported
    1.16 + */
    1.17 +extern DECLSPEC int SDLCALL SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh);
    1.18 +
    1.19 +/**
    1.20 + *  \brief Unbind a texture from the current OpenGL/ES/ES2 context.
    1.21 + *
    1.22 + *  \param texture  The SDL texture to unbind
    1.23 + *
    1.24 + *  \return 0 on success, or -1 if the operation is not supported
    1.25 + */
    1.26 +extern DECLSPEC int SDLCALL SDL_GL_UnbindTexture(SDL_Texture *texture);
    1.27 +
    1.28 +
    1.29  /* Ends C function definitions when using C++ */
    1.30  #ifdef __cplusplus
    1.31  /* *INDENT-OFF* */
     2.1 --- a/src/render/SDL_render.c	Sun Sep 02 19:37:36 2012 -0400
     2.2 +++ b/src/render/SDL_render.c	Mon Sep 03 11:16:12 2012 -0300
     2.3 @@ -1382,4 +1382,32 @@
     2.4      renderer->DestroyRenderer(renderer);
     2.5  }
     2.6  
     2.7 +int SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh)
     2.8 +{
     2.9 +    SDL_Renderer *renderer;
    2.10 +
    2.11 +    CHECK_TEXTURE_MAGIC(texture, );
    2.12 +    renderer = texture->renderer;
    2.13 +    if (renderer && renderer->GL_BindTexture) {
    2.14 +        return renderer->GL_BindTexture(renderer, texture, texw, texh);
    2.15 +    }
    2.16 +
    2.17 +    SDL_Unsupported();
    2.18 +    return -1;
    2.19 +}
    2.20 +
    2.21 +int SDL_GL_UnbindTexture(SDL_Texture *texture)
    2.22 +{
    2.23 +    SDL_Renderer *renderer;
    2.24 +
    2.25 +    CHECK_TEXTURE_MAGIC(texture, );
    2.26 +    renderer = texture->renderer;
    2.27 +    if (renderer && renderer->GL_UnbindTexture) {
    2.28 +        return renderer->GL_UnbindTexture(renderer, texture);
    2.29 +    }
    2.30 +
    2.31 +    SDL_Unsupported();
    2.32 +    return -1;
    2.33 +}
    2.34 +
    2.35  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/render/SDL_sysrender.h	Sun Sep 02 19:37:36 2012 -0400
     3.2 +++ b/src/render/SDL_sysrender.h	Mon Sep 03 11:16:12 2012 -0300
     3.3 @@ -98,6 +98,9 @@
     3.4  
     3.5      void (*DestroyRenderer) (SDL_Renderer * renderer);
     3.6  
     3.7 +    int (*GL_BindTexture) (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
     3.8 +    int (*GL_UnbindTexture) (SDL_Renderer * renderer, SDL_Texture *texture);
     3.9 +
    3.10      /* The current renderer info */
    3.11      SDL_RendererInfo info;
    3.12  
     4.1 --- a/src/render/opengl/SDL_render_gl.c	Sun Sep 02 19:37:36 2012 -0400
     4.2 +++ b/src/render/opengl/SDL_render_gl.c	Mon Sep 03 11:16:12 2012 -0300
     4.3 @@ -73,7 +73,8 @@
     4.4  static void GL_RenderPresent(SDL_Renderer * renderer);
     4.5  static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
     4.6  static void GL_DestroyRenderer(SDL_Renderer * renderer);
     4.7 -
     4.8 +static int GL_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
     4.9 +static int GL_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
    4.10  
    4.11  SDL_RenderDriver GL_RenderDriver = {
    4.12      GL_CreateRenderer,
    4.13 @@ -322,6 +323,8 @@
    4.14      renderer->RenderPresent = GL_RenderPresent;
    4.15      renderer->DestroyTexture = GL_DestroyTexture;
    4.16      renderer->DestroyRenderer = GL_DestroyRenderer;
    4.17 +    renderer->GL_BindTexture = GL_BindTexture;
    4.18 +    renderer->GL_UnbindTexture = GL_UnbindTexture;
    4.19      renderer->info = GL_RenderDriver.info;
    4.20      renderer->info.flags = SDL_RENDERER_ACCELERATED;
    4.21      renderer->driverdata = data;
    4.22 @@ -1228,6 +1231,49 @@
    4.23      SDL_free(renderer);
    4.24  }
    4.25  
    4.26 +static int GL_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh) {
    4.27 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
    4.28 +    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
    4.29 +    GL_ActivateRenderer(renderer);
    4.30 +
    4.31 +    data->glEnable(texturedata->type);
    4.32 +    if (texturedata->yuv) {
    4.33 +        data->glActiveTextureARB(GL_TEXTURE2_ARB);
    4.34 +        data->glBindTexture(texturedata->type, texturedata->vtexture);
    4.35 +
    4.36 +        data->glActiveTextureARB(GL_TEXTURE1_ARB);
    4.37 +        data->glBindTexture(texturedata->type, texturedata->utexture);
    4.38 +
    4.39 +        data->glActiveTextureARB(GL_TEXTURE0_ARB);
    4.40 +    }
    4.41 +    data->glBindTexture(texturedata->type, texturedata->texture);
    4.42 +
    4.43 +    if(texw) *texw = (float)texturedata->texw;
    4.44 +    if(texh) *texh = (float)texturedata->texh;
    4.45 +
    4.46 +    return 0;
    4.47 +}
    4.48 +
    4.49 +static int GL_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture) {
    4.50 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
    4.51 +    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
    4.52 +    GL_ActivateRenderer(renderer);
    4.53 +
    4.54 +    if (texturedata->yuv) {
    4.55 +        data->glActiveTextureARB(GL_TEXTURE2_ARB);
    4.56 +        data->glDisable(texturedata->type);
    4.57 +
    4.58 +        data->glActiveTextureARB(GL_TEXTURE1_ARB);
    4.59 +        data->glDisable(texturedata->type);
    4.60 +
    4.61 +        data->glActiveTextureARB(GL_TEXTURE0_ARB);
    4.62 +    }
    4.63 +    
    4.64 +    data->glDisable(texturedata->type);
    4.65 +
    4.66 +    return 0;
    4.67 +}
    4.68 +
    4.69  #endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
    4.70  
    4.71  /* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/render/opengles/SDL_render_gles.c	Sun Sep 02 19:37:36 2012 -0400
     5.2 +++ b/src/render/opengles/SDL_render_gles.c	Mon Sep 03 11:16:12 2012 -0300
     5.3 @@ -78,6 +78,8 @@
     5.4  static void GLES_DestroyTexture(SDL_Renderer * renderer,
     5.5                                  SDL_Texture * texture);
     5.6  static void GLES_DestroyRenderer(SDL_Renderer * renderer);
     5.7 +static int GLES_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
     5.8 +static int GLES_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
     5.9  
    5.10  typedef struct GLES_FBOList GLES_FBOList;
    5.11  
    5.12 @@ -312,6 +314,8 @@
    5.13      renderer->RenderPresent = GLES_RenderPresent;
    5.14      renderer->DestroyTexture = GLES_DestroyTexture;
    5.15      renderer->DestroyRenderer = GLES_DestroyRenderer;
    5.16 +    renderer->GL_BindTexture = GLES_BindTexture;
    5.17 +    renderer->GL_UnbindTexture = GLES_UnbindTexture;
    5.18      renderer->info = GLES_RenderDriver.info;
    5.19      renderer->info.flags = SDL_RENDERER_ACCELERATED;
    5.20      renderer->driverdata = data;
    5.21 @@ -1105,6 +1109,30 @@
    5.22      SDL_free(renderer);
    5.23  }
    5.24  
    5.25 +static int GLES_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh) {
    5.26 +    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
    5.27 +    GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
    5.28 +    GLES_ActivateRenderer(renderer);
    5.29 +
    5.30 +    data->glEnable(GL_TEXTURE_2D);
    5.31 +    data->glBindTexture(texturedata->type, texturedata->texture);
    5.32 +
    5.33 +    if(texw) *texw = (float)texturedata->texw;
    5.34 +    if(texh) *texh = (float)texturedata->texh;
    5.35 +
    5.36 +    return 0;
    5.37 +}
    5.38 +
    5.39 +static int GLES_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture) {
    5.40 +    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
    5.41 +    GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
    5.42 +    GLES_ActivateRenderer(renderer);
    5.43 +    data->glDisable(texturedata->type);
    5.44 +
    5.45 +    return 0;
    5.46 +}
    5.47 +
    5.48 +
    5.49  #endif /* SDL_VIDEO_RENDER_OGL_ES && !SDL_RENDER_DISABLED */
    5.50  
    5.51  /* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/render/opengles2/SDL_render_gles2.c	Sun Sep 02 19:37:36 2012 -0400
     6.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Mon Sep 03 11:16:12 2012 -0300
     6.3 @@ -1564,6 +1564,39 @@
     6.4      SDL_GL_SwapWindow(renderer->window);
     6.5  }
     6.6  
     6.7 +
     6.8 +/*************************************************************************************************
     6.9 + * Bind/unbinding of textures
    6.10 + *************************************************************************************************/
    6.11 +static int GLES2_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
    6.12 +static int GLES2_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
    6.13 +
    6.14 +static int GLES2_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh) {
    6.15 +    GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata;
    6.16 +    GLES2_TextureData *texturedata = (GLES2_TextureData *)texture->driverdata;
    6.17 +    GLES2_ActivateRenderer(renderer);
    6.18 +
    6.19 +    data->glActiveTexture(GL_TEXTURE0);
    6.20 +    data->glBindTexture(texturedata->texture_type, texturedata->texture);
    6.21 +
    6.22 +    if(texw) *texw = 1.0;
    6.23 +    if(texh) *texh = 1.0;
    6.24 +
    6.25 +    return 0;
    6.26 +}
    6.27 +
    6.28 +static int GLES2_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture) {
    6.29 +    GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata;
    6.30 +    GLES2_TextureData *texturedata = (GLES2_TextureData *)texture->driverdata;
    6.31 +    GLES2_ActivateRenderer(renderer);
    6.32 +
    6.33 +    data->glActiveTexture(GL_TEXTURE0);
    6.34 +    data->glDisable(texturedata->texture_type);
    6.35 +
    6.36 +    return 0;
    6.37 +}
    6.38 +
    6.39 +
    6.40  /*************************************************************************************************
    6.41   * Renderer instantiation                                                                        *
    6.42   *************************************************************************************************/
    6.43 @@ -1712,6 +1745,8 @@
    6.44      renderer->RenderPresent       = &GLES2_RenderPresent;
    6.45      renderer->DestroyTexture      = &GLES2_DestroyTexture;
    6.46      renderer->DestroyRenderer     = &GLES2_DestroyRenderer;
    6.47 +    renderer->GL_BindTexture      = &GLES2_BindTexture;
    6.48 +    renderer->GL_UnbindTexture    = &GLES2_UnbindTexture;
    6.49  
    6.50      GLES2_ResetState(renderer);
    6.51