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

📄 main.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
字号:
//-----------------------------------------------------------------------------
// Name: DInput_Simple
//
// Description: Example code showing how to use DirectInput
//
// File Function: Main body of the program (main.cpp)
//
// Code: 
//		Copyright (c) 2002-2003 LostLogic Corporation. All rights reserved.
//
// Libraries Required:
//		dxguid.lib dinput8.lib
//
// Local Files Required:
//		main.cpp
//		main.h
//
// DX Files: 
//		Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include "main.h"

//--------------------------------------------------------------------------------------
//
// Main windows function, ie main()
//
//--------------------------------------------------------------------------------------
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	HWND		hWnd;
	MSG			msg;
	WNDCLASSEX	wndclass;
	int			iResult;
	int			i;
		
    // Set up window class
	wndclass.cbSize			= sizeof(wndclass);
	wndclass.style			= CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc	= fnMessageProcessor;
	wndclass.cbClsExtra		= 0;
	wndclass.cbWndExtra		= 0;
	wndclass.hInstance		= hInstance;
	wndclass.hIcon			= LoadIcon( NULL, IDI_APPLICATION );
	wndclass.hCursor		= LoadCursor( NULL, IDC_ARROW );
	wndclass.hbrBackground	= (HBRUSH) GetStockObject (WHITE_BRUSH);
	wndclass.lpszMenuName	= NULL;
	wndclass.lpszClassName	= "Direct Input Demo";	// Registered Class Name
	wndclass.hIconSm		= LoadIcon( NULL, IDI_APPLICATION );

	// Register the window class
	if( RegisterClassEx( &wndclass ) == NULL ) 
	{
		// The program failed, exit
		exit(1);
	}

	// Create the window
	hWnd = CreateWindowEx(	WS_EX_OVERLAPPEDWINDOW,
							"Direct Input Demo",	// Class Name
							"DInput_Simple",		// Name Displayed on Title Bar
							WS_OVERLAPPEDWINDOW,
							0,
							0,
							320,
							200,
							NULL,
							NULL,
							hInstance,
							NULL );

	// Display the window
	ShowWindow( hWnd, iCmdShow );

	// Set the global instance of the program
	g_hInstance = hInstance;

	// Initialize Direct Input
	iResult = iInitDirectInput();
	if( iResult != INPUTERROR_SUCCESS ) {
		MessageBox( hWnd, "DirectInput Error", "Unable to initialize Direct Input.", MB_ICONERROR );
		vCleanup();
		exit( 1 );
	}
	
	// Initialize DI Keyboard
	iResult = iInitKeyboard(hWnd);
	if( iResult != INPUTERROR_SUCCESS ) {
		MessageBox( hWnd, "DirectInput Error", "Unable to initialize Keyboard.", MB_ICONERROR );
		vCleanup();
		exit( 1 );
	}
	
	// Clear out message structure
	ZeroMemory( &msg, sizeof(msg) );
    
    // Enter the message loop
    while( msg.message!=WM_QUIT ) {
        if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
		}
		else {
			// Read from the keyboard buffer
			iResult = iReadKeyboard();
			// Check how many key presses were returned
			if( iResult ) {
				// Loop through result data
				for( i = 0; i < iResult; i++ ) {
					// Exit the program if the ESC key is hit
					if( diks[ DIK_ESCAPE ][ i ] ) {
						PostQuitMessage( 0 );
					}
					else if ( ascKeys[ 13 ][ i ] ) {
						PostQuitMessage( 0 );
					}
				}
			}
		}
    }

	// Clean up everything and exit the app
    vCleanup();
    UnregisterClass( "Direct Input Demo", wndclass.hInstance );
    return 0;
}

//--------------------------------------------------------------------------------------
//
// Windows message processor
//
//--------------------------------------------------------------------------------------
LRESULT WINAPI fnMessageProcessor( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	HDC			hdc;
	PAINTSTRUCT	ps;
	char		szMessage[ 256 ];
	
	// Force a redraw
	InvalidateRect( hWnd, NULL, 0 );

	switch( msg ) 
	{
		// Called when the window is refreshed
		case WM_PAINT:
			// Start painting
			hdc = BeginPaint( hWnd, &ps );
			// Populate the string to output
			sprintf( szMessage, "Press The Esc Key To Exit" ); 
			// Draw the text 
			TextOut( hdc, 60, 70, szMessage, strlen( szMessage ) );
			// End the painting
			EndPaint( hWnd, &ps );
			break;
		
		case WM_LBUTTONDOWN:
			break;
		
		case WM_DESTROY:
            PostQuitMessage( 0 );
            return 0;
		
		default:
			break;
    }
    return DefWindowProc( hWnd, msg, wParam, lParam );
}

//--------------------------------------------------------------------------------------
//
// Function to cleanup allocated objects
//
//--------------------------------------------------------------------------------------
void vCleanup( void )
{
	// Release the keyboard device
	if( pKeyboard ) {
		pKeyboard->Unacquire();
		pKeyboard->Release();
		pKeyboard = NULL;
	}
	// Release DirectInput
	if( pDI ) {
		pDI->Release();
		pDI = NULL;
	}
}

//-----------------------------------------------------------------------------
//
// Initializes the DirectInput system
//
//-----------------------------------------------------------------------------
int	iInitDirectInput(void)
{
	HRESULT	hReturn;
	
	// Do not try to create Direct Input if already created
	if( !pDI ) {
		// Create a DInput object
		if( FAILED( hReturn = DirectInput8Create( 
			g_hInstance, DIRECTINPUT_VERSION, 
			IID_IDirectInput8, (VOID**)&pDI, NULL ) ) ) {
			
			return( INPUTERROR_NODI );
		}
	}
	else {
		return( INPUTERROR_KEYBOARDEXISTS );
	}
	
	return( INPUTERROR_SUCCESS );
}

//-----------------------------------------------------------------------------
//
// Reads the keyboard data buffer for input actions
//
//-----------------------------------------------------------------------------
int iInitKeyboard(HWND hWnd)
{
	HRESULT			hReturn = 0;
	DIPROPDWORD		dipdw; 
	
	// Don't try to create the keyboard twice
	if( pKeyboard ) {
		return( INPUTERROR_KEYBOARDEXISTS );
	}
	
	// Exit out if no Direct Input interface found
	else if ( !pDI ) {
		return( INPUTERROR_NODI );
	}

	// Obtain an interface to the system keyboard device
	if( FAILED( hReturn = pDI->CreateDevice( 
		GUID_SysKeyboard, 
		&pKeyboard, 
		NULL ) ) ) {

		return( INPUTERROR_NOKEYBOARD );
	}
	
	// Create buffer to hold keyboard data
	ZeroMemory( &dipdw, sizeof( DIPROPDWORD ) );
	dipdw.diph.dwSize		= sizeof( DIPROPDWORD ); 
	dipdw.diph.dwHeaderSize = sizeof( DIPROPHEADER ); 
	dipdw.diph.dwObj		= 0; 
	dipdw.diph.dwHow		= DIPH_DEVICE; 
	dipdw.dwData			= KEYBOARD_BUFFERSIZE;
	
	// Set the size of the buffer
	if( FAILED( hReturn = pKeyboard->SetProperty( 
		DIPROP_BUFFERSIZE, 
		&dipdw.diph ) ) ) {
		
		return( INPUTERROR_NOKEYBOARD );
	}
	 
	// Set the format of the keyboard
	if( FAILED( hReturn = pKeyboard->SetDataFormat( 
		&c_dfDIKeyboard ) ) ) {
		
		return( INPUTERROR_NOKEYBOARD );
	}
	
	// Set the co-operative level to exclusive access
	if( FAILED( hReturn = pKeyboard->SetCooperativeLevel( 
		hWnd, 
		DISCL_NONEXCLUSIVE | DISCL_FOREGROUND 
		) ) ) {
		
		return( INPUTERROR_NOKEYBOARD );
	}
	
	// Acquire the keyboard device
	pKeyboard->Acquire();

	// Get the keyboard layout, this is required
	// for converting scan codes to ascii codes.
	g_Layout = GetKeyboardLayout( 0 );
	
	return( INPUTERROR_SUCCESS );
}

//-----------------------------------------------------------------------------
//
// Reads the keyboard data buffer for input actions
//
//-----------------------------------------------------------------------------
int iReadKeyboard( void )
{
	HRESULT				hr;
	DWORD				dwCurBuffer;
	DIDEVICEOBJECTDATA	didKeyboardBuffer[KEYBOARD_BUFFERSIZE];
	DWORD				dwItems = KEYBOARD_BUFFERSIZE;
	BYTE				byteASCII;

	// Dont try to read the keyboard if the interface or device is invalid
    if( !pKeyboard || !pDI ) {
        return( INPUTERROR_NOKEYBOARD );
	}

	// Read the buffered data
	hr = pKeyboard->GetDeviceData( 
		sizeof( DIDEVICEOBJECTDATA ), 
		didKeyboardBuffer, 
		&dwItems, 
		0 ); 
	
	// Keyboard may have been lost, reacquire it
	if( FAILED( hr ) ) {
		pKeyboard->Acquire();
		return( INPUTERROR_SUCCESS );
	}
	
	// Process data if there is data to read
	if ( dwItems ) {
		// Process the data
		for( dwCurBuffer = 0; dwCurBuffer < dwItems; dwCurBuffer++ ) {
			
			// Map scan-code to ascii code
			byteASCII = Scan2Ascii( didKeyboardBuffer[dwCurBuffer].dwOfs );
			
			// Set key to be down (depressed)
			if( didKeyboardBuffer[ dwCurBuffer ].dwData & 0x80 ) {
				ascKeys[ byteASCII ][ dwCurBuffer ] = 1;
				diks[ didKeyboardBuffer[ dwCurBuffer ].dwOfs ]
					[ dwCurBuffer ]= 1;
			}
			// Set key to be up
			else {
				ascKeys[ byteASCII ][ dwCurBuffer ] = 0;
				diks[ didKeyboardBuffer[ dwCurBuffer ].dwOfs ]
					[ dwCurBuffer ] = 0;
			}
		}
	} 
	// Return # of items read
    return ( dwItems );
}

//-----------------------------------------------------------------------------
//
// Converts scan codes to ASCII codes
//
//-----------------------------------------------------------------------------
BYTE Scan2Ascii(DWORD scancode)
{
	UINT vk;
	
	// Map the scancode to an ascii code
	vk = MapVirtualKeyEx( scancode, 1, g_Layout);

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

⌨️ 快捷键说明

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