Added function SDL_RenderSetClipRect()
authorSam Lantinga <slouken@libsdl.org>
Mon, 07 Feb 2011 20:06:26 -0800
changeset 52242178ffe17222
parent 5223 ab0d7cecc0f6
child 5225 5d01d426f2ea
Added function SDL_RenderSetClipRect()
include/SDL_render.h
src/render/SDL_render.c
src/render/SDL_sysrender.h
src/render/direct3d/SDL_render_d3d.c
src/render/opengl/SDL_glfuncs.h
src/render/opengl/SDL_render_gl.c
src/render/opengles/SDL_render_gles.c
src/render/opengles2/SDL_render_gles2.c
src/render/software/SDL_render_sw.c
     1.1 --- a/include/SDL_render.h	Mon Feb 07 20:05:52 2011 -0800
     1.2 +++ b/include/SDL_render.h	Mon Feb 07 20:06:26 2011 -0800
     1.3 @@ -364,6 +364,18 @@
     1.4  extern DECLSPEC void SDLCALL SDL_UnlockTexture(SDL_Texture * texture);
     1.5  
     1.6  /**
     1.7 + *  \brief Set the clipping rectangle for rendering on the current target
     1.8 + *
     1.9 + *  \param rect The rectangle to clip rendering to, or NULL to disable clipping.
    1.10 + *
    1.11 + *  The contents of the window are not defined after calling
    1.12 + *  SDL_RenderPresent(), so you should clear the clip rectangle and draw
    1.13 + *  over the entire window each frame.
    1.14 + */
    1.15 +extern DECLSPEC void SDLCALL SDL_RenderSetClipRect(SDL_Renderer * renderer,
    1.16 +                                                   const SDL_Rect * rect);
    1.17 +
    1.18 +/**
    1.19   *  \brief Set the color used for drawing operations (Fill and Line).
    1.20   *  
    1.21   *  \param r The red value used to draw on the rendering target.
     2.1 --- a/src/render/SDL_render.c	Mon Feb 07 20:05:52 2011 -0800
     2.2 +++ b/src/render/SDL_render.c	Mon Feb 07 20:06:26 2011 -0800
     2.3 @@ -725,6 +725,14 @@
     2.4      }
     2.5  }
     2.6  
     2.7 +void
     2.8 +SDL_RenderSetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
     2.9 +{
    2.10 +    CHECK_RENDERER_MAGIC(renderer, );
    2.11 +
    2.12 +    renderer->SetClipRect(renderer, rect);
    2.13 +}
    2.14 +
    2.15  int
    2.16  SDL_SetRenderDrawColor(SDL_Renderer * renderer,
    2.17                         Uint8 r, Uint8 g, Uint8 b, Uint8 a)
     3.1 --- a/src/render/SDL_sysrender.h	Mon Feb 07 20:05:52 2011 -0800
     3.2 +++ b/src/render/SDL_sysrender.h	Mon Feb 07 20:06:26 2011 -0800
     3.3 @@ -78,6 +78,7 @@
     3.4      int (*LockTexture) (SDL_Renderer * renderer, SDL_Texture * texture,
     3.5                          const SDL_Rect * rect, void **pixels, int *pitch);
     3.6      void (*UnlockTexture) (SDL_Renderer * renderer, SDL_Texture * texture);
     3.7 +    void (*SetClipRect) (SDL_Renderer * renderer, const SDL_Rect *rect);
     3.8      int (*RenderClear) (SDL_Renderer * renderer);
     3.9      int (*RenderDrawPoints) (SDL_Renderer * renderer, const SDL_Point * points,
    3.10                               int count);
     4.1 --- a/src/render/direct3d/SDL_render_d3d.c	Mon Feb 07 20:05:52 2011 -0800
     4.2 +++ b/src/render/direct3d/SDL_render_d3d.c	Mon Feb 07 20:06:26 2011 -0800
     4.3 @@ -96,6 +96,7 @@
     4.4  static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
     4.5                             const SDL_Rect * rect, void **pixels, int *pitch);
     4.6  static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
     4.7 +static void D3D_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
     4.8  static int D3D_RenderDrawPoints(SDL_Renderer * renderer,
     4.9                                  const SDL_Point * points, int count);
    4.10  static int D3D_RenderDrawLines(SDL_Renderer * renderer,
    4.11 @@ -308,6 +309,7 @@
    4.12      renderer->UpdateTexture = D3D_UpdateTexture;
    4.13      renderer->LockTexture = D3D_LockTexture;
    4.14      renderer->UnlockTexture = D3D_UnlockTexture;
    4.15 +    renderer->SetClipRect = D3D_SetClipRect;
    4.16      renderer->RenderDrawPoints = D3D_RenderDrawPoints;
    4.17      renderer->RenderDrawLines = D3D_RenderDrawLines;
    4.18      renderer->RenderFillRects = D3D_RenderFillRects;
    4.19 @@ -602,6 +604,27 @@
    4.20  }
    4.21  
    4.22  static void
    4.23 +D3D_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
    4.24 +{
    4.25 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
    4.26 +
    4.27 +    if (rect) {
    4.28 +        RECT d3drect;
    4.29 +
    4.30 +        d3drect.left = rect->x;
    4.31 +        d3drect.right = rect->x + rect->w;
    4.32 +        d3drect.top = rect->y;
    4.33 +        d3drect.bottom = rect->y + rect->h;
    4.34 +        IDirect3DDevice9_SetScissorRect(data->device, &d3drect);
    4.35 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE,
    4.36 +                                        TRUE);
    4.37 +    } else {
    4.38 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE,
    4.39 +                                        FALSE);
    4.40 +    }
    4.41 +}
    4.42 +
    4.43 +static void
    4.44  D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
    4.45  {
    4.46      switch (blendMode) {
     5.1 --- a/src/render/opengl/SDL_glfuncs.h	Mon Feb 07 20:05:52 2011 -0800
     5.2 +++ b/src/render/opengl/SDL_glfuncs.h	Mon Feb 07 20:06:26 2011 -0800
     5.3 @@ -337,8 +337,7 @@
     5.4                  (GLfloat angle, GLfloat x, GLfloat y, GLfloat z))
     5.5  SDL_PROC_UNUSED(void, glScaled, (GLdouble x, GLdouble y, GLdouble z))
     5.6  SDL_PROC_UNUSED(void, glScalef, (GLfloat x, GLfloat y, GLfloat z))
     5.7 -SDL_PROC_UNUSED(void, glScissor,
     5.8 -                (GLint x, GLint y, GLsizei width, GLsizei height))
     5.9 +SDL_PROC(void, glScissor, (GLint x, GLint y, GLsizei width, GLsizei height))
    5.10  SDL_PROC_UNUSED(void, glSelectBuffer, (GLsizei size, GLuint * buffer))
    5.11  SDL_PROC_UNUSED(void, glShadeModel, (GLenum mode))
    5.12  SDL_PROC_UNUSED(void, glStencilFunc, (GLenum func, GLint ref, GLuint mask))
     6.1 --- a/src/render/opengl/SDL_render_gl.c	Mon Feb 07 20:05:52 2011 -0800
     6.2 +++ b/src/render/opengl/SDL_render_gl.c	Mon Feb 07 20:06:26 2011 -0800
     6.3 @@ -52,6 +52,7 @@
     6.4  static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
     6.5                            const SDL_Rect * rect, void **pixels, int *pitch);
     6.6  static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
     6.7 +static void GL_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
     6.8  static int GL_RenderClear(SDL_Renderer * renderer);
     6.9  static int GL_RenderDrawPoints(SDL_Renderer * renderer,
    6.10                                 const SDL_Point * points, int count);
    6.11 @@ -199,6 +200,7 @@
    6.12      renderer->UpdateTexture = GL_UpdateTexture;
    6.13      renderer->LockTexture = GL_LockTexture;
    6.14      renderer->UnlockTexture = GL_UnlockTexture;
    6.15 +    renderer->SetClipRect = GL_SetClipRect;
    6.16      renderer->RenderClear = GL_RenderClear;
    6.17      renderer->RenderDrawPoints = GL_RenderDrawPoints;
    6.18      renderer->RenderDrawLines = GL_RenderDrawLines;
    6.19 @@ -511,6 +513,24 @@
    6.20  }
    6.21  
    6.22  static void
    6.23 +GL_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
    6.24 +{
    6.25 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
    6.26 +
    6.27 +    GL_ActivateRenderer(renderer);
    6.28 +
    6.29 +    if (rect) {
    6.30 +        int w, h;
    6.31 +
    6.32 +        SDL_GetWindowSize(renderer->window, &w, &h);
    6.33 +        data->glScissor(rect->x, (h-(rect->y+rect->h)), rect->w, rect->h);
    6.34 +        data->glEnable(GL_SCISSOR_TEST);
    6.35 +    } else {
    6.36 +        data->glDisable(GL_SCISSOR_TEST);
    6.37 +    }
    6.38 +}
    6.39 +
    6.40 +static void
    6.41  GL_SetBlendMode(GL_RenderData * data, int blendMode)
    6.42  {
    6.43      if (blendMode != data->blendMode) {
     7.1 --- a/src/render/opengles/SDL_render_gles.c	Mon Feb 07 20:05:52 2011 -0800
     7.2 +++ b/src/render/opengles/SDL_render_gles.c	Mon Feb 07 20:06:26 2011 -0800
     7.3 @@ -56,6 +56,7 @@
     7.4                              const SDL_Rect * rect, void **pixels, int *pitch);
     7.5  static void GLES_UnlockTexture(SDL_Renderer * renderer,
     7.6                                 SDL_Texture * texture);
     7.7 +static void GLES_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
     7.8  static int GLES_RenderDrawPoints(SDL_Renderer * renderer,
     7.9                                   const SDL_Point * points, int count);
    7.10  static int GLES_RenderDrawLines(SDL_Renderer * renderer,
    7.11 @@ -172,6 +173,7 @@
    7.12      renderer->UpdateTexture = GLES_UpdateTexture;
    7.13      renderer->LockTexture = GLES_LockTexture;
    7.14      renderer->UnlockTexture = GLES_UnlockTexture;
    7.15 +    renderer->SetClipRect = GLES_SetClipRect;
    7.16      renderer->RenderDrawPoints = GLES_RenderDrawPoints;
    7.17      renderer->RenderDrawLines = GLES_RenderDrawLines;
    7.18      renderer->RenderFillRects = GLES_RenderFillRects;
    7.19 @@ -444,6 +446,22 @@
    7.20  }
    7.21  
    7.22  static void
    7.23 +GLES_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
    7.24 +{
    7.25 +    GL_ActivateRenderer(renderer);
    7.26 +
    7.27 +    if (rect) {
    7.28 +        int w, h;
    7.29 +
    7.30 +        SDL_GetWindowSize(renderer->window, &w, &h);
    7.31 +        glScissor(rect->x, (h-(rect->y+rect->h)), rect->w, rect->h);
    7.32 +        glEnable(GL_SCISSOR_TEST);
    7.33 +    } else {
    7.34 +        glDisable(GL_SCISSOR_TEST);
    7.35 +    }
    7.36 +}
    7.37 +
    7.38 +static void
    7.39  GLES_SetBlendMode(GLES_RenderData * data, int blendMode)
    7.40  {
    7.41      if (blendMode != data->blendMode) {
     8.1 --- a/src/render/opengles2/SDL_render_gles2.c	Mon Feb 07 20:05:52 2011 -0800
     8.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Mon Feb 07 20:06:26 2011 -0800
     8.3 @@ -133,10 +133,10 @@
     8.4   * Renderer state APIs                                                                           *
     8.5   *************************************************************************************************/
     8.6  
     8.7 +static int GLES2_ActivateRenderer(SDL_Renderer *renderer);
     8.8  static void GLES2_WindowEvent(SDL_Renderer * renderer,
     8.9                                const SDL_WindowEvent *event);
    8.10 -static int GLES2_ActivateRenderer(SDL_Renderer *renderer);
    8.11 -static int GLES2_DisplayModeChanged(SDL_Renderer *renderer);
    8.12 +static void GLES2_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
    8.13  static void GLES2_DestroyRenderer(SDL_Renderer *renderer);
    8.14  
    8.15  static SDL_GLContext SDL_CurrentContext = NULL;
    8.16 @@ -179,6 +179,22 @@
    8.17  }
    8.18  
    8.19  static void
    8.20 +GLES2_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
    8.21 +{
    8.22 +    GLES2_ActivateRenderer(renderer);
    8.23 +
    8.24 +    if (rect) {
    8.25 +        int w, h;
    8.26 +
    8.27 +        SDL_GetWindowSize(renderer->window, &w, &h);
    8.28 +        glScissor(rect->x, (h-(rect->y+rect->h)), rect->w, rect->h);
    8.29 +        glEnable(GL_SCISSOR_TEST);
    8.30 +    } else {
    8.31 +        glDisable(GL_SCISSOR_TEST);
    8.32 +    }
    8.33 +}
    8.34 +
    8.35 +static void
    8.36  GLES2_DestroyRenderer(SDL_Renderer *renderer)
    8.37  {
    8.38      GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
    8.39 @@ -761,6 +777,8 @@
    8.40   * Rendering functions                                                                           *
    8.41   *************************************************************************************************/
    8.42  
    8.43 +static const float inv255f = 1.0f / 255.0f;
    8.44 +
    8.45  static int GLES2_RenderClear(SDL_Renderer *renderer);
    8.46  static int GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int count);
    8.47  static int GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count);
    8.48 @@ -772,10 +790,10 @@
    8.49  static int
    8.50  GLES2_RenderClear(SDL_Renderer *renderer)
    8.51  {
    8.52 -    float r = (float)renderer->r / 255.0f;
    8.53 -    float g = (float)renderer->g / 255.0f;
    8.54 -    float b = (float)renderer->b / 255.0f;
    8.55 -    float a = (float)renderer->a / 255.0f;
    8.56 +    float r = (float)renderer->r * inv255f;
    8.57 +    float g = (float)renderer->g * inv255f;
    8.58 +    float b = (float)renderer->b * inv255f;
    8.59 +    float a = (float)renderer->a * inv255f;
    8.60  
    8.61      GLES2_ActivateRenderer(renderer);
    8.62  
    8.63 @@ -832,10 +850,10 @@
    8.64      locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
    8.65      glGetError();
    8.66      glUniform4f(locColor,
    8.67 -                renderer->r / 255.0f,
    8.68 -                renderer->g / 255.0f,
    8.69 -                renderer->b / 255.0f,
    8.70 -                alpha / 255.0f);
    8.71 +                renderer->r * inv255f,
    8.72 +                renderer->g * inv255f,
    8.73 +                renderer->b * inv255f,
    8.74 +                alpha * inv255f);
    8.75  
    8.76      /* Configure the correct blend mode */
    8.77      GLES2_SetBlendMode(blendMode);
    8.78 @@ -886,10 +904,10 @@
    8.79      locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
    8.80      glGetError();
    8.81      glUniform4f(locColor,
    8.82 -                renderer->r / 255.0f,
    8.83 -                renderer->g / 255.0f,
    8.84 -                renderer->b / 255.0f,
    8.85 -                alpha / 255.0f);
    8.86 +                renderer->r * inv255f,
    8.87 +                renderer->g * inv255f,
    8.88 +                renderer->b * inv255f,
    8.89 +                alpha * inv255f);
    8.90  
    8.91      /* Configure the correct blend mode */
    8.92      GLES2_SetBlendMode(blendMode);
    8.93 @@ -940,10 +958,10 @@
    8.94      locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
    8.95      glGetError();
    8.96      glUniform4f(locColor,
    8.97 -                renderer->r / 255.0f,
    8.98 -                renderer->g / 255.0f,
    8.99 -                renderer->b / 255.0f,
   8.100 -                alpha / 255.0f);
   8.101 +                renderer->r * inv255f,
   8.102 +                renderer->g * inv255f,
   8.103 +                renderer->b * inv255f,
   8.104 +                alpha * inv255f);
   8.105  
   8.106      /* Configure the correct blend mode */
   8.107      GLES2_SetBlendMode(blendMode);
   8.108 @@ -1014,10 +1032,10 @@
   8.109      /* Configure color modulation */
   8.110      locModulation = rdata->current_program->uniform_locations[GLES2_UNIFORM_MODULATION];
   8.111      glUniform4f(locModulation,
   8.112 -                texture->r / 255.0f,
   8.113 -                texture->g / 255.0f,
   8.114 -                texture->b / 255.0f,
   8.115 -                alpha / 255.0f);
   8.116 +                texture->r * inv255f,
   8.117 +                texture->g * inv255f,
   8.118 +                texture->b * inv255f,
   8.119 +                alpha * inv255f);
   8.120  
   8.121      /* Emit the textured quad */
   8.122      glEnableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
   8.123 @@ -1066,6 +1084,9 @@
   8.124  
   8.125  #define GL_NVIDIA_PLATFORM_BINARY_NV 0x890B
   8.126  
   8.127 +/* Used to re-create the window with OpenGL capability */
   8.128 +extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
   8.129 +
   8.130  static SDL_Renderer *
   8.131  GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
   8.132  {
   8.133 @@ -1168,6 +1189,7 @@
   8.134      renderer->UpdateTexture       = &GLES2_UpdateTexture;
   8.135      renderer->LockTexture         = &GLES2_LockTexture;
   8.136      renderer->UnlockTexture       = &GLES2_UnlockTexture;
   8.137 +    renderer->SetClipRect         = &GLES2_SetClipRect;
   8.138      renderer->RenderClear         = &GLES2_RenderClear;
   8.139      renderer->RenderDrawPoints    = &GLES2_RenderDrawPoints;
   8.140      renderer->RenderDrawLines     = &GLES2_RenderDrawLines;
     9.1 --- a/src/render/software/SDL_render_sw.c	Mon Feb 07 20:05:52 2011 -0800
     9.2 +++ b/src/render/software/SDL_render_sw.c	Mon Feb 07 20:06:26 2011 -0800
     9.3 @@ -50,6 +50,7 @@
     9.4  static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
     9.5                            const SDL_Rect * rect, void **pixels, int *pitch);
     9.6  static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
     9.7 +static void SW_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
     9.8  static int SW_RenderDrawPoints(SDL_Renderer * renderer,
     9.9                                 const SDL_Point * points, int count);
    9.10  static int SW_RenderDrawLines(SDL_Renderer * renderer,
    9.11 @@ -125,6 +126,7 @@
    9.12      renderer->UpdateTexture = SW_UpdateTexture;
    9.13      renderer->LockTexture = SW_LockTexture;
    9.14      renderer->UnlockTexture = SW_UnlockTexture;
    9.15 +    renderer->SetClipRect = SW_SetClipRect;
    9.16      renderer->DestroyTexture = SW_DestroyTexture;
    9.17      renderer->RenderDrawPoints = SW_RenderDrawPoints;
    9.18      renderer->RenderDrawLines = SW_RenderDrawLines;
    9.19 @@ -266,6 +268,17 @@
    9.20  {
    9.21  }
    9.22  
    9.23 +static void
    9.24 +SW_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
    9.25 +{
    9.26 +    SDL_Surface *surface = SW_ActivateRenderer(renderer);
    9.27 +
    9.28 +    if (!surface) {
    9.29 +        return;
    9.30 +    }
    9.31 +    SDL_SetClipRect(surface, rect);
    9.32 +}
    9.33 +
    9.34  static int
    9.35  SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
    9.36                      int count)