• Advertisement

Archived

This topic is now archived and is closed to further replies.

Correct D3D start-up in Managed DX9 / C#

This topic is 5026 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone give me a high-level rundown of the proper initialization sequence for Direct3D in Managed DX9 under C#? The problem is, all the tutorials and examples I''ve seen tend to gloss over the initialization. It wouldn''t bother me so much except that they often do things in slightly different ways -- different sequences, differences in what code goes into which event handlers, and so on. This is even more confusing because they all seem to work. I''ve even shuffled things around experimentally and things don''t break that often. I''m still quite the newbie (obviously), but I finally have enough of the basics down-pat that I want to get started on a simple but real project -- just a little 3D screensaver for my own use. Given the nature of a screensaver (running unattended for long periods of time), I''d really like to get started on the right foot. Actually, I guess you could say I''m looking for more of a digram showing the flow of a D3D "lifecycle" with some associated explanations. I say this because I''ve also noticed big inconsistencies in terms of how tutorials and demos handle other things later in the lifecycle, such as losing and reacquiring a device. It was really cool just getting 3D junk up on the screen doing interesting stuff -- but now I''d like to make sure I''m doing it RIGHT...

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Sorry I don''t know. But I would like to ask YOU something! When you first created your Direct3D program, did the window ever open and then immediately close?!?! Does this happen to anybody?!?! Thanks!

REPLY TO MCGUIRES QUESTION!!! (then mine... )

Share this post


Link to post
Share on other sites
Managed Direct3D (and Managed DirectX in general) applications rely on the Windows Forms event model. There are events fired when the form is resized, closed, etc (most of them are properties of the System.Windows.Forms.Form class). Initialization of the form is done during the constructor, which is where you want to initialize your Direct3D device as well.

The "diagram" is the same as with a regular form.

Share this post


Link to post
Share on other sites
(Anonymous: Yes, I had that happen when I started. However, it went away when I reinstalled the DX SDK. Sorry, wish I had a better answer.)

Maybe I should be more specific. The list below shows what I''ve been doing, because it seems to be the most common among the various tutorials I''ve used (obviously this is pseudo-code and leaves out things like exception handling, grabbing input, etc.).

I suppose I''m asking two questions. First, should the "initialize_D3D" stuff really be done before the window is shown? Also, am I doing the right things in the OnCreateDevice and OnResetDevice areas? Is there anything else I should do there?

I can understand why the tutorials would gloss over this -- getting started with 3D programming is a bit daunting, and the finer points of initialization aren''t going to help a beginner much. But the SDK documentation is rather sparse when it comes to explaining what exactly needs to be done in the case of (for example) an ResetDevice event. (Heh... all it says is, "Occurs after a device is reset." Recursive: See Recursive.)

Here is what I''m doing now:


constructor
-- initialize_D3D
-- form.Show
-- initialize_data
-- while form.Created
----- render
----- Application.DoEvents

OnPaint
-- render

initialize_D3D
-- check devcaps
-- set presentParams
-- create Device
-- set OnCreateDevice and OnResetDevice delegates

initialize_data
-- create my various 3D objects

render
-- device.Clear
-- device.BeginScene
-- set device view/projection matrices
-- call 3D objects'' output functions
-- device.EndScene
-- device.Present

OnResetDevice
-- set device cull mode, zbuff, lighting

OnCreateDevice
-- create VertexBuffers
-- set OnCreateVertexBuffer delegates

OnCreateVertexBuffer
-- setup the vbuf

Share this post


Link to post
Share on other sites
quote:
Original post by McGuireV10
First, should the "initialize_D3D" stuff really be done before the window is shown?
Yes, or you''ll get some rather ugly visual glitches as the form changes mode/presentation. Try it and see.

quote:
Also, am I doing the right things in the OnCreateDevice and OnResetDevice areas?
Pretty good.

quote:
But the SDK documentation is rather sparse when it comes to explaining what exactly needs to be done in the case of (for example) an ResetDevice event. (Heh... all it says is, "Occurs after a device is reset." Recursive: See Recursive.)
You''re right, the SDK documentation for managed languages is sparse (try looking at the DirectInput section - there isn''t even a tutorial). However, there are rather well-developed samples, though they tend to focus more on various effects rather than basic initialization.

It''s going to take a while for high-quality documentation for Managed DirectX to surface. Maybe after Tom Miller''s book is published.

Share this post


Link to post
Share on other sites
Thank you.

I was rather looking forward to a book I had pre-ordered with a title along the lines of "Managed DX9 with C#" but apparently it was canceled. What is the book you are referring to?

Share this post


Link to post
Share on other sites
Tom Miller''s book "Managed DirectX Game and Graphics Programming" was released about 3 months ago. I believe I bought my copy at Barnes and Noble. It is pretty much the only good reference for Direct3D in C# that I have found. It is far from comprehensive, but it covers every topic pretty well.

Share this post


Link to post
Share on other sites

  • Advertisement