[SDL] Linedrawing
Jonathan Dearborn
grimfang4 at hotmail.com
Sat Jan 5 13:34:00 PST 2008
Hey,
You can check out SPriG as well. It's my up-to-date branch of SGE found here: http://pubpages.unh.edu/~jmb97
But to make your immediate search easy, here's the code from SPriG/SGE for drawing a line:
//==================================================================================
// Draws a line
//==================================================================================
void _Line(SDL_Surface *surface, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color)
{
//if ( !clipLine(surface, &x1, &y1, &x2, &y2) )
//return;
Sint16 dx, dy, sdx, sdy, x, y;
dx = x2 - x1;
dy = y2 - y1;
sdx = (dx < 0) ? -1 : 1;
sdy = (dy < 0) ? -1 : 1;
dx = sdx * dx + 1;
dy = sdy * dy + 1;
x = y = 0;
Sint16 pixx = surface->format->BytesPerPixel;
Sint16 pixy = surface->pitch;
Uint8 *pixel = (Uint8*)surface->pixels + y1*pixy + x1*pixx;
pixx *= sdx;
pixy *= sdy;
if (dx < dy)
{
Sint32 tmp = dx;
dx = dy;
dy = Sint16(tmp);
tmp = pixx;
pixx = pixy;
pixy = tmp;
}
switch (surface->format->BytesPerPixel)
{
case 1:
{
for (x=0; x < dx; x++)
{
*pixel = color;
y += dy;
if (y >= dx)
{
y -= dx;
pixel += pixy;
}
pixel += pixx;
}
}
break;
case 2:
{
for (x=0; x < dx; x++)
{
*(Uint16*)pixel = color;
y += dy;
if (y >= dx)
{
y -= dx;
pixel += pixy;
}
pixel += pixx;
}
}
break;
case 3:
{
Uint8 rshift8 = surface->format->Rshift/8;
Uint8 gshift8 = surface->format->Gshift/8;
Uint8 bshift8 = surface->format->Bshift/8;
Uint8 ashift8 = surface->format->Ashift/8;
Uint8 R = (color>>surface->format->Rshift)&0xff;
Uint8 G = (color>>surface->format->Gshift)&0xff;
Uint8 B = (color>>surface->format->Bshift)&0xff;
Uint8 A = (color>>surface->format->Ashift)&0xff;
for (x=0; x < dx; x++)
{
*(pixel+rshift8) = R;
*(pixel+gshift8) = G;
*(pixel+bshift8) = B;
*(pixel+ashift8) = A;
y += dy;
if (y >= dx)
{
y -= dx;
pixel += pixy;
}
pixel += pixx;
}
}
break;
case 4:
{
for (x=0; x < dx; x++)
{
*(Uint32*)pixel = color;
y += dy;
if (y >= dx)
{
y -= dx;
pixel += pixy;
}
pixel += pixx;
}
}
break;
}
}
Good luck,
Jonny D
> To: sdl at libsdl.org
> From: norbert.melzer at gmx.net
> Date: Sun, 6 Jan 2008 00:16:20 +0500
> Subject: Re: [SDL] Linedrawing
>
> Am Sat, 5 Jan 2008 17:08:42 +0000 (UTC) schrieb Zedr0n:
>
> > Norbert Melzer <norbert.melzer <at> gmx.net> writes:
> >
> >>
> >> Hi Group!
> >>
> >> I have a problem with drawing lines. I googled a bit and found finished
> >> c-code for a Bresenham-line in the wikipedia, I adopted this code into my
> >> C++ class (original code is the same, except that that the function is
> >> embedded in a class and putpixel is now this->putpixel).
> >
> > Any particular reason you don't want to use openGL? If not, then you
> > can just use gl functions to draw a line and be done with it :)
>
> There is no particular reason for that, except, I didnt know that OGL can
> do that :-) On the other side, I wanted to keep requirements low. It is
> hard to justify the requirement of a state of the art PC for a turnbased
> strategy game with simple 2 dimensional linedrawings and some text...
>
> Or is OGL not screwing requirements like this?
>
> TIA
> Norbert
>
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
_________________________________________________________________
Share life as it happens with the new Windows Live.
http://www.windowslive.com/share.html?ocid=TXT_TAGHM_Wave2_sharelife_012008
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20080105/9cf8b729/attachment.htm
More information about the SDL
mailing list