Whiplash8

Sorry if this is a dumb question but im a beginner at DirectX development. My game window is drawn and works well (nothing else is drawn at this point). I have no code errors except when I hit the X button on the window I get this:

The Code:
dGDevice.Clear(ClearFlags.Target, System.Drawing.Color.SlateGray, 1.0f, 0);

The Error:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="Microsoft.DirectX.Direct3D"
StackTrace:
at Microsoft.DirectX.Direct3D.Device.Clear(ClearFlags flags, Int32 color, Single zdepth, Int32 stencil, Rectangle[] regions)
at Microsoft.DirectX.Direct3D.Device.Clear(ClearFlags flags, Color color, Single zdepth, Int32 stencil)

I have a using statement at the top:
using DGDevice = Microsoft.DirectX.Direct3D.Device;

A Static variable:
static DGDevice dGDevice;

And it is declared in a method before the Clear is called:
dGDevice = new DGDevice(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, gameWindow, CreateFlags.SoftwareVertexProcessing, presentParams);

Also, when I minimize the program it makes this weird noise on the computer. Not from the speakers so I dont know whats up. Any help would be great!


Re: Game Technologies: DirectX 101 DirectX clearing device problem

Pieter Germishuys

What seems to be your problem is that you are trying to use the device when it's been disposed. One of the ways to solve the problem is to implement the disposal pattern.

So in your device/renderer class/object, you could create a piece of code like the following

public void Dispose()
{
Dispose(true)
GC.SurpressFinalize(this);
}

public void Dispose(bool disposing)
{
if(disposing)
{
if(device != null)
device.Dispose();
device = null;
}
}

~Device()
{
Dispose(false);
}

And then in the form/window you could override the dispose method and just call Renderer.Dispose(); which should release your resources.

Take a look at handling the device lost state. This can occur when the window has lost focus or a couple of other events.

Take a look at This Blog post I made a while ago.

I hope this helps.
Take care.





Re: Game Technologies: DirectX 101 DirectX clearing device problem

Whiplash8

Will this also fix the annoying sound when the game is minimized



Re: Game Technologies: DirectX 101 DirectX clearing device problem

Whiplash8

Fixed it, but its still making a weird sound when its minimized

Edit:  2 other problems.  When I bring it back from minimize, the sprite is moved to the far left.  Also, the sprite has a black border around it.  Have no idea why.

Here is the texture loader code:
ship.Texture = TextureLoader.FromFile(Device, spritePath + "ship.jpg", 0, 0, 1, Usage.None, Format.Unknown, Pool.Managed, Filter.None, Filter.None, Color.FromArgb(0, 0,0).ToArgb());




Re: Game Technologies: DirectX 101 DirectX clearing device problem

Whiplash8

ok fixed the other 2 problems. But what is the deal with the weird noise I followed everything from Digipen's webcast and source code.




Re: Game Technologies: DirectX 101 DirectX clearing device problem

Whiplash8

bump...

Heres the Source code and exe if you want to try it out. I do not know if it is damaging my video card or not but it sounds really bad. I could use some help. Looking through Digipen's code, I dont think I skipped anything important.

Source: http://www.phstudios.com/lowq/PhoenixDX9.zip




Re: Game Technologies: DirectX 101 DirectX clearing device problem

Whiplash8

sorry to keep bugging you but could somebody please help me out I tried it out on my other computer and it doesnt make the noise so I have no clue what the deal is.

Also, when I send it to people, some can play it, some cant and they get a file not found error. How can I check if it works on my computer




Re: Game Technologies: DirectX 101 DirectX clearing device problem

Canuck1

What kind of a program loop are you using



Re: Game Technologies: DirectX 101 DirectX clearing device problem

Whiplash8

Just the one from digipen

initialize();

initializeResources();

while (running)

{

stopWatch.Reset();

stopWatch.Start();

Application.DoEvents();

Keyboard.update();

internalUpdate();

render();

stopWatch.Stop();

currentFrameTime = (double)stopWatch.ElapsedTicks;

}





Re: Game Technologies: DirectX 101 DirectX clearing device problem

Canuck1

If the app continues to render your scene when the main window is minimized, this could be the cause of your problem. Although normally this would just increase the CPU and GPU usage to 99 or 100%, not cause any strange noises. So it seems there's something wrong with your processor or graphics card as well.



Re: Game Technologies: DirectX 101 DirectX clearing device problem

Whiplash8

Yeah I had a feeling that my video card has been damaged for a while now. I had about 10 other people try the program and their video card doesnt make noise. Is there code I can write to not render it while minimized



Re: Game Technologies: DirectX 101 DirectX clearing device problem

Canuck1

Well the simplest way to not render when minimized is this:

Add an if (form.WindowState != FormWindowState.Minimized) around your render(); statement (and possibly your internalUpdate(); statement, if it isn't necessary while minimized.)

However, although this will stop your video card strain when the program is minimized, you will still probably be taking up most of your system's CPU cycles. If you want to stop that as well (and this part is optional,) you could do it this way:

1. Implement the program loop described in Tom Miller's Blog: http://blogs.msdn.com/tmiller/archive/2005/05/05/415008.aspx

2. Add the if (form.WindowState != FormWindowState.Minimized) condition around the while (AppStillIdle) loop.

3. If you get a compiler error on the public WindowMessage msg; line, the WindowMessage enum is defined in the dxmutmisc.cs file (located in [DirectX SDK folder]\Samples\Managed\Common\,) so just add that to your project.

Hope that helps.  Happy Holidays