SDL  2.0
SDL_fillrect.c File Reference
#include "../SDL_internal.h"
#include "SDL_video.h"
#include "SDL_blit.h"
+ Include dependency graph for SDL_fillrect.c:

Go to the source code of this file.

Functions

static void SDL_FillRect1 (Uint8 *pixels, int pitch, Uint32 color, int w, int h)
 
static void SDL_FillRect2 (Uint8 *pixels, int pitch, Uint32 color, int w, int h)
 
static void SDL_FillRect3 (Uint8 *pixels, int pitch, Uint32 color, int w, int h)
 
static void SDL_FillRect4 (Uint8 *pixels, int pitch, Uint32 color, int w, int h)
 
int SDL_FillRect (SDL_Surface *dst, const SDL_Rect *rect, Uint32 color)
 
int SDL_FillRects (SDL_Surface *dst, const SDL_Rect *rects, int count, Uint32 color)
 

Function Documentation

◆ SDL_FillRect()

int SDL_FillRect ( SDL_Surface dst,
const SDL_Rect rect,
Uint32  color 
)

Performs a fast fill of the given rectangle with color.

If rect is NULL, the whole surface will be filled with color.

The color should be a pixel of the format used by the surface, and can be generated by the SDL_MapRGB() function.

Returns
0 on success, or -1 on error.

Definition at line 237 of file SDL_fillrect.c.

References SDL_PixelFormat::BitsPerPixel, SDL_PixelFormat::BytesPerPixel, SDL_Surface::clip_rect, SDL_Surface::format, SDL_Rect::h, SDL_Surface::pitch, SDL_Surface::pixels, SDL_FillRect1(), SDL_FillRect2(), SDL_FillRect3(), SDL_FillRect4(), SDL_HasSSE, SDL_IntersectRect, SDL_RectEmpty(), SDL_SetError, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_FillRects().

238 {
239  SDL_Rect clipped;
240  Uint8 *pixels;
241 
242  if (!dst) {
243  return SDL_SetError("Passed NULL destination surface");
244  }
245 
246  /* This function doesn't work on surfaces < 8 bpp */
247  if (dst->format->BitsPerPixel < 8) {
248  return SDL_SetError("SDL_FillRect(): Unsupported surface format");
249  }
250 
251  /* If 'rect' == NULL, then fill the whole surface */
252  if (rect) {
253  /* Perform clipping */
254  if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
255  return 0;
256  }
257  rect = &clipped;
258  } else {
259  rect = &dst->clip_rect;
260  /* Don't attempt to fill if the surface's clip_rect is empty */
261  if (SDL_RectEmpty(rect)) {
262  return 0;
263  }
264  }
265 
266  /* Perform software fill */
267  if (!dst->pixels) {
268  return SDL_SetError("SDL_FillRect(): You must lock the surface");
269  }
270 
271  pixels = (Uint8 *) dst->pixels + rect->y * dst->pitch +
272  rect->x * dst->format->BytesPerPixel;
273 
274  switch (dst->format->BytesPerPixel) {
275  case 1:
276  {
277  color |= (color << 8);
278  color |= (color << 16);
279 #ifdef __SSE__
280  if (SDL_HasSSE()) {
281  SDL_FillRect1SSE(pixels, dst->pitch, color, rect->w, rect->h);
282  break;
283  }
284 #endif
285  SDL_FillRect1(pixels, dst->pitch, color, rect->w, rect->h);
286  break;
287  }
288 
289  case 2:
290  {
291  color |= (color << 16);
292 #ifdef __SSE__
293  if (SDL_HasSSE()) {
294  SDL_FillRect2SSE(pixels, dst->pitch, color, rect->w, rect->h);
295  break;
296  }
297 #endif
298  SDL_FillRect2(pixels, dst->pitch, color, rect->w, rect->h);
299  break;
300  }
301 
302  case 3:
303  /* 24-bit RGB is a slow path, at least for now. */
304  {
305  SDL_FillRect3(pixels, dst->pitch, color, rect->w, rect->h);
306  break;
307  }
308 
309  case 4:
310  {
311 #ifdef __SSE__
312  if (SDL_HasSSE()) {
313  SDL_FillRect4SSE(pixels, dst->pitch, color, rect->w, rect->h);
314  break;
315  }
316 #endif
317  SDL_FillRect4(pixels, dst->pitch, color, rect->w, rect->h);
318  break;
319  }
320  }
321 
322  /* We're done! */
323  return 0;
324 }
Uint8 BytesPerPixel
Definition: SDL_pixels.h:320
#define SDL_IntersectRect
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Returns true if the rectangle has no area.
Definition: SDL_rect.h:82
static void SDL_FillRect3(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:197
void * pixels
Definition: SDL_surface.h:75
uint8_t Uint8
Definition: SDL_stdinc.h:157
Uint8 BitsPerPixel
Definition: SDL_pixels.h:319
int x
Definition: SDL_rect.h:66
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
int w
Definition: SDL_rect.h:67
SDL_Rect clip_rect
Definition: SDL_surface.h:85
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
static void SDL_FillRect2(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:173
int h
Definition: SDL_rect.h:67
GLuint color
static void SDL_FillRect4(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:225
static void SDL_FillRect1(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:134
#define SDL_HasSSE
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

◆ SDL_FillRect1()

static void SDL_FillRect1 ( Uint8 pixels,
int  pitch,
Uint32  color,
int  w,
int  h 
)
static

Definition at line 134 of file SDL_fillrect.c.

References NULL, and SDL_memset4().

Referenced by SDL_FillRect().

135 {
136  int n;
137  Uint8 *p = NULL;
138 
139  while (h--) {
140  n = w;
141  p = pixels;
142 
143  if (n > 3) {
144  switch ((uintptr_t) p & 3) {
145  case 1:
146  *p++ = (Uint8) color;
147  --n; /* fallthrough */
148  case 2:
149  *p++ = (Uint8) color;
150  --n; /* fallthrough */
151  case 3:
152  *p++ = (Uint8) color;
153  --n; /* fallthrough */
154  }
155  SDL_memset4(p, color, (n >> 2));
156  }
157  if (n & 3) {
158  p += (n & ~3);
159  switch (n & 3) {
160  case 3:
161  *p++ = (Uint8) color; /* fallthrough */
162  case 2:
163  *p++ = (Uint8) color; /* fallthrough */
164  case 1:
165  *p++ = (Uint8) color; /* fallthrough */
166  }
167  }
168  pixels += pitch;
169  }
170 }
GLfloat GLfloat GLfloat GLfloat h
GLfloat GLfloat p
SDL_FORCE_INLINE void SDL_memset4(void *dst, Uint32 val, size_t dwords)
Definition: SDL_stdinc.h:420
uint8_t Uint8
Definition: SDL_stdinc.h:157
GLubyte GLubyte GLubyte GLubyte w
unsigned int uintptr_t
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
#define NULL
Definition: begin_code.h:164
GLuint color
GLdouble n

◆ SDL_FillRect2()

static void SDL_FillRect2 ( Uint8 pixels,
int  pitch,
Uint32  color,
int  w,
int  h 
)
static

Definition at line 173 of file SDL_fillrect.c.

References NULL, and SDL_memset4().

Referenced by SDL_FillRect().

174 {
175  int n;
176  Uint16 *p = NULL;
177 
178  while (h--) {
179  n = w;
180  p = (Uint16 *) pixels;
181 
182  if (n > 1) {
183  if ((uintptr_t) p & 2) {
184  *p++ = (Uint16) color;
185  --n;
186  }
187  SDL_memset4(p, color, (n >> 1));
188  }
189  if (n & 1) {
190  p[n - 1] = (Uint16) color;
191  }
192  pixels += pitch;
193  }
194 }
GLfloat GLfloat GLfloat GLfloat h
GLfloat GLfloat p
SDL_FORCE_INLINE void SDL_memset4(void *dst, Uint32 val, size_t dwords)
Definition: SDL_stdinc.h:420
GLubyte GLubyte GLubyte GLubyte w
unsigned int uintptr_t
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
#define NULL
Definition: begin_code.h:164
GLuint color
GLdouble n
uint16_t Uint16
Definition: SDL_stdinc.h:169

◆ SDL_FillRect3()

static void SDL_FillRect3 ( Uint8 pixels,
int  pitch,
Uint32  color,
int  w,
int  h 
)
static

Definition at line 197 of file SDL_fillrect.c.

References NULL.

Referenced by SDL_FillRect().

198 {
199 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
200  Uint8 b1 = (Uint8) (color & 0xFF);
201  Uint8 b2 = (Uint8) ((color >> 8) & 0xFF);
202  Uint8 b3 = (Uint8) ((color >> 16) & 0xFF);
203 #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
204  Uint8 b1 = (Uint8) ((color >> 16) & 0xFF);
205  Uint8 b2 = (Uint8) ((color >> 8) & 0xFF);
206  Uint8 b3 = (Uint8) (color & 0xFF);
207 #endif
208  int n;
209  Uint8 *p = NULL;
210 
211  while (h--) {
212  n = w;
213  p = pixels;
214 
215  while (n--) {
216  *p++ = b1;
217  *p++ = b2;
218  *p++ = b3;
219  }
220  pixels += pitch;
221  }
222 }
GLfloat GLfloat GLfloat GLfloat h
GLfloat GLfloat p
uint8_t Uint8
Definition: SDL_stdinc.h:157
GLubyte GLubyte GLubyte GLubyte w
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
#define NULL
Definition: begin_code.h:164
GLuint color
GLdouble n

◆ SDL_FillRect4()

static void SDL_FillRect4 ( Uint8 pixels,
int  pitch,
Uint32  color,
int  w,
int  h 
)
static

Definition at line 225 of file SDL_fillrect.c.

References SDL_memset4().

Referenced by SDL_FillRect().

226 {
227  while (h--) {
229  pixels += pitch;
230  }
231 }
GLfloat GLfloat GLfloat GLfloat h
SDL_FORCE_INLINE void SDL_memset4(void *dst, Uint32 val, size_t dwords)
Definition: SDL_stdinc.h:420
GLubyte GLubyte GLubyte GLubyte w
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
GLuint color

◆ SDL_FillRects()

int SDL_FillRects ( SDL_Surface dst,
const SDL_Rect rects,
int  count,
Uint32  color 
)

Definition at line 327 of file SDL_fillrect.c.

References i, SDL_FillRect(), and SDL_SetError.

329 {
330  int i;
331  int status = 0;
332 
333  if (!rects) {
334  return SDL_SetError("SDL_FillRects() passed NULL rects");
335  }
336 
337  for (i = 0; i < count; ++i) {
338  status += SDL_FillRect(dst, &rects[i], color);
339  }
340  return status;
341 }
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
int SDL_FillRect(SDL_Surface *dst, const SDL_Rect *rect, Uint32 color)
Definition: SDL_fillrect.c:237
#define SDL_SetError
GLuint color