📄 testapp.cpp
字号:
#include "stdcdx.h"
#include "cdxapp.h"
#include "testapp.h"
BOOL AppInitHook( void )
{
#ifdef _DEBUG
pApp->SetDebugOptions( "DEBUG.TXT",CDXApp::DBF_FILE|CDXApp::DBF_WINDOW );
pApp->EnableStats( TRUE );
#endif
/* Associate our menu */
pApp->SetHMenu( ::LoadMenu(pApp->GetHInstance(),MAKEINTRESOURCE(IDR_MENU)) );
/* Associate our accelerators */
pApp->SetAccelerator( ::LoadAccelerators(pApp->GetHInstance(),MAKEINTRESOURCE(IDR_ACCEL1)) );
/* Set the initial mode we want */
pApp->SetVideoMode( 640,480,16,FALSE );
return( TRUE );
}
BOOL AppCreateScene( void )
{
/* This routine should set up your visuals and add them to the scene. */
/* These would of course be meshes, lights, textures, etc. You should */
/* also define the positions and attach them to the Scene frame */
HRESULT err;
BOOL bOkay=FALSE;
LPDIRECT3DRMFRAME2 lpWorld=NULL;
LPDIRECT3DRMFRAME2 lpLights=NULL;
LPDIRECT3DRMMESHBUILDER2 lpMesh=NULL;
LPDIRECT3DRMLIGHT lpLight1=NULL, lpLight2=NULL;
LPDIRECT3DRMWRAP lpWrap=NULL;
LPDIRECT3DRMTEXTURE2 lpTexture=NULL;
D3DRMBOX box;
D3DVECTOR vecAvg, vecSize;
char szTitle[128];
/* Create the world frame that will contain everything */
if ( (err=pApp->GetD3DRM2()->CreateFrame(pApp->GetScene(),&lpWorld))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","CreateFrame( World ) Failed",err );
goto ReleaseInterfaces;
}
/* Create the lights frame */
if ( (err=pApp->GetD3DRM2()->CreateFrame(pApp->GetScene(),&lpLights))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","CreateFrame( Lights ) Failed",err );
goto ReleaseInterfaces;
}
/* Set up the lights */
if ( (err=pApp->GetD3DRM2()->CreateLightRGB(D3DRMLIGHT_DIRECTIONAL,D3DVAL(0.9),D3DVAL(0.9),D3DVAL(0.9),&lpLight1))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","CreateLightRGB( Light1 ) Failed",err );
goto ReleaseInterfaces;
}
if ( (err=pApp->GetD3DRM2()->CreateLightRGB(D3DRMLIGHT_AMBIENT,D3DVAL(0.1),D3DVAL(0.1),D3DVAL(0.1),&lpLight2))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","CreateLightRGB( Light2 ) Failed",err );
goto ReleaseInterfaces;
}
/* Add the lights */
if ( (err=lpLights->AddLight(lpLight1))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","AddLight( light1 ) Failed",err );
goto ReleaseInterfaces;
}
if ( (err=lpLights->AddLight(lpLight2))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","AddLight( light2 ) Failed",err );
goto ReleaseInterfaces;
}
/* Set the position, orientation and rotation of the light, camera and world */
/* frames. */
if ( (err=lpLights->SetPosition(pApp->GetScene(),D3DVAL(2.0),D3DVAL(20.0),D3DVAL(22.0)))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","SetPosition( lights ) Failed",err );
goto ReleaseInterfaces;
}
if ( (err=pApp->GetCamera()->SetPosition(pApp->GetScene(),D3DVAL(0.0),D3DVAL(0.0),D3DVAL(0.0)))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","SetPosition( camera ) Failed",err );
goto ReleaseInterfaces;
}
if ( (err=pApp->GetCamera()->SetOrientation(pApp->GetScene(),D3DVAL(0.0),D3DVAL(0.0),D3DVAL(1.0),D3DVAL(0.0),D3DVAL(1.0),D3DVAL(0.0)))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","SetOrientation( camera ) Failed",err );
goto ReleaseInterfaces;
}
if ( (err=lpWorld->SetPosition(pApp->GetScene(),D3DVAL(0.0),D3DVAL(0.0),D3DVAL(15.0)))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","SetOrientation( world ) Failed",err );
goto ReleaseInterfaces;
}
if ( (err=lpWorld->SetOrientation(pApp->GetScene(),D3DVAL(0.0),D3DVAL(0.0),D3DVAL(1.0),D3DVAL(0.0),D3DVAL(1.0),D3DVAL(0.0)))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","SetOrientation( world ) Failed",err );
goto ReleaseInterfaces;
}
if ( (err=lpWorld->SetRotation(pApp->GetScene(),D3DVAL(0.1),D3DVAL(0.1),D3DVAL(0.1),D3DVAL(0.05)))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","SetRotation( world ) Failed",err );
goto ReleaseInterfaces;
}
/* Create the visual object (mesh) */
if ( (err=pApp->GetD3DRM2()->CreateMeshBuilder(&lpMesh))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","CreateMeshBuilder Failed",err );
goto ReleaseInterfaces;
}
if ( (err=lpMesh->Load("sphere3.x",NULL,D3DRMLOAD_FROMFILE,NULL,NULL))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","Load( mesh ) Failed",err );
goto ReleaseInterfaces;
}
/* Determine scale for object to occupy half of screen */
if ( (err=lpMesh->GetBox(&box))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","GetBox( mesh ) Failed",err );
goto ReleaseInterfaces;
}
D3DVALUE valueScale, valueMost;
/* Pick the largest dimension */
vecSize.x = box.max.x - box.min.x;
vecSize.y = box.max.y - box.min.y;
vecSize.z = box.max.z - box.min.z;
valueMost = vecSize.x;
if ( vecSize.y>valueMost ) valueMost=vecSize.y;
if ( vecSize.z>valueMost ) valueMost=vecSize.z;
if ( valueMost==D3DVAL(0.0) ) valueMost=D3DVAL(1.0);
/* Use 'hand' calculated factor */
valueScale = D3DVAL(pApp->GetScreenSize().cx)/(D3DVAL(50.0)*valueMost);
if ( (err=lpMesh->Scale(valueScale,valueScale,valueScale))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","Scale( mesh ) Failed",err );
goto ReleaseInterfaces;
}
/* Ensure object is centered about it's own axis */
if ( (err=lpMesh->GetBox(&box))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","GetBox( mesh ) Failed",err );
goto ReleaseInterfaces;
}
/* Determine centre point (average) of min and max vectors */
vecAvg.x = (box.min.x+box.max.x)/D3DVAL(2.0);
vecAvg.y = (box.min.y+box.max.y)/D3DVAL(2.0);
vecAvg.z = (box.min.z+box.max.z)/D3DVAL(2.0);
/* Translate mesh vertices relative to this midpoint */
if ( (err=lpMesh->Translate(-vecAvg.x,-vecAvg.y,-vecAvg.z))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","Transform( mesh ) Failed",err );
goto ReleaseInterfaces;
}
wsprintf( szTitle,"Mesh Demo. Faces=%d Points=%d",lpMesh->GetFaceCount(),lpMesh->GetVertexCount() );
pApp->SetWindowTitle( szTitle );
/* Set the object colour to avoid any unexpected texture blending results */
if ( (err=lpMesh->SetColorRGB(D3DVAL(1.0),D3DVAL(1.0),D3DVAL(1.0)))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","SetColorRGB( mesh ) Failed",err );
goto ReleaseInterfaces;
}
/* If you want to create wraps and textures, here's the place to do it */
D3DVALUE miny, maxy, height;
if ( (err=lpMesh->GetBox(&box))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","GetBox Failed",err );
goto ReleaseInterfaces;
}
maxy = box.max.y;
miny = box.min.y;
height = maxy - miny;
if ( (err=pApp->GetD3DRM2()->CreateWrap(D3DRMWRAP_CYLINDER,NULL,
D3DVAL(0.0),D3DVAL(0.0),D3DVAL(0.0),
D3DVAL(0.0),D3DVAL(0.0),D3DVAL(1.0),
D3DVAL(0.0),-D3DVAL(1.0),D3DVAL(0.0),
D3DVAL(0.0),D3DDivide(miny,height),D3DVAL(1.0),
D3DDivide(-D3DVAL(1.0),height),&lpWrap))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","CreateWrap Failed",err );
goto ReleaseInterfaces;
}
if ( (err=lpWrap->Apply((LPDIRECT3DRMOBJECT)lpMesh))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","Apply( Wrap ) Failed",err );
goto ReleaseInterfaces;
}
/* Create the texture */
if ( (err=pApp->GetD3DRM2()->LoadTexture("eurocom.bmp",&lpTexture))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","LoadTexture Failed",err );
goto ReleaseInterfaces;
}
/* If you need more than 16 shades call SetShades() here */
if ( (err=lpMesh->SetTexture(lpTexture))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","SetTexture Failed",err );
goto ReleaseInterfaces;
}
/* Add the object to the world frame */
if ( (err=lpWorld->AddVisual((LPDIRECT3DRMVISUAL)lpMesh))!=D3DRM_OK )
{
pApp->ErrHandler( "AppCreateScene()","AddVisual( mesh ) Failed",err );
goto ReleaseInterfaces;
}
/* All done. Flag as okay and release interfaces anyway */
bOkay = TRUE;
ReleaseInterfaces:
RELEASE( lpTexture );
RELEASE( lpWrap );
RELEASE( lpMesh );
RELEASE( lpLight1 );
RELEASE( lpLight2 );
RELEASE( lpLights );
RELEASE( lpWorld );
return( bOkay );
}
void AppIdleHook( void )
{
/* Do application idle processing here. The CDXApp message pump loop */
/* will call this function when there are no messages pending in the */
/* queue. */
}
LONG AppWindowProcHook( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam )
{
/* CDXApp Win32 interface will call us (not Win32 directly) */
switch( msg )
{
case WM_CHAR:
/* Allow ESCape to exit our app */
if ( wParam==VK_ESCAPE && pApp!=NULL ) pApp->SetQuit();
if ( wParam=='f' && pApp!=NULL )
{
/* Toggle between window and full screen mode */
SIZE size=pApp->GetScreenSize();
pApp->SetVideoMode( size.cx,size.cy,pApp->GetBitDepth(),!pApp->IsFullScreen() );
}
//*TBA*
if ( wParam=='p' && pApp!=NULL ) pApp->PauseApp( !pApp->IsPaused() );
if ( wParam=='m' && pApp!=NULL )
{
/* Message tester for full screen debugging */
pApp->ErrHandler( "Message!" );
}
if ( wParam=='d' && pApp!=NULL )
{
::Sleep( 2000 );
TRACE( "Sleeeep..." );
}
if ( wParam=='t' && pApp!=NULL ) pApp->ActivateMenu();
//*TBA*
break;
case WM_COMMAND:
switch( LOWORD(wParam) )
{
case IDM_EXIT:
if ( pApp!=NULL ) pApp->SetQuit();
return( 0L );
}
break;
}
return( ::DefWindowProc(hWnd,msg,wParam,lParam) );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -