Linedrawing

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).

But that code produces crapy lines in North and South (~10 to ~2 o’clock
and ~4 to ~8 o’clock) direction. Has anyone of you working code? Or at
least a link to it or a ready-usable library that makes drawing primitives
easy? It would be best if it uses Bresenham-style without floatingpoint
arithmetics. I have already a selfcoded one with floating point, but it is
sensible slower than the Bresenhams.

TIA
Norbert

Norbert Melzer <norbert.melzer 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 :slight_smile:

Or maybe SGE or SDL_gfx :stuck_out_tongue:

Zedr0n wrote:> Norbert Melzer <norbert.melzer 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 :slight_smile:


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

http://www.antigrain.com

This has C++ code for doing Bresenham with sub-pixel accuracy. It does use
floating point, but is still blindingly fast.

JeffOn Sat January 5 2008 08:59, Norbert Melzer wrote:

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).

But that code produces crapy lines in North and South (~10 to ~2 o’clock
and ~4 to ~8 o’clock) direction. Has anyone of you working code? Or at
least a link to it or a ready-usable library that makes drawing primitives
easy? It would be best if it uses Bresenham-style without floatingpoint
arithmetics. I have already a selfcoded one with floating point, but it is
sensible slower than the Bresenhams.

You can take a look at the line drawing code of Guichan, it’s pretty
straight forward and it’s found here:

http://guichan.sourceforge.net/api/0.7.1/sdlgraphics_8cpp-source.html#l00504

/OlofOn 05/01/2008, Leo M. Cabrera wrote:

Or maybe SGE or SDL_gfx :stuck_out_tongue:

Zedr0n wrote:

Norbert Melzer <norbert.melzer 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 :slight_smile:


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

You can take a look at the line drawing code of Guichan, it’s pretty
straight forward and it’s found here:

http://guichan.sourceforge.net/api/0.7.1/sdlgraphics_8cpp-source.html#l00504

/Olof

Or maybe SGE or SDL_gfx :stuck_out_tongue:

I will look on all three of it, but lacking of time and a stable internet
connection, could you post the missing URLs?

TIA
NorbertAm Sat, 5 Jan 2008 19:41:24 +0100 schrieb Olof N?ss?n:

On 05/01/2008, Leo M. Cabrera wrote:

http://www.antigrain.com

This has C++ code for doing Bresenham with sub-pixel accuracy. It does use
floating point, but is still blindingly fast.

Jeff

I looked at it, but couldnt figur out how to use it wit SDL. Since I
understand the documentation right, this would only possible with Mac and
X11, but primary developement system is Win32 until mid of April. Theres no
*nix running on my Laptop at the moment. And my main PC is still at home.

Really said, I couldnt figur out anything with AGG, and the other thing is,
I dont need subpixel or antialiasing. That would be overblown.

An other minus on AGG is the lack of documentation. I cant find any
API-Documentation, no reference, just the tutorial like Manual.

But I think I would look further into it, perhaps I get something running
with it.

Bye
NorbertAm Sat, 5 Jan 2008 10:43:18 -0800 schrieb Jeff:

Norbert Melzer <norbert.melzer 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 :slight_smile:

There is no particular reason for that, except, I didnt know that OGL can
do that :slight_smile: 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
NorbertAm Sat, 5 Jan 2008 17:08:42 +0000 (UTC) schrieb Zedr0n:

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 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 :slight_smile:

There is no particular reason for that, except, I didnt know that OGL can
do that :slight_smile: 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

About OpenGL, you shouldn’t worry about it. If you’re already working on implementing your design, you shouldn’t bother learning such a big API as OpenGL. Save it for a future project. Also, all Windows machines should have a software implementation of OpenGL (ensuring portability). If you were to do only line drawings with it, the hardware requirements would not be very high at all (i.e. no graphics card, low-end Pentium). You’d have to do some more work to get text in OpenGL, though.

Jonny DFrom: grimfang4@hotmail.com
To: sdl at lists.libsdl.org
Date: Sat, 5 Jan 2008 16:34:00 -0500
Subject: Re: [SDL] Linedrawing

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 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 :slight_smile:

There is no particular reason for that, except, I didnt know that OGL can
do that :slight_smile: 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. Start sharing!


Share life as it happens with the new Windows Live.
http://www.windowslive.com/share.html?ocid=TXT_TAGHM_Wave2_sharelife_012008

There is no particular reason for that, except, I didnt know that
OGL can
do that :slight_smile: On the other side, I wanted to keep requirements low.

i think almost everything except embedded supports opengl, dont see
how that would be a problem> 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

About OpenGL, you shouldn’t worry about it. If you’re already working on
implementing your design, you shouldn’t bother learning such a big API
as OpenGL. Save it for a future project. Also, all Windows machines
should have a software implementation of OpenGL (ensuring portability).
If you were to do only line drawings with it, the hardware requirements
would not be very high at all (i.e. no graphics card, low-end Pentium).
You’d have to do some more work to get text in OpenGL, though.

Naive as I am, I had guessed, that I can use SDL_ttf in SDL + openGL as
well as in SDL standalone…

Bye
NorbertAm Sat, 5 Jan 2008 16:56:40 -0500 schrieb Jonathan Dearborn:

About OpenGL, you shouldn’t worry about it. If you’re already working on
implementing your design, you shouldn’t bother learning such a big API
as OpenGL. Save it for a future project. Also, all Windows machines
should have a software implementation of OpenGL (ensuring portability).
If you were to do only line drawings with it, the hardware requirements
would not be very high at all (i.e. no graphics card, low-end Pentium).
You’d have to do some more work to get text in OpenGL, though.

Naive as I am, I had guessed, that I can use SDL_ttf in SDL + openGL as
well as in SDL standalone…

You can. Here’s an example, with discussion:

-CrystalOn Sun, 6 Jan 2008, Norbert Melzer wrote:

Am Sat, 5 Jan 2008 16:56:40 -0500 schrieb Jonathan Dearborn: