treewars/drawutils.cpp

89 lines
1.7 KiB
C++

#include "drawutils.h"
SDL_Surface* DrawUtils::load(string file)
{
SDL_Surface* raw = NULL;
SDL_Surface* cooked = NULL;
raw = SDL_LoadBMP(file.c_str());
if (raw == NULL) return NULL;
cooked = SDL_DisplayFormat(raw);
SDL_FreeSurface(raw); // don't consume raw surfaces
return cooked;
}
bool DrawUtils::draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y)
{
if (dest == NULL || drawable == NULL) return false;
SDL_Rect dest_r;
dest_r.x = x;
dest_r.y = y;
SDL_BlitSurface(drawable, NULL, dest, &dest_r);
return true;
}
bool DrawUtils::draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y,
int x2, int y2, int w, int h)
{
SDL_Rect dest_r;
dest_r.x = x;
dest_r.y = y;
SDL_Rect src_r;
src_r.x = x2;
src_r.y = y2;
src_r.w = w;
src_r.h = h;
SDL_BlitSurface(drawable, &src_r, dest, &dest_r);
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;
}
}