source: GTP/trunk/App/Demos/Illum/PathMap/PathMap.cpp @ 896

Revision 896, 11.1 KB checked in by szirmay, 19 years ago (diff)
Line 
1//--------------------------------------------------------------------------------------
2// File: PathMap.cpp
3//
4// Empty starting point for new Direct3D applications
5//
6// Copyright (c) Microsoft Corporation. All rights reserved.
7//--------------------------------------------------------------------------------------
8#include "dxstdafx.h"
9#include "resource.h"
10#include "PathMapEffect.h"
11#include "Parameters.h"
12
13PathMapEffect*  pathMapEffect = NULL;
14
15bool                                            gShowUI = true;
16CDXUTDialogResourceManager      gDialogResourceManager;         // manager for shared resources of dialogs
17CD3DSettingsDlg                         gSettingsDlg;                           // Device settings dialog
18CDXUTDialog                                     gHUD;                                           // Dialog for sample specific controls
19
20ID3DXFont*                                      gFont = NULL;                           // Font for drawing text
21ID3DXSprite*                            gTextSprite = NULL;                     // Sprite for batching draw text calls
22
23Parameters                                      gParameters;
24
25
26//--------------------------------------------------------------------------------------
27// Rejects any devices that aren't acceptable by returning false
28//--------------------------------------------------------------------------------------
29bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat,
30                                  D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
31{
32    // Typically want to skip backbuffer formats that don't support alpha blending
33    IDirect3D9* pD3D = DXUTGetD3DObject();
34    if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
35                    AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
36                    D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
37        return false;
38
39    return true;
40}
41
42
43//--------------------------------------------------------------------------------------
44// Before a device is created, modify the device settings as needed
45//--------------------------------------------------------------------------------------
46bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
47{
48        pDeviceSettings->pp.Flags &= ~D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;
49        // VSync off
50        pDeviceSettings->pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
51    return true;
52}
53
54
55//--------------------------------------------------------------------------------------
56// Create any D3DPOOL_MANAGED resources here
57//--------------------------------------------------------------------------------------
58HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
59{
60        gDialogResourceManager.OnCreateDevice( pd3dDevice );
61        gSettingsDlg.OnCreateDevice( pd3dDevice );
62
63    // Initialize the font
64        D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
65                         OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
66                         L"Arial", &gFont );
67
68    return S_OK;
69}
70
71
72//--------------------------------------------------------------------------------------
73// Create any D3DPOOL_DEFAULT resources here
74//--------------------------------------------------------------------------------------
75HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice,
76                                const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
77{
78        gDialogResourceManager.OnResetDevice();
79        gSettingsDlg.OnResetDevice();
80
81    if( gFont )
82                gFont->OnResetDevice();
83 
84    // Create a sprite to help batch calls when drawing many lines of text
85        D3DXCreateSprite( pd3dDevice, &gTextSprite );
86
87    // Position the on-screen dialog
88    gHUD.SetLocation( 0, 0 );
89    gHUD.SetSize( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
90
91        pathMapEffect = new PathMapEffect(pd3dDevice);
92    return S_OK;
93}
94
95
96//--------------------------------------------------------------------------------------
97// Handle updates to the scene
98//--------------------------------------------------------------------------------------
99void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
100{
101        if(pathMapEffect)
102                pathMapEffect->move(fElapsedTime);
103}
104
105
106//--------------------------------------------------------------------------------------
107// Render the help and statistics text.
108//--------------------------------------------------------------------------------------
109void RenderText()
110{
111        const D3DSURFACE_DESC* backBufferDesc = DXUTGetBackBufferSurfaceDesc();
112
113    CDXUTTextHelper txtHelper( gFont, gTextSprite, 15 );
114    txtHelper.Begin();
115
116        txtHelper.SetInsertionPos( 5, 5 );
117        txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
118
119        if(gShowUI)
120        {
121                txtHelper.DrawFormattedTextLine( L"%.2f fps @ %i x %i",
122                DXUTGetFPS(), backBufferDesc->Width, backBufferDesc->Height );
123                txtHelper.DrawTextLine( DXUTGetFrameStats() );
124                txtHelper.DrawTextLine( DXUTGetDeviceStats() );
125               
126                txtHelper.SetInsertionPos( 5, 180 );
127                txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
128       
129                if(pathMapEffect)
130                {
131                        txtHelper.DrawFormattedTextLine( pathMapEffect->getCurrentMethodName());
132//                      txtHelper.DrawFormattedTextLine( pathMapEffect->getWeightsString());
133                }
134
135        }
136        txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
137
138        if ( gParameters.Get( bShowHelp ) )
139        {
140                txtHelper.SetInsertionPos( backBufferDesc->Width - 260, backBufferDesc->Height-24*8 );
141
142                txtHelper.DrawTextLine(
143                                L"Controls (F1 to hide):\n\n"
144                                L"___________________________________\n"
145                                L"                     GENERAL CONTROLS\n"
146                                L"Left click drag: Turn camera\n"
147                                L"Space: Show/Hide UI Elements\n"
148                                L"___________________________________\n"
149                                L"___________________________________\n"
150                                L"                             Quit: ESC");
151        }
152        else
153        {
154        //      txtHelper.DrawTextLine( L"Press F1 for help" );
155        }
156
157        txtHelper.End();
158}
159
160//--------------------------------------------------------------------------------------
161// Render the scene
162//--------------------------------------------------------------------------------------
163void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
164{
165    HRESULT hr;
166
167        pathMapEffect->render();
168        pd3dDevice->BeginScene();
169        RenderText();
170        if(gShowUI)
171                gHUD.OnRender( fElapsedTime );
172        pd3dDevice->EndScene();}
173
174
175//--------------------------------------------------------------------------------------
176// Handle messages to the application
177//--------------------------------------------------------------------------------------
178LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
179                          bool* pbNoFurtherProcessing, void* pUserContext )
180{
181    *pbNoFurtherProcessing = gDialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam );
182    if( *pbNoFurtherProcessing )
183        return 0;
184
185    if( gSettingsDlg.IsActive() )
186    {
187        gSettingsDlg.MsgProc( hWnd, uMsg, wParam, lParam );
188        return 0;
189    }
190
191        // Give the dialogs a chance to handle the message first
192    *pbNoFurtherProcessing = gHUD.MsgProc( hWnd, uMsg, wParam, lParam );
193    if( *pbNoFurtherProcessing )
194        return 0;
195
196        if(pathMapEffect)
197                pathMapEffect->handleMessage(hWnd, uMsg, wParam, lParam);
198
199
200    return 0;
201}
202
203
204void    CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext )
205{
206        if( bKeyDown )
207    {
208                switch(nChar)
209                {
210                        case VK_SPACE:
211                                gShowUI=!gShowUI;
212                                break;
213                        case VK_TAB:
214                                if(pathMapEffect)
215                                        pathMapEffect->nextMethod();
216                                break;
217                        case '0':
218                                if(pathMapEffect)
219                                        pathMapEffect->prevMethod();
220                                break;
221                        case VK_DOWN:
222                                break;
223                        case VK_UP:
224                                break;
225                        case VK_LEFT:
226                                break;
227                        case VK_RIGHT:
228                                break;
229                        case VK_DELETE:
230                                break;
231                        case VK_END:
232                                break;                         
233                        default:
234                                break;
235                }
236        }
237}
238
239void    CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
240{
241        gParameters.UpdateFromHUD( nControlID );
242        if(nControlID == IDC_GEN_BUTTON)
243        {
244                LPDIRECT3DDEVICE9 dev = pathMapEffect->getDevice();
245                delete pathMapEffect;
246                pathMapEffect = new PathMapEffect(dev);
247        }
248}
249
250void OnLoad()
251{
252}
253
254void OnSave()
255{
256}
257
258void OnReset()
259{
260}
261
262//--------------------------------------------------------------------------------------
263// Release resources created in the OnResetDevice callback here
264//--------------------------------------------------------------------------------------
265void CALLBACK OnLostDevice( void* pUserContext )
266{
267        gDialogResourceManager.OnLostDevice();
268    gSettingsDlg.OnLostDevice();
269
270    if( gFont )
271                gFont->OnLostDevice();
272   
273        gTextSprite->Release();
274
275        delete pathMapEffect;
276        pathMapEffect = NULL;
277}
278
279
280//--------------------------------------------------------------------------------------
281// Release resources created in the OnCreateDevice callback here
282//--------------------------------------------------------------------------------------
283void CALLBACK OnDestroyDevice( void* pUserContext )
284{
285        gDialogResourceManager.OnDestroyDevice();
286    gSettingsDlg.OnDestroyDevice();
287
288        gFont->Release();
289
290}
291
292
293
294//--------------------------------------------------------------------------------------
295// Initialize everything and go into a render loop
296//--------------------------------------------------------------------------------------
297INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
298{
299    // Enable run-time memory check for debug builds.
300#if defined(DEBUG) | defined(_DEBUG)
301    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
302#endif
303
304    // Set the callback functions
305    DXUTSetCallbackDeviceCreated( OnCreateDevice );
306    DXUTSetCallbackDeviceReset( OnResetDevice );
307    DXUTSetCallbackDeviceLost( OnLostDevice );
308    DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
309    DXUTSetCallbackMsgProc( MsgProc );
310    DXUTSetCallbackFrameRender( OnFrameRender );
311    DXUTSetCallbackFrameMove( OnFrameMove );
312        DXUTSetCallbackKeyboard( KeyboardProc );
313   
314    // TODO: Perform any application-level initialization here
315
316        gSettingsDlg.Init( &gDialogResourceManager );
317        gHUD.Init( &gDialogResourceManager );
318        gHUD.SetCallback( OnGUIEvent ); // Event handling
319
320        gParameters.Setup( &gHUD, OnReset, OnSave, OnLoad);     // you can add callbacks to these actions
321       
322        gParameters.Add( bShowHelp, "Show Help", VK_F1 );
323
324        PathMapEffect::addUiParameters(&gParameters);
325       
326        gParameters.UpdateFromHUD( IDC_RESET_BUTTON );  // do a reset
327
328        PathMapEffect::setUiParameterDefaults(&gParameters);
329
330    // Initialize DXUT and create the desired Win32 window and Direct3D device for the application
331    DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
332    DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
333    DXUTCreateWindow( L"PathMap" );
334    DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 640, 480, IsDeviceAcceptable, ModifyDeviceSettings );
335
336    // Start the render loop
337    DXUTMainLoop();
338
339    // TODO: Perform any application-level cleanup here
340
341    return DXUTGetExitCode();
342}
343
344
Note: See TracBrowser for help on using the repository browser.