C# and DirectDraw basic issues

Started by
0 comments, last by UltimaX 12 years, 3 months ago
Hi.

I´m trying to get managed directX to work in a small project.
What I want is to enable windowed mode and draw with DirectDraw.

The following code is resulting in an invisible window (at least for me, using Visual Studio 2010 Ultimate, with DX11 installed and references added by browsing to C:\Windows\assembly\GAC\Microsoft.DirectX*\ and adding the dlls)


public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);
public Microsoft.DirectX.DirectDraw.Device dx_device = null;
public Surface dxs_primary;
public Surface dxs_backbuffer;
private Rectangle drawRect;
public Form1()
{
InitializeComponent();
this.Show();
}
public void InitDX()
{
try
{
dx_device = new Microsoft.DirectX.DirectDraw.Device();
dx_device.SetCooperativeLevel(this, Microsoft.DirectX.DirectDraw.CooperativeLevelFlags.Normal);

SurfaceDescription dx_surf_desc = new SurfaceDescription();
dx_surf_desc.SurfaceCaps.PrimarySurface = true;
dxs_primary = new Surface(dx_surf_desc, dx_device);
Clipper dx_clipper = new Clipper(dx_device);
dx_clipper.Window = panel1;
dxs_primary.Clipper = dx_clipper;
dx_surf_desc.Clear();
dx_surf_desc.SurfaceCaps.OffScreenPlain = true;
dx_surf_desc.Width = this.ClientSize.Width;
dx_surf_desc.Height = this.ClientSize.Height;
dxs_backbuffer = new Surface(dx_surf_desc, dx_device);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error initializing Managed DirectX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

public void Loop()
{
do
{
if (!this.Focused)
{
Application.DoEvents();
}
else
{
dxs_backbuffer.ColorFill(Color.BlueViolet);
dxs_primary.Flip(dxs_backbuffer, FlipFlags.Wait);
Application.DoEvents();
}
} while (this.Created);
dxs_backbuffer.Dispose();
dxs_primary.Dispose();
dx_device.Dispose();
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
InitDX();
Loop();
}
}


The only added control to the prorgam is a panel with default name.

I dont really see a reason for using Direct3D instead of DirectDraw but I do admit that this statement is due to my lack of knowledge in the subject. If D3D is better (for 2D graphics which is my primary target and windowed mode) then I should switch.
Advertisement

Warning: Microsoft DirectDraw has been deprecated. Deprecated components of Microsoft DirectX 9.0 for Managed Code are considered obsolete. While these components are still supported in this release of DirectX 9.0 for Managed Code, they may be removed in the future. When writing new applications, you should avoid using these deprecated components. When modifying existing applications, you are strongly encouraged to remove any dependency on these components.
DirectDraw enables you to directly manipulate display memory, the hardware blitter, hardware overlay support, and flipping surface support. The following tables list the members exposed by the Microsoft.DirectX.DirectDraw namespace.
[/quote]

http://msdn.microsof...v=vs.85%29.aspx


DirectDraw has been deprecated for a while and there is no reason to use it unless you have a legacy product you are supporting. From the sounds of it this isn't the case.

You should consider Direct2D or even Direct3D. Take a look at SlimDX as it supports both of these.
http://slimdx.org/

[EDIT]
Should also include this as well:


As of DirectX version 8.0, DirectDraw was merged into a new package called DirectX Graphics, which is really just Direct3D with a few DirectDraw API additions. DirectDraw can still be used by programmers, but they must use older DirectX interfaces (DirectX 7 and below). As of the release of the June 2010 DirectX SDK package, the DirectDraw header file and library are no longed included.

DirectDraw has been deprecated since version 7 but is still included with DirectX, although updates are no longer made. Developers have been instructed to use textured quads in Direct3D for 2D graphics. Managed DirectX includes a managed wrapper for DirectDraw.

DirectDraw is now replaced by Direct2D.
[/quote]

http://en.wikipedia.org/wiki/DirectDraw
Thanks for info, althou I knew about deprecation status.

Decided to go for SlimDX but is Slim.DX.Direct3D9 a good option for 2D games ? (what pitfalls etc should I be aware of?)

I´m not familiar with 3D math, but I presume I could set some kind of default viewpoint in the 3D world and then use sprites easly ?

This topic is closed to new replies.

Advertisement