Added a helper function SDL_LockTextureToSurface()
authorSylvain Becker <sylvain.becker@gmail.com>
Mon, 30 Sep 2019 20:58:44 +0200
changeset 130999599e2bf9e3a
parent 13098 18ede62f3f14
child 13100 2bef0dd99b8b
Added a helper function SDL_LockTextureToSurface()
Similar to SDL_LockTexture(), except the locked area is exposed as a SDL surface.
WhatsNew.txt
include/SDL_render.h
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/render/SDL_render.c
src/render/SDL_sysrender.h
     1.1 --- a/WhatsNew.txt	Fri Sep 27 20:15:42 2019 -0300
     1.2 +++ b/WhatsNew.txt	Mon Sep 30 20:58:44 2019 +0200
     1.3 @@ -2,6 +2,13 @@
     1.4  This is a list of major changes in SDL's version history.
     1.5  
     1.6  ---------------------------------------------------------------------------
     1.7 +2.0.11/12:
     1.8 +---------------------------------------------------------------------------
     1.9 +
    1.10 +General:
    1.11 +* Added SDL_LockTextureToSurface(), similar to SDL_LockTexture() but the locked area is exposed as a SDL surface.
    1.12 +
    1.13 +---------------------------------------------------------------------------
    1.14  2.0.10:
    1.15  ---------------------------------------------------------------------------
    1.16  
     2.1 --- a/include/SDL_render.h	Fri Sep 27 20:15:42 2019 -0300
     2.2 +++ b/include/SDL_render.h	Mon Sep 30 20:58:44 2019 +0200
     2.3 @@ -431,9 +431,30 @@
     2.4                                              void **pixels, int *pitch);
     2.5  
     2.6  /**
     2.7 + *  \brief Lock a portion of the texture for write-only pixel access.
     2.8 + *         Expose it as a SDL surface.
     2.9 + *
    2.10 + *  \param texture   The texture to lock for access, which was created with
    2.11 + *                   ::SDL_TEXTUREACCESS_STREAMING.
    2.12 + *  \param rect      A pointer to the rectangle to lock for access. If the rect
    2.13 + *                   is NULL, the entire texture will be locked.
    2.14 + *  \param surface   This is filled in with a SDL surface representing the locked area
    2.15 + *                   Surface is freed internally after calling SDL_UnlockTexture or SDL_DestroyTexture.
    2.16 + *
    2.17 + *  \return 0 on success, or -1 if the texture is not valid or was not created with ::SDL_TEXTUREACCESS_STREAMING.
    2.18 + *
    2.19 + *  \sa SDL_UnlockTexture()
    2.20 + */
    2.21 +extern DECLSPEC int SDLCALL SDL_LockTextureToSurface(SDL_Texture *texture,
    2.22 +                                            const SDL_Rect *rect,
    2.23 +                                            SDL_Surface **surface);
    2.24 +
    2.25 +/**
    2.26   *  \brief Unlock a texture, uploading the changes to video memory, if needed.
    2.27 + *         If SDL_LockTextureToSurface() was called for locking, the SDL surface is freed.
    2.28   *
    2.29   *  \sa SDL_LockTexture()
    2.30 + *  \sa SDL_LockTextureToSurface()
    2.31   */
    2.32  extern DECLSPEC void SDLCALL SDL_UnlockTexture(SDL_Texture * texture);
    2.33  
     3.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Fri Sep 27 20:15:42 2019 -0300
     3.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Mon Sep 30 20:58:44 2019 +0200
     3.3 @@ -315,6 +315,7 @@
     3.4  #define SDL_UpdateTexture SDL_UpdateTexture_REAL
     3.5  #define SDL_UpdateYUVTexture SDL_UpdateYUVTexture_REAL
     3.6  #define SDL_LockTexture SDL_LockTexture_REAL
     3.7 +#define SDL_LockTextureToSurface SDL_LockTextureToSurface_REAL
     3.8  #define SDL_UnlockTexture SDL_UnlockTexture_REAL
     3.9  #define SDL_RenderTargetSupported SDL_RenderTargetSupported_REAL
    3.10  #define SDL_SetRenderTarget SDL_SetRenderTarget_REAL
     4.1 --- a/src/dynapi/SDL_dynapi_procs.h	Fri Sep 27 20:15:42 2019 -0300
     4.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Mon Sep 30 20:58:44 2019 +0200
     4.3 @@ -346,6 +346,7 @@
     4.4  SDL_DYNAPI_PROC(int,SDL_UpdateTexture,(SDL_Texture *a, const SDL_Rect *b, const void *c, int d),(a,b,c,d),return)
     4.5  SDL_DYNAPI_PROC(int,SDL_UpdateYUVTexture,(SDL_Texture *a, const SDL_Rect *b, const Uint8 *c, int d, const Uint8 *e, int f, const Uint8 *g, int h),(a,b,c,d,e,f,g,h),return)
     4.6  SDL_DYNAPI_PROC(int,SDL_LockTexture,(SDL_Texture *a, const SDL_Rect *b, void **c, int *d),(a,b,c,d),return)
     4.7 +SDL_DYNAPI_PROC(int,SDL_LockTextureToSurface,(SDL_Texture *a, const SDL_Rect *b, SDL_Surface **c),(a,b,c),return)
     4.8  SDL_DYNAPI_PROC(void,SDL_UnlockTexture,(SDL_Texture *a),(a),)
     4.9  SDL_DYNAPI_PROC(SDL_bool,SDL_RenderTargetSupported,(SDL_Renderer *a),(a),return)
    4.10  SDL_DYNAPI_PROC(int,SDL_SetRenderTarget,(SDL_Renderer *a, SDL_Texture *b),(a,b),return)
     5.1 --- a/src/render/SDL_render.c	Fri Sep 27 20:15:42 2019 -0300
     5.2 +++ b/src/render/SDL_render.c	Mon Sep 30 20:58:44 2019 +0200
     5.3 @@ -1680,6 +1680,45 @@
     5.4      }
     5.5  }
     5.6  
     5.7 +int
     5.8 +SDL_LockTextureToSurface(SDL_Texture *texture, const SDL_Rect *rect,
     5.9 +                         SDL_Surface **surface)
    5.10 +{
    5.11 +    SDL_Rect r;
    5.12 +    void *pixels = NULL;
    5.13 +    int pitch, ret;
    5.14 +
    5.15 +    if (texture == NULL || surface == NULL) {
    5.16 +        return -1;
    5.17 +    }
    5.18 +
    5.19 +    if (rect == NULL) {
    5.20 +        r.x = 0;
    5.21 +        r.y = 0;
    5.22 +        r.w = texture->w;
    5.23 +        r.h = texture->h;
    5.24 +    } else {
    5.25 +        r.x = rect->x;
    5.26 +        r.y = rect->y;
    5.27 +        r.w = SDL_min(texture->w - rect->x, rect->w);
    5.28 +        r.h = SDL_min(texture->h - rect->y, rect->h);
    5.29 +    }
    5.30 +
    5.31 +    ret = SDL_LockTexture(texture, &r, &pixels, &pitch);
    5.32 +    if (ret < 0) {
    5.33 +        return ret;
    5.34 +    }
    5.35 +
    5.36 +    texture->locked_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, r.w, r.h, 0, pitch, texture->format);
    5.37 +    if (texture->locked_surface == NULL) {
    5.38 +        SDL_UnlockTexture(texture);
    5.39 +        return -1;
    5.40 +    }
    5.41 +
    5.42 +    *surface = texture->locked_surface;
    5.43 +    return 0;
    5.44 +}
    5.45 +
    5.46  static void
    5.47  SDL_UnlockTextureYUV(SDL_Texture * texture)
    5.48  {
    5.49 @@ -1738,6 +1777,9 @@
    5.50          SDL_Renderer *renderer = texture->renderer;
    5.51          renderer->UnlockTexture(renderer, texture);
    5.52      }
    5.53 +
    5.54 +    SDL_FreeSurface(texture->locked_surface);
    5.55 +    texture->locked_surface = NULL;
    5.56  }
    5.57  
    5.58  SDL_bool
    5.59 @@ -3090,6 +3132,10 @@
    5.60      SDL_free(texture->pixels);
    5.61  
    5.62      renderer->DestroyTexture(renderer, texture);
    5.63 +
    5.64 +    SDL_FreeSurface(texture->locked_surface);
    5.65 +    texture->locked_surface = NULL;
    5.66 +
    5.67      SDL_free(texture);
    5.68  }
    5.69  
     6.1 --- a/src/render/SDL_sysrender.h	Fri Sep 27 20:15:42 2019 -0300
     6.2 +++ b/src/render/SDL_sysrender.h	Mon Sep 30 20:58:44 2019 +0200
     6.3 @@ -60,6 +60,7 @@
     6.4      void *pixels;
     6.5      int pitch;
     6.6      SDL_Rect locked_rect;
     6.7 +    SDL_Surface *locked_surface;  /**< Locked region exposed as a SDL surface */
     6.8  
     6.9      Uint32 last_command_generation; /* last command queue generation this texture was in. */
    6.10