2011-06-22 20:12:22 +00:00
|
|
|
#include "drawutils.h"
|
2011-06-22 21:57:44 +00:00
|
|
|
#include <cmath>
|
2011-06-23 16:07:45 +00:00
|
|
|
#include "mathutils.h"
|
2011-06-22 20:12:22 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2011-06-22 20:42:54 +00:00
|
|
|
|
2011-06-23 01:54:35 +00:00
|
|
|
void DrawUtils::draw_line(SDL_Surface* dest, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint16 width, Uint32 colour)
|
2011-06-22 20:42:54 +00:00
|
|
|
{
|
|
|
|
float dx, dy, len;
|
|
|
|
float cx, cy; // our current coords
|
|
|
|
|
|
|
|
SDL_Rect pen;
|
|
|
|
|
2011-06-23 16:07:45 +00:00
|
|
|
len = MathUtils::distance(x1,y1,x2,y2);
|
2011-06-22 20:42:54 +00:00
|
|
|
|
2011-06-23 16:37:43 +00:00
|
|
|
// dx and dy represent the amount we move
|
2011-06-22 20:42:54 +00:00
|
|
|
// each step in our drawing loop
|
2011-06-23 16:07:45 +00:00
|
|
|
dx = static_cast<float>(x2-x1) / len;
|
|
|
|
dy = static_cast<float>(y2-y1) / len;
|
2011-06-22 20:42:54 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2011-06-22 22:24:20 +00:00
|
|
|
|
|
|
|
|
2011-06-23 03:52:22 +00:00
|
|
|
void DrawUtils::draw_circle_filled(SDL_Surface* dest, Sint16 int_x, Sint16 int_y, Uint16 int_r, Uint32 colour, Uint8 alpha)
|
|
|
|
{
|
|
|
|
float x = static_cast<float> (int_x);
|
|
|
|
float y = static_cast<float> (int_y);
|
|
|
|
float r = static_cast<float> (int_r);
|
|
|
|
|
|
|
|
SDL_Rect pen;
|
|
|
|
float i;
|
|
|
|
|
|
|
|
for (i=0; i < 6.28318531; i += 0.0034906585)
|
|
|
|
{
|
|
|
|
pen.x = static_cast<int> (x + cos(i) * r);
|
|
|
|
pen.y = static_cast<int> (y + sin(i) * r);
|
2011-06-23 04:09:34 +00:00
|
|
|
int w = static_cast<int> (x - pen.x);
|
|
|
|
int h = static_cast<int> (y - pen.y);
|
|
|
|
|
|
|
|
if (w == 0) pen.w = 1;
|
|
|
|
else if (w < 0)
|
|
|
|
{
|
|
|
|
pen.x = x;
|
|
|
|
pen.w = abs(w);
|
|
|
|
}
|
|
|
|
else pen.w = w;
|
|
|
|
|
|
|
|
if (h == 0) pen.h = 1;
|
|
|
|
else if (h < 0)
|
|
|
|
{
|
|
|
|
pen.y = y;
|
|
|
|
pen.h = abs(h);
|
|
|
|
}
|
|
|
|
else pen.h = h;
|
2011-06-23 03:52:22 +00:00
|
|
|
|
|
|
|
if (pen.x >= dest->clip_rect.x &&
|
|
|
|
pen.y >= dest->clip_rect.y &&
|
|
|
|
pen.x + pen.w <= dest->clip_rect.w &&
|
|
|
|
pen.y + pen.h <= dest->clip_rect.h)
|
|
|
|
SDL_FillRect(dest, &pen,
|
|
|
|
SDL_MapRGBA(dest->format,
|
|
|
|
(colour >> 16) & 0xff,
|
|
|
|
(colour >> 8) & 0xff,
|
2011-06-23 04:09:34 +00:00
|
|
|
colour & 0xff, 1));
|
2011-06-23 03:52:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-06-22 22:24:20 +00:00
|
|
|
bool DrawUtils::transpare(SDL_Surface* surface, int r, int g, int b)
|
|
|
|
{
|
|
|
|
if (surface == NULL) return false;
|
|
|
|
|
|
|
|
SDL_SetColorKey(surface, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(surface->format, r, g, b));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|