Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Added function SDL_RenderSetClipRect()
  • Loading branch information
slouken committed Feb 8, 2011
1 parent 58f42d3 commit 96c491f
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 24 deletions.
12 changes: 12 additions & 0 deletions include/SDL_render.h
Expand Up @@ -363,6 +363,18 @@ extern DECLSPEC int SDLCALL SDL_LockTexture(SDL_Texture * texture,
*/
extern DECLSPEC void SDLCALL SDL_UnlockTexture(SDL_Texture * texture);

/**
* \brief Set the clipping rectangle for rendering on the current target
*
* \param rect The rectangle to clip rendering to, or NULL to disable clipping.
*
* The contents of the window are not defined after calling
* SDL_RenderPresent(), so you should clear the clip rectangle and draw
* over the entire window each frame.
*/
extern DECLSPEC void SDLCALL SDL_RenderSetClipRect(SDL_Renderer * renderer,
const SDL_Rect * rect);

/**
* \brief Set the color used for drawing operations (Fill and Line).
*
Expand Down
8 changes: 8 additions & 0 deletions src/render/SDL_render.c
Expand Up @@ -725,6 +725,14 @@ SDL_UnlockTexture(SDL_Texture * texture)
}
}

void
SDL_RenderSetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
{
CHECK_RENDERER_MAGIC(renderer, );

renderer->SetClipRect(renderer, rect);
}

int
SDL_SetRenderDrawColor(SDL_Renderer * renderer,
Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Expand Down
1 change: 1 addition & 0 deletions src/render/SDL_sysrender.h
Expand Up @@ -78,6 +78,7 @@ struct SDL_Renderer
int (*LockTexture) (SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch);
void (*UnlockTexture) (SDL_Renderer * renderer, SDL_Texture * texture);
void (*SetClipRect) (SDL_Renderer * renderer, const SDL_Rect *rect);
int (*RenderClear) (SDL_Renderer * renderer);
int (*RenderDrawPoints) (SDL_Renderer * renderer, const SDL_Point * points,
int count);
Expand Down
23 changes: 23 additions & 0 deletions src/render/direct3d/SDL_render_d3d.c
Expand Up @@ -96,6 +96,7 @@ static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch);
static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void D3D_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
static int D3D_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int D3D_RenderDrawLines(SDL_Renderer * renderer,
Expand Down Expand Up @@ -308,6 +309,7 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
renderer->UpdateTexture = D3D_UpdateTexture;
renderer->LockTexture = D3D_LockTexture;
renderer->UnlockTexture = D3D_UnlockTexture;
renderer->SetClipRect = D3D_SetClipRect;
renderer->RenderDrawPoints = D3D_RenderDrawPoints;
renderer->RenderDrawLines = D3D_RenderDrawLines;
renderer->RenderFillRects = D3D_RenderFillRects;
Expand Down Expand Up @@ -601,6 +603,27 @@ D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
IDirect3DTexture9_UnlockRect(data->texture, 0);
}

static void
D3D_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
{
D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;

if (rect) {
RECT d3drect;

d3drect.left = rect->x;
d3drect.right = rect->x + rect->w;
d3drect.top = rect->y;
d3drect.bottom = rect->y + rect->h;
IDirect3DDevice9_SetScissorRect(data->device, &d3drect);
IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE,
TRUE);
} else {
IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE,
FALSE);
}
}

static void
D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
{
Expand Down
3 changes: 1 addition & 2 deletions src/render/opengl/SDL_glfuncs.h
Expand Up @@ -337,8 +337,7 @@ SDL_PROC_UNUSED(void, glRotatef,
(GLfloat angle, GLfloat x, GLfloat y, GLfloat z))
SDL_PROC_UNUSED(void, glScaled, (GLdouble x, GLdouble y, GLdouble z))
SDL_PROC_UNUSED(void, glScalef, (GLfloat x, GLfloat y, GLfloat z))
SDL_PROC_UNUSED(void, glScissor,
(GLint x, GLint y, GLsizei width, GLsizei height))
SDL_PROC(void, glScissor, (GLint x, GLint y, GLsizei width, GLsizei height))
SDL_PROC_UNUSED(void, glSelectBuffer, (GLsizei size, GLuint * buffer))
SDL_PROC_UNUSED(void, glShadeModel, (GLenum mode))
SDL_PROC_UNUSED(void, glStencilFunc, (GLenum func, GLint ref, GLuint mask))
Expand Down
20 changes: 20 additions & 0 deletions src/render/opengl/SDL_render_gl.c
Expand Up @@ -52,6 +52,7 @@ static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch);
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void GL_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
static int GL_RenderClear(SDL_Renderer * renderer);
static int GL_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_Point * points, int count);
Expand Down Expand Up @@ -199,6 +200,7 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
renderer->UpdateTexture = GL_UpdateTexture;
renderer->LockTexture = GL_LockTexture;
renderer->UnlockTexture = GL_UnlockTexture;
renderer->SetClipRect = GL_SetClipRect;
renderer->RenderClear = GL_RenderClear;
renderer->RenderDrawPoints = GL_RenderDrawPoints;
renderer->RenderDrawLines = GL_RenderDrawLines;
Expand Down Expand Up @@ -510,6 +512,24 @@ GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
renderdata->glDisable(data->type);
}

static void
GL_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;

GL_ActivateRenderer(renderer);

if (rect) {
int w, h;

SDL_GetWindowSize(renderer->window, &w, &h);
data->glScissor(rect->x, (h-(rect->y+rect->h)), rect->w, rect->h);
data->glEnable(GL_SCISSOR_TEST);
} else {
data->glDisable(GL_SCISSOR_TEST);
}
}

static void
GL_SetBlendMode(GL_RenderData * data, int blendMode)
{
Expand Down
18 changes: 18 additions & 0 deletions src/render/opengles/SDL_render_gles.c
Expand Up @@ -56,6 +56,7 @@ static int GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch);
static void GLES_UnlockTexture(SDL_Renderer * renderer,
SDL_Texture * texture);
static void GLES_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
static int GLES_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int GLES_RenderDrawLines(SDL_Renderer * renderer,
Expand Down Expand Up @@ -172,6 +173,7 @@ GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
renderer->UpdateTexture = GLES_UpdateTexture;
renderer->LockTexture = GLES_LockTexture;
renderer->UnlockTexture = GLES_UnlockTexture;
renderer->SetClipRect = GLES_SetClipRect;
renderer->RenderDrawPoints = GLES_RenderDrawPoints;
renderer->RenderDrawLines = GLES_RenderDrawLines;
renderer->RenderFillRects = GLES_RenderFillRects;
Expand Down Expand Up @@ -443,6 +445,22 @@ GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
glDisable(data->type);
}

static void
GLES_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
{
GL_ActivateRenderer(renderer);

if (rect) {
int w, h;

SDL_GetWindowSize(renderer->window, &w, &h);
glScissor(rect->x, (h-(rect->y+rect->h)), rect->w, rect->h);
glEnable(GL_SCISSOR_TEST);
} else {
glDisable(GL_SCISSOR_TEST);
}
}

static void
GLES_SetBlendMode(GLES_RenderData * data, int blendMode)
{
Expand Down
66 changes: 44 additions & 22 deletions src/render/opengles2/SDL_render_gles2.c
Expand Up @@ -133,10 +133,10 @@ typedef struct GLES2_DriverContext
* Renderer state APIs *
*************************************************************************************************/

static int GLES2_ActivateRenderer(SDL_Renderer *renderer);
static void GLES2_WindowEvent(SDL_Renderer * renderer,
const SDL_WindowEvent *event);
static int GLES2_ActivateRenderer(SDL_Renderer *renderer);
static int GLES2_DisplayModeChanged(SDL_Renderer *renderer);
static void GLES2_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
static void GLES2_DestroyRenderer(SDL_Renderer *renderer);

static SDL_GLContext SDL_CurrentContext = NULL;
Expand Down Expand Up @@ -178,6 +178,22 @@ GLES2_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
}
}

static void
GLES2_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
{
GLES2_ActivateRenderer(renderer);

if (rect) {
int w, h;

SDL_GetWindowSize(renderer->window, &w, &h);
glScissor(rect->x, (h-(rect->y+rect->h)), rect->w, rect->h);
glEnable(GL_SCISSOR_TEST);
} else {
glDisable(GL_SCISSOR_TEST);
}
}

static void
GLES2_DestroyRenderer(SDL_Renderer *renderer)
{
Expand Down Expand Up @@ -761,6 +777,8 @@ GLES2_SetOrthographicProjection(SDL_Renderer *renderer)
* Rendering functions *
*************************************************************************************************/

static const float inv255f = 1.0f / 255.0f;

static int GLES2_RenderClear(SDL_Renderer *renderer);
static int GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int count);
static int GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count);
Expand All @@ -772,10 +790,10 @@ static void GLES2_RenderPresent(SDL_Renderer *renderer);
static int
GLES2_RenderClear(SDL_Renderer *renderer)
{
float r = (float)renderer->r / 255.0f;
float g = (float)renderer->g / 255.0f;
float b = (float)renderer->b / 255.0f;
float a = (float)renderer->a / 255.0f;
float r = (float)renderer->r * inv255f;
float g = (float)renderer->g * inv255f;
float b = (float)renderer->b * inv255f;
float a = (float)renderer->a * inv255f;

GLES2_ActivateRenderer(renderer);

Expand Down Expand Up @@ -832,10 +850,10 @@ GLES2_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points, int coun
locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
glGetError();
glUniform4f(locColor,
renderer->r / 255.0f,
renderer->g / 255.0f,
renderer->b / 255.0f,
alpha / 255.0f);
renderer->r * inv255f,
renderer->g * inv255f,
renderer->b * inv255f,
alpha * inv255f);

/* Configure the correct blend mode */
GLES2_SetBlendMode(blendMode);
Expand Down Expand Up @@ -886,10 +904,10 @@ GLES2_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points, int count
locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
glGetError();
glUniform4f(locColor,
renderer->r / 255.0f,
renderer->g / 255.0f,
renderer->b / 255.0f,
alpha / 255.0f);
renderer->r * inv255f,
renderer->g * inv255f,
renderer->b * inv255f,
alpha * inv255f);

/* Configure the correct blend mode */
GLES2_SetBlendMode(blendMode);
Expand Down Expand Up @@ -940,10 +958,10 @@ GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect **rects, int count)
locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
glGetError();
glUniform4f(locColor,
renderer->r / 255.0f,
renderer->g / 255.0f,
renderer->b / 255.0f,
alpha / 255.0f);
renderer->r * inv255f,
renderer->g * inv255f,
renderer->b * inv255f,
alpha * inv255f);

/* Configure the correct blend mode */
GLES2_SetBlendMode(blendMode);
Expand Down Expand Up @@ -1014,10 +1032,10 @@ GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *s
/* Configure color modulation */
locModulation = rdata->current_program->uniform_locations[GLES2_UNIFORM_MODULATION];
glUniform4f(locModulation,
texture->r / 255.0f,
texture->g / 255.0f,
texture->b / 255.0f,
alpha / 255.0f);
texture->r * inv255f,
texture->g * inv255f,
texture->b * inv255f,
alpha * inv255f);

/* Emit the textured quad */
glEnableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);
Expand Down Expand Up @@ -1066,6 +1084,9 @@ GLES2_RenderPresent(SDL_Renderer *renderer)

#define GL_NVIDIA_PLATFORM_BINARY_NV 0x890B

/* Used to re-create the window with OpenGL capability */
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);

static SDL_Renderer *
GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
{
Expand Down Expand Up @@ -1168,6 +1189,7 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
renderer->UpdateTexture = &GLES2_UpdateTexture;
renderer->LockTexture = &GLES2_LockTexture;
renderer->UnlockTexture = &GLES2_UnlockTexture;
renderer->SetClipRect = &GLES2_SetClipRect;
renderer->RenderClear = &GLES2_RenderClear;
renderer->RenderDrawPoints = &GLES2_RenderDrawPoints;
renderer->RenderDrawLines = &GLES2_RenderDrawLines;
Expand Down
13 changes: 13 additions & 0 deletions src/render/software/SDL_render_sw.c
Expand Up @@ -50,6 +50,7 @@ static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch);
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void SW_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
static int SW_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int SW_RenderDrawLines(SDL_Renderer * renderer,
Expand Down Expand Up @@ -125,6 +126,7 @@ SW_CreateRendererForSurface(SDL_Surface * surface)
renderer->UpdateTexture = SW_UpdateTexture;
renderer->LockTexture = SW_LockTexture;
renderer->UnlockTexture = SW_UnlockTexture;
renderer->SetClipRect = SW_SetClipRect;
renderer->DestroyTexture = SW_DestroyTexture;
renderer->RenderDrawPoints = SW_RenderDrawPoints;
renderer->RenderDrawLines = SW_RenderDrawLines;
Expand Down Expand Up @@ -266,6 +268,17 @@ SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
}

static void
SW_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
{
SDL_Surface *surface = SW_ActivateRenderer(renderer);

if (!surface) {
return;
}
SDL_SetClipRect(surface, rect);
}

static int
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Expand Down

0 comments on commit 96c491f

Please sign in to comment.