0001:
0002:
0003:
0004:
0005:
0006: #define STRICT
0007: #define DIRECTINPUT_VERSION 0x0800
0008: #include <windows.h>
0009: #include <commctrl.h>
0010: #include <commdlg.h>
0011: #include <basetsd.h>
0012: #include <math.h>
0013: #include <stdio.h>
0014: #include <d3dx9.h>
0015: #include <dxerr9.h>
0016: #include <tchar.h>
0017: #include <dinput.h>
0018: #include "DXUtil.h"
0019: #include "D3DEnumeration.h"
0020: #include "D3DSettings.h"
0021: #include "D3DApp.h"
0022: #include "D3DFont.h"
0023: #include "D3DFile.h"
0024: #include "D3DUtil.h"
0025: #include "DIUtil.h"
0026: #include "DMUtil.h"
0027: #include "resource.h"
0028: #include "DirectX9Application1.h"
0029:
0030:
0031:
0032:
0033:
0034:
0035:
0036:
0037:
0038: GUID g_guidApp = { 0x4CBF02D2, 0xCE10, 0x4C41, { 0x80, 0x15, 0xEC, 0x63, 0x30, 0x30, 0x43, 0x1 } };
0039:
0040:
0041:
0042: enum INPUT_SEMANTICS
0043: {
0044:
0045:
0046: INPUT_ROTATE_AXIS_LR=1, INPUT_ROTATE_AXIS_UD,
0047: INPUT_ROTATE_LEFT, INPUT_ROTATE_RIGHT,
0048: INPUT_ROTATE_UP, INPUT_ROTATE_DOWN,
0049: INPUT_CONFIG_INPUT, INPUT_CONFIG_DISPLAY,
0050: INPUT_PLAY_SOUND,
0051: };
0052:
0053:
0054: DIACTION g_rgGameAction[] =
0055: {
0056:
0057:
0058:
0059:
0060:
0061:
0062:
0063: { INPUT_ROTATE_AXIS_LR, DIAXIS_3DCONTROL_LATERAL, 0, TEXT("Rotate left/right"), },
0064: { INPUT_ROTATE_AXIS_UD, DIAXIS_3DCONTROL_MOVE, 0, TEXT("Rotate up/down"), },
0065: { INPUT_PLAY_SOUND, DIBUTTON_3DCONTROL_SPECIAL, 0, TEXT("Play sound"), },
0066:
0067:
0068: { INPUT_ROTATE_LEFT, DIKEYBOARD_LEFT, 0, TEXT("Rotate left"), },
0069: { INPUT_ROTATE_RIGHT, DIKEYBOARD_RIGHT, 0, TEXT("Rotate right"), },
0070: { INPUT_ROTATE_UP, DIKEYBOARD_UP, 0, TEXT("Rotate up"), },
0071: { INPUT_ROTATE_DOWN, DIKEYBOARD_DOWN, 0, TEXT("Rotate down"), },
0072: { INPUT_PLAY_SOUND, DIKEYBOARD_F5, 0, TEXT("Play sound"), },
0073: { INPUT_CONFIG_DISPLAY, DIKEYBOARD_F2, DIA_APPFIXED, TEXT("Configure Display"), },
0074: { INPUT_CONFIG_INPUT, DIKEYBOARD_F3, DIA_APPFIXED, TEXT("Configure Input"), },
0075: };
0076:
0077: #define NUMBER_OF_GAMEACTIONS (sizeof(g_rgGameAction)/sizeof(DIACTION))
0078:
0079:
0080:
0081:
0082:
0083:
0084:
0085: CMyD3DApplication* g_pApp = NULL;
0086: HINSTANCE g_hInst = NULL;
0087:
0088:
0089:
0090:
0091:
0092:
0093:
0094:
0095:
0096: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0097: {
0098: CMyD3DApplication d3dApp;
0099:
0100: g_pApp = &d3dApp;
0101: g_hInst = hInst;
0102:
0103: InitCommonControls();
0104: if( FAILED( d3dApp.Create( hInst ) ) )
0105: return 0;
0106:
0107: return d3dApp.Run();
0108: }
0109:
0110:
0111:
0112:
0113:
0114:
0115:
0116:
0117:
0118:
0119:
0120:
0121: CMyD3DApplication::CMyD3DApplication()
0122: {
0123:
0124: m_pEffect = NULL;
0125: m_hmWVP = NULL;
0126:
0127:
0128: m_dwCreationWidth = 500;
0129: m_dwCreationHeight = 375;
0130: m_strWindowTitle = TEXT( "DirectX9Application1" );
0131: m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
0132: m_bStartFullscreen = false;
0133: m_bShowCursorWhenFullscreen = false;
0134:
0135:
0136: m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0137: m_bLoadingApp = TRUE;
0138: m_pD3DXMesh = NULL;
0139: m_pInputDeviceManager = NULL;
0140: m_pMusicManager = NULL;
0141: m_pBounceSound = NULL;
0142: m_pDIConfigSurface = NULL;
0143:
0144: ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0145: m_fWorldRotX = 0.0f;
0146: m_fWorldRotY = 0.0f;
0147:
0148:
0149: ReadSettings();
0150: }
0151:
0152:
0153:
0154:
0155:
0156:
0157:
0158:
0159: CMyD3DApplication::~CMyD3DApplication()
0160: {
0161: }
0162:
0163:
0164:
0165:
0166:
0167:
0168:
0169:
0170:
0171:
0172:
0173:
0174: HRESULT CMyD3DApplication::OneTimeSceneInit()
0175: {
0176:
0177:
0178:
0179: SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0180:
0181:
0182: InitInput( m_hWnd );
0183:
0184:
0185: InitAudio( m_hWnd );
0186:
0187: m_bLoadingApp = FALSE;
0188:
0189: return S_OK;
0190: }
0191:
0192:
0193:
0194:
0195:
0196:
0197:
0198:
0199: VOID CMyD3DApplication::ReadSettings()
0200: {
0201: HKEY hkey;
0202: if( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CURRENT_USER, DXAPP_KEY,
0203: 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ) )
0204: {
0205:
0206:
0207:
0208:
0209: DXUtil_ReadIntRegKey( hkey, TEXT("Width"), &m_dwCreationWidth, m_dwCreationWidth );
0210: DXUtil_ReadIntRegKey( hkey, TEXT("Height"), &m_dwCreationHeight, m_dwCreationHeight );
0211:
0212: RegCloseKey( hkey );
0213: }
0214: }
0215:
0216:
0217:
0218:
0219:
0220:
0221:
0222:
0223: VOID CMyD3DApplication::WriteSettings()
0224: {
0225: HKEY hkey;
0226:
0227: if( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CURRENT_USER, DXAPP_KEY,
0228: 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ) )
0229: {
0230:
0231:
0232:
0233:
0234: DXUtil_WriteIntRegKey( hkey, TEXT("Width"), m_rcWindowClient.right );
0235: DXUtil_WriteIntRegKey( hkey, TEXT("Height"), m_rcWindowClient.bottom );
0236:
0237: RegCloseKey( hkey );
0238: }
0239: }
0240:
0241:
0242:
0243:
0244:
0245:
0246:
0247:
0248:
0249: HRESULT CALLBACK CMyD3DApplication::StaticInputAddDeviceCB(
0250: CInputDeviceManager::DeviceInfo* pDeviceInfo,
0251: const DIDEVICEINSTANCE* pdidi,
0252: LPVOID pParam )
0253: {
0254: CMyD3DApplication* pApp = (CMyD3DApplication*) pParam;
0255: return pApp->InputAddDeviceCB( pDeviceInfo, pdidi );
0256: }
0257:
0258:
0259:
0260:
0261:
0262:
0263:
0264:
0265:
0266: HRESULT CMyD3DApplication::InputAddDeviceCB( CInputDeviceManager::DeviceInfo* pDeviceInfo,
0267: const DIDEVICEINSTANCE* pdidi )
0268: {
0269: UNREFERENCED_PARAMETER( pdidi );
0270:
0271:
0272: DIPROPDWORD dipdw;
0273: dipdw.diph.dwSize = sizeof(DIPROPDWORD);
0274: dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
0275: dipdw.diph.dwObj = 0;
0276: dipdw.diph.dwHow = DIPH_DEVICE;
0277: dipdw.dwData = 500;
0278: pDeviceInfo->pdidDevice->SetProperty( DIPROP_DEADZONE, &dipdw.diph );
0279:
0280:
0281:
0282: InputDeviceState* pNewInputDeviceState = new InputDeviceState;
0283: ZeroMemory( pNewInputDeviceState, sizeof(InputDeviceState) );
0284: pDeviceInfo->pParam = (LPVOID) pNewInputDeviceState;
0285:
0286: return S_OK;
0287: }
0288:
0289:
0290:
0291:
0292:
0293:
0294:
0295:
0296: HRESULT CMyD3DApplication::InitInput( HWND hWnd )
0297: {
0298: HRESULT hr;
0299:
0300:
0301: ZeroMemory( &m_diafGame, sizeof(DIACTIONFORMAT) );
0302: m_diafGame.dwSize = sizeof(DIACTIONFORMAT);
0303: m_diafGame.dwActionSize = sizeof(DIACTION);
0304: m_diafGame.dwDataSize = NUMBER_OF_GAMEACTIONS * sizeof(DWORD);
0305: m_diafGame.guidActionMap = g_guidApp;
0306:
0307:
0308: m_diafGame.dwGenre = DIVIRTUAL_CAD_3DCONTROL;
0309:
0310: m_diafGame.dwNumActions = NUMBER_OF_GAMEACTIONS;
0311: m_diafGame.rgoAction = g_rgGameAction;
0312: m_diafGame.lAxisMin = -100;
0313: m_diafGame.lAxisMax = 100;
0314: m_diafGame.dwBufferSize = 16;
0315: _tcscpy( m_diafGame.tszActionMap, _T("DirectX9Application1 Game") );
0316:
0317:
0318: m_pInputDeviceManager = new CInputDeviceManager();
0319:
0320: if( FAILED( hr = m_pInputDeviceManager->Create( hWnd, NULL, m_diafGame,
0321: StaticInputAddDeviceCB, this ) ) )
0322: return DXTRACE_ERR( "m_pInputDeviceManager->Create", hr );
0323:
0324: return S_OK;
0325: }
0326:
0327:
0328:
0329:
0330:
0331:
0332:
0333:
0334: HRESULT CMyD3DApplication::InitAudio( HWND hWnd )
0335: {
0336: HRESULT hr;
0337:
0338:
0339: m_pMusicManager = new CMusicManager();
0340: if( FAILED( hr = m_pMusicManager->Initialize( hWnd ) ) )
0341: return DXTRACE_ERR( "m_pMusicManager->Initialize", hr );
0342:
0343:
0344:
0345: TCHAR szPath[MAX_PATH];
0346: GetCurrentDirectory( MAX_PATH, szPath );
0347: m_pMusicManager->SetSearchDirectory( szPath );
0348:
0349:
0350: m_pMusicManager->CreateSegmentFromResource( &m_pBounceSound, _T("BOUNCE"), _T("WAVE") );
0351:
0352: return S_OK;
0353: }
0354:
0355:
0356:
0357:
0358:
0359:
0360:
0361:
0362:
0363: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior,
0364: D3DFORMAT Format )
0365: {
0366: UNREFERENCED_PARAMETER( Format );
0367: UNREFERENCED_PARAMETER( dwBehavior );
0368: UNREFERENCED_PARAMETER( pCaps );
0369:
0370: BOOL bCapsAcceptable;
0371:
0372:
0373: bCapsAcceptable = TRUE;
0374:
0375: if( bCapsAcceptable )
0376: return S_OK;
0377: else
0378: return E_FAIL;
0379: }
0380:
0381:
0382:
0383:
0384:
0385:
0386:
0387:
0388:
0389:
0390:
0391:
0392:
0393:
0394: HRESULT CMyD3DApplication::InitDeviceObjects()
0395: {
0396:
0397:
0398: HRESULT hr;
0399:
0400:
0401: m_pFont->InitDeviceObjects( m_pd3dDevice );
0402:
0403:
0404: if( FAILED( hr = D3DXCreateTeapot( m_pd3dDevice, &m_pD3DXMesh, NULL ) ) )
0405: return DXTRACE_ERR( "D3DXCreateTeapot", hr );
0406:
0407:
0408: if( FAILED( hr = D3DXCreateEffectFromFile( m_pd3dDevice, "hlsl.fx", NULL, NULL,
0409: 0, NULL, &m_pEffect, NULL ) ) )
0410: {
0411: return hr;
0412: }
0413: D3DXEFFECT_DESC m_EffectDesc;
0414: D3DXPARAMETER_DESC ParamDesc;
0415: D3DXHANDLE hParam;
0416: m_pEffect->GetDesc( &m_EffectDesc );
0417: for( UINT iParam = 0; iParam < m_EffectDesc.Parameters; iParam++ )
0418: {
0419: hParam = m_pEffect->GetParameter ( NULL, iParam );
0420: m_pEffect->GetParameterDesc( hParam, &ParamDesc );
0421: if( ParamDesc.Name != NULL &&
0422: ( ParamDesc.Class == D3DXPC_MATRIX_ROWS || ParamDesc.Class == D3DXPC_MATRIX_COLUMNS ) )
0423: {
0424: if( strcmpi( ParamDesc.Name, "mWVP" ) == 0 )
0425: m_hmWVP = hParam;
0426: }
0427: }
0428:
0429: return S_OK;
0430: }
0431:
0432:
0433:
0434:
0435:
0436:
0437:
0438:
0439:
0440:
0441:
0442:
0443:
0444: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0445: {
0446:
0447: HRESULT hr;
0448:
0449:
0450: D3DMATERIAL9 mtrl;
0451: D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
0452: m_pd3dDevice->SetMaterial( &mtrl );
0453:
0454:
0455: m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
0456: m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0457: m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0458: m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
0459: m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
0460: m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
0461: m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0462: m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0463:
0464:
0465: m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, FALSE );
0466: m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
0467: m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
0468:
0469: m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
0470:
0471:
0472: D3DXMATRIX matIdentity;
0473: D3DXMatrixIdentity( &matIdentity );
0474:
0475:
0476:
0477:
0478:
0479:
0480: D3DXMATRIX matView;
0481: D3DXVECTOR3 vFromPt = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
0482: D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0483: D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0484:
0485: D3DXMatrixLookAtLH( &m_matView, &vFromPt, &vLookatPt, &vUpVec );
0486:
0487:
0488:
0489: D3DXMATRIX matProj;
0490: FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0491: D3DXMatrixPerspectiveFovLH( &m_matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
0492:
0493:
0494:
0495: #if 0
0496:
0497: D3DLIGHT9 light;
0498: D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, -1.0f, -1.0f, 2.0f );
0499: m_pd3dDevice->SetLight( 0, &light );
0500: m_pd3dDevice->LightEnable( 0, TRUE );
0501: m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
0502: #endif
0503:
0504:
0505: m_pFont->RestoreDeviceObjects();
0506:
0507: if( !m_bWindowed )
0508: {
0509:
0510: if( FAILED( hr = m_pd3dDevice->CreateOffscreenPlainSurface( 640, 480,
0511: m_d3dsdBackBuffer.Format, D3DPOOL_DEFAULT,
0512: &m_pDIConfigSurface, NULL ) ) )
0513: return DXTRACE_ERR( "CreateOffscreenPlainSurface", hr );
0514: }
0515:
0516:
0517: if( m_pEffect != NULL ) m_pEffect->OnResetDevice();
0518:
0519: return S_OK;
0520: }
0521:
0522:
0523:
0524:
0525:
0526:
0527:
0528:
0529: BOOL CALLBACK CMyD3DApplication::StaticConfigureInputDevicesCB(
0530: IUnknown* pUnknown, VOID* pUserData )
0531: {
0532: CMyD3DApplication* pApp = (CMyD3DApplication*) pUserData;
0533: return pApp->ConfigureInputDevicesCB( pUnknown );
0534: }
0535:
0536:
0537:
0538:
0539:
0540:
0541:
0542:
0543:
0544:
0545: BOOL CMyD3DApplication::ConfigureInputDevicesCB( IUnknown* pUnknown )
0546: {
0547:
0548: LPDIRECT3DSURFACE9 pConfigSurface = NULL;
0549: if( FAILED( pUnknown->QueryInterface( IID_IDirect3DSurface9,
0550: (VOID**)&pConfigSurface ) ) )
0551: return TRUE;
0552:
0553:
0554: Render();
0555:
0556: RECT rcSrc;
0557: SetRect( &rcSrc, 0, 0, 640, 480 );
0558:
0559: POINT ptDst;
0560: ptDst.x = (m_d3dsdBackBuffer.Width-640)/2;
0561: ptDst.y = (m_d3dsdBackBuffer.Height-480)/2;
0562:
0563: LPDIRECT3DSURFACE9 pBackBuffer;
0564: m_pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
0565: m_pd3dDevice->UpdateSurface( pConfigSurface, &rcSrc, pBackBuffer, &ptDst );
0566: pBackBuffer->Release();
0567:
0568:
0569: m_pd3dDevice->Present( 0, 0, 0, 0 );
0570:
0571:
0572: pConfigSurface->Release();
0573:
0574: return TRUE;
0575: }
0576:
0577:
0578:
0579:
0580:
0581:
0582:
0583:
0584:
0585: HRESULT CMyD3DApplication::FrameMove()
0586: {
0587:
0588:
0589:
0590: UpdateInput( &m_UserInput );
0591:
0592:
0593: if( m_UserInput.bDoConfigureInput && m_bWindowed )
0594: {
0595:
0596: m_UserInput.bDoConfigureInput = FALSE;
0597:
0598: Pause( true );
0599:
0600:
0601:
0602: CInputDeviceManager::DeviceInfo* pDeviceInfos;
0603: DWORD dwNumDevices;
0604: m_pInputDeviceManager->GetDevices( &pDeviceInfos, &dwNumDevices );
0605:
0606: for( DWORD i=0; i<dwNumDevices; i++ )
0607: {
0608: InputDeviceState* pInputDeviceState = (InputDeviceState*) pDeviceInfos[i].pParam;
0609: SAFE_DELETE( pInputDeviceState );
0610: pDeviceInfos[i].pParam = NULL;
0611: }
0612:
0613:
0614: if( m_bWindowed )
0615: m_pInputDeviceManager->ConfigureDevices( m_hWnd, NULL, NULL, DICD_EDIT, NULL );
0616: else
0617: m_pInputDeviceManager->ConfigureDevices( m_hWnd,
0618: m_pDIConfigSurface,
0619: (VOID*)StaticConfigureInputDevicesCB,
0620: DICD_EDIT, (LPVOID) this );
0621:
0622: Pause( false );
0623: }
0624:
0625: if( m_UserInput.bDoConfigureDisplay )
0626: {
0627:
0628: m_UserInput.bDoConfigureDisplay = FALSE;
0629:
0630: Pause(true);
0631:
0632:
0633: UserSelectNewDevice();
0634:
0635: Pause(false);
0636: }
0637:
0638:
0639: D3DXMATRIX matWorld;
0640: D3DXMATRIX matRotY;
0641: D3DXMATRIX matRotX;
0642:
0643: if( m_UserInput.fAxisRotateLR )
0644: m_fWorldRotY += m_fElapsedTime * m_UserInput.fAxisRotateLR;
0645:
0646: if( m_UserInput.fAxisRotateUD )
0647: m_fWorldRotX += m_fElapsedTime * m_UserInput.fAxisRotateUD;
0648:
0649: D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0650: D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0651:
0652:
0653: D3DXMatrixMultiply( &m_matWorld, &matRotX, &matRotY );
0654:
0655:
0656:
0657: if( m_UserInput.bPlaySoundButtonDown )
0658: {
0659: m_fSoundPlayRepeatCountdown -= m_fElapsedTime;
0660: if( m_fSoundPlayRepeatCountdown <= 0.0f )
0661: {
0662: m_fSoundPlayRepeatCountdown = 0.5f;
0663: if( m_pBounceSound )
0664: m_pBounceSound->Play();
0665: }
0666: }
0667: else
0668: {
0669: m_fSoundPlayRepeatCountdown = 0.0f;
0670: }
0671:
0672: return S_OK;
0673: }
0674:
0675:
0676:
0677:
0678:
0679:
0680:
0681:
0682: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0683: {
0684: if( NULL == m_pInputDeviceManager )
0685: return;
0686:
0687:
0688: CInputDeviceManager::DeviceInfo* pDeviceInfos;
0689: DWORD dwNumDevices;
0690: m_pInputDeviceManager->GetDevices( &pDeviceInfos, &dwNumDevices );
0691:
0692:
0693: for( DWORD i=0; i<dwNumDevices; i++ )
0694: {
0695: DIDEVICEOBJECTDATA rgdod[10];
0696: DWORD dwItems = 10;
0697: HRESULT hr;
0698: LPDIRECTINPUTDEVICE8 pdidDevice = pDeviceInfos[i].pdidDevice;
0699: InputDeviceState* pInputDeviceState = (InputDeviceState*) pDeviceInfos[i].pParam;
0700:
0701: hr = pdidDevice->Acquire();
0702: hr = pdidDevice->Poll();
0703: hr = pdidDevice->GetDeviceData( sizeof(DIDEVICEOBJECTDATA),
0704: rgdod, &dwItems, 0 );
0705: if( FAILED(hr) )
0706: continue;
0707:
0708:
0709: for( DWORD j=0; j<dwItems; j++ )
0710: {
0711: BOOL bButtonState = (rgdod[j].dwData==0x80) ? TRUE : FALSE;
0712: FLOAT fButtonState = (rgdod[j].dwData==0x80) ? 1.0f : 0.0f;
0713: FLOAT fAxisState = (FLOAT)((int)rgdod[j].dwData)/100.0f;
0714: UNREFERENCED_PARAMETER( fButtonState );
0715:
0716: switch( rgdod[j].uAppData )
0717: {
0718:
0719:
0720:
0721: case INPUT_ROTATE_AXIS_LR:
0722: pInputDeviceState->fAxisRotateLR = -fAxisState;
0723: break;
0724: case INPUT_ROTATE_AXIS_UD:
0725: pInputDeviceState->fAxisRotateUD = -fAxisState;
0726: break;
0727:
0728:
0729:
0730:
0731: case INPUT_ROTATE_LEFT: pInputDeviceState->bButtonRotateLeft = bButtonState; break;
0732: case INPUT_ROTATE_RIGHT: pInputDeviceState->bButtonRotateRight = bButtonState; break;
0733: case INPUT_ROTATE_UP: pInputDeviceState->bButtonRotateUp = bButtonState; break;
0734: case INPUT_ROTATE_DOWN: pInputDeviceState->bButtonRotateDown = bButtonState; break;
0735: case INPUT_PLAY_SOUND: pInputDeviceState->bButtonPlaySoundButtonDown = bButtonState; break;
0736:
0737:
0738: case INPUT_CONFIG_INPUT: if( bButtonState ) pUserInput->bDoConfigureInput = TRUE; break;
0739: case INPUT_CONFIG_DISPLAY: if( bButtonState ) pUserInput->bDoConfigureDisplay = TRUE; break;
0740: }
0741: }
0742: }
0743:
0744:
0745:
0746:
0747: pUserInput->fAxisRotateLR = 0.0f;
0748: pUserInput->fAxisRotateUD = 0.0f;
0749: pUserInput->bPlaySoundButtonDown = FALSE;
0750:
0751:
0752: for( i=0; i<dwNumDevices; i++ )
0753: {
0754: InputDeviceState* pInputDeviceState = (InputDeviceState*) pDeviceInfos[i].pParam;
0755:
0756:
0757: if( fabs(pInputDeviceState->fAxisRotateLR) > fabs(pUserInput->fAxisRotateLR) )
0758: pUserInput->fAxisRotateLR = pInputDeviceState->fAxisRotateLR;
0759:
0760: if( fabs(pInputDeviceState->fAxisRotateUD) > fabs(pUserInput->fAxisRotateUD) )
0761: pUserInput->fAxisRotateUD = pInputDeviceState->fAxisRotateUD;
0762:
0763:
0764: if( pInputDeviceState->bButtonRotateLeft )
0765: pUserInput->fAxisRotateLR = 1.0f;
0766: else if( pInputDeviceState->bButtonRotateRight )
0767: pUserInput->fAxisRotateLR = -1.0f;
0768:
0769: if( pInputDeviceState->bButtonRotateUp )
0770: pUserInput->fAxisRotateUD = 1.0f;
0771: else if( pInputDeviceState->bButtonRotateDown )
0772: pUserInput->fAxisRotateUD = -1.0f;
0773:
0774: if( pInputDeviceState->bButtonPlaySoundButtonDown )
0775: pUserInput->bPlaySoundButtonDown = TRUE;
0776: }
0777: }
0778:
0779:
0780:
0781:
0782:
0783:
0784:
0785:
0786:
0787:
0788: HRESULT CMyD3DApplication::Render()
0789: {
0790:
0791: m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
0792: 0x000000ff, 1.0f, 0L );
0793:
0794:
0795: if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0796: {
0797: #if 1
0798: UINT nPasses;
0799: UINT iPass;
0800:
0801: if( m_pEffect != NULL )
0802: {
0803:
0804: D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0805: m_pEffect->SetTechnique( hTechnique );
0806:
0807:
0808: D3DXMATRIX m = m_matWorld*m_matView*m_matProj;
0809: if( m_hmWVP != NULL )
0810: m_pEffect->SetMatrix( m_hmWVP, &m );
0811:
0812:
0813: m_pEffect->Begin( &nPasses, 0 );
0814: for( iPass = 0; iPass < nPasses; iPass ++ )
0815: {
0816: m_pEffect->Pass( iPass );
0817:
0818: m_pD3DXMesh->DrawSubset( 0 );
0819: }
0820: m_pEffect->End();
0821: }
0822: #else
0823:
0824: m_pD3DXMesh->DrawSubset(0);
0825: #endif
0826:
0827:
0828: RenderText();
0829:
0830:
0831: m_pd3dDevice->EndScene();
0832: }
0833:
0834: return S_OK;
0835: }
0836:
0837:
0838:
0839:
0840:
0841:
0842:
0843:
0844: HRESULT CMyD3DApplication::RenderText()
0845: {
0846: D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0);
0847: TCHAR szMsg[MAX_PATH] = TEXT("");
0848:
0849:
0850: FLOAT fNextLine = 40.0f;
0851:
0852: lstrcpy( szMsg, m_strDeviceStats );
0853: fNextLine -= 20.0f;
0854: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0855:
0856: lstrcpy( szMsg, m_strFrameStats );
0857: fNextLine -= 20.0f;
0858: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0859:
0860:
0861: fNextLine = (FLOAT) m_d3dsdBackBuffer.Height;
0862: sprintf( szMsg, TEXT("Left/Right Axis: %0.2f Up/Down Axis: %0.2f "),
0863: m_UserInput.fAxisRotateLR, m_UserInput.fAxisRotateUD );
0864: fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0865: lstrcpy( szMsg, TEXT("Use arrow keys or joystick to rotate object") );
0866: fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0867: lstrcpy( szMsg, TEXT("Hold 'F5' down to play and repeat a sound") );
0868: fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0869: lstrcpy( szMsg, TEXT("Press 'F3' to configure input") );
0870: fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0871: lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
0872: fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0873: return S_OK;
0874: }
0875:
0876:
0877:
0878:
0879:
0880:
0881:
0882:
0883: VOID CMyD3DApplication::Pause( bool bPause )
0884: {
0885:
0886:
0887:
0888:
0889:
0890:
0891:
0892:
0893:
0894: CInputDeviceManager::DeviceInfo* pDeviceInfos;
0895: DWORD dwNumDevices;
0896: m_pInputDeviceManager->GetDevices( &pDeviceInfos, &dwNumDevices );
0897:
0898: for( DWORD i=0; i<dwNumDevices; i++ )
0899: {
0900: InputDeviceState* pInputDeviceState = (InputDeviceState*) pDeviceInfos[i].pParam;
0901: ZeroMemory( pInputDeviceState, sizeof(InputDeviceState) );
0902: }
0903:
0904: CD3DApplication::Pause( bPause );
0905: }
0906:
0907:
0908:
0909:
0910:
0911:
0912:
0913:
0914:
0915: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg, WPARAM wParam,
0916: LPARAM lParam )
0917: {
0918: switch( msg )
0919: {
0920: case WM_PAINT:
0921: {
0922: if( m_bLoadingApp )
0923: {
0924:
0925:
0926: HDC hDC = GetDC( hWnd );
0927: TCHAR strMsg[MAX_PATH];
0928: wsprintf( strMsg, TEXT("Loading... Please wait") );
0929: RECT rct;
0930: GetClientRect( hWnd, &rct );
0931: DrawText( hDC, strMsg, -1, &rct, DT_CENTER|DT_VCENTER|DT_SINGLELINE );
0932: ReleaseDC( hWnd, hDC );
0933: }
0934: break;
0935: }
0936:
0937: case WM_COMMAND:
0938: {
0939: switch( LOWORD(wParam) )
0940: {
0941: case IDM_CONFIGINPUT:
0942: m_UserInput.bDoConfigureInput = TRUE;
0943: break;
0944:
0945: case IDM_CHANGEDEVICE:
0946: m_UserInput.bDoConfigureDisplay = TRUE;
0947: return 0;
0948: }
0949: break;
0950: }
0951:
0952: }
0953:
0954: return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
0955: }
0956:
0957:
0958:
0959:
0960:
0961:
0962:
0963:
0964: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
0965: {
0966:
0967: m_pFont->InvalidateDeviceObjects();
0968: SAFE_RELEASE( m_pDIConfigSurface );
0969:
0970:
0971: if( m_pEffect != NULL ) m_pEffect->OnLostDevice();
0972:
0973: return S_OK;
0974: }
0975:
0976:
0977:
0978:
0979:
0980:
0981:
0982:
0983:
0984:
0985: HRESULT CMyD3DApplication::DeleteDeviceObjects()
0986: {
0987:
0988: SAFE_RELEASE( m_pEffect );
0989:
0990:
0991: m_pFont->DeleteDeviceObjects();
0992: SAFE_RELEASE( m_pD3DXMesh );
0993:
0994: return S_OK;
0995: }
0996:
0997:
0998:
0999:
1000:
1001:
1002:
1003:
1004:
1005:
1006: HRESULT CMyD3DApplication::FinalCleanup()
1007: {
1008:
1009:
1010: SAFE_DELETE( m_pFont );
1011:
1012:
1013: CleanupDirectInput();
1014:
1015:
1016: SAFE_DELETE( m_pBounceSound );
1017: SAFE_DELETE( m_pMusicManager );
1018:
1019: WriteSettings();
1020:
1021: return S_OK;
1022: }
1023:
1024:
1025:
1026:
1027:
1028:
1029:
1030:
1031: VOID CMyD3DApplication::CleanupDirectInput()
1032: {
1033: if( NULL == m_pInputDeviceManager )
1034: return;
1035:
1036:
1037:
1038: CInputDeviceManager::DeviceInfo* pDeviceInfos;
1039: DWORD dwNumDevices;
1040: m_pInputDeviceManager->GetDevices( &pDeviceInfos, &dwNumDevices );
1041:
1042: for( DWORD i=0; i<dwNumDevices; i++ )
1043: {
1044: InputDeviceState* pInputDeviceState = (InputDeviceState*) pDeviceInfos[i].pParam;
1045: SAFE_DELETE( pInputDeviceState );
1046: pDeviceInfos[i].pParam = NULL;
1047: }
1048:
1049:
1050: SAFE_DELETE( m_pInputDeviceManager );
1051:
1052: }
1053:
1054:
1055:
1056:
1057: