diff --git a/drawutils.cpp b/drawutils.cpp index e1f3b30..2d28c9c 100644 --- a/drawutils.cpp +++ b/drawutils.cpp @@ -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(x2-x1); + dy = static_cast(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(len); + + cx = static_cast(x1 - (width>>1)); + cy = static_cast(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; + } +} diff --git a/drawutils.h b/drawutils.h index 9114ea2..bf91073 100644 --- a/drawutils.h +++ b/drawutils.h @@ -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