🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Drawing Lines in DirectDraw

Started by
5 comments, last by GameDev.net 24 years, 7 months ago
DirectDraw doesn't support line drawing... Check CGPP or preferably Abrash' black book of graphics programming for a really fast line drawing algorithm (it also has a fast one for antialiased lines)...

/Niels

<b>/NJ</b>
Advertisement
You can draw your own lines by plotting pixels on a surface. I can dig up some functions if you need them, but at least 3 books I've seen on DirectX go into this.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

Well. You want to draw lines.
And you have access to D3DIM?

Use DrawPrimitive with the LINE function.

If you dont have the D3DIM ( which you should have, its very simple to do lines with it ) , you either lock the surface
down and pixel it. Or you use the the GDI.

Locking the surface down and using a line
drawing routine would be the best if you
dont use D3DIM.

Well, I hope this helps some. It just draws a straight vertical line. Pretty uselesse, but its just a few lines of code away from what you want

//--------------------------------------------------------------------------------------------
// Draw_VLine()
//--------------------------------------------------------------------------------------------
BOOL Draw_VLine( LPDIRECTDRAWSURFACE4 lpDDSTemp, int x, int y, int y2, WORD rgbColor )
{
int index;
WORD *wPixels;
DDSURFACEDESC2 ddsd;

ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );

if ( FAILED (lpDDSTemp->Lock( NULL, &ddsd, DDLOCK_WAIT | DDLOCK_WRITEONLY, NULL ) ) )
return Error( "> Error: Couldn't lock the Temp surface.\n> Function: Draw_Pixel().\n", DEBUG );

wPixels = ( WORD * )ddsd.lpSurface;

for ( index = 0; index <= ( y2 - y ); index++ )
wPixels[( y + index ) * ( ddsd.lPitch >> 1 ) + x] = rgbColor;

if ( FAILED (lpDDSTemp->Unlock( NULL ) ) )
return Error( "> Error: Couldn't unlock the Temp surface.\n> Function: Draw_Pixel().\n", DEBUG );

return TRUE;
}

------------------
- mallen22@concentric.net
- http://members.tripod.com/mxf_entertainment/

Here's some more! line drawing routines. Even though these are not written in
assembly, they might actually be faster than ones written in assembly if
you have VC++6.0 and optimizations turned on.

Don't ask me why this is faster, I guess MSVC++ can write better
assembly than me.

The locking/unlocking of the surface is done outside the function, so
you can use them several times if you need to draw more than one line, like
a rectangle. surface_buffer is your pointer to the DD surface. Oh yeah,
these were written for 16-bit color.

But, if you want to speed it up even more, just move everything
into one function.

code:
//// Draw a horizontal line (dy=0) from left to right. //void  Draw_Primitive_HLine_No_Clip(INT width, WORD color, WORD *surface_buffer) {  while(width--) {    // set the color and advance one pixel    *surface_buffer++ = color; }  }

code:
//// Draw a vertical line (dx=0) from top to bottom.//voidDraw_Primitive_VLine_No_Clip(INT height, WORD color, WORD *surface_buffer, DWORD pitch) {  while(height--) {    // set the pixel    *surface_buffer = color;    // move to the next scanline pixel    surface_buffer += pitch; }  }


Reaver

[This message has been edited by Reaver (edited November 05, 1999).]

Hello everybody. I have a teensy little
question.

In DirectDraw I am writing a dodgy asteroids
clone. Now what I need is the ability to
draw straight lines, even quickly if possible.

Does Direct-X supply primitives?
How else could I do it? GDI?

I have little windows programming experience,
but if it's there's no quick way availble I
can always do a port of my DOS code.

Personally, I wouldn't trust the VC optimizer, I got a double up in speed by handcoding a few inner loops (compared to optimized C++), and I'm NOT a Ix86 ASM wizard.

And I still recommend reading Abrash' black book - He starts out with plain Bresenhams line drawing, covers "run-length slice drawing", and ends up antialiasing it... Check it out - that book can not be recommended enough !

/Niels

<b>/NJ</b>

This topic is closed to new replies.

Advertisement