MMOwned - World of Warcraft Exploits, Hacks, Bots and Guides

Homepage Register FAQ Members Mark Forums Read Advertise Marketplace FPSowned


Go Back   MMOwned - World of Warcraft Exploits, Hacks, Bots and Guides > Programming > Programming section > C#
Reload this Page Quick DirectX tutorial in C#
C# Discussions about C#

Reply
 
LinkBack Thread Tools
Quick DirectX tutorial in C#
(#1)
Old
Apoc's Avatar
Apoc is Offline
c|_| My care cup is empty
Rep Power: 4
Reputation: 468
Apoc is a glorious beacon of lightApoc is a glorious beacon of lightApoc is a glorious beacon of lightApoc is a glorious beacon of lightApoc is a glorious beacon of light
 
Posts: 575
Join Date: Jan 2008
Quick DirectX tutorial in C# - 03-16-2008

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 [Only registered and activated users can see links. ]. (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.


[Only registered and activated users can see links. ]
Reply With Quote

Donate to remove ads.
(#2)
Old
EcHoEs's Avatar
EcHoEs is Online
Contributor
Rep Power: 3
Reputation: 206
EcHoEs has a spectacular aura aboutEcHoEs has a spectacular aura aboutEcHoEs has a spectacular aura about
 
Posts: 1,101
Join Date: Sep 2006
Location: in ur cmputer
03-16-2008

Gj Apoc ^^
P.S. Loved the P.S.



Reply With Quote
(#3)
Old
Tom_2001's Avatar
Tom_2001 is Offline
Contributor
Rep Power: 2
Reputation: 176
Tom_2001 has a spectacular aura aboutTom_2001 has a spectacular aura about
 
Posts: 608
Join Date: Oct 2007
Location: liverpool
03-23-2008

nice, but looks Copy N pasted


Reply With Quote
Reply

Donate to remove ads.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On




Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
vBulletin Skin developed by: vBStyles.com


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327