• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
CruzWoman

Drawing diagonal lines with SDL

7 posts in this topic

Hello,
I tried to draw a diagonal line with SDL, but I only get a dashed horizontal line. I have no idea what's the problem. I tried a simple algorithm (commented out) and then I found some soucecode for the bresenham algorithm. Both draw only dashed horizontal lines (the first and half of the second line of the surface)
[source lang="cpp"]void ProcessSDL::DrawLine(SDL_Surface *screen, int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b)
{
//simple algorithm
/*double x = x2 - x1;
double y = y2 - y1;
double length = sqrt( x*x + y*y );

double addx = x / length;
double addy = y / length;

x = x1;
y = y1;

if( SDL_MUSTLOCK( m_pScreen ) ) { SDL_LockSurface( m_pScreen ); }

for(double i = 0; i < length; i += 1)
{
setSdlPixel( screen, (int)x, (int)y, c_green);
x += addx;
y += addy;
}

if( SDL_MUSTLOCK( m_pScreen ) ) { SDL_UnlockSurface( m_pScreen ); }
SDL_Flip(screen);*/


bresenham_line(screen, x1, y1, x2, y2, c_green);
SDL_Flip(screen);
}

void ProcessSDL::bresenham_line(SDL_Surface *screen, Uint32 x1, Uint32 y1, Uint32 x2, Uint32 y2, Uint32 color)
{
int lg_delta;
int sh_delta;
int cycle;
int lg_step;
int sh_step;

lg_delta = x2 - x1;
sh_delta = y2 - y1;
lg_step = SGN(lg_delta);
lg_delta = ABS(lg_delta);
sh_step = SGN(sh_delta);
sh_delta = ABS(sh_delta);
if (sh_delta < lg_delta) {
cycle = lg_delta >> 1;
while (x1 != x2) {
setSdlPixel(screen, x1, y1, color);
cycle += sh_delta;
if (cycle > lg_delta) {
cycle -= lg_delta;
y1 += sh_step;
}
x1 += lg_step;
}
setSdlPixel(screen, x1, y1, color);
}
cycle = sh_delta >> 1;
while (y1 != y2) {
setSdlPixel(screen, x1, y1, color);
cycle += lg_delta;
if (cycle > sh_delta) {
cycle -= sh_delta;
x1 += lg_step;
}
y1 += sh_step;
}
setSdlPixel(screen, x1, y1, color);
}

void ProcessSDL::setSdlPixel(SDL_Surface *screen, int x, int y, Uint32 color)
{
Uint32 *pixmem32;
Uint32 colour;

//colour = SDL_MapRGB( screen->format, r, g, b );

pixmem32 = (Uint32*) screen->pixels + y + x;
*pixmem32 = color;
}[/source]

My init_SDL looks like this. Perhaps here's the problem:
[source lang="cpp"]bool ProcessSDL::initSDL() {
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false; }
//
atexit(SDL_Quit);
// init screen
SDL_putenv("SDL_VIDEO_CENTERED=center");
SDL_WM_SetCaption("Ear Tracker", NULL);
m_pScreen = SDL_SetVideoMode( 1280,720, 32, SDL_SWSURFACE);
if ( m_pScreen == NULL )
{
return false;
}

//init colours
c_white = SDL_MapRGB(m_pScreen->format, 255,255,255);
c_gray = SDL_MapRGB(m_pScreen->format, 200,200,200);
c_dgray= SDL_MapRGB(m_pScreen->format, 64,64,64);
c_cyan = SDL_MapRGB(m_pScreen->format, 32,255,255);
c_magenta = SDL_MapRGB(m_pScreen->format, 255,0,255);
c_yellow = SDL_MapRGB(m_pScreen->format, 255,255,0);
c_black = SDL_MapRGB(m_pScreen->format, 0,0,0);
c_red = SDL_MapRGB(m_pScreen->format, 255,0,0);
c_green = SDL_MapRGB(m_pScreen->format, 0,255,0);
c_blue = SDL_MapRGB(m_pScreen->format, 0,0,255);

//If everything initialized fine
return true;
}[/source]
Could anyone help me to find out whats wrong?
Thank you very much.
Greetings.
0

Share this post


Link to post
Share on other sites
I've never seen line drawing code that uses a hypotenuse. It's going to be really slow because of that square root. Instead the usual way is to go per pixel along the short side of the triangle, and draw long straight lines as you go.

Heres my line code, for reference. Not sure if it's the fastest around, but it works, and it uses straight line drawing code instead of individual pixels:
[source lang="cpp"]void Surface::line(int x1,int y1,int x2,int y2,uint color=0xFFFFFFFF)
{
float dx=x2-x1;
float dy=y2-y1;
if(dx==0)
{
vline(x1,y1,dy,color);
return;
}
if(dy==0)
{
hline(x1,y1,dx,color);
return;
}
if(fabs(dx)>fabs(dy))
{
if(dy<0)
{
float temp=x1;
x1=x2;
x2=temp;
temp=y1;
y1=y2;
y2=temp;
dy=-dy;
}
float cx=dx/dy;
float x=x1;
for(int j=y1;j<y2;j++)
{
hline(x,j,cx,color);
x+=cx;
}
}
else
{
if(dx<0)
{
float temp=x1;
x1=x2;
x2=temp;
temp=y1;
y1=y2;
y2=temp;
dx=-dx;
}
float cy=dy/dx;
float y=y1;
for(int i=x1;i<x2;i++)
{
vline(i,y,cy,color);
y+=cy;
}
}
}[/source]
0

Share this post


Link to post
Share on other sites
Thanks for your reply.

Do you use SDL_gfx? I tried to compile it with VC++ 2010 Express, but got compile errors. I tried SDL_draw lib too, but I got an uncaught exception while running my program with SDL_draw lib. And the debugger shows me that the problem was in der SDL_draw code...
I'll try to compile SDL_gfx once more. I guess it's just a problem with setting the paths or with the linker...

If someone knows why my code above only draws horizontal lines, feel free to give me a hint.

Greetings.
0

Share this post


Link to post
Share on other sites
Apologies, it seems my code got cut off (unless thats a page load error on my end, my internet is acting weird today)
[CODE]
void Surface::line(int x1,int y1,int x2,int y2,uint color=0xFFFFFFFF)
{
float dx=x2-x1;
float dy=y2-y1;
if(dx==0)
{
vline(x1,y1,dy,color);
return;
}
if(dy==0)
{
hline(x1,y1,dx,color);
return;
}
if(fabs(dx)>fabs(dy))
{
if(dy<0)
{
float temp=x1;
x1=x2;
x2=temp;
temp=y1;
y1=y2;
y2=temp;
dy=-dy;
}
float cx=dx/dy;
float x=x1;
for(int j=y1;j<y2;j++)
{
hline(x,j,cx,color);
x+=cx;
}
}
else
{
if(dx<0)
{
float temp=x1;
x1=x2;
x2=temp;
temp=y1;
y1=y2;
y2=temp;
dx=-dx;
}
float cy=dy/dx;
float y=y1;
for(int i=x1;i<x2;i++)
{
vline(i,y,cy,color);
y+=cy;
}
}
}
[/CODE]
This code is using my own SDL/other drawing library wrapper, so youd need to, for instance, make sure all the vline and hline calls are rewritten for whatever graphics library you use (whether its _gfx, _draw, or just plain SDL).
The definitions for plain SDL should be:
[CODE]
void hline(int x,int y,int width,Uint32 color)
{
if(w<0)
{
x+=w;
w=-w;
}
r.x=x;
r.y=y;
r.w=w;
r.h=1;
SDL_FillRect(screen,&r,color);
}

void vline(int x,int y,int height,Uint32 color)
{
if(h<0)
{
y+=h;
h=-h;
}
r.x=x;
r.y=y;
r.w=1;
r.h=h;
SDL_FillRect(screen,&r,color);
}
[/CODE]
0

Share this post


Link to post
Share on other sites
From my experience, SDL_draw crash if you draw on a surface that have a different bpp than the back buffer surface. I modified the code to take the BPP of the surface you draw on instead of the back buffer. As for SDL_gfx, it's more advanced but slower and the low-resolution circle were not looking good enough for my needs.

As for drawing a line, I used the line Bresenham algorithm, works perfectly.
0

Share this post


Link to post
Share on other sites
Here's my lib for SDL_gfx. I'm not sure if it will work with your set-up, or how the hell I compiled it (I compiled it on a different computer more than five years ago)
http://zacaj.com/SDL_gfx.lib
0

Share this post


Link to post
Share on other sites
Thanks for all replies. I got the SDL_gfx compiled :)

I got a diagonal line when compiling the Release Version, but in Debug mode I got an error (LINK : fatal error LNK1104: Datei "SDL_gfx.lib" kann nicht geöffnet werden.(=>could not open file "SDL_gfx.lib")) The configuration for Debug and Release seem to be the same.

@ zacaj: I tried your lib-file, but got the same error in Debug mode... Thanks for sharing it :) I guess it's a problem with my preferences.

Tomorrow I'll go on... In germany it's late and my kids will wake me up early in the morning.
Greetings and good night for now.
0

Share this post


Link to post
Share on other sites
Hello,
everything works now. I use the function lineRGBA from SDL_gfx for now to draw a horizontal line. Hope it'll be not too slow, but I guess it'll be ok.
Thanks for your help.
Greetings.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0