📄 main.cpp
字号:
//-----------------------------------------------------------------------------
// Name: D3D_AnimationEditor
//
// Description: Example code showing how edit animations.
//
// File Function: Main body of the program (main.cpp)
//
// Code:
// Copyright (c) 2003 LostLogic Corporation. All rights reserved.
//
// Libraries Required:
// d3d9.lib dxguid.lib d3dx9dt.lib d3dxof.lib comctl32.lib winmm.lib dinput8.lib
//
// Local Files Required:
// main.cpp
// main.h
// Object3DClass.cpp
// Object3DClass.h
// ExceptionClass.cpp
// ExceptionClass.h
// C3DAnimation.cpp
// C3DAnimation.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;
RECT rcWindowClient;
DWORD dwInputTimer = 0;
int iResult = 0;
// 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 = "D3D_AnimationEditor"; // 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,
"D3D_AnimationEditor", // Class Name
"D3D_AnimationEditor", // Name Displayed on Title Bar
WS_OVERLAPPEDWINDOW,
0,
0,
g_iWindowWidth,
g_iWindowHeight,
NULL,
NULL,
hInstance,
NULL );
// Store window handle globally for access later
g_hWnd = hWnd;
// Display the window
ShowWindow( hWnd, iCmdShow );
// Set the global instance of the program
g_hInstance = hInstance;
// Figure out the client work area
GetClientRect( hWnd, &rcWindowClient );
// Calculate the rendering offsets based on the client size
g_iXOffset = (g_iWindowWidth-(rcWindowClient.right-rcWindowClient.left));
g_iYOffset = (g_iWindowHeight-(rcWindowClient.bottom-rcWindowClient.top));
// Resize the window to be truely the resolution desired
SetWindowPos(
hWnd,
NULL,
0, // X-Pos
0, // Y-Pos
g_iWindowWidth + g_iXOffset, // Width
g_iWindowHeight + g_iYOffset, // Height
NULL);
// Clear out message structure
ZeroMemory( &msg, sizeof(msg) );
// 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 );
}
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) ) {
// Initialize the virtual buffer for the display quad
vInitInterfaceObjects();
// Initialize the toolbar
vCreateToolbar( hWnd, hInstance );
// Initialize animation information
vInitAnimation();
// Initialize lights
vCreateLights();
// Enter the message loop
while( msg.message!=WM_QUIT ) {
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else {
// Check if it is ok to process mouse clicks
if( timeGetTime() > dwInputTimer ) {
// Check for input
vCheckInput();
dwInputTimer = timeGetTime()+50;
}
// Render the scene
vRender();
}
}
}
// Clean up everything and exit the app
vCleanup();
UnregisterClass( "D3D_AnimationEditor", wndclass.hInstance );
return 0;
}
//--------------------------------------------------------------------------------------
//
// Windows message processor
//
//--------------------------------------------------------------------------------------
LRESULT WINAPI fnMessageProcessor( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
char szBuffer[ 32 ];
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
case WM_LBUTTONDOWN:
g_iLeftButtonDown = 1;
vCheckMouse();
break;
case WM_LBUTTONUP:
g_iLeftButtonDown = 0;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
// Check toolbar controls
case ID_BUTTON_SAVE:
//
// *FOR YOU TO DO* Add the ability
// to save the animation to a
// specified filename.
//
animTest.vSave( "RobotIdle" );
break;
case ID_BUTTON_LOAD:
//
// *FOR YOU TO DO* Add the ability
// to load any animation desired here.
//
animTest.vLoad( "RobotIdle" );
// Update the toolbar information
vUpdateToolbarStats();
vRender();
break;
case ID_BUTTON_NEWFRAME:
// Create a new frame for the animation
animTest.vNewFrame();
SetActiveWindow( g_hWnd );
// Update the toolbar information
vUpdateToolbarStats();
break;
case ID_BUTTON_LOADOBJ:
// Load a 3D object into the scene
vLoadObject();
break;
// Start or stop the animation
case ID_BUTTON_STARTSTOP:
// If it is active, stop it
if( g_iAnimActive ) {
g_iAnimActive = 0;
}
// It is not active, start it
else {
g_iAnimActive = 1;
}
SetActiveWindow( g_hWnd );
vUpdateToolbarStats();
break;
// Cycle to the next object
case ID_BUTTON_NEXTOBJ:
// Increment the object count
g_iCurObj++;
// If past the end, loop back to the beginning
if( g_iCurObj >= animTest.m_iNumObjects ) {
g_iCurObj = 0;
}
SetActiveWindow( g_hWnd );
vUpdateToolbarStats();
break;
// Cycle to the prev object
case ID_BUTTON_PREVOBJ:
// Decrement the object count
g_iCurObj--;
// Check if object count less than zero
if( g_iCurObj < 0 ) {
// Make sure there are objects
if( animTest.m_iNumObjects ) {
// Set the current object to the end
g_iCurObj = animTest.m_iNumObjects-1;
}
// No objects in scene, go to frame 0
else {
g_iCurObj = 0;
}
}
SetActiveWindow( g_hWnd );
vUpdateToolbarStats();
break;
// Cycle to next frame
case ID_BUTTON_NEXTFRAME:
animTest.iNextFrame();
SetActiveWindow( g_hWnd );
vUpdateToolbarStats();
break;
// Cycle to prev frame
case ID_BUTTON_PREVFRAME:
animTest.iPrevFrame();
SetActiveWindow( g_hWnd );
vUpdateToolbarStats();
break;
//
// EDIT BOXES
//
case ID_EDITBOX_X:
memset( szBuffer, 0x00, 32 );
GetWindowText( hEDITBOX_X, szBuffer, 32 );
if( animTest.m_iNumFrames > 0 ) {
animTest.m_keyFrames[ g_iCurObj ][ animTest.m_iCurFrame ]->m_vecTrans.x = (float)atof( szBuffer );
}
break;
case ID_EDITBOX_Y:
memset( szBuffer, 0x00, 32 );
GetWindowText( hEDITBOX_Y, szBuffer, 32 );
if( animTest.m_iNumFrames > 0 ) {
animTest.m_keyFrames[ g_iCurObj ][ animTest.m_iCurFrame ]->m_vecTrans.y = (float)atof( szBuffer );
}
break;
case ID_EDITBOX_Z:
memset( szBuffer, 0x00, 32 );
GetWindowText( hEDITBOX_Z, szBuffer, 32 );
if( animTest.m_iNumFrames > 0 ) {
animTest.m_keyFrames[ g_iCurObj ][ animTest.m_iCurFrame ]->m_vecTrans.z = (float)atof( szBuffer );
}
break;
case ID_EDITBOX_XROT:
memset( szBuffer, 0x00, 32 );
GetWindowText( hEDITBOX_XROT, szBuffer, 32 );
if( animTest.m_iNumFrames > 0 ) {
animTest.m_keyFrames[ g_iCurObj ][ animTest.m_iCurFrame ]->m_vecRot.x = (float)atof( szBuffer );
}
break;
case ID_EDITBOX_YROT:
memset( szBuffer, 0x00, 32 );
GetWindowText( hEDITBOX_YROT, szBuffer, 32 );
if( animTest.m_iNumFrames > 0 ) {
animTest.m_keyFrames[ g_iCurObj ][ animTest.m_iCurFrame ]->m_vecRot.y = (float)atof( szBuffer );
}
break;
case ID_EDITBOX_ZROT:
memset( szBuffer, 0x00, 32 );
GetWindowText( hEDITBOX_ZROT, szBuffer, 32 );
if( animTest.m_iNumFrames > 0 ) {
animTest.m_keyFrames[ g_iCurObj ][ animTest.m_iCurFrame ]->m_vecRot.z = (float)atof( szBuffer );
}
break;
default:
break;
}
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//--------------------------------------------------------------------------------------
//
// Function to initialize Direct 3D
//
//--------------------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
D3DPRESENT_PARAMETERS d3dpp;
D3DXMATRIX matproj, matview;
D3DDISPLAYMODE d3ddm;
// Create the D3D object
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// Get the current desktop display mode, so we can set up a back
// buffer of the same format
if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
return E_FAIL;
// Setup the back buffer and swap format
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.EnableAutoDepthStencil = true;
//
// ** CHANGE THIS IS YOUR DISPLAY FAILS TO INITIALIZE **
// Try D3DFMT_D16 or D3DFMT_D32
//
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
// Create the D3DDevice
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
//---------------------------------------------
// Setup 3D View and Rendering States
//---------------------------------------------
// Set up view area
g_vecCameraOrigin = D3DXVECTOR3( 0.0f, 8.0, -100.0 );
g_vecCameraTarget = D3DXVECTOR3( 0.0, 0.0, 0.0 );
vSetupView( g_vecCameraOrigin, g_vecCameraTarget );
// Setup Projection
D3DXMatrixPerspectiveFovLH( &matproj, D3DX_PI/4, 1.0f, 1.0f, 1000.0f );
// Tell the device to use the above matrix for projection
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matproj );
// Set render states
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
return S_OK;
}
//--------------------------------------------------------------------------------------
//
// Function to cleanup allocated objects
//
//--------------------------------------------------------------------------------------
void vCleanup( void )
{
// Free the vertex buffer
if( g_pVBInterface != NULL )
g_pVBInterface->Release();
// Free the 3D device
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
// Free Direct 3D
if( g_pD3D != NULL )
g_pD3D->Release();
}
//--------------------------------------------------------------------------------------
//
// Function to draw the 3D scene
//
//--------------------------------------------------------------------------------------
void vRender( void )
{
RECT rectText;
char szOutput[ 256 ];
bool bFrameChanged = 0;
int iCFrame;
// Clear the backbuffer to a blue color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_RGBA( 200, 250, 255, 255 ), 1.0f, 0 );
// Begin the scene
g_pd3dDevice->BeginScene();
// Set default material
g_pd3dDevice->SetMaterial( &g_mtrlDefault );
// Set fill state to solid
g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
// Render the ground object
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -