|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionWelcome to my first tutorial on Managed DirectX, included with DirectX 9 SDK. Most C# developers were waiting for this release. Before Managed DirectX, C# developers were using DirectX via COM Interop of DirectX 7 or 8 VB component. The new Managed DirectX components offers best performance and easier programming over DirectX COM Interop. This tutorial is for newcomers in DirectX development, like me, or for people who were using COM Interop for DirectX development. In this tutorial, we will make a clone of Super Metroid game called Managed Metroid. We will try to use all the components of Managed DirectX (DirectDraw, DirectSound, DirectInput, Direct3D, DirectPlay and AudioVideoPlayback). In part 1, we will start with basic DirectDraw implementation by showing the title screen and text in full screen. RequirementsThese articles require some knowledge of C#. Also a basic game development background would be useful. Software requirements:
The Title ScreenAdding DirectX namespaces to the projectAdd the references to Microsoft.DirectX.dll and Microsoft.DirectX.DirectDraw.dll to your project and add these namespaces to the project. using Microsoft.DirectX;
using Microsoft.DirectX.DirectDraw;
Adding DirectX variablesFirst of all, we need to create a DirectDraw device. After that, we create the // The DirectDraw Device, used in all the application
private Device display;
// The Front Surface
private Surface front = null;
// The Back Surface
private Surface back = null;
// Surface to store the title screen
private Surface title = null;
// Surface to store the text
private Surface text = null;
// The Clipper
private Clipper clip = null;
string titlescreen = Application.StartupPath + "\\title.bmp";
Initialize DirectDrawFor initializing any Managed DirectX component, I suggest you to create methods for each component you use. Here is the method I used to init my private void InitDirectDraw()
{
// Used to describe a Surface
SurfaceDescription description = new SurfaceDescription();
// Init the Device
display = new Device();
#if DEBUG
display.SetCooperativeLevel(this, CooperativeLevelFlags.Normal);
#else
// Set the Cooperative Level and parent,
//Setted to Full Screen Exclusive to the form)
display.SetCooperativeLevel(this,
CooperativeLevelFlags.FullscreenExclusive);
// Set the resolution and color depth
//used in full screen(640x480, 16 bit color)
display.SetDisplayMode(640, 480, 16, 0, false);
#endif
// Define the attributes for the front Surface
description.SurfaceCaps.PrimarySurface = true;
#if DEBUG
front = new Surface(description, display);
#else
description.SurfaceCaps.Flip = true;
description.SurfaceCaps.Complex = true;
// Set the Back Buffer count
description.BackBufferCount = 1;
// Create the Surface with specifed description and device)
front = new Surface(description, display);
#endif
description.Clear();
#if DEBUG
description.Width = front.SurfaceDescription.Width;
description.Height = front.SurfaceDescription.Height;
description.SurfaceCaps.OffScreenPlain = true;
back = new Surface(description, display);
#else
// A Caps is a set of attributes used by most of DirectX components
SurfaceCaps caps = new SurfaceCaps();
// Yes, we are using a back buffer
caps.BackBuffer = true;
// Associate the front buffer to back buffer with specified caps
back = front.GetAttachedSurface(caps);
#endif
// Create the Clipper
clip = new Clipper(display);
/// Set the region to this form
clip.Window = this;
// Set the clipper for the front Surface
front.Clipper = clip;
// Reset the description
description.Clear();
// Create the title screen
title = new Surface(titlescreen, description, display);
description.Clear();
// Set the height and width of the text.
description.Width = 600;
description.Height = 16;
// OffScreenPlain means that this Surface
//is not a front, back, alpha Surface.
description.SurfaceCaps.OffScreenPlain = true;
// Create the text Surface
text = new Surface(description, display);
// Set the backgroup color
text.ColorFill(Color.Black);
// Set the fore color of the text
text.ForeColor = Color.White;
// Draw the Text to the Surface to coords (0,0)
text.DrawText(0, 0,
"Managned DirectX Tutorial 1 - Press Enter or Escape to exit",
true);
}
The Draw methodThe private void Draw()
{
// If the front isn't create, ignore this function
if (front == null)
{
return;
}
// If the form is minimized, ignore this function
if(this.WindowState == FormWindowState.Minimized)
{
return;
}
try
{
// Draw the title to the back buffer using source copy blit
back.DrawFast(0, 0, title, DrawFastFlags.Wait);
// Draw the text also to the back buffer using source copy blit
back.DrawFast(10, 10, text, DrawFastFlags.Wait);
#if DEBUG
// Draw all this to the front
front.Draw(back, DrawFlags.Wait);
#else
// Doing a flip to transfer back buffer to the front, faster
front.Flip(back, FlipFlags.Wait);
#endif
}
catch(WasStillDrawingException)
{
return;
}
catch(SurfaceLostException)
{
// If we lost the surfaces, restore the surfaces
RestoreSurfaces();
}
}
The RestoreSurfaces methodThe private void RestoreSurfaces()
{
// Used to describe a Surface
SurfaceDescription description = new SurfaceDescription();
// Restore al the surface associed with the device
display.RestoreAllSurfaces();
// Redraw the text
text.ColorFill(Color.Black);
text.DrawText(0, 0,
"Managned DirectX Tutorial 1 - Press Enter or Escape to exit",
true);
// For the title screen, we need to
//dispose it first and then re-create it
title.Dispose();
title = null;
title = new Surface(titlescreen, description, display);
return;
}
The Final touchAt last, add the method public Tutorial1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// Initialize DirectDraw stuffs
InitDirectDraw();
// Remove the cursor
this.Cursor.Dispose();
// Show the form if isn't already do
this.Show();
// The main loop
while(Created)
{
Draw();
// Make sure that the application
//process the messages
Application.DoEvents();
}
}
Made a change to the static void Main()
{
Tutorial1 app = new Tutorial1();
Application.Exit();
}
The private void Tutorial1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
// If the user press Escape or Enter, the tutorial exits
if(e.KeyCode == Keys.Escape || e.KeyCode == Keys.Enter)
this.Close();
}
ConclusionIn this tutorial, we learned how to show a bitmap in full screen using HistoryUpdate 1.1
Update 1.0
| ||||||||||||||||||||