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.

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 }

References SDL_Rect::h, rect, 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().

◆ 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.

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 }

References NULL, and SDL_memset4().

Referenced by SDL_FillRect().

◆ 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.

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 }

References NULL, and SDL_memset4().

Referenced by SDL_FillRect().

◆ 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.

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 }

References NULL.

Referenced by SDL_FillRect().

◆ 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.

226 {
227  while (h--) {
229  pixels += pitch;
230  }
231 }

References SDL_memset4().

Referenced by SDL_FillRect().

◆ 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.

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 }

References i, SDL_FillRect(), and SDL_SetError.

Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179
SDL_FillRect
int SDL_FillRect(SDL_Surface *dst, const SDL_Rect *rect, Uint32 color)
Definition: SDL_fillrect.c:237
Uint16
uint16_t Uint16
Definition: SDL_stdinc.h:191
NULL
#define NULL
Definition: begin_code.h:164
count
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
SDL_FillRect4
static void SDL_FillRect4(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:225
SDL_IntersectRect
#define SDL_IntersectRect
Definition: SDL_dynapi_overrides.h:294
SDL_RectEmpty
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Returns true if the rectangle has no area.
Definition: SDL_rect.h:82
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1946
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_Rect::w
int w
Definition: SDL_rect.h:67
n
GLdouble n
Definition: SDL_opengl_glext.h:1952
dst
GLenum GLenum dst
Definition: SDL_opengl_glext.h:1737
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11090
SDL_FillRect1
static void SDL_FillRect1(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:134
SDL_FillRect2
static void SDL_FillRect2(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:173
color
GLuint color
Definition: SDL_opengl_glext.h:1148
SDL_Rect::y
int y
Definition: SDL_rect.h:66
SDL_Rect::h
int h
Definition: SDL_rect.h:67
rect
SDL_Rect rect
Definition: testrelative.c:27
pixels
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
SDL_memset4
SDL_FORCE_INLINE void SDL_memset4(void *dst, Uint32 val, size_t dwords)
Definition: SDL_stdinc.h:420
SDL_FillRect3
static void SDL_FillRect3(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:197
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_Rect
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:65
SDL_HasSSE
#define SDL_HasSSE
Definition: SDL_dynapi_overrides.h:107
rects
EGLSurface EGLint * rects
Definition: eglext.h:282
i
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
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:731
uintptr_t
unsigned int uintptr_t
Definition: SDL_config_windows.h:70