1 //--------------------------------------------------------------------------------------
2 // File: EmptyProject.cpp
3 //
4 // Empty starting point for new Direct3D applications
5 //
6 // Copyright (c) Microsoft Corporation. All rights reserved.
7 //--------------------------------------------------------------------------------------
8 #include ".\dxut\dxstdafx.h"
9 #include "resource.h"
10 #include "d3dapp.h"
11 
12 ID3DXFont*              g_pFont = NULL;         // Font for drawing text
13 bool					g_bLeftMouseDown = false;
14 CFirstPersonCamera		g_Camera;               // Camera
15 ID3DXSprite*			g_pTextSprite = NULL;   // Sprite for batching draw text calls
16 
17 
18 HRESULT CALLBACK	OnCreateDevice		( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
19 void CALLBACK		OnFrameMove			( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext );
20 void CALLBACK		OnFrameRender		( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext );
21 bool CALLBACK		IsDeviceAcceptable	( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat,
22 											D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext );
23 bool CALLBACK		ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext );
24 HRESULT CALLBACK	OnResetDevice		( IDirect3DDevice9* pd3dDevice,
25 											const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
26 LRESULT CALLBACK	MsgProc				( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
27 											bool* pbNoFurtherProcessing, void* pUserContext );
28 void CALLBACK		OnLostDevice		( void* pUserContext );
29 void CALLBACK		OnDestroyDevice		( void* pUserContext );
30 void CALLBACK		KeyboardProc		( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext );
31 
OnCreateDevice(IDirect3DDevice9 * pd3dDevice,const D3DSURFACE_DESC * pBackBufferSurfaceDesc,void * pUserContext)32 HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
33 {
34 	HRESULT hr;
35 
36     // Initialize the font
37     V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
38                          OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
39                          L"Arial", &g_pFont ) );
40 
41 
42 
43     return OnMyAppCreateDevice( pd3dDevice, pBackBufferSurfaceDesc, pUserContext );
44 }
45 
OnFrameMove(IDirect3DDevice9 * pd3dDevice,double fTime,float fElapsedTime,void * pUserContext)46 void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
47 {
48 	// Update the camera's position based on user input
49     g_Camera.FrameMove( fElapsedTime );
50 
51 	OnMyAppFrameMove( pd3dDevice, fTime, fElapsedTime, pUserContext );
52 }
53 
OnFrameRender(IDirect3DDevice9 * pd3dDevice,double fTime,float fElapsedTime,void * pUserContext)54 void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
55 {
56     HRESULT hr;
57 
58 	// Clear the render target and the zbuffer
59     V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 45, 50, 170), 1.0f, 0) );
60 
61     // Render the scene
62     if( SUCCEEDED( pd3dDevice->BeginScene() ) )
63     {
64 		OnMyAppFrameRender( pd3dDevice, fTime, fElapsedTime, pUserContext );
65 
66         V( pd3dDevice->EndScene() );
67     }
68 }
69 
70 //--------------------------------------------------------------------------------------
71 // Rejects any devices that aren't acceptable by returning false
72 //--------------------------------------------------------------------------------------
IsDeviceAcceptable(D3DCAPS9 * pCaps,D3DFORMAT AdapterFormat,D3DFORMAT BackBufferFormat,bool bWindowed,void * pUserContext)73 bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat,
74                                   D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
75 {
76     // Typically want to skip backbuffer formats that don't support alpha blending
77     IDirect3D9* pD3D = DXUTGetD3DObject();
78     if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
79                     AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
80                     D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
81         return false;
82 
83     return true;
84 }
85 
86 
87 //--------------------------------------------------------------------------------------
88 // Before a device is created, modify the device settings as needed
89 //--------------------------------------------------------------------------------------
ModifyDeviceSettings(DXUTDeviceSettings * pDeviceSettings,const D3DCAPS9 * pCaps,void * pUserContext)90 bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
91 {
92     return true;
93 }
94 
95 
96 //--------------------------------------------------------------------------------------
97 // Create any D3DPOOL_DEFAULT resources here
98 //--------------------------------------------------------------------------------------
OnResetDevice(IDirect3DDevice9 * pd3dDevice,const D3DSURFACE_DESC * pBackBufferSurfaceDesc,void * pUserContext)99 HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice,
100                                 const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
101 {
102 	HRESULT hr;
103 
104     if( g_pFont )
105         V_RETURN( g_pFont->OnResetDevice() );
106 //    if( g_pEffect )
107 //        V_RETURN( g_pEffect->OnResetDevice() );
108 
109 //    g_Cell.RestoreDeviceObjects( pd3dDevice );
110 
111 	V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pTextSprite ) );
112 
113     return OnMyAppResetDevice( pd3dDevice,
114                           pBackBufferSurfaceDesc, pUserContext );
115 }
116 
117 
118 
119 
120 
121 //--------------------------------------------------------------------------------------
122 // Handle messages to the application
123 //--------------------------------------------------------------------------------------
MsgProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,bool * pbNoFurtherProcessing,void * pUserContext)124 LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
125                           bool* pbNoFurtherProcessing, void* pUserContext )
126 {
127     // Pass all remaining windows messages to camera so it can respond to user input
128     g_Camera.HandleMessages( hWnd, uMsg, wParam, lParam );
129 
130     switch( uMsg )
131     {
132         case WM_LBUTTONDOWN:
133         case WM_LBUTTONDBLCLK:
134             g_bLeftMouseDown = true;
135             break;
136         case WM_LBUTTONUP:
137             g_bLeftMouseDown = false;
138             break;
139         case WM_CAPTURECHANGED:
140             if( (HWND)lParam != hWnd )
141                 g_bLeftMouseDown = false;
142             break;
143     }
144 
145     return MyAppMsgProc(hWnd, uMsg, wParam, lParam,
146                           pbNoFurtherProcessing, pUserContext);
147 }
148 
149 
150 //--------------------------------------------------------------------------------------
151 // Release resources created in the OnResetDevice callback here
152 //--------------------------------------------------------------------------------------
OnLostDevice(void * pUserContext)153 void CALLBACK OnLostDevice( void* pUserContext )
154 {
155     if( g_pFont )
156         g_pFont->OnLostDevice();
157 //    if( g_pEffect )
158 //        g_pEffect->OnLostDevice();
159     SAFE_RELEASE( g_pTextSprite );
160 //    g_Cell.InvalidateDeviceObjects();
161 
162 	OnMyAppLostDevice( pUserContext );
163 }
164 
165 
166 //--------------------------------------------------------------------------------------
167 // Release resources created in the OnCreateDevice callback here
168 //--------------------------------------------------------------------------------------
OnDestroyDevice(void * pUserContext)169 void CALLBACK OnDestroyDevice( void* pUserContext )
170 {
171     SAFE_RELEASE( g_pFont );
172 //    SAFE_RELEASE( g_pDefaultTex );
173 //    g_Cell.Destroy();
174 	OnMyAppDestroyDevice( pUserContext );
175 }
176 
KeyboardProc(UINT nChar,bool bKeyDown,bool bAltDown,void * pUserContext)177 void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext )
178 {
179     if( bKeyDown )
180     {
181 //        switch( nChar )
182 //      {
183 //            case VK_F1: g_bShowHelp = !g_bShowHelp; break;
184 //		default:
185 //			break;
186 //        }
187     }
188 	MyAppKeyboardProc( nChar, bKeyDown, bAltDown, pUserContext );
189 }
190 
191 //--------------------------------------------------------------------------------------
192 // Initialize everything and go into a render loop
193 //--------------------------------------------------------------------------------------
194 
WinMain(HINSTANCE,HINSTANCE,LPSTR,int)195 INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
196 {
197     // Enable run-time memory check for debug builds.
198 #if defined(DEBUG) | defined(_DEBUG)
199     _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
200 #endif
201 
202     // Set the callback functions
203     DXUTSetCallbackDeviceCreated( OnCreateDevice );
204     DXUTSetCallbackDeviceReset( OnResetDevice );
205     DXUTSetCallbackDeviceLost( OnLostDevice );
206     DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
207     DXUTSetCallbackMsgProc( MsgProc );
208     DXUTSetCallbackKeyboard( KeyboardProc );
209     DXUTSetCallbackFrameRender( OnFrameRender );
210     DXUTSetCallbackFrameMove( OnFrameMove );
211 
212     // Initialize world matrices
213 //    D3DXMatrixScaling( &g_mCellWorld, 1.0f, GROUND_Y / 3.0f, 1.0f );
214 
215     // Initialize DXUT and create the desired Win32 window and Direct3D device for the application
216     DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
217     DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
218     DXUTCreateWindow( L"d3dapp" );
219 	DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 640, 480, IsDeviceAcceptable, ModifyDeviceSettings );
220 
221 	MyAppInit();
222     // Start the render loop
223     DXUTMainLoop();
224 
225     // TODO: Perform any application-level cleanup here
226 
227     return DXUTGetExitCode();
228 }
229 
230