Web Hosting Philippines, Offshore Programming, Offshore SEO Philippines, Cheap Webhosting Manila Philippines
Home -> Resources -> .NET -> Managed Direct3D via C# - Part 1

The Absolute Simplest C# Managed DirectX Setup Sample Code

I recently discovered Ron Penton's Beginning C# Game Programming (watch out before clicking the link, your OS - not just browser (!) might run out of memory loading the HUUUUGE slide) book and found it to be very well written and easy to understand. Additionally, Ron Penton seems to share my values of code clarity via brevity. However, an issue with the book is the fact that the complete source code is no longer provided or anywhere to be found. Still, the info is very well written, and it inspired me to cobble up an even simpler Managed Direct3D setup code for illustration purposes than that found in the book. Below is the resulting code. Remember that you can terminate the app via pressing ALT-F4.

using System.Drawing; // for Color.Green
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;

class MainForm : Form	{
  static void Main() {
    MainForm mf = new MainForm();

    // use the DisplayMode you are on right now for Adapter 0
    DisplayMode dm = Manager.Adapters[0].CurrentDisplayMode;

    // create a Presentation Parameters object
    PresentParameters pp  = new PresentParameters();
    pp.SwapEffect         = SwapEffect.Discard; // most performant swap buffer setting for modern cards
    pp.Windowed           = false;  // or true for a windowed app
    // windowed apps don't need the pp.BackBufferXXX assignments below
    pp.BackBufferCount    = 1;
    pp.BackBufferFormat   = dm.Format;
    pp.BackBufferWidth    = dm.Width;
    pp.BackBufferHeight   = dm.Height;

    /* create a new Direct3D hardware device object pointing to Adapter 0
       with hardware (software also fine) vertex processing, associating it with mainForm,
       and using the Presentation Parameters set above */
    Device device = new Device( 0, DeviceType.Hardware, mf, CreateFlags.HardwareVertexProcessing, pp );

    mf.Show();

    while (mf.Created) {
      /* ClearFlags.Target indicates that the 2nd parameter, Color.Green,
         will be used to clears the device's "render target".
         If ClearFlags.ZBuffer is also set (via the | operator), the 3rd parameter, 1.0f, will be used,
         If ClearFlags.Stencil is also set, the last parameter, 0, will be used */
      device.Clear(ClearFlags.Target, Color.Green, 1.0f, 0);
      device.BeginScene();
      // do your rendering stuff here
      device.EndScene();
      device.Present();
      Application.DoEvents(); // surrender control to the main application loop
    }
  }
}

We create a slightly more elaborate version below by separating the MainForm class from the class that holds the Main() function. That class is now renamed Program and declared as static. This is mainly done for clarity by indicating that there will only ever be one instance of this class, which also happens to contain the main entry point of the program. One useful addition here is hooking on to the OnKeyDown event of MainForm so that it will terminate if it detects the ESC key.

using System.Drawing; // for Color.Green
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;

static class Program {
  static void Main() {
    MainForm mf = new MainForm();
    mf.Show();

    while (mf.Created) {
      mf.device.Clear(ClearFlags.Target, Color.Green, 1.0f, 0);
      mf.device.BeginScene();
      mf.device.EndScene();
      mf.device.Present();
      Application.DoEvents();
    }
  }
}

class MainForm : Form	{
  public Device device;

  public MainForm() {
    DisplayMode dm = Manager.Adapters[0].CurrentDisplayMode;

    PresentParameters pp  = new PresentParameters();
    pp.SwapEffect         = SwapEffect.Discard;
    pp.Windowed           = false;
    pp.BackBufferCount    = 1;
    pp.BackBufferFormat   = dm.Format;
    pp.BackBufferWidth    = dm.Width;
    pp.BackBufferHeight   = dm.Height;

    device = new Device( 0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, pp );
  }

  /* See StackOverflow's "What's the difference between KeyDown and KeyPress
     in .NET?" for info on below; Penton used OnKeyPress which is not as clean. */
  protected override void OnKeyDown(KeyEventArgs e) {
    base.OnKeyDown(e);
    if (e.KeyCode == Keys.Escape) Application.Exit(); // or just Close()
  }
}

Discussion:


Back to Top

© 2017 by Andy Sy
last updated: 2012-Jun-3




Web Development / Rich Internet Applications (RIA) Development

Programming Languages

Platforms

Database Development

Web Hosting Philippines, Offshore SEO, Guaranteed SEO Philippines
   © 2003-2015 Neotitans Technologies Inc.