Jump to content
  • Advertisement
Sign in to follow this  
Toolmaker

Managed DirectX 9 Resolution selection dialog

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently toying around with Direct3D and I'm learning to get started in 3D games. Most games have the ability to change resolution before or during runtime(Max Payne and Man Hunt both allowed you to select resolution before runtime), so I figured, why not make a standard component out of that. Therefor, I present the Resolution Picker: Resolution Picker It's free of charge and you don't need to credit me in any form(although it's appreciated [wink]). Source for the dialog:
////////////////////////////////////////////////////////////////////
/// Managed Direct3D 9 Resolution Picker, Copyright (c) 2005, Toolmaker software
/// 
/// This software is provided 'as-is', without any expressed or implied warranty. 
/// In no event will (Hereby called, 'the author' be held liable 
/// for any damages arising from the use of this software.
/// 
/// You may legally use and distribute this software for any purpose, provided 
/// that you abide by the laws of your government and do not misrepresent the original 
/// creators of this software. 
/// 
/// You may freely distribute this package, stated that you:
/// 1) Do not remove or alter the license/readme in any form
/// 2) Do not claim this package to be yours
/// 3) Modified version of the code must be marked as such
/// 
/// DirectX and Direct3D are registered trademarks of Microsoft Corporation.
////////////////////////////////////////////////////////////////////

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Toolmaker
{
	/// <summary>
	/// Summary description for SelectResolution.
	/// </summary>
	public class SelectResolution : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnOk;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.ComboBox cboDisplayMode;
		private System.Windows.Forms.ComboBox cboAdapters;

		private AdapterInformation selectedAdapter;
		private DisplayMode	selectedDisplayMode;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

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

			EnumerateAdapters();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SelectResolution));
			this.btnOk = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.cboAdapters = new System.Windows.Forms.ComboBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.cboDisplayMode = new System.Windows.Forms.ComboBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnOk
			// 
			this.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.btnOk.Location = new System.Drawing.Point(104, 152);
			this.btnOk.Name = "btnOk";
			this.btnOk.TabIndex = 0;
			this.btnOk.Text = "&OK";
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(192, 152);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "&Cancel";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.cboAdapters);
			this.groupBox1.Location = new System.Drawing.Point(16, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(256, 56);
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Select adapter";
			// 
			// cboAdapters
			// 
			this.cboAdapters.Location = new System.Drawing.Point(16, 22);
			this.cboAdapters.Name = "cboAdapters";
			this.cboAdapters.Size = new System.Drawing.Size(224, 21);
			this.cboAdapters.TabIndex = 3;
			this.cboAdapters.SelectedIndexChanged += new System.EventHandler(this.cboAdapters_SelectedIndexChanged);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.cboDisplayMode);
			this.groupBox2.Location = new System.Drawing.Point(16, 80);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(256, 56);
			this.groupBox2.TabIndex = 5;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Select resolution";
			// 
			// cboDisplayMode
			// 
			this.cboDisplayMode.Location = new System.Drawing.Point(16, 24);
			this.cboDisplayMode.Name = "cboDisplayMode";
			this.cboDisplayMode.Size = new System.Drawing.Size(224, 21);
			this.cboDisplayMode.TabIndex = 4;
			this.cboDisplayMode.SelectedIndexChanged += new System.EventHandler(this.cboDisplayMode_SelectedIndexChanged);
			// 
			// SelectResolution
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(282, 184);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOk);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "SelectResolution";
			this.ShowInTaskbar = false;
			this.Text = "Select resolution";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		#region Combobox helper classes
		/// <summary>
		/// Small helper class to store the AdapterInformation inside the dropdown. ToString function is
		/// used to return an appropriate text to display in the dropdown.
		/// </summary>
		private class AdapterContainer
		{
			public AdapterInformation ai;

			public AdapterContainer(AdapterInformation ai)
			{
				this.ai = ai;
			}

			public override string ToString()
			{
				if (Manager.Adapters.Default.Adapter == ai.Adapter)
					return string.Format("{0} (Default)", ai.Information.Description);
				else
					return ai.Information.Description;
			}
		}

		/// <summary>
		/// Helper class to store DisplayMode inside the dropdown box. ToString function is used
		/// to return an appropriate text to display in the dropdown box. If defaultDisplayMode 
		/// is set to true, (Default) will appear behind the description in the combobox
		/// </summary>
		private class DisplayModeContainer
		{
			public DisplayMode dm;
			public bool defaultDisplayMode = false;

			public DisplayModeContainer(DisplayMode dm, bool defaultDisplayMode)
			{
				this.dm = dm;
				this.defaultDisplayMode = defaultDisplayMode;
				if (defaultDisplayMode)
					Console.WriteLine("Default Display Mode: " + defaultDisplayMode);
			}

			public override string ToString()
			{
				if (defaultDisplayMode)
					return string.Format("{0} x {1} x {2} (Default)", dm.Width, dm.Height, dm.Format);
				else
					return string.Format("{0} x {1} x {2}", dm.Width, dm.Height, dm.Format);
			}

		}
		#endregion

		#region Events
		private void cboAdapters_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			AdapterInformation adapter = ((AdapterContainer)cboAdapters.SelectedItem).ai;

			// Prevent unnecassary enumeration of the display modes
			if (selectedAdapter != adapter)
			{
				selectedAdapter = adapter;
				EnumerateDisplayModes();	
			}
		}

		private void cboDisplayMode_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			selectedDisplayMode = ((DisplayModeContainer)cboDisplayMode.SelectedItem).dm;
		}
		#endregion

		#region Properties
		public AdapterInformation AdapterInformation
		{
			get { return selectedAdapter; }
		}

		public DisplayMode DisplayMode
		{
			get { return selectedDisplayMode; }
		}
		#endregion

		#region Enumeration functions
		private void EnumerateAdapters()
		{
			int defaultAdapter = 0;
			foreach (AdapterInformation ai in Manager.Adapters)
				cboAdapters.Items.Add(new AdapterContainer(ai));
			
			cboAdapters.SelectedIndex = defaultAdapter;
		}

		private void EnumerateDisplayModes()
		{
			int defaultMode = 0;
			DisplayMode lastMode = new DisplayMode();

			// Clear the current list
			cboDisplayMode.Items.Clear();

			foreach (DisplayMode dm in selectedAdapter.SupportedDisplayModes)
			{
				if (lastMode.Width != dm.Width || lastMode.Height != dm.Height)
				{
					bool defMode = selectedAdapter.CurrentDisplayMode.Equals(dm);
					int index = cboDisplayMode.Items.Add(new DisplayModeContainer(dm, defMode));

					if (defMode)
						defaultMode = index;
				}

				lastMode = dm;
			}

			cboDisplayMode.SelectedIndex = defaultMode;
		}
		#endregion
	}
}


And usage:
			using (SelectResolution sr = new SelectResolution())
			{
				if (sr.ShowDialog() == DialogResult.OK)
				{
					AdapterInformation adapterInformation = sr.AdapterInformation;
					DisplayMode displayMode = sr.DisplayMode;

					// Do your Direct3D stuffage here
				}
			}


If you got problems, or made some changes, let me know, I'm interested in feedback. Toolmaker [Edited by - Toolmaker on July 27, 2009 5:01:40 AM]

Share this post


Link to post
Share on other sites
Advertisement
It's a great thing that I'm doing a 2D game with a set resolution... :/

This thing looks cool. [smile]

[Edited by - Daniel Miller on July 21, 2005 4:48:03 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Syranide
Oooh, *shudder* MX420 ;)


I'm behind my parent's PC, while my girlfriend is playing the Sims 2 on my machine. This machine grinds to a halt when playing the Sims 2, so I just swapped PCs.

I got an Ati Radeon X300SE in my own PC. It's not the best card, but it was sure cheap as hell ^_^.

Toolmaker

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!