Project Generation
- Press File
- Press New
- Select "Project Workspace"
- Select MFC Application
- Call the project "Direct3D"
- Step 1 select "Multiple Document"
- Press Next
- Step 2 Press Next
- Step 3 Press Next
- Step 4 Press Next
- Step 5 Press Next
- Step 6 Press Finish and Ok
Implementing the DirectSound,Direct3D,DirectInput classes
Step 1: Add the following header and pragma defines to direct3dview.cpp#include <dinput.h>
#include <basetsd.h>
#pragma comment ( lib, "dxguid.lib" ) /* guiid definitions */
//#if DIRECTINPUT_VERSION == 0x0800
//#pragma comment ( lib, "dinput8.lib" )
//#else
#pragma comment ( lib, "dinput.lib" )
#pragma comment ( lib, "dsound.lib" )
#pragma comment ( lib, "winmm.lib" )
#pragma comment ( lib, "strmbase.lib")
//Direct3D libraries.
#pragma comment ( lib, "d3d8.lib")
#pragma comment ( lib, "d3dx8.lib")
#pragma comment ( lib, "dxerr8.lib" )
Step 2: Create a Method called ProcessGeometry. Add void ProcessGeometry
to the direct3dview.h file. Insert source into direct3dview.cppvoid CDirect3dView::ProcessGeometry()
{
mD3DEngine.Render();
}
Step 3: Use the Class Wizard to build a message map method for OnInitialUpdate
for the following reasons
- To Instantiate the DirectInput class. The DirectInput class captures all keyboard, mouse, and joystick events.
- To create the DirectGraphics Devices. The DirectGraphic Devices allow the application to manage system and video card resources through apis.
- To Create the DirectMusic class. The DirectMusic class initializes and loads music segments to be played.
- To Create a Timer event running at maximum speed.
- Insert the following source into direct3dview.cpp
void CDirect3dView::OnInitialUpdate()
{
CView::OnInitialUpdate(); #ifdef _WIN64
HINSTANCE hInst = (HINSTANCE) GetWindowLongPtr( AfxGetMainWnd()->m_hWnd,
GWLP_HINSTANCE );
#else
HINSTANCE hInst = (HINSTANCE) GetWindowLong( AfxGetMainWnd()->m_hWnd,
GWL_HINSTANCE );
#endif
mDIInput.Create(hInst,AfxGetMainWnd()->m_hWnd);
mD3DEngine.InitializeEngine(AfxGetMainWnd()->m_hWnd);
mD3DEngine.EnvironmentSetup();
mDXMusic.Create(AfxGetMainWnd()->m_hWnd);
SetTimer( 0, 0, NULL );
}
Add the following source into direct3dview.h
- The include directivs for the directinput, direct3d, and directmusic
- Class variables for directinput, direct3d, and directmusic
#include "cdxinput.h"
#include "dsnEngine.h"
#include "music.h"
Public
CDXInput mDIInput;
//3D Engine
D3DEngine mD3DEngine;
//Music Manager
DXMusic mDXMusic;
Step 4 Add Source code to the WM_TIMER method OnTimer
- Arrow key events control camera movement
- Keys 1 and 2 play music segments
void CDirect3dView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
long MouseX, MouseY;
HRESULT hr;
mDIInput.Update();
mDIInput.GetMouseDeltas(&MouseX, &MouseY);
if(mDIInput.GetKeyState(CDXKEY_UPARROW))
{
mD3DEngine.AdvanceCamera();
}
if(mDIInput.GetKeyState(CDXKEY_DOWNARROW))
{
mD3DEngine.RetreatCamera();
}
if(mDIInput.GetKeyState(CDXKEY_LEFTARROW))
{
mD3DEngine.CameraLeft();
}
if(mDIInput.GetKeyState(CDXKEY_RIGHTARROW))
{
mD3DEngine.CameraRight();
}
if(mDIInput.GetKeyState(CDXKEY_LEFTCTRL) || mDIInput.GetKeyState(CDXKEY_RIGHTCTRL))
{
}
if(mDIInput.GetKeyState(CDXKEY_1))
{
hr = mDXMusic.g_pMusicSegments[0]->Play( DMUS_SEGF_DEFAULT,
mDXMusic.g_p3DAudiopath );
}
if (mDIInput.GetKeyState(CDXKEY_2))
{
hr = mDXMusic.g_pMusicSegments[1]->Play( DMUS_SEGF_DEFAULT
| DMUS_SEGF_SECONDARY,
mDXMusic.g_p3DAudiopath );
}
ProcessGeometry();
CView::OnTimer(nIDEvent);
}
Comments