⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
📖 第 1 页 / 共 3 页
字号:

	// Return the ascii code
	return( vk );
}

//--------------------------------------------------------------------------------------
//
// Checks input from the keyboard 
// and acts on it accordingly.
//
//--------------------------------------------------------------------------------------
void vCheckInput( void )
{
	//
	// KEYBOARD INPUT
	//
	
	// Read from the keyboard buffer
	int iResult = iReadKeyboard();
	
	// Check how many key presses were returned
	if( iResult ) {
		
		// Loop through result data
		for( int i = 0; i < iResult; i++ ) {

			// Exit the program if the ESC key is hit
			if( diks[ DIK_ESCAPE ][ i ] ) {
				PostQuitMessage( 0 );
			}

			//
			// Camera movements
			//
			if( diks[ DIK_LEFT ][ i ] ) {
				g_vecCameraOrigin.x--;
				g_vecCameraTarget.x--;
				vSetupView( g_vecCameraOrigin, g_vecCameraTarget );
			}
			
			if( diks[ DIK_RIGHT ][ i ] ) {
				g_vecCameraOrigin.x++;
				g_vecCameraTarget.x++;
				vSetupView( g_vecCameraOrigin, g_vecCameraTarget );
			}

			if( diks[ DIK_UP ][ i ] ) {
				g_vecCameraOrigin.y++;
				g_vecCameraTarget.y++;
				vSetupView( g_vecCameraOrigin, g_vecCameraTarget );
			}
			
			if( diks[ DIK_DOWN ][ i ] ) {
				g_vecCameraOrigin.y--;
				g_vecCameraTarget.y--;
				vSetupView( g_vecCameraOrigin, g_vecCameraTarget );
			}

			//
			// Object Movement
			//

			if( animTest.m_iNumFrames > 0 ) {
				// Rotate - Y
				if( diks[ DIK_Q ][ i ] ) {
					animTest.vUpdateRot( 
						g_iCurObj, 
						animTest.m_iCurFrame, 
						D3DXVECTOR3(0.0f, -1.0f, 0.0f) );
				}
				// Rotate + Y
				if( diks[ DIK_E ][ i ] ) {
					animTest.vUpdateRot( 
						g_iCurObj, 
						animTest.m_iCurFrame, 
						D3DXVECTOR3(0.0f, 1.0f, 0.0f) );
				}
				// Rotate - X
				if( diks[ DIK_Z ][ i ] ) {
					animTest.vUpdateRot( 
						g_iCurObj, 
						animTest.m_iCurFrame, 
						D3DXVECTOR3(-1.0f, 0.0f, 0.0f) );
				}
				// Rotate + X
				if( diks[ DIK_C ][ i ] ) {
					animTest.vUpdateRot( 
						g_iCurObj, 
						animTest.m_iCurFrame, 
						D3DXVECTOR3(1.0f, 0.0f, 0.0f) );
				}
				// Rotate - Z
				if( diks[ DIK_F ][ i ] ) {
					animTest.vUpdateRot( 
						g_iCurObj, 
						animTest.m_iCurFrame, 
						D3DXVECTOR3(0.0f, 0.0f, -1.0f) );
				}
				// Rotate + Z
				if( diks[ DIK_G ][ i ] ) {
					animTest.vUpdateRot( 
						g_iCurObj, 
						animTest.m_iCurFrame, 
						D3DXVECTOR3(0.0f, 0.0f, 1.0f) );
				}

				// Trans - Y
				if( diks[ DIK_S ][ i ] ) {
					animTest.vUpdateTrans( 
						g_iCurObj, 
						animTest.m_iCurFrame, 
						D3DXVECTOR3(0.0f, -1.0f, 0.0f) );
				}
				// Trans + Y
				if( diks[ DIK_W ][ i ] ) {
					animTest.vUpdateTrans( 
						g_iCurObj, 
						animTest.m_iCurFrame, 
						D3DXVECTOR3(0.0f, 1.0f, 0.0f) );
				}
				// Trans - X
				if( diks[ DIK_A ][ i ] ) {
					animTest.vUpdateTrans( 
						g_iCurObj, 
						animTest.m_iCurFrame, 
						D3DXVECTOR3(-1.0f, 0.0f, 0.0f) );
				}
				// Trans + X
				if( diks[ DIK_D ][ i ] ) {
					animTest.vUpdateTrans( 
						g_iCurObj, 
						animTest.m_iCurFrame, 
						D3DXVECTOR3(1.0f, 0.0f, 0.0f) );
				}
				// Trans - Z
				if( diks[ DIK_V ][ i ] ) {
					animTest.vUpdateTrans( 
						g_iCurObj, 
						animTest.m_iCurFrame, 
						D3DXVECTOR3(0.0f, 0.0f, -1.0f) );
				}
				// Trans + Z
				if( diks[ DIK_B ][ i ] ) {
					animTest.vUpdateTrans( 
						g_iCurObj, 
						animTest.m_iCurFrame, 
						D3DXVECTOR3(0.0f, 0.0f, 1.0f) );
				}

				//
				vUpdateToolbarStats();
			}
		}
	}
}

//--------------------------------------------------------------------------------------
//
// Creates the toolbar that contains the various commands
// for the editor
//
//--------------------------------------------------------------------------------------
void vCreateToolbar(HWND hwnd, HINSTANCE hinst)
{
	WNDCLASSEX	wcToolBar;

	// Set up and register toolbar window class
	wcToolBar.cbSize		= sizeof(wcToolBar);
	wcToolBar.style			= CS_HREDRAW | CS_VREDRAW;
	wcToolBar.lpfnWndProc	= fnMessageProcessor;
	wcToolBar.cbClsExtra	= 0;
	wcToolBar.cbWndExtra	= 0;
	wcToolBar.hInstance		= hinst;
	wcToolBar.hIcon			= LoadIcon( NULL, IDI_APPLICATION );
	wcToolBar.hCursor		= LoadCursor( NULL, IDC_ARROW );
	wcToolBar.hbrBackground	= (HBRUSH) GetStockObject (COLOR_BACKGROUND);
	wcToolBar.lpszMenuName	= NULL;
	wcToolBar.lpszClassName	= "ToolBar";	// Registered Class Name
	wcToolBar.hIconSm		= LoadIcon( NULL, IDI_APPLICATION );
    RegisterClassEx(&wcToolBar);
    
	// Create toolbar window
	hWndToolBar = CreateWindowEx ( 
		WS_EX_LEFT|WS_EX_TOPMOST|WS_EX_TOOLWINDOW,
		"ToolBar", "ToolBar",
		WS_BORDER | WS_VISIBLE | WS_CAPTION | WS_MINIMIZEBOX,	
		g_iWindowWidth+15 , g_iYOffset, 150, g_iWindowHeight-20, hwnd, NULL, hinst, NULL);

	hBUTTON_SAVE = CreateWindow(
		"BUTTON", "Save Anim",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		5, 410, 135, 20, hWndToolBar, (HMENU)ID_BUTTON_SAVE, hinst, NULL);

	hBUTTON_LOAD = CreateWindow(
		"BUTTON", "Load Anim",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		5, 385, 135, 20, hWndToolBar, (HMENU)ID_BUTTON_LOAD, hinst, NULL);

	hBUTTON_LOADOBJ = CreateWindow(
		"BUTTON", "Load Objects",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		5, 320, 135, 20, hWndToolBar, (HMENU)ID_BUTTON_LOADOBJ, hinst, NULL);

	hBUTTON_PREVOBJ = CreateWindow(
		"BUTTON", "Prev Obj",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		5, 255, 135, 20, hWndToolBar, (HMENU)ID_BUTTON_PREVOBJ, hinst, NULL);

	hBUTTON_NEXTOBJ = CreateWindow(
		"BUTTON", "Next Obj",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		5, 280, 135, 20, hWndToolBar, (HMENU)ID_BUTTON_NEXTOBJ, hinst, NULL);

	hBUTTON_PREVFRAME = CreateWindow(
		"BUTTON", "Prev Frame",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		5, 5, 135, 20, hWndToolBar, (HMENU)ID_BUTTON_PREVFRAME, hinst, NULL);

	hBUTTON_NEXTFRAME = CreateWindow(
		"BUTTON", "Next Frame",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		5, 30, 135, 20, hWndToolBar, (HMENU)ID_BUTTON_NEXTFRAME, hinst, NULL);

	hBUTTON_NEWFRAME = CreateWindow(
		"BUTTON", "New Frame",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		5, 65, 135, 20, hWndToolBar, (HMENU)ID_BUTTON_NEWFRAME, hinst, NULL);

	hBUTTON_STARTSTOP = CreateWindow(
		"BUTTON", "Start / Stop Anim",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		5, 130, 135, 20, hWndToolBar, (HMENU)ID_BUTTON_STARTSTOP, hinst, NULL);

	//
	// Text Entry Fields
	//
	hEDITBOX_X = CreateWindow(
		"EDIT", NULL,
		WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL,
		5, 170, 60, 20, hWndToolBar, (HMENU)ID_EDITBOX_X, hinst, NULL);

	hEDITBOX_Y = CreateWindow(
		"EDIT", NULL,
		WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL,
		5, 195, 60, 20, hWndToolBar, (HMENU)ID_EDITBOX_Y, hinst, NULL);

	hEDITBOX_Z = CreateWindow(
		"EDIT", NULL,
		WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL,
		5, 220, 60, 20, hWndToolBar, (HMENU)ID_EDITBOX_Z, hinst, NULL);

	hEDITBOX_XROT = CreateWindow(
		"EDIT", NULL,
		WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL,
		75, 170, 60, 20, hWndToolBar, (HMENU)ID_EDITBOX_XROT, hinst, NULL);

	hEDITBOX_YROT = CreateWindow(
		"EDIT", NULL,
		WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL,
		75, 195, 60, 20, hWndToolBar, (HMENU)ID_EDITBOX_YROT, hinst, NULL);

	hEDITBOX_ZROT = CreateWindow(
		"EDIT", NULL,
		WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL,
		75, 220, 60, 20, hWndToolBar, (HMENU)ID_EDITBOX_ZROT, hinst, NULL);

	// Activate edit area
	SetActiveWindow( g_hWnd );
}

//--------------------------------------------------------------------------------------
//
// Checks the mouse buttons and reacts accordingly
//
//--------------------------------------------------------------------------------------
void vCheckMouse( void )
{
	RECT		rcWindow;
	POINT		Point;
	int			iMouseX;
	int			iMouseY;

	// Get mouse coordinates
	GetCursorPos( &Point );
	iMouseX = Point.x;
	iMouseY = Point.y;
	
	// Figure out the toolbar work area
	GetWindowRect( hWndToolBar, &rcWindow );

	// Check if mouse within tool-bar window
	if( iMouseX > rcWindow.left &&
		iMouseX < rcWindow.right && 
		iMouseY > rcWindow.top &&
		iMouseY < rcWindow.bottom ) 
	{
		// Adjust mouse coords to be local to toolbar
		iMouseX -= rcWindow.right;
		iMouseY -= rcWindow.top;
	}

	// Check if mouse within edit window
	else {
		GetWindowRect( g_hWnd, &rcWindow );

		if( iMouseX > rcWindow.left &&
			iMouseX < rcWindow.right && 
			iMouseY > rcWindow.top &&
			iMouseY < rcWindow.bottom ) 
		{
			// Adjust mouse coords to be local to edit window
			iMouseX -= rcWindow.left+g_iXOffset;
			iMouseY -= rcWindow.top+g_iYOffset;
		}
	}
}

//--------------------------------------------------------------------------------------
//
// Creates lights for the 3D scene
//
//--------------------------------------------------------------------------------------
void vCreateLights(void)
{
	D3DLIGHT9		d3dLight;

	// Initialize the default scene material
    ZeroMemory( &g_mtrlDefault, sizeof(D3DMATERIAL9) );
    g_mtrlDefault.Ambient.r = 1.0f;
	g_mtrlDefault.Ambient.g = 1.0f;
	g_mtrlDefault.Ambient.b = 1.0f;
	g_mtrlDefault.Ambient.a = 1.0f;
	g_mtrlDefault.Diffuse.r = 1.0f;
	g_mtrlDefault.Diffuse.g = 1.0f;
	g_mtrlDefault.Diffuse.b = 1.0f;
	g_mtrlDefault.Diffuse.a = 1.0f;
	g_pd3dDevice->SetMaterial( &g_mtrlDefault );
	
	// Clear out light structure
	ZeroMemory(&d3dLight, sizeof(D3DLIGHT9));
	d3dLight.Type = D3DLIGHT_POINT;
	d3dLight.Diffuse.r  = 1.0f;
	d3dLight.Diffuse.g  = 1.0f;
	d3dLight.Diffuse.b  = 1.0f;
	d3dLight.Position.x = 0.0f;
	d3dLight.Position.y = 30.0f;
	d3dLight.Position.z = -100.0f;
	d3dLight.Attenuation0 = 1.0f; 
	d3dLight.Attenuation1 = 0.0025f;
	d3dLight.Range      = 1000.0f;
	// Set the active light
    g_pd3dDevice->SetLight( 0, &d3dLight );
	// Enable the light
    g_pd3dDevice->LightEnable( 0, TRUE );
	// Set ambient light
	g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00606060 );
}

//--------------------------------------------------------------------------------------
//
// Sets up the view matrix for the 3D scene.  This puts the 
// camera in its proper place
//
//--------------------------------------------------------------------------------------
void vSetupView( D3DXVECTOR3 origin, D3DXVECTOR3 target )
{
	D3DXMATRIX	matview;

	// Setup Camera View
	D3DXMatrixLookAtLH( &matview, 
						&origin, 
						&target, 
						&D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );

	// Tell the device to use the camera view for the viewport
	g_pd3dDevice->SetTransform( D3DTS_VIEW, &matview );
}

//--------------------------------------------------------------------------------------
//
// Updates the text on the toolbar window.  For now this includes
// the translation and rotation data for the current object.
//
// *FOR YOU TO DO* Add input and output for the scale parameters
// of the object.
//
//--------------------------------------------------------------------------------------
void vUpdateToolbarStats( void )
{
	char szBuffer[ 32 ];

	if( animTest.m_iNumFrames > 0 ) {
		// Translation
		sprintf( szBuffer, "%.2f", animTest.m_keyFrames[ g_iCurObj][ animTest.m_iCurFrame ]->m_vecTrans.x );
		SetWindowText( hEDITBOX_X, szBuffer );
		sprintf( szBuffer, "%.2f", animTest.m_keyFrames[ g_iCurObj][ animTest.m_iCurFrame ]->m_vecTrans.y );
		SetWindowText( hEDITBOX_Y, szBuffer );
		sprintf( szBuffer, "%.2f", animTest.m_keyFrames[ g_iCurObj][ animTest.m_iCurFrame ]->m_vecTrans.z );
		SetWindowText( hEDITBOX_Z, szBuffer );
		// Rotation
		sprintf( szBuffer, "%.2f", animTest.m_keyFrames[ g_iCurObj][ animTest.m_iCurFrame ]->m_vecRot.x );
		SetWindowText( hEDITBOX_XROT, szBuffer );
		sprintf( szBuffer, "%.2f", animTest.m_keyFrames[ g_iCurObj][ animTest.m_iCurFrame ]->m_vecRot.y );
		SetWindowText( hEDITBOX_YROT, szBuffer );
		sprintf( szBuffer, "%.2f", animTest.m_keyFrames[ g_iCurObj][ animTest.m_iCurFrame ]->m_vecRot.z );
		SetWindowText( hEDITBOX_ZROT, szBuffer );
	}
}

//--------------------------------------------------------------------------------------
//
// Sets up the animation to be edited.  For now, all that
// you must do is set the internal Direct3D pointer
//
//--------------------------------------------------------------------------------------
void vInitAnimation( void )
{
	// Set the 3d device
	animTest.vSet3DDevice( g_pd3dDevice );
}

//--------------------------------------------------------------------------------------
//
// Loads objects into the animation scene.  For now this
// is hardcoded.  You should modify it to load objects
// dynamically.
//
//--------------------------------------------------------------------------------------
void vLoadObject( void )
{
	//
	// LOAD THE ROBOT OBJECTS
	// *NOTE* This is hardcoded only for
	// demonstration purposes.
	//
	// *FOR YOU TO DO* Add the ability 
	// to load objects from a file 
	// requestor.
	//

	// Reset the current animation
	animTest.vReset();

	// Load the hard-coded objects
	animTest.iNewObj( "droid_still" );
	animTest.iNewObj( "radar_dish" );

	// Render the scene
	vRender();

	// Update the toolbar
	vUpdateToolbarStats();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -