📄 main.cpp
字号:
pVertices[2].tu = 1.0f;
pVertices[2].tv = 1.0f;
pVertices[2].tu2 = 1.0f;
pVertices[2].tv2 = 1.0f;
pVertices[2].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
pVertices[3].position = D3DXVECTOR3( 1.0f, 1.0f, 0.0f );
pVertices[3].tu = 1.0f;
pVertices[3].tv = 0.0f;
pVertices[3].tu2 = 1.0f;
pVertices[3].tv2 = 0.0f;
pVertices[3].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
// Done editing, unlock the buffer
g_pVBInterface->Unlock();
//---------------------------------------------
// LOAD TEXTURES
//---------------------------------------------
// Load the tiles
for( int i = 0; i < 18; i++ ) {
// Set the name
sprintf( szTileName, "tile%d.tga", i );
// Load it
if( FAILED( D3DXCreateTextureFromFile(
g_pd3dDevice,
szTileName,
&g_pTexture[ i ] ) ) ) {
return;
}
}
// Load the tile overlay
if( FAILED( D3DXCreateTextureFromFile(
g_pd3dDevice,
"overlay.tga",
&g_pTexture[ 18 ] ) ) ) {
return;
}
// Text Font
hFont = CreateFont(
16, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, PROOF_QUALITY,
0, "fixedsys" );
// Create the Direct3D Font
D3DXCreateFont( g_pd3dDevice, hFont, &pD3DXFont );
};
//--------------------------------------------------------------------------------------
//
// Function to draw a 3D object in 2D space
//
// iXPos = x-position in screen-space of the top-left corner of the object
// iYPos = y-position in screen-space of the top-left corner of the object
// fXSize = Width of the object in screen-space (pixels)
// fYSize = Height of the object in screen-space (pixels)
// iTexture = Index into the texture array of object to draw
//
//--------------------------------------------------------------------------------------
void vDrawInterfaceObject( int iXPos, int iYPos, float fXSize, float fYSize, int iTexture )
{
D3DXMATRIX matWorld, matRotation, matTranslation, matScale;
float fXPos, fYPos;
// Set default position,scale,rotation
D3DXMatrixIdentity( &matTranslation );
// Scale the sprite
D3DXMatrixScaling( &matScale, fXSize, fYSize, 1.0f );
D3DXMatrixMultiply( &matTranslation, &matTranslation, &matScale );
// Rotate the sprite
D3DXMatrixRotationZ( &matRotation, 0.0f );
D3DXMatrixMultiply( &matWorld, &matTranslation, &matRotation );
// Calculate the position in screen-space
fXPos = (float)(-(g_iWindowWidth/2)+iXPos);
fYPos = (g_iWindowHeight/2)-fYSize-iYPos;
// Move the sprite
matWorld._41 = fXPos; // X
matWorld._42 = fYPos; // Y
// Set matrix
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
g_pd3dDevice->SetTexture( 0, g_pTexture[ iTexture ] );
g_pd3dDevice->SetStreamSource( 0, g_pVBInterface, 0, sizeof( CUSTOMVERTEX ) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
// Dereference texture
g_pd3dDevice->SetTexture(0, NULL);
}
//--------------------------------------------------------------------------------------
//
// Function to initialize the map
//
//--------------------------------------------------------------------------------------
void vInitMap( void )
{
int i;
// Clear the map
for( i = 0; i < g_iMapWidth * g_iMapHeight; i++ ) {
g_iTileMap[ i ] = 1;
// Randomly place rocks
if( rand()%10 == 5 ) {
g_iTileMap[ i ] = 2;
}
}
}
//-----------------------------------------------------------------------------
//
// 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 );
}
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 );
}
//
// SCROLL AROUND THE MAP
//
// Up
if( diks[ DIK_UP ][ i ] ) {
g_iYPos--;
}
// Down
if( diks[ DIK_DOWN ][ i ] ) {
g_iYPos++;
}
// Left
if( diks[ DIK_LEFT ][ i ] ) {
g_iXPos--;
}
// Right
if( diks[ DIK_RIGHT ][ i ] ) {
g_iXPos++;
}
// Make sure in bounds
if( g_iYPos < 0 )
g_iYPos = 0;
else if ( g_iYPos >= (g_iMapHeight-g_iTilesHigh) )
g_iYPos = (g_iMapHeight-g_iTilesHigh);
if( g_iXPos < 0 )
g_iXPos = 0;
else if ( g_iXPos >= (g_iMapWidth-g_iTilesWide) )
g_iXPos = (g_iMapWidth-g_iTilesWide);
}
}
}
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-100, g_iYOffset, 100, g_iWindowHeight-20, hwnd, NULL, hinst, NULL);
// Previous Tile Button
hBUTTON_PREVTILE = CreateWindow(
"BUTTON", "<",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
10, 405, 20, 20, hWndToolBar, (HMENU)ID_BUTTON_PREVTILE, hinst, NULL);
// Next Tile Button
hBUTTON_NEXTTILE = CreateWindow(
"BUTTON", ">",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
65, 405, 20, 20, hWndToolBar, (HMENU)ID_BUTTON_NEXTTILE, hinst, NULL);
// Save Button
hBUTTON_SAVE = CreateWindow(
"BUTTON", "Save",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
5, 365, 85, 20, hWndToolBar, (HMENU)ID_BUTTON_SAVE, hinst, NULL);
// Load Button
hBUTTON_LOAD = CreateWindow(
"BUTTON", "Load",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
5, 325, 85, 20, hWndToolBar, (HMENU)ID_BUTTON_LOAD, hinst, NULL);
// Activate edit area
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -