SDL  2.0
SDL_drawline.c File Reference
#include "../../SDL_internal.h"
#include "SDL_draw.h"
#include "SDL_drawline.h"
#include "SDL_drawpoint.h"
+ Include dependency graph for SDL_drawline.c:

Go to the source code of this file.

Typedefs

typedef void(* DrawLineFunc )(SDL_Surface *dst, int x1, int y1, int x2, int y2, Uint32 color, SDL_bool draw_end)

Functions

static void SDL_DrawLine1 (SDL_Surface *dst, int x1, int y1, int x2, int y2, Uint32 color, SDL_bool draw_end)
static void SDL_DrawLine2 (SDL_Surface *dst, int x1, int y1, int x2, int y2, Uint32 color, SDL_bool draw_end)
static void SDL_DrawLine4 (SDL_Surface *dst, int x1, int y1, int x2, int y2, Uint32 color, SDL_bool draw_end)
static DrawLineFunc SDL_CalculateDrawLineFunc (const SDL_PixelFormat *fmt)
int SDL_DrawLine (SDL_Surface *dst, int x1, int y1, int x2, int y2, Uint32 color)
int SDL_DrawLines (SDL_Surface *dst, const SDL_Point *points, int count, Uint32 color)

Typedef Documentation

typedef void(* DrawLineFunc)(SDL_Surface *dst, int x1, int y1, int x2, int y2, Uint32 color, SDL_bool draw_end)

Definition at line 120 of file SDL_drawline.c.

Function Documentation

static DrawLineFunc SDL_CalculateDrawLineFunc ( const SDL_PixelFormat fmt)
static

Definition at line 125 of file SDL_drawline.c.

References SDL_PixelFormat::BitsPerPixel, SDL_PixelFormat::BytesPerPixel, NULL, SDL_DrawLine1(), SDL_DrawLine2(), and SDL_DrawLine4().

Referenced by SDL_DrawLine(), and SDL_DrawLines().

{
switch (fmt->BytesPerPixel) {
case 1:
if (fmt->BitsPerPixel < 8) {
break;
}
return SDL_DrawLine1;
case 2:
return SDL_DrawLine2;
case 4:
return SDL_DrawLine4;
}
return NULL;
}
int SDL_DrawLine ( SDL_Surface dst,
int  x1,
int  y1,
int  x2,
int  y2,
Uint32  color 
)

Definition at line 142 of file SDL_drawline.c.

References SDL_Surface::clip_rect, SDL_Surface::format, SDL_CalculateDrawLineFunc(), SDL_IntersectRectAndLine, SDL_SetError, and SDL_TRUE.

{
if (!dst) {
return SDL_SetError("SDL_DrawLine(): Passed NULL destination surface");
}
if (!func) {
return SDL_SetError("SDL_DrawLine(): Unsupported surface format");
}
/* Perform clipping */
/* FIXME: We don't actually want to clip, as it may change line slope */
if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
return 0;
}
func(dst, x1, y1, x2, y2, color, SDL_TRUE);
return 0;
}
static void SDL_DrawLine1 ( SDL_Surface dst,
int  x1,
int  y1,
int  x2,
int  y2,
Uint32  color,
SDL_bool  draw_end 
)
static

Definition at line 31 of file SDL_drawline.c.

References ABS, BLINE, SDL_PixelFormat::BytesPerPixel, DLINE, DRAW_FASTSETPIXEL1, DRAW_FASTSETPIXELXY1, SDL_Surface::format, SDL_Surface::pitch, SDL_Surface::pixels, SDL_memset, and VLINE.

Referenced by SDL_CalculateDrawLineFunc().

{
if (y1 == y2) {
int length;
int pitch = (dst->pitch / dst->format->BytesPerPixel);
Uint8 *pixel;
if (x1 <= x2) {
pixel = (Uint8 *)dst->pixels + y1 * pitch + x1;
length = draw_end ? (x2-x1+1) : (x2-x1);
} else {
pixel = (Uint8 *)dst->pixels + y1 * pitch + x2;
if (!draw_end) {
++pixel;
}
length = draw_end ? (x1-x2+1) : (x1-x2);
}
SDL_memset(pixel, color, length);
} else if (x1 == x2) {
} else if (ABS(x1 - x2) == ABS(y1 - y2)) {
} else {
BLINE(x1, y1, x2, y2, DRAW_FASTSETPIXELXY1, draw_end);
}
}
static void SDL_DrawLine2 ( SDL_Surface dst,
int  x1,
int  y1,
int  x2,
int  y2,
Uint32  color,
SDL_bool  draw_end 
)
static

Definition at line 59 of file SDL_drawline.c.

References AALINE, ABS, DLINE, DRAW_FASTSETPIXEL2, DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY2_BLEND_RGB, DRAW_SETPIXELXY_BLEND_RGB555, DRAW_SETPIXELXY_BLEND_RGB565, SDL_Surface::format, HLINE, SDL_PixelFormat::Rmask, SDL_GetRGBA, and VLINE.

Referenced by SDL_CalculateDrawLineFunc().

{
if (y1 == y2) {
} else if (x1 == x2) {
} else if (ABS(x1 - x2) == ABS(y1 - y2)) {
} else {
Uint8 _r, _g, _b, _a;
const SDL_PixelFormat * fmt = dst->format;
SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
if (fmt->Rmask == 0x7C00) {
draw_end);
} else if (fmt->Rmask == 0xF800) {
draw_end);
} else {
draw_end);
}
}
}
static void SDL_DrawLine4 ( SDL_Surface dst,
int  x1,
int  y1,
int  x2,
int  y2,
Uint32  color,
SDL_bool  draw_end 
)
static

Definition at line 89 of file SDL_drawline.c.

References AALINE, ABS, SDL_PixelFormat::Amask, DLINE, DRAW_FASTSETPIXEL4, DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY4_BLEND_RGB, DRAW_SETPIXELXY_BLEND_ARGB8888, DRAW_SETPIXELXY_BLEND_RGB888, SDL_Surface::format, HLINE, SDL_PixelFormat::Rmask, SDL_GetRGBA, and VLINE.

Referenced by SDL_CalculateDrawLineFunc().

{
if (y1 == y2) {
} else if (x1 == x2) {
} else if (ABS(x1 - x2) == ABS(y1 - y2)) {
} else {
Uint8 _r, _g, _b, _a;
const SDL_PixelFormat * fmt = dst->format;
SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
if (fmt->Rmask == 0x00FF0000) {
if (!fmt->Amask) {
draw_end);
} else {
draw_end);
}
} else {
draw_end);
}
}
}
int SDL_DrawLines ( SDL_Surface dst,
const SDL_Point points,
int  count,
Uint32  color 
)

Definition at line 166 of file SDL_drawline.c.

References SDL_Surface::clip_rect, SDL_Surface::format, i, SDL_CalculateDrawLineFunc(), SDL_DrawPoint(), SDL_IntersectRectAndLine, SDL_SetError, SDL_Point::x, and SDL_Point::y.

Referenced by SW_RenderDrawLines().

{
int i;
int x1, y1;
int x2, y2;
SDL_bool draw_end;
if (!dst) {
return SDL_SetError("SDL_DrawLines(): Passed NULL destination surface");
}
if (!func) {
return SDL_SetError("SDL_DrawLines(): Unsupported surface format");
}
for (i = 1; i < count; ++i) {
x1 = points[i-1].x;
y1 = points[i-1].y;
x2 = points[i].x;
y2 = points[i].y;
/* Perform clipping */
/* FIXME: We don't actually want to clip, as it may change line slope */
if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
continue;
}
/* Draw the end if it was clipped */
draw_end = (x2 != points[i].x || y2 != points[i].y);
func(dst, x1, y1, x2, y2, color, draw_end);
}
if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
SDL_DrawPoint(dst, points[count-1].x, points[count-1].y, color);
}
return 0;
}