/Niels
Drawing Lines in DirectDraw
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development
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.
//--------------------------------------------------------------------------------------------
// 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/
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).]
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.
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