Added function to draw a straight line
This commit is contained in:
parent
27c6f1e0b6
commit
9691974629
|
@ -49,3 +49,40 @@ bool DrawUtils::draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y,
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
void DrawUtils::draw_line(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint16 width, Uint32 colour, SDL_Surface* dest)
|
||||
{
|
||||
float dx, dy, len;
|
||||
float cx, cy; // our current coords
|
||||
|
||||
SDL_Rect pen;
|
||||
|
||||
dx = static_cast<float>(x2-x1);
|
||||
dy = static_cast<float>(y2-y1);
|
||||
|
||||
len = sqrt(dx*dx + dy*dy);
|
||||
|
||||
// changing dx and dy's meanings. Now they represent the amount we move
|
||||
// each step in our drawing loop
|
||||
dx /= len;
|
||||
dy /= len;
|
||||
|
||||
int j = static_cast<int>(len);
|
||||
|
||||
cx = static_cast<float>(x1 - (width>>1));
|
||||
cy = static_cast<float>(y1 - (width>>1));
|
||||
|
||||
int i;
|
||||
for(i = 0; i < j; i++)
|
||||
{
|
||||
pen.x = (Sint16)cx;
|
||||
pen.y = (Sint16)cy;
|
||||
pen.w = width;
|
||||
pen.h = width;
|
||||
|
||||
SDL_FillRect(dest, &pen, colour);
|
||||
|
||||
cx += dx;
|
||||
cy += dy;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ class DrawUtils
|
|||
static bool draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y);
|
||||
static bool draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y,
|
||||
int x2, int y2, int w, int h);
|
||||
static void draw_line(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint16 width, Uint32 colour, SDL_Surface* dest);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue
Block a user