Howto: Fullscreen mode in D3D?

clearz

Newcomer
Joined
Jul 21, 2003
Messages
22
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
 
Last edited:
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
 
This is simple example i hope thats helps you:

Code:
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
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();
			}
		}
	}
}
 
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?
 
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.
 
Last 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)


Code:
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
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();
			}
		}
	}
}
 

Attachments

Last edited:
Figured it out.

You have to cancel the resize event. That solved my problem

I'm attaching a working fullscreen class
 

Attachments

This is for catching the exception in the Render sub:
C#:
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
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:
C#:
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
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();
			}
		}
	}
}
 
Back
Top