Error when trying to restore surfaces.

wyrd

Senior Contributor
Joined
Aug 23, 2002
Messages
1,405
Location
California
I have a method here which tests the cooperative level of the device and attempts to restore the surfaces if it has to;

C#:
                                /// <summary>
		/// Insures that the surfaces for this GameSurface are accessible.
		/// </summary>
		private void _checkSurfaces() 
		{
			if (_device.TestCooperativeLevel() == false) {
				// TODO: Bug - Throws error in FullscreenExclusive.
				_device.RestoreAllSurfaces();
			}
		}

The problem is, it throws an exception when trying to perform RestoreAllSurfaces().

An unhandled exception of type 'Microsoft.DirectX.DirectDraw.WrongModeException' occurred in microsoft.directx.directdraw.dll

Additional information: Error in the application.

I've also tried catching the SurfaceLostException, but it gives me the same error when trying to perform RestoreAllSurfaces().

I get no errors when running in windowed mode, so I can only assume it has something to do with FullscreenExclusive.

Any ideas on how to fix this?
 
Before you restore you have to manually clean up any resources defined in a pool NOT part of managed (can't remember the enum to look for offhand). That may or may not be your problem.

Have you run dbmon, a tool in the SDKs bin folder, to see what it shows as an error? It often gives more detailed info about DX errors.

-ner
 
Hmm.. in a pool? Do you mean I have to Dispose and reinstantiate every single surface in my game? (basically as if I was first loading up the game).

I've never used dbmon, I'll take a look and see if I can figure out how it works. :)
 
I've looked at a few tutorials to try and figure out the problem (DD documentation sucks). The only way I've found is to dispose of all of my surfaces and reinstantiate them. This is really, really ugly because it could cause a performance hit.

How do you handle this in your Ms. Pacman game?
 
This may seem obvious and is probably of no use but 'Microsoft.DirectX.DirectDraw.WrongModeException' is usually thrown when you try to use a DirectDraw Device when you've lost exclusive mode, you're supposed to loop until it's restored:
C#:
private void _checkSurfaces() 
        {
            if (_device.TestCooperativeLevel() == false) {
                do{
                   Application.DoEvents();
                } while(_device.TestCooperativeLevel() == false);
                _device.RestoreAllSurfaces();
                //Reload all pictures from file
            }
        }
 
I noticed that you have this line;

//Reload all pictures from file

If you do _device.RestoreAllSurfaces(), wouldn't that reload everything for you? It doesn't make sense to me that you'd still have to reload the graphics. If that's the case, then using RestoreAllSurfaces seems rather useless.

Am I missing something?

EDIT:
I tried the code and it works. Thanks for that. I'm still confused as to why I'd need to reload the pictures though.
 
Last edited:
I didn't like how it automatically "paused" the game when trying to restore surfaces, so I changed the logic.. that way the game is still updated (just not drawn) even while minimized;

C#:
		/// <summary>
		/// Attempts to restore surfaces if they are not available.
		/// </summary>
		private void _restoreSurfaces() 
		{
			// Surfaces may need to be restored.
			if (!_surfacesAvailable) {
				try {
					_device.RestoreAllSurfaces();
					_surfacesAvailable = true;
				}
				catch (Exception) {
					// Ignore errors.
				}
			}

			// Check if surfaces were lost.
			if (!_device.TestCooperativeLevel()) {
				_surfacesAvailable = false;
			}
		}
 
Back
Top