Sign in to follow this  
R67en

Drawing Lines

Recommended Posts

Im using DirectX 8.1 with VC++ 6.0. My program shows an image on the screen. I would like to draw primitives like lines on this image depending on user input. Here is the relevant code:
//constants
	//window class name
	const char* WINDOWCLASS = "StarPuzzle" ;
	//window title
	const char* WINDOWTITLE = "Star" ;
	//screen width and height
	const int SCREENWIDTH = 640 ;
	const int SCREENHEIGHT = 480 ;

//globals
	//instance handle
	HINSTANCE g_hInstance = NULL ;
	//window handle
	HWND g_hWnd = NULL ;
	//IDirect3D8 pointer
	IDirect3D8* g_pd3d = NULL ;
	//device type in use
	D3DDEVTYPE g_devtype ;
	//device pointer
	IDirect3DDevice8* g_pd3ddev = NULL ;
	//main viewport
	D3DVIEWPORT8 g_vpmain ;
	//image surface
	IDirect3DSurface8* g_pImageSurface = NULL ;
	IDirect3DSurface8* star ;
	IDirect3DSurface8* Solved			= NULL ;
	D3DDISPLAYMODE mode	;




//initialization
void Prog_Init ( ) 
{





	if(testing)
	{
		fstream file;
		file.open("zpoints.txt", ios::out);
		file.close();

		file.open("zclickedpoints.txt", ios::out);
		file.close();


		file.open("xys.txt", ios::out);
		file.close();
	}









	//create the IDirect3D8 object
	g_pd3d = Direct3DCreate8 ( D3D_SDK_VERSION ) ;

	//error check
	if ( g_pd3d )
	{
		//success
		fprintf ( stdout , "IDirect3D8 object created successfully.\n" ) ;
	}
	else
	{
		//failure
		fprintf ( stdout , "IDirect3D8 object creation failed.\n" ) ;

		//cannot proceed, so return
		return ;
	}

	//find display mode
//	D3DDISPLAYMODE 
	mode ;
	UINT nDisplayModeCount = g_pd3d->GetAdapterModeCount ( D3DADAPTER_DEFAULT ) ;

	//loop through display modes
	for ( UINT nDisplayMode = 0 ; nDisplayMode < nDisplayModeCount ; nDisplayMode ++ ) 
	{
		//check next display mode
		g_pd3d->EnumAdapterModes ( D3DADAPTER_DEFAULT , nDisplayMode , &mode ) ;

		//if it matches desired screen width, break out of loop
		if ( mode.Width == SCREENWIDTH && mode.Height == SCREENHEIGHT ) break ;
	}

	//check for proper sized mode
	if ( mode.Width != SCREENWIDTH || mode.Height != SCREENHEIGHT ) 
	{
		//did not find mode
		//post quit message
		PostQuitMessage ( 0 ) ;

		//report
		fprintf ( stdout , "Did not find display mode!\n" ) ;

		//return
		return ;
	}
	else
	{
		//found mode
		//report
		fprintf ( stdout , "Found display mode.\n" ) ;
	}


	//set up presentation parameters
	D3DPRESENT_PARAMETERS parms;

	//back buffer information
	parms.BackBufferWidth = mode.Width ; //use mode width
	parms.BackBufferHeight = mode.Height ; //use mode height
	parms.BackBufferFormat = mode.Format ; //use format of mode
	parms.BackBufferCount = 1 ; //make one back buffer

	//multisampling 
	parms.MultiSampleType = D3DMULTISAMPLE_NONE ;

	//swap effect
	parms.SwapEffect = D3DSWAPEFFECT_FLIP ; //we want to copy from back buffer to screen

	//destination window
	parms.hDeviceWindow = g_hWnd ; 
	parms.Windowed = FALSE ;

	//depth buffer information
	parms.EnableAutoDepthStencil = FALSE ;
	parms.AutoDepthStencilFormat = D3DFMT_UNKNOWN ;

	//flags
	parms.Flags = 0 ;

	//refresh rate and presentation interval
	parms.FullScreen_RefreshRateInHz = mode.RefreshRate ; //use mode's refresh rate
	parms.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT ;

	//attempt to create a HAL device
	HRESULT hr ; //store return values in this variable
	hr = g_pd3d->CreateDevice ( D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL , g_hWnd , D3DCREATE_SOFTWARE_VERTEXPROCESSING , &parms , &g_pd3ddev ) ;

	//error check
	if ( FAILED ( hr ) )
	{
		//could not create HAL device
		fprintf ( stdout , "Could not create HAL device!\n" ) ;

		//attempt to make REF device
		hr = g_pd3d->CreateDevice ( D3DADAPTER_DEFAULT , D3DDEVTYPE_REF , g_hWnd , D3DCREATE_SOFTWARE_VERTEXPROCESSING , &parms , &g_pd3ddev ) ;

		if ( FAILED ( hr ) )
		{
			//could not create REF device
			fprintf ( stdout , "Could not create REF device!\n" ) ;

			//post a quit message
			PostQuitMessage ( 0 ) ;
		}
		else
		{
			//successfully made REF device, store this value in a global
			g_devtype = D3DDEVTYPE_REF ;

			//report
			fprintf ( stdout , "Successfully created REF device.\n" ) ;
		}
	}
	else
	{
		//successfully made a HAL device, store this value in a global
		g_devtype = D3DDEVTYPE_HAL ;

		//report
		fprintf ( stdout , "Successfully created HAL device.\n" ) ;
	}

	//set up viewports
	//main viewport
	g_vpmain.X = 0 ;
	g_vpmain.Y = 0 ;
	g_vpmain.Width = SCREENWIDTH ;
	g_vpmain.Height = SCREENHEIGHT ;
	g_vpmain.MinZ = 0.0f ;
	g_vpmain.MaxZ = 1.0f ;

	//set the viewport
	g_pd3ddev->SetViewport ( &g_vpmain ) ;


	//memory dc
	HDC hdc = CreateCompatibleDC ( NULL ) ;

	//load in image
	HBITMAP hbmnew = ( HBITMAP ) LoadImage ( NULL , "StarPuzzle.t7g" , IMAGE_BITMAP , 0 , 0 , LR_LOADFROMFILE ) ;

	//get width and height of bitmap
	BITMAP bmp ;
	GetObject ( hbmnew , sizeof ( BITMAP ) , &bmp ) ;

	//select bitmap into dc
	HBITMAP hbmold = ( HBITMAP ) SelectObject ( hdc , hbmnew ) ;

	//set up image surface
	hr = g_pd3ddev->CreateImageSurface ( bmp.bmWidth , bmp.bmHeight , mode.Format , &g_pImageSurface ) ;


	star = ImageSurfaceFromBMP ( g_pd3ddev , "star.t7g" , mode.Format ) ;

	//error check
	if ( FAILED ( hr ) )
	{
		//error
		//report
		fprintf ( stdout , "Failed to create image surface!\n" ) ;

		//quit
		PostQuitMessage ( 0 ) ;

		//return
		return ;
	}
	else
	{
		//no error
		//report
		fprintf ( stdout , "Created image surface.\n" ) ;
	}

	//lock the surface
	D3DLOCKED_RECT lockedrect ;
	g_pImageSurface->LockRect ( &lockedrect , NULL , 0 ) ;

	//variables for color scan
	COLORREF crColor ;	//source color
	DWORD dwColor ;	//destination color

	DWORD dwRed ;	//red component
	DWORD dwGreen ;	//green component
	DWORD dwBlue ;	//blue component

	DWORD dwRedShiftL = 8 - GetFormatRBits ( mode.Format ) ;	//shift left red value
	DWORD dwGreenShiftL = 8 - GetFormatGBits ( mode.Format ) ;	//shift left green value
	DWORD dwBlueShiftL = 8 - GetFormatBBits ( mode.Format ) ;		//shift left blue value

	DWORD dwRedShiftR = GetFormatGBits ( mode.Format ) + GetFormatBBits ( mode.Format ) ;	//shift right red value
	DWORD dwGreenShiftR = GetFormatBBits ( mode.Format ) ;	//shift right green value
	DWORD dwBlueShiftR = 0 ;	//shift right blue value
//rh
	DWORD dwBytesPerPixel = ( //GetFormatXBits ( mode.Format ) + 
		GetFormatABits ( mode.Format ) + GetFormatRBits ( mode.Format ) + GetFormatGBits ( mode.Format ) + GetFormatBBits ( mode.Format ) ) / 8 ;//bytes per pixel

	BYTE* pbyBuffer = ( BYTE* ) lockedrect.pBits ; //buffer to image data
	int iBufferPosition ;

	//scan the image
	for ( int y = 0 ; y < bmp.bmHeight ; y ++ )
	{
		for ( int x = 0 ; x < bmp.bmWidth ; x ++ )
		{
			//calculate position in buffer
			iBufferPosition = y * lockedrect.Pitch + x * dwBytesPerPixel ;

			//grab pixel
			crColor = GetPixel ( hdc , x , y ) ;
	
			//dissect pixel
			dwRed = GetRValue ( crColor ) ;
			dwGreen = GetGValue ( crColor ) ;
			dwBlue = GetBValue ( crColor ) ;

			//shift pixel data left
			dwRed >>= dwRedShiftL ;
			dwGreen >>= dwGreenShiftL ;
			dwBlue >>= dwBlueShiftL ;

			//shift pixel data right
			dwRed <<= dwRedShiftR ;
			dwGreen <<= dwGreenShiftR ;
			dwBlue <<= dwBlueShiftR ;

			//recombine pixel data
			dwColor = dwRed | dwGreen | dwBlue ;

			//copy to surface
			memcpy ( &pbyBuffer [ iBufferPosition ] , &dwColor , dwBytesPerPixel ) ;

		}
	}
	//set up for alpha testing
	g_pd3ddev->SetRenderState ( D3DRS_ALPHATESTENABLE , TRUE ) ;
	g_pd3ddev->SetRenderState ( D3DRS_ALPHAFUNC , D3DCMP_GREATEREQUAL ) ;
	g_pd3ddev->SetRenderState ( D3DRS_ALPHAREF , 0) ;

	//unlock the surface
	g_pImageSurface->UnlockRect ( ) ;

	//restore old bitmap
	SelectObject ( hdc , hbmold ) ;

	//destroy bitmap
	DeleteObject ( hbmnew ) ;

	//destroy dc
	DeleteDC ( hdc ) ;

	//redraw the frame
	RedrawFrame ( ) ;
	AllowPlay(true);
}




//redraw frame
void RedrawFrame ( ) 
{
	//clear the screen
	g_pd3ddev->Clear ( 0 , NULL , D3DCLEAR_TARGET , D3DCOLOR_XRGB ( 255 , 255 , 255 ) , 0.0f, 0 ) ;
	//source rectangle
	RECT rcSrc ;
	SetRect ( &rcSrc , 0 , 0 , SCREENWIDTH , SCREENHEIGHT ) ;

	//destination point
	POINT ptDst ;
	ptDst.x = 0 ;
	ptDst.y = 0 ;

	//back buffer surface
	IDirect3DSurface8* pBackBuffer ;

	//grab back buffer
	g_pd3ddev->GetBackBuffer ( 0 , D3DBACKBUFFER_TYPE_MONO , &pBackBuffer ) ;

	//copy rectangle
	g_pd3ddev->CopyRects ( g_pImageSurface , &rcSrc , 1 , pBackBuffer , &ptDst ) ;


	
	//release the back buffer surface
	pBackBuffer->Release ( ) ;

	//present the scene
	g_pd3ddev->Present ( NULL , NULL , NULL , NULL ) ;
}


[Edit: Added [source] tags. In the future, paste only relevant code - Oluseyi] I have tried several things that work without using the CopyRects line in the RedrawFrame function, How do I draw to the backbuffer after the CopyRects line? Any help would be greatly appreciated. Thanks. [Edited by - Oluseyi on September 21, 2004 8:43:05 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Know thy HTML!

why not &#91; and &#93;?? [razz]

Pertaining to the OP's question: the best way to do this is with line primitives. These are the same thing as triangle primitives, except they only have 2 vertices each. They have all the other components: texture coordinates, diffuse color, etc, and you can put textures and materials on them as well.

To render use one of the following instead of what you're using:

D3DPT_LINELIST
D3DPT_LINESTRIP

For more info, refer to the DXSDK.


Mushu - trying to help those he doesn't know, with things he doesn't know.
Why won't he just go away? An question the universe may never have an answer to...

Share this post


Link to post
Share on other sites
Quote:
Original post by Mushu
Quote:
Original post by Zahlman
Know thy HTML!

why not [ and ]?? [razz]


That works, but I find the ASCII values for a-zA-Z easier to remember than those for punctuation.

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