Jump to content
  • Advertisement
Sign in to follow this  
flashinpan

Managed DirectX paint form BLUE does not work

This topic is 4818 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

When I click the button on the form to call some REPAINT code...I get a null reference exception....WHY? device is a private member variable available to the entire class at runtime. I am referencing device ONLY after the form has displayed and device has been assigned to already ( in the call to InitializeGraphics( ) ) ==================================== An unhandled exception of type 'System.NullReferenceException' occurred in DXChpOne.exe Additional information: Object reference not set to an instance of an object. Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at DXChpOne.Form1.OnCallPaint() at DXChpOne.Form1.button1_Click(Object sender, EventArgs e) at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at DXChpOne.Form1.Main()The program '[2284] DXChpOne.exe' has exited with code 0 (0x0). here is my paint code
private void OnCallPaint()
{
	device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);
	device.Present();
}

private void button1_Click(object sender, System.EventArgs e)
{
	OnCallPaint();
}




HERE IS MY CURRENT SOURCE CODE (Managed DirextX in C#):
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;


namespace DXChpOne
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button button1;

		private Device device = null;

		//page 9 and 10,  DX Kick Start
		public void InitializeGraphics()
		{
			//Set our presentation parameters
			PresentParameters presentParams = new PresentParameters();

			//presentParams.BackBufferFormat = Format.Unknown;

			presentParams.DeviceWindow = this;

			presentParams.Windowed = true;
			presentParams.SwapEffect = SwapEffect.Discard;

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

				device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);
				device.Present();
				
			}
			catch(Exception e)
			{
				System.Diagnostics.Debug.WriteLine(e.ToString());
			}
		}


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

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(512, 288);
			this.button1.Name = "button1";
			this.button1.TabIndex = 0;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(616, 454);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.button1});
			this.Name = "Form1";
			this.Text = "Form1";
			this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			//page 11 DX Kick Start
			using (Form1 frm = new Form1())
			{
				//Show our form and initialize our graphics engine
				frm.Show();				
				frm.InitializeGraphics();
				Application.Run(frm);				
			}
			
		}

		
		private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
//			device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);
//			Console.WriteLine("after line one");
//			device.Present();
//			Console.WriteLine("after line two");
		}

		private void OnCallPaint()
		{
			device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);
			device.Present();
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			OnCallPaint();
		}
	}
}









NOTE: Also...if I run the code ABOVE in RELEASE mode, let the form open, then close the form...I get no errors. If I do the same thing in DEBUG mode...I get the following error: Error in the application. -2005530516 (D3DERR_INVALIDCALL) at Microsoft.DirectX.Direct3D.Device..ctor(Int32 adapter, DeviceType deviceType, Control renderWindow, CreateFlags behaviorFlags, PresentParameters[] presentationParameters) at DXChpOne.Form1.InitializeGraphics() in c:\documents and settings\syndi\desktop\dxchpone\form1.cs:line 41 The program '[3196] DXChpOne.exe' has exited with code 0 (0x0). What difference is DEBUG mode making vs RELEASE?

Share this post


Link to post
Share on other sites
Advertisement
UPDATE on this:



I tried checking to see if device is in fact NULL...and it is:




private void OnCallPaint()
{
if(device == null)
{
Console.WriteLine("device is null"); THIS LINE FIRES, BUT WHY?
}

device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);
device.Present();

}

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!