Jump to content
  • Advertisement
Sign in to follow this  
jor1980

[SlimDX] creating custom device control problem

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

Hi, i am trying to create a control for .net that acts like a device por slimdx.
I am transalting the code from this one: http://www.codegod.d...trol-AID98.aspx

And in my result when i call the init function of mydevice i get an error in this line: OnCreateDevice(this,new DeviceEventArgs(_device)); the error says null reference exception

this is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SlimDX;
using SlimDX.Direct3D9;
using System.Drawing;

namespace DevicePanelControlNet4
{
public class DevicePanelControl: Panel
{


#region Members

// The DirectX device
public Device _device = null;

// The Form to place the DevicePanel onto
public Form _parent = null;

//My direct3D
public Direct3D panelDirect3D = new Direct3D();

// On this event we can start to render our scene
public event RenderEventHandler OnRender;

// Now we know that the device is created
public event CreateDeviceEventHandler OnCreateDevice;

#endregion





#region Init DX

/// <summary>
/// Init the DirectX-Stuff here
/// </summary>
/// <param name="parent">parent of the DevicePanel</param>
public void Init(Form parent)
{
try
{
_parent = parent;

// Setup our D3D stuff
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
_device = new Device(panelDirect3D, 0, DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, presentParams);

this.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.Opaque, true);
OnCreateDevice(this,new DeviceEventArgs(_device));

parent.Show();
parent.Focus();

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

}
catch (SlimDXException ex)
{
throw new ArgumentException("Error initializing DirectX", ex);
}
}

#endregion


#region Properties

/// <summary>
/// Extend this list of properties if you like
/// </summary>
private Color _deviceBackColor = Color.Gray;

public Color DeviceBackColor
{
get { return _deviceBackColor; }
set { _deviceBackColor = value; }
}

#endregion

#region Rendering

/// <summary>
/// Rendering-method
/// </summary>
public void Render()
{
if (_device == null)
return;

//Clear the backbuffer
_device.Clear(ClearFlags.Target, _deviceBackColor, 1.0f, 0);

//Begin the scene
_device.BeginScene();

// Render of scene here
if (OnRender != null)
OnRender(this, new DeviceEventArgs(_device));

//End the scene
_device.EndScene();
_device.Present();
}


#endregion

#region Overrides

protected override void OnPaint(PaintEventArgs e)
{
// Render on each Paint
this.Render();
}

protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
// Close on escape
if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
_parent.Close();
}

#endregion



//Events
public delegate void RenderEventHandler(object sender, DeviceEventArgs e);
public delegate void CreateDeviceEventHandler(object sender, DeviceEventArgs e);

public class DeviceEventArgs : EventArgs
{
private Device _device;

public Device Device
{
get
{
return _device;
}
}

public DeviceEventArgs(Device device)
{
_device = device;
}


}
}
}

Share this post


Link to post
Share on other sites
Advertisement
I'm going to go out on a limb here and guess that your problem is probably that you have a null reference. Fixing a null reference is incredibly simple; find the one that's null (your debugger should literally point you right at it) and make it *not* null. Problem solved.

Share this post


Link to post
Share on other sites
I am confused

The error is in this line:

OnCreateDevice(this,new DeviceEventArgs(_device));

but "this" and "_device" ar not nulls,

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!