Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

C# DirectX FullScreen Mode

 

Hi

 

I have been searching the net looking for a tutorial that tells you how to start a direct3d application in fullscreen mode in c# without any luck. All the tutorials seem to display in a window. Can anyone tell me how it is done.

 

Thanks

John

Edited by clearz
  • *Experts*
Posted

If you're using the Project Template to get a D3D app going, simply add this to the constructor of GraphicsClass:

startFullscreen = true;

 

Build the default D3D project with a teapot (select New Project, select the DirectX template project and choose Direct3D (the default) and add a teapot (also the default)). You'll have a number of files, including D3DMesh.cs. That's the main form (the form that will get called by Application.Run) which inherits from GraphicSample - a really nice class provided by MS. The base form is in d3dapp.cs, which contains the protected variable startFullScreen mentioned above. Since the inherited form GraphicsClass contains that variable as protected, you can change it's value to true and voila! Fullscreen app with one line of code :)

 

To see what's going on when you change that flag, you'll have to walk through a TON of EXCELLENT code in d3dapp.cs. I would strongly suggest looking through as much of that code as possible.

 

-Ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

This is simple example i hope thats helps you:

 

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System;
using System.Drawing;
using System.Windows.Forms;
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
class Fullscreen : Form
{
private Device device = null;

public Fullscreen()
{
	this.FormBorderStyle = FormBorderStyle.None;
}

public void InitDirect3D()
{
	PresentParameters presentParams = new PresentParameters();
	presentParams.BackBufferCount	= 1;
	presentParams.BackBufferFormat	= Manager.Adapters[0].CurrentDisplayMode.Format;
	presentParams.BackBufferHeight	= 768;
	presentParams.BackBufferWidth	= 1024;
	presentParams.FullScreenRefreshRateInHz = Manager.Adapters[0].CurrentDisplayMode.RefreshRate;
	presentParams.SwapEffect = SwapEffect.Discard;
	presentParams.Windowed = false;

	device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
}

public void Render()
{
	device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);

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

public static void Main()
{
	using(Fullscreen frm = new Fullscreen())
	{
		frm.InitDirect3D();

		frm.Show();

		while(frm.Created)
		{
			frm.Render();
			Application.DoEvents();
		}
	}
}
}

  • 4 weeks later...
Posted

Alt-Tab

 

I love this snippet. Took me forever to come up with this, however it has the same problem as my class.

 

It does not handle alt tab :(

 

What needs to be done to make this handle alt-tab?

Posted (edited)
Look for DeviceLostExceptions in the Render Sub, if you catch one, release all resources marked as Pool.Default then call Device.Reset(WantedDisplaySettingsOrCreationParameters) if the window is still out of focus the Reset call will fail. You can also put in the loop a call to the polling function of the device to see if it's lost, and if it is then don't draw just process the other parts of the loop instead. Edited by AndreRyan
.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
Posted (edited)

still no go

 

I updated it as such (File attached for convienience) and am having the same problem. A window the size of the previous screen simply restores itself and it errors out(even tho I believe I've trapped every line calling d3d)

 

 

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System;
using System.Drawing;
using System.Windows.Forms;
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
class Fullscreen : Form
{
private Device device = null;
PresentParameters presentParams;
private bool deviceLost;

public Fullscreen()
{
	this.FormBorderStyle = FormBorderStyle.None;
}

public void InitDirect3D()
{
	presentParams = new PresentParameters();
	presentParams.BackBufferCount	= 1;
	presentParams.BackBufferFormat	= Manager.Adapters[0].CurrentDisplayMode.Format;
	presentParams.BackBufferHeight	= 768;
	presentParams.BackBufferWidth	= 1024;
	presentParams.FullScreenRefreshRateInHz = Manager.Adapters[0].CurrentDisplayMode.RefreshRate;
	presentParams.SwapEffect = SwapEffect.Discard;
	presentParams.Windowed = false;

	device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
	deviceLost = false;
}

public void Render()
{

	if(deviceLost)
	{
		try
		{
			device.TestCooperativeLevel();
			device.Reset(presentParams);
			deviceLost = false;
		}
		catch
		{
			return;
		}
	}

	try
	{
		device.TestCooperativeLevel();
		device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);

		device.BeginScene();
		device.EndScene();
		device.Present();
	}
	catch(DeviceLostException)
	{
		deviceLost = true;
	}
}

public static void Main()
{
	using(Fullscreen frm = new Fullscreen())
	{
		frm.InitDirect3D();

		frm.Show();

		while(frm.Created)
		{
			frm.Render();
			Application.DoEvents();
		}
	}
}
}

class1.cs

Edited by Slade
Posted

This is for catching the exception in the Render sub:

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System;
using System.Drawing;
using System.Windows.Forms;
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
class Fullscreen : Form
{
private Device device = null;
PresentParameters presentParams;

public Fullscreen()
{
	this.FormBorderStyle = FormBorderStyle.None;
}

public void InitDirect3D()
{
	presentParams = new PresentParameters();
	presentParams.BackBufferCount	= 1;
	presentParams.BackBufferFormat	= Manager.Adapters[0].CurrentDisplayMode.Format;
	presentParams.BackBufferHeight	= 768;
	presentParams.BackBufferWidth	= 1024;
	presentParams.FullScreenRefreshRateInHz = Manager.Adapters[0].CurrentDisplayMode.RefreshRate;
	presentParams.SwapEffect = SwapEffect.Discard;
	presentParams.Windowed = false;

	device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
	deviceLost = false;
}

public void Render()
{
               if(device.TestCooperativeLevel == False)
               {
		try
		{
			device.Reset(presentParams);
		}
		catch
		{
			return;
		}
               }

	try
	{
		device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);

		device.BeginScene();
		device.EndScene();
		device.Present();
	}
	catch(DeviceLostException)
	{
		return;
	}
}

public static void Main()
{
	using(Fullscreen frm = new Fullscreen())
	{
		frm.InitDirect3D();

		frm.Show();

		while(frm.Created)
		{
			frm.Render();
			Application.DoEvents();
		}
	}
}
}

This is for polling in the Loop:

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System;
using System.Drawing;
using System.Windows.Forms;
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
class Fullscreen : Form
{
private Device device = null;
               PresentParameters presentParams;


public Fullscreen()
{
	this.FormBorderStyle = FormBorderStyle.None;
}

public void InitDirect3D()
{
	presentParams = new PresentParameters();
	presentParams.BackBufferCount	= 1;
	presentParams.BackBufferFormat	= Manager.Adapters[0].CurrentDisplayMode.Format;
	presentParams.BackBufferHeight	= 768;
	presentParams.BackBufferWidth	= 1024;
	presentParams.FullScreenRefreshRateInHz = Manager.Adapters[0].CurrentDisplayMode.RefreshRate;
	presentParams.SwapEffect = SwapEffect.Discard;
	presentParams.Windowed = false;

	device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
}

public void Render()
{
            try
            {
	device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);

	device.BeginScene();
	device.EndScene();
	device.Present();
            }
            catch
            {
                      return;
            }
}

public static void Main()
{
	using(Fullscreen frm = new Fullscreen())
	{
		frm.InitDirect3D();

		frm.Show();

		while(frm.Created)
		{
			if(device.CheckCooperativeLevel == False)
                                                               {
                                                                       frm.Render();
                                                               }
                                                               else
                                                               {
                                                                       device.Reset(presParams)
                                                               }
			Application.DoEvents();
		}
	}
}
}

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...