Quick DirectX tutorial in C# menu

User Tag List

Results 1 to 3 of 3
  1. #1
    Apoc's Avatar Angry Penguin
    Reputation
    1387
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/12
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick DirectX tutorial in C#

    This tutorial will not be very "in depth" but will give you a basic understanding of the language. We are going to simply make a single 500x500 win form application, and draw a triangle, and make it spin!

    First things first, you need to get the DXSDK from here. (Very big, so make sure you have some room)
    And of course you need the .NET environment. (I work in 3.5, but 2.0 works just as well)

    Now obviously, we're going to need to create a new Windows Forms Project. Name it DX9 Tutorial.

    Now, on our main form, change the size of the form to 500x500. Don't add any buttons or anything, we want a canvas to paint on.

    Now, in our form we want to actually drop in the using statements for DirectX.

    Code:
      using Microsoft.DirectX;
      using Microsoft.DirectX.Direct3D;
    Also, you want to update your references. (You should know how to do this, if not search google, or press F1 in Visual Studio for help, not going to bother to explain.)

    You want to add, Microsoft.DirectX and Microsoft.DirectX.Direct3D. (Do not add anything else) Be sure NOT to select DirectX version 2.0.

    Now let's get into the good stuff.


    In our Form1.cs, add the following code:

    Code:
            private Device device;
            private void InitDevice()
            {
                var presentParams = new PresentParameters{Windowed = true, SwapEffect = SwapEffect.Discard};
                device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
            }
    The first line "private Device device;" is a reference to the DirectX object "Device".

    The next method "private void InitDevice() is our Device initializer method.

    The first line "var presentParams" creates the Presentation Parameters, which we need in order to tell the "Device" how to behave the way we want. We tell the device that we don't want to be in full screen, and discard the SwapEffect, so you can write to the device immediately, and finally not to have an extra back buffer.

    The second line, "device = new Device(...);" does the following:

    -The 0 represents the first graphic adapter on your PC. This is the device we will use to draw on the screen with. Keep in mind, this tutorial is VERY basic, so I will not get into multiple monitor support. For now, this will be your "main" monitor, for most users, this makes no difference however.
    -We want to render the graphics using hardware. If you don't have an actual video card, first go buy one. (Kidding!) You can just change DeviceType.Hardware to DeviceType.Reference. It's a lot slower, but it will work none the less.
    -"this" means we want to draw on the current form. (Form1 unless you changed it)
    -We then tell the device that we want all vertices to be processed on this device.
    And finally we pass it the presentParameters variable we created above it.

    Now that we have that done, we need to make sure we run this method ONCE. This is incredibly simple. Find the following:

    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    And change it into the following:

    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                InitDevice();
            }
    Now if you were to compile this as it is, you wouldn't see anything. All you'd see is a blank 500x500 form. Which is fine, because our device has been initialized in the background!

    Let's add a little bit of color

    Each frame, a windows form sends an event to "paint" the form. Now, since the System.Windows.Forms.OnPaint method is a virtual method, we can override this to use our own method. Add the following code.

    Code:
            protected override void OnPaint(PaintEventArgs e)
            {
                device.Clear(ClearFlags.Target, Color.DarkBlue, 1f, 0);
                device.Present();
            }
    Notice this method is protected and overriding. This is to avoid the compiler overwriting our method, with the System.Windows.Forms.OnPaint method.

    Each time the Paint event fires, we will now call this method. The first line is saying, "Clear our current form, paint it with a dark blue colored background." You can ignore the last parameters. (1f is just a typecast to a float)

    And the Present(); method is what actually updates the form.

    Go ahead and compile, you should end up with a dark blue background on your form!

    Congrats, we now have the start of a DirectX application. Kinda boring though isn't it? Let's give it some flash, and add a triangle. :P

    First of all, all things in DirectX are drawn in triangles. (Same goes for everything in WoW). They are called, you guessed it, polygons.

    Let's change our OnPaint method to the following:

    Code:
            protected override void OnPaint(PaintEventArgs e)
            {
                CustomVertex.TransformedColored[] vertices = new CustomVertex.TransformedColored[3];
    
                vertices[0].Position = new Vector4(150f, 100f, 0f, 1f);
                vertices[0].Color = Color.Red.ToArgb();
                vertices[1].Position = new Vector4(this.Width / 2 + 100f, 100f, 0f, 1f);
                vertices[1].Color = Color.Green.ToArgb();
                vertices[2].Position = new Vector4(250f, 300f, 0f, 1f);
                vertices[2].Color = Color.Yellow.ToArgb();
    
                device.Clear(ClearFlags.Target, Color.DarkBlue, 1f, 0);
    
                device.BeginScene();
    
                device.VertexFormat = CustomVertex.TransformedColored.Format;
                device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
    
                device.EndScene();
    
                device.Present();
            }
    First, we tell the compiler that we want this object to have 3 vertices. (A triangle) Next we tell it where the vertices should be located in XYZ coords, and what color. Then we clear the current screen and assign it our blue background. Now we tell the compiler that at this point, we want to actually begin drawing our scene, and finally end our current frames scene, and draw the picture.

    Compile it and see what you get.

    Pretty cool eh?

    You may end up with some weird behavior when resizing the window. To fix this just add the following to the bottom of the OnPaint method.

    Code:
    Invalidate();
    And in the "public Form1()" add the following:
    Code:
    SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
    These are to make sure anything we have drawn before, is now invalid, and must be drawn again.

    That's the end of this tutorial. Hope you enjoyed.

    P.S. don't bother posting DarkBasic crap in here. You have been warned.

    Quick DirectX tutorial in C#
  2. #2
    EcHoEs's Avatar lol why u mad
    Reputation
    374
    Join Date
    Sep 2006
    Posts
    1,647
    Thanks G/R
    3/1
    Trade Feedback
    2 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gj Apoc ^^
    P.S. Loved the P.S.


  3. #3
    Tom_2001's Avatar Member
    Reputation
    177
    Join Date
    Oct 2007
    Posts
    609
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nice, but looks Copy N pasted

Similar Threads

  1. [Guide] FREE FONTS! Extremely Noob Friendly! VERY QUICK AND EASY TUTORIAL!
    By Ghosthopper in forum Art & Graphic Design
    Replies: 5
    Last Post: 04-05-2011, 04:25 AM
  2. [Video Tutorial] Quick and simple. Watch and learn!
    By MadameGrip in forum WoW ME Tools & Guides
    Replies: 2
    Last Post: 03-09-2011, 11:53 AM
  3. CSS Quick Tutorial
    By Airwavez in forum Programming
    Replies: 0
    Last Post: 03-09-2008, 08:27 PM
  4. Quick Money - Scam
    By janzi9 in forum WoW Scam Prevention
    Replies: 8
    Last Post: 11-29-2006, 07:40 AM
All times are GMT -5. The time now is 04:20 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search