Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    51
  • comments
    129
  • views
    82784

Managed DirectX, Part 1

Sign in to follow this  
Muhammad Haggag

360 views

MainForm.cs:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Direct3D = Microsoft.DirectX.Direct3D;

namespace Skeleton
{
public class MainForm : System.Windows.Forms.Form
{
///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
...
}

#region Windows Form Designer generated code
...
#endregion

#region Declarations

Direct3D.Device device = null;

#endregion

public MainForm()
{
// Required for Windows Form Designer support
InitializeComponent();

Application.Idle += new EventHandler(OnApplicationIdle);
InitializeGraphics();
}

// The main entry point for the application.
[STAThread]
static void Main()
{
using(MainForm form = new MainForm())
Application.Run(form);
}

void OnApplicationIdle(object sender, EventArgs e)
{
while(IsApplicationIdle)
{
// Render a frame during idle time (no messages are waiting)
device.Clear(Direct3D.ClearFlags.ZBuffer | Direct3D.ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0, null);
device.BeginScene();

device.EndScene();
device.Present();
}
}

bool IsApplicationIdle
{
get
{
NativeMethods.Message msg;
return !NativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}
}

void InitializeGraphics()
{
device = new Direct3D.Device(GetAdapter(), GetDeviceType(), GetGraphicsWindow(), GetCreateFlags(),
GetPresentParameters());
}

int GetAdapter()
{
return 0;
}

Direct3D.DeviceType GetDeviceType()
{
return Direct3D.DeviceType.Hardware;
}

System.Windows.Forms.Control GetGraphicsWindow()
{
return this;
}

Direct3D.CreateFlags GetCreateFlags()
{
return Direct3D.CreateFlags.SoftwareVertexProcessing;
}

Direct3D.PresentParameters GetPresentParameters()
{
Direct3D.PresentParameters presentParameters = new Direct3D.PresentParameters();
presentParameters.Windowed = true;
presentParameters.EnableAutoDepthStencil = true;
presentParameters.AutoDepthStencilFormat = Direct3D.DepthFormat.D16;
presentParameters.PresentationInterval = Direct3D.PresentInterval.Immediate;
presentParameters.BackBufferFormat = Direct3D.Format.Unknown;
presentParameters.SwapEffect = Direct3D.SwapEffect.Discard;

return presentParameters;
}
}
}




NativeMethods.cs:
using System;
using System.Runtime.InteropServices;

class NativeMethods
{
[StructLayout(LayoutKind.Sequential)]
public struct Message
{
public IntPtr hWnd;
public uint msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}

[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[DllImport("User32.dll", CharSet=CharSet.Auto)]
public static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
}
Sign in to follow this  


2 Comments


Recommended Comments

Is that code from the SDK, or absolutely minimal stuff you wrote from scratch?

Looks quite neat-and-tidy, which is nice.

Quote:
// We won't use this maliciously

[lol] Honest? I'll trust you then...

Jack

Share this comment


Link to comment
Quote:
Is that code from the SDK, or absolutely minimal stuff you wrote from scratch?

I wrote the code from scratch, but the message-loop code is adapted (i.e. stolen shamelessly) from Tom Miller's Blog.


Quote:
Quote:
// We won't use this maliciously

Honest? I'll trust you then...

[lol]
It's Tom's comment, as is. I thought it was funny too, when I saw the code [smile]

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!