source: GTP/trunk/App/Demos/Illum/Standalone/HierRayEngine/HierRayEngine.cpp @ 1481

Revision 1481, 10.9 KB checked in by szirmay, 18 years ago (diff)
Line 
1//--------------------------------------------------------------------------------------
2// File: HierRayEngine.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
11#include "HREEffect.h"
12#include "Parameters.h"
13
14HREEffect*      hreEffect = NULL;
15
16bool                                            gShowUI = true;
17CDXUTDialogResourceManager      gDialogResourceManager;         // manager for shared resources of dialogs
18CD3DSettingsDlg                         gSettingsDlg;                           // Device settings dialog
19CDXUTDialog                                     gHUD;                                           // Dialog for sample specific controls
20
21ID3DXFont*                                      gFont = NULL;                           // Font for drawing text
22ID3DXSprite*                            gTextSprite = NULL;                     // Sprite for batching draw text calls
23
24Parameters                                      gParameters;
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        hreEffect = new HREEffect(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(hreEffect)
102                hreEffect->move(fElapsedTime);
103}
104
105//--------------------------------------------------------------------------------------
106// Render the help and statistics text.
107//--------------------------------------------------------------------------------------
108void RenderText()
109{
110        const D3DSURFACE_DESC* backBufferDesc = DXUTGetBackBufferSurfaceDesc();
111
112    CDXUTTextHelper txtHelper( gFont, gTextSprite, 15 );
113    txtHelper.Begin();
114
115        txtHelper.SetInsertionPos( 5, 5 );
116        txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
117
118        if(gShowUI)
119        {
120                txtHelper.DrawFormattedTextLine( L"%.2f fps @ %i x %i",
121                DXUTGetFPS(), backBufferDesc->Width, backBufferDesc->Height );
122                txtHelper.DrawTextLine( DXUTGetFrameStats() );
123                txtHelper.DrawTextLine( DXUTGetDeviceStats() );
124               
125                txtHelper.SetInsertionPos( 5, 180 );
126                txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
127       
128                if(hreEffect)
129                {
130                        txtHelper.DrawFormattedTextLine( hreEffect->getCurrentMethodName());
131                }
132
133        }
134        txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
135
136        if ( gParameters.Get( bShowHelp ) )
137        {
138                txtHelper.SetInsertionPos( backBufferDesc->Width - 260, backBufferDesc->Height-24*8 );
139
140                txtHelper.DrawTextLine(
141                                L"Controls (F1 to hide):\n\n"
142                                L"___________________________________\n"
143                                L"                     GENERAL CONTROLS\n"
144                                L"Left click drag: Turn camera\n"
145                                L"Space: Show/Hide UI Elements\n"
146                                L"___________________________________\n"
147                                L"___________________________________\n"
148                                L"                             Quit: ESC");
149        }
150        else
151        {
152        //      txtHelper.DrawTextLine( L"Press F1 for help" );
153        }
154
155        txtHelper.End();
156}
157
158//--------------------------------------------------------------------------------------
159// Render the scene
160//--------------------------------------------------------------------------------------
161void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
162{
163        hreEffect->render();
164        pd3dDevice->BeginScene();
165        RenderText();
166        if(gShowUI)
167                gHUD.OnRender( fElapsedTime );
168        pd3dDevice->EndScene();
169}
170
171
172//--------------------------------------------------------------------------------------
173// Handle messages to the application
174//--------------------------------------------------------------------------------------
175LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
176                          bool* pbNoFurtherProcessing, void* pUserContext )
177{
178    *pbNoFurtherProcessing = gDialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam );
179    if( *pbNoFurtherProcessing )
180        return 0;
181
182    if( gSettingsDlg.IsActive() )
183    {
184        gSettingsDlg.MsgProc( hWnd, uMsg, wParam, lParam );
185        return 0;
186    }
187
188        // Give the dialogs a chance to handle the message first
189    *pbNoFurtherProcessing = gHUD.MsgProc( hWnd, uMsg, wParam, lParam );
190    if( *pbNoFurtherProcessing )
191        return 0;
192
193        if(hreEffect)
194                hreEffect->handleMessage(hWnd, uMsg, wParam, lParam);
195
196    return 0;
197}
198
199void    CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext )
200{
201        if( bKeyDown )
202    {
203                switch(nChar)
204                {
205                        case VK_SPACE:
206                                gShowUI=!gShowUI;
207                                break;
208                        case VK_TAB:
209                                if(hreEffect)
210                                        hreEffect->nextMethod();
211                                break;
212                        case '0':
213                                if(hreEffect)
214                                        hreEffect->prevMethod();
215                                break;
216                        case VK_DOWN:
217                                break;
218                        case VK_UP:
219                                break;
220                        case VK_LEFT:
221                                break;
222                        case VK_RIGHT:
223                                break;
224                        case VK_DELETE:
225                                break;
226                        case VK_END:
227                                break;                         
228                        default:
229                                break;
230                }
231        }
232}
233
234void    CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
235{
236        gParameters.UpdateFromHUD( nControlID );
237        if(nControlID == IDC_GEN_BUTTON)
238        {
239                LPDIRECT3DDEVICE9 dev = hreEffect->getDevice();
240                delete hreEffect;
241                hreEffect = new HREEffect(dev);
242        }
243}
244
245void OnLoad()
246{
247}
248
249void OnSave()
250{
251}
252
253void OnReset()
254{
255}
256
257//--------------------------------------------------------------------------------------
258// Release resources created in the OnResetDevice callback here
259//--------------------------------------------------------------------------------------
260void CALLBACK OnLostDevice( void* pUserContext )
261{
262                gDialogResourceManager.OnLostDevice();
263    gSettingsDlg.OnLostDevice();
264
265    if( gFont )
266                gFont->OnLostDevice();
267   
268        gTextSprite->Release();
269
270        delete hreEffect;
271        hreEffect = NULL;
272}
273
274
275//--------------------------------------------------------------------------------------
276// Release resources created in the OnCreateDevice callback here
277//--------------------------------------------------------------------------------------
278void CALLBACK OnDestroyDevice( void* pUserContext )
279{
280        gDialogResourceManager.OnDestroyDevice();
281    gSettingsDlg.OnDestroyDevice();
282
283        gFont->Release();
284}
285
286//--------------------------------------------------------------------------------------
287// Initialize everything and go into a render loop
288//--------------------------------------------------------------------------------------
289INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
290{
291    // Enable run-time memory check for debug builds.
292#if defined(DEBUG) | defined(_DEBUG)
293    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
294#endif
295
296    // Set the callback functions
297    DXUTSetCallbackDeviceCreated( OnCreateDevice );
298    DXUTSetCallbackDeviceReset( OnResetDevice );
299    DXUTSetCallbackDeviceLost( OnLostDevice );
300    DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
301    DXUTSetCallbackMsgProc( MsgProc );
302    DXUTSetCallbackFrameRender( OnFrameRender );
303    DXUTSetCallbackFrameMove( OnFrameMove );
304        DXUTSetCallbackKeyboard( KeyboardProc );
305   
306    // TODO: Perform any application-level initialization here
307        gSettingsDlg.Init( &gDialogResourceManager );
308        gHUD.Init( &gDialogResourceManager );
309        gHUD.SetCallback( OnGUIEvent ); // Event handling
310
311        gParameters.Setup( &gHUD, OnReset, OnSave, OnLoad);     // you can add callbacks to these actions
312       
313        gParameters.Add( bShowHelp, "Show Help", VK_F1 );
314
315        HREEffect::addUiParameters(&gParameters);
316       
317        gParameters.UpdateFromHUD( IDC_RESET_BUTTON );  // do a reset
318
319
320    // Initialize DXUT and create the desired Win32 window and Direct3D device for the application
321    DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
322    DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
323    DXUTCreateWindow( L"Hierarchical Ray Engine" );
324    DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 512, 512, IsDeviceAcceptable, ModifyDeviceSettings );
325
326    // Start the render loop
327    DXUTMainLoop();
328
329    // TODO: Perform any application-level cleanup here
330
331    return DXUTGetExitCode();
332}
333
334
Note: See TracBrowser for help on using the repository browser.