Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Drawing diagonal lines with SDL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 CruzWoman   Members   -  Reputation: 150

Like
0Likes
Like

Posted 07 July 2012 - 12:00 PM

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.

Sponsor:

#2 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 07 July 2012 - 12:11 PM

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]

#3 CruzWoman   Members   -  Reputation: 150

Like
0Likes
Like

Posted 07 July 2012 - 02:14 PM

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.

#4 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 07 July 2012 - 02:32 PM

Apologies, it seems my code got cut off (unless thats a page load error on my end, my internet is acting weird today)
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;
  }
}
}
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:
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);
}


#5 Dunge   Members   -  Reputation: 405

Like
0Likes
Like

Posted 07 July 2012 - 02:32 PM

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.

#6 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 07 July 2012 - 02:36 PM

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

#7 CruzWoman   Members   -  Reputation: 150

Like
0Likes
Like

Posted 07 July 2012 - 03:39 PM

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.

#8 CruzWoman   Members   -  Reputation: 150

Like
0Likes
Like

Posted 09 July 2012 - 04:21 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS