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

📄 main.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
📖 第 1 页 / 共 3 页
字号:
			if( !stricmp( szZoneHit, "EXIT_BUTTON" ) ) {
				// Set current screen to exit screen
				g_iCurrentScreen = 2;
				// Setup the mouse zones
				vSetupMouseZones( 2 );
			}
		}
		//
		// OPTIONS MENU LOGIC
		//
		else if( g_iCurrentScreen == 7 ) {
			// Go to the title screen
			if( !stricmp( szZoneHit, "EXIT_BUTTON" ) ) {
				// Set current screen to exit screen
				g_iCurrentScreen = 2;
				// Setup the mouse zones
				vSetupMouseZones( 2 );
			}
			// Go back to main menu
			else if( !stricmp( szZoneHit, "OPTIONS_BACK" ) ) {
				// Set current screen to main menu
				g_iCurrentScreen = 1;
				// Setup the mouse zones
				vSetupMouseZones( 1 );
			}
		}
	}

	//
	// 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 );
			}
			//
			// TEXT INPUT BOX LOGIC
			//
			if( g_bTextInputActive ) {
				// Don't add to text unless there is still room to type
				if( g_shTextInputPosition <  g_shTextMaxSize ) {
					// Store keys that are depressed
					for( int j = 32; j < 123; j++ ) {
						// Make sure is valid character
						if( ( j > 96 )
							|| ( j == 32 )
							|| ( j > 47 && j < 58 ) ) {
							// Check if key is depressed
							if( ascKeys[ j ][ i ] ) {
								if( g_bShift ) {
									g_szTextInputBuffer[ g_shTextInputPosition ] = toupper( j );
								}
								else {
									g_szTextInputBuffer[ g_shTextInputPosition ] = j;
								}
								g_shTextInputPosition++;
							}
						}
					}
				}
				// Check for backspace
				if( diks[ DIK_BACK ][ i ] ) {
					// Check if text has been entered
					if( g_shTextInputPosition ) {
						// Clear last character
						g_szTextInputBuffer[ g_shTextInputPosition-1 ] = '\0';
						// Backup the cursor
						g_shTextInputPosition--;
					}
				}
				// Check for ENTER key
				if( diks[ DIK_RETURN ][ i ] ) {
					// Turn off name input
					g_bTextInputActive = 0;

					//
					// ACTIVATE THE NEW GAME
					//
					if( g_iTextInputFieldID == GAMEINPUT_NAME ) {
						// Set screen to main game screen
						g_iCurrentScreen = 5;
						// Setup the mouse zones
						vSetupMouseZones( 5 );
					}
					break;
				}
			}
		}
	}
}

//--------------------------------------------------------------------------------------
//
// Function to setup mouse zones for the various screens and menus
// in the program.
//
//--------------------------------------------------------------------------------------
void vSetupMouseZones( int iMenu )
{
	// Title screen
	if( iMenu == 0 ) {
		MZones.vFreeZones();
		MZones.vInitialize( 2 );
		MZones.iAddZone( "TITLE_SCREEN", 0, 0, 640, 480, 2 );	// Left or right activated
		MZones.iAddZone( "EXIT_BUTTON", 587, 0, 53, 24, 0 );	// Left activated
	}
	// Main menu
	else if( iMenu == 1 ) {
		MZones.vFreeZones();
		MZones.vInitialize( 10 );
		MZones.iAddZone( "BACKGROUND", 0, 0, 640, 480, 3 );
		MZones.iAddZone( "EXIT_BUTTON", 587, 0, 53, 24, 0 );
		MZones.iAddZone( "MAINMENU_NEWGAME", 192, 64, 256, 64, 0 );
		MZones.iAddZone( "MAINMENU_LOADGAME", 192, 128, 256, 64, 0 );
		MZones.iAddZone( "MAINMENU_SAVEGAME", 192, 192, 256, 64, 0 );
		MZones.iAddZone( "MAINMENU_OPTIONS", 192, 256, 256, 64, 0 );
		// Highlight zones (for graphic looks only)
		MZones.iAddZone( "MAINMENU_NEWGAME_H", 192, 64, 256, 64, 3 );
		MZones.iAddZone( "MAINMENU_LOADGAME_H", 192, 128, 256, 64, 3 );
		MZones.iAddZone( "MAINMENU_SAVEGAME_H", 192, 192, 256, 64, 3 );
		MZones.iAddZone( "MAINMENU_OPTIONS_H", 192, 256, 256, 64, 3 );
	}
	// Exit splash screen
	else if( iMenu == 2 ) {
		MZones.vFreeZones();
		MZones.vInitialize( 1 );
		MZones.iAddZone( "EXIT_SCREEN", 0, 0, 640, 480, 2 );
	}
	// New game screen
	else if( iMenu == 4 ) {
		MZones.vFreeZones();
		MZones.vInitialize( 1 );
		MZones.iAddZone( "EXIT_BUTTON", 587, 0, 53, 24, 0 );

		//
		// Setup input box
		//

		// Set cursor position
		g_shTextInputXPos = 200;
		g_shTextInputYPos = 196;
		// Clear text data
		memset( g_szTextInputBuffer, 0x00, 64 );
		// Set data position
		g_shTextInputPosition = 0;
		// Set data field active
		g_iTextInputFieldID = GAMEINPUT_NAME;
		// Set text input active
		g_bTextInputActive = 1;
		// Set cursor flash timer
		g_dwTextInputTimer = 0;
		// Set cursor flash to off
		g_bTextInputCursorFlash = 0;
		// Set the max name size to 20 characters
		g_shTextMaxSize = 20;
	}
	// In-game screen
	else if( iMenu == 5) {
		MZones.vFreeZones();
		MZones.vInitialize( 1 );
		MZones.iAddZone( "EXIT_BUTTON", 587, 0, 53, 24, 0 );
	}
	// Options menu
	else if( iMenu == 7 ) {
		MZones.vFreeZones();
		MZones.vInitialize( 6 );
		MZones.iAddZone( "BACKGROUND", 0, 0, 640, 480, 3 );
		MZones.iAddZone( "EXIT_BUTTON", 587, 0, 53, 24, 0 );
		MZones.iAddZone( "OPTIONS_AUDIO", 192, 64, 256, 64, 0 );
		MZones.iAddZone( "OPTIONS_VIDEO", 192, 128, 256, 64, 0 );
		MZones.iAddZone( "OPTIONS_DIFF", 192, 192, 256, 64, 0 );
		MZones.iAddZone( "OPTIONS_BACK", 192, 256, 256, 64, 0 );
	}
}

//-----------------------------------------------------------------------------
//
// 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++ ) {
			
			// Default all keys to being "up"
			for( int j = 0; j < 256; j++ ) {
				ascKeys[ j ][ dwCurBuffer ] = 0;
				diks[ j ][ dwCurBuffer ] = 0;
			}
			
			// 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;

				// Check if SHIFT key changed
				if( didKeyboardBuffer[ dwCurBuffer ].dwOfs == DIK_LSHIFT || 
					didKeyboardBuffer[ dwCurBuffer ].dwOfs == DIK_RSHIFT ) {
					g_bShift = 1;
				}
			}
			// Set key to be up
			else {
				ascKeys[ byteASCII ][ dwCurBuffer ] = 0;
				diks[ didKeyboardBuffer[ dwCurBuffer ].dwOfs ]
					[ dwCurBuffer ] = 0;
				
				// Check if SHIFT key changed
				if( didKeyboardBuffer[ dwCurBuffer ].dwOfs == DIK_LSHIFT || 
					didKeyboardBuffer[ dwCurBuffer ].dwOfs == DIK_RSHIFT ) {
					g_bShift = 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);

	// Map to lower-case
	vk = tolower( vk );

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

⌨️ 快捷键说明

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