View ManagedDirectX: Starting Out with EmptyProject
Managed Direct X Example Deconstruction
We're starting with EmptyProject. See Creating an Application Template for an example.
The Device Event Handlers
There are four handlers related to DirectX devices creation and destruction. A device is a "rendering context", and when the screen mode changes, the resources associated with the rendering context (buffers, loaded DirectX resources, etc) need to be rebuilt.
OnCreateDevice (senderObj, EventArgs)
This event will be fired immediately after the Direct3D device has been created, which will happen during application initialization and windowed/full screen toggles. This is the best location to create Pool.Managed resources since these resources need to be reloaded whenever the device is destroyed. Resources created here should be released in the Disposing event.I am noting that it's reading the D3DX shader file and creating an "effect. It is also setting up the camera, which has its own set of view parameters
OnResetDevice (senderObj, EventArgs)
This event will be fired immediately after the Direct3D device has been reset, which will happen after a lost device scenario. This is the best location to create Pool.Default resources since these resources need to be reloaded whenever the device is lost. Resources created here should be released in the OnLostDevice event.I am noting that this is creating a new textSprite, setting up the camera parameters, and moving UI elements to their proper location. It's all property setting.
OnLostDevice (senderObj, EventArgs)
This event function will be called fired after the Direct3D device has entered a lost state and before Device.Reset() is called. Resources created in the OnResetDevice callback should be released here, which generally includes all Pool.Default resources. See the "Lost Devices" section of the documentation for information about lost devices.I am noting that the textSprite resource is being disposed of. However, the camera and hud are not, because they were not allocated in OnResetDevice.
OnDestroyDevice (senderObj, EventArgs)
This callback function will be called immediately after the Direct3D device has been destroyed, which generally happens as a result of application termination or windowed/full screen toggles. Resources created in the OnCreateDevice callback should be released here, which generally includes all Pool.Managed resources.I am noting that there isn't anything being released here, though an effect parameter was loaded in OnCreateDevice.
The Game Rendering and Event Handlers
Once the devices are set up in Main, it's FrameMove() and FrameRender() that become the main "looping" points. FrameMove() is called every frame to update the game logic and data structures. FrameRender() is called when it's time to actually draw everything.
OnFrameMove (device, appTime, elapsedTime)
This callback function will be called once at the beginning of every frame. This is the best location for your application to handle updates to the scene, but is not intended to contain actual rendering calls, which should instead be placed in the OnFrameRender callback.It updates the camera position data structure, but doesn't actually render. That's for OnFrameRender().
OnFrameRender (device, appTime, elapsedTime)
This callback function will be called at the end of every frame to perform all the rendering calls for the scene, and it will also be called if the window needs to be repainted. After this function has returned, the sample framework will call Device.Present to display the contents of the next buffer in the swap chainFirst it clears the device and zbuffer. Then it begins the scene. Effects are updated. It draws some text. And it shows the UI.
Handling Key and Mouse events would be acquired through the OnKeyEvent() handler. Other Windows events are captured through OnMsgProc().
OnKeyEvent (senderObj, SysWinForms.KeyEventArgs)
As a convenience, the sample framework inspects the incoming windows messages for keystroke messages and decodes the message parameters to pass relevant keyboard messages to the application. The framework does not remove the underlying keystroke messages, which are still passed to the application's MsgProc callback.In the sample framework it checks scan codes for the F1 key.
OnMsgProc (hWnd, WinMsg, wParam, lParam, DoneProcFlag)
Before handling window messages, the sample framework passes incoming windows messages to the application through this callback function. If the application sets noFurtherProcessing to true, the sample framework will not process the messageIt passes the event to the other forms (hud, sampleUI), and also to the camera object. Returns 0.
Classes to look at later...
- ModelViewerCamera camera
- dx3d Effect
- dxmut Element
- dxmut Dialog
- dx3d Font
- dx3d Sprite
Sample Framework Handlers
- OnApplicationIdle (object, System.EventArgs)
- OnCursorChanged (object, System.EventArgs)
- OnDeviceDisposing (object, System.EventArgs)
- OnDeviceLose (object, System.EventArgs)
- OnDeviceReset (object, System.EventArgs)
- OnFormActivated (object, System.EventArgs)
- OnFormClosed (object, System.EventArgs)
- OnFormDeactivated (object, System.EventArgs)
- OnKeyDown (object, System.EventArgs)
- OnMenuEnd (object, System.EventArgs)
- OnMenuStart (object, System.EventArgs)
- OnMouseMove (object, System.EventArgs)
- OnPaint (object, System.EventArgs)
- OnResize (object, System.EventArgs)
