1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
bool putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel); //applies pixel to surface
void putline(SDL_Surface* surface,int x0, int y0, int x1, int y1,Uint32 pixel); //basic interpolation
bool putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel){
if (y<0 || x < 0 || x > surface->w || y > surface->h)
return false;
int bpp = surface->format->BytesPerPixel;
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
if (bpp == 1){
*p = pixel;
}
else if (bpp == 2){
*(Uint16 *)p = pixel;
}
else if (bpp == 3){
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
}
else if (bpp == 4){
*(Uint32 *)p = pixel;
}
return true;
}
void putline( SDL_Surface* surface,int x0, int y0, int x1, int y1, Uint32 pixel){
int dx = abs(x1-x0);
int dy = abs(y1-y0);
int sx=0,sy=0;
if (x0 < x1)
sx = 1;
else
sx = -1;
if (y0 < y1)
sy = 1;
else
sy = -1;
int err = dx-dy;
int e2;
while(x0!=x1 || y0!=y1){
putpixel(surface,x0,y0,pixel);
e2=2*err;
if (e2>-dy){
err -= dy;
x0 += sx;
}
if (e2 < dx){
err += dx;
y0 += sy;
}
}
}
|