Skip to main content
GameDev.net gamedev.net
🔒 Locked

A line drawing function....

Started by MTclip Sep 15, 2005 at 10:45 AM 4 replies 1.1k views
Original Post
MTclip
MTclip
I am trying to make a line drawing function ie void DrawLine(float x1, float y1, float x2, float y2, D3DCOLOR color); i just cant seem to get it to work.... im sure the problem is in the DrawLine() function.. when i run it it runs with out failing it just does not draw the line... or even if some one wants to link me to a better line drawing function im fine with that..........

#define HandleHRESULT(x,y) {HRESULT hr; hr =(x);if(FAILED(hr)){MessageBox(NULL,y,"DIRECTX ERROR", MB_OK);}}
#define VERTEX ( D3DFVF_XYZRHW | D3DFVF_DIFFUSE )

std::vector<Line*> g_lines;


struct Vertex2
{
	float x, y, z, w;
    DWORD color;   
};


struct Line
{
	float x1,y1;
	float x2,y2;
	Line(float fx1, float fy1, float fx2, float fy2)
	{
		x1 = fx1;
		y1 = fy1;
		x2 = fx2;
		y2 = fy2;
	};
};


std::vector<Line*> g_lines;
g_lines.push_back(new Line(20, 20, 175, 175));


/////// IN THE RENDER FUNCTION ///////
////////////////////////////////////////
////// 
D3DCOLOR color = D3DCOLOR_XRGB(100, 50, 25);
	for(int i = 0; i < g_lines.size(); i ++)
	{
		DrawLine(g_lines.at(i)->x1,g_lines.at(i)->y1,
			 g_lines.at(i)->x2,g_lines.at(i)->y2,
			color);
	}
//////////////////////////////


void DrawLine(float x1, float y1, float x2, float y2, D3DCOLOR color)
{	
	
	Vertex2 line[] = 
	{
		{x1,y1,0,1,color},
		{x1,y1,0,1,color},
	};


	LPDIRECT3DVERTEXBUFFER9 pLineList_vb = NULL;

	HandleHRESULT(g_pd3dDevice->CreateVertexBuffer( 2*sizeof(Vertex2), 0, VERTEX,
	              D3DPOOL_DEFAULT, &pLineList_vb,
	              NULL ), "FAILED TO CREATE VERTEX BUFFER");
	
	Vertex2 *pVertices2 = NULL;

	HandleHRESULT(pLineList_vb->Lock( 0, sizeof(newline), (void**)&pVertices2, 0 ), "FAILED TO LOCK THE VERTEX BUFFER");

         memcpy( pVertices2, newline, sizeof(newline) );
         HandleHRESULT(pLineList_vb->Unlock(), "FAILED TO UNLOCK THE VERTEX BUFFER");
/// DRAW ///

	HandleHRESULT(g_pd3dDevice->SetStreamSource( 0, pLineList_vb, 0, sizeof(Vertex2) ), "setStreamSource Failed");
	HandleHRESULT(g_pd3dDevice->SetFVF( VERTEX ),"SETFVF FAILED");
	HandleHRESULT(g_pd3dDevice->DrawPrimitive( D3DPT_LINELIST, 0, 1),"Line List failed to draw");
	
	pLineList_vb->Release();
	
}


thanks for any help
njpaul
njpaul
DirectX lets you draw lines, I believe. I haven't used this but this page may help.Link
MTclip
MTclip
yeah i was just trying to wrap that functionality..
but im having issues...
Kwizatz
Kwizatz
Here is mine, its DX9:
Notes:
Sin16: signed 16 bit int.
GUIColor: a fancy union which in the end just holds a 32 bit color as 4 8 bit values for B,G,R and A.

// Our custom FVF, which describes our custom vertex structure#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)struct CUSTOMVERTEX{    FLOAT x, y, z;    DWORD color;	FLOAT u, v;};void DrawLine(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2,GUIColor color){   CUSTOMVERTEX* pVertices;    if( FAILED( lpRectangle->Lock( 0, 2, (void**)&pVertices, 0 ) ) )        return;	pVertices[0].x=float(x1);	pVertices[0].y=float(y1);	pVertices[0].z=0.0f;	pVertices[0].color=color.color32;	pVertices[1].x=float(x2);	pVertices[1].y=float(y2);	pVertices[1].z=0.0f;	pVertices[1].color=color.color32;	lpRectangle->Unlock();    lpDevice->SetStreamSource( 0, lpRectangle, 0, sizeof(CUSTOMVERTEX) );    lpDevice->DrawPrimitive( D3DPT_LINELIST, 0, 1 ); }
programering
programering
Are you updating the screen?

Also, why isn't this code within a function?
std::vector<Line*> g_lines;g_lines.push_back(new Line(20, 20, 175, 175));/////// IN THE RENDER FUNCTION ///////////////////////////////////////////////////// D3DCOLOR color = D3DCOLOR_XRGB(100, 50, 25);	for(int i = 0; i < g_lines.size(); i ++)	{		DrawLine(g_lines.at(i)->x1,g_lines.at(i)->y1,			 g_lines.at(i)->x2,g_lines.at(i)->y2,			color);	}//////////////////////////////

EDIT: sorry, you're saying with the comments it's inside a/the rendering func.
But this code is outside, why?
std::vector<Line*> g_lines;g_lines.push_back(new Line(20, 20, 175, 175));


EDIT 2: Ok, I assume it's within function in your real code,
but that you didn't bother to post the whole code in here so
you accidently just dropped it somwhere outside.
MTclip
MTclip
ok i get it to draw a single line...
but if i have a bunch of lines say held in some container ie
i would want to send them all the the vertexbuffer ....

but now that has problems... eh..
..see if some one can find a problem with this

i am assuming the problem is when i lock the buffer and call memcopy..



void draw2dLineList(std::vector<Vertex_2D*> vlist){	int numVertices = vlist.size();	int numLines	= numVertices * 0.5f;		// create appropriately sized array	Vertex_2D  * lines = new Vertex_2D[numVertices];		// copy data from vector vlist into identical index as lines[]	for(int i = 0; i < numVertices; i++)	{		lines.x		= vlist.at(i)->x;		lines.y		= vlist.at(i)->y;		lines.z		= 0;		lines.w		= 1;		lines.color	= D3DCOLOR_XRGB(200, 50, 25);	}		Vertex_2D *pVertices = NULL;		LPDIRECT3DVERTEXBUFFER9 line_vb      = NULL;		// create buffer	g_pd3dDevice->CreateVertexBuffer( numVertices *sizeof(Vertex_2D), 0, D3DFVF_MY_2DVERTEX,									  D3DPOOL_DEFAULT, &line_vb,									  NULL );	// fill buffer	line_vb->Lock( 0, sizeof(lines), (void**)&pVertices, 0 );    memcpy( pVertices, lines, sizeof(lines) );    line_vb->Unlock();	// draw lines	g_pd3dDevice->SetStreamSource( 0, line_vb, 0, sizeof(Vertex_2D) );	g_pd3dDevice->SetFVF( D3DFVF_MY_2DVERTEX );	g_pd3dDevice->DrawPrimitive( D3DPT_LINELIST, 0, numLines);	line_vb->Release();	delete [] lines;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.