fixed crash on resources loading when hardware is not available (for example, window is minimized)

This commit is contained in:
codeimp 2009-01-13 06:31:53 +00:00
parent 599b8ce667
commit 7317bd5b30
3 changed files with 68 additions and 44 deletions

View file

@ -271,7 +271,7 @@ namespace CodeImp.DoomBuilder.Data
if(loadfailed) img = Properties.Resources.Failed;
// Write to memory stream and read from memory
memstream = new MemoryStream();
memstream = new MemoryStream((img.Size.Width * img.Size.Height * 4) + 4096);
img.Save(memstream, ImageFormat.Bmp);
memstream.Seek(0, SeekOrigin.Begin);
texture = Texture.FromStream(General.Map.Graphics.Device, memstream, (int)memstream.Length,

View file

@ -312,21 +312,24 @@ namespace CodeImp.DoomBuilder.Map
// Any vertices?
else if(flatvertices.Length > 0)
{
FlatVertex[] buffervertices = new FlatVertex[triangles.Vertices.Count];
flatvertices.CopyTo(buffervertices, 0);
if(General.Map.Graphics.CheckAvailability())
{
FlatVertex[] buffervertices = new FlatVertex[triangles.Vertices.Count];
flatvertices.CopyTo(buffervertices, 0);
// Raise event to allow plugins to modify this data
General.Plugins.OnSectorFloorSurfaceUpdate(this, ref buffervertices);
// Raise event to allow plugins to modify this data
General.Plugins.OnSectorFloorSurfaceUpdate(this, ref buffervertices);
// Make the buffer
flatfloorbuffer = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffervertices.Length,
Usage.WriteOnly | Usage.Dynamic, VertexFormat.None, Pool.Default);
// Make the buffer
flatfloorbuffer = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffervertices.Length,
Usage.WriteOnly | Usage.Dynamic, VertexFormat.None, Pool.Default);
// Fill it
DataStream bufferstream = flatfloorbuffer.Lock(0, FlatVertex.Stride * buffervertices.Length, LockFlags.Discard);
bufferstream.WriteRange<FlatVertex>(buffervertices);
flatfloorbuffer.Unlock();
bufferstream.Dispose();
// Fill it
DataStream bufferstream = flatfloorbuffer.Lock(0, FlatVertex.Stride * buffervertices.Length, LockFlags.Discard);
bufferstream.WriteRange<FlatVertex>(buffervertices);
flatfloorbuffer.Unlock();
bufferstream.Dispose();
}
}
}
@ -350,21 +353,24 @@ namespace CodeImp.DoomBuilder.Map
// Any vertices?
else if(flatvertices.Length > 0)
{
FlatVertex[] buffervertices = new FlatVertex[triangles.Vertices.Count];
flatvertices.CopyTo(buffervertices, 0);
if(General.Map.Graphics.CheckAvailability())
{
FlatVertex[] buffervertices = new FlatVertex[triangles.Vertices.Count];
flatvertices.CopyTo(buffervertices, 0);
// Raise event to allow plugins to modify this data
General.Plugins.OnSectorCeilingSurfaceUpdate(this, ref buffervertices);
// Raise event to allow plugins to modify this data
General.Plugins.OnSectorCeilingSurfaceUpdate(this, ref buffervertices);
// Make the buffer
flatceilingbuffer = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffervertices.Length,
Usage.WriteOnly | Usage.Dynamic, VertexFormat.None, Pool.Default);
// Make the buffer
flatceilingbuffer = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffervertices.Length,
Usage.WriteOnly | Usage.Dynamic, VertexFormat.None, Pool.Default);
// Fill it
DataStream bufferstream = flatceilingbuffer.Lock(0, FlatVertex.Stride * buffervertices.Length, LockFlags.Discard);
bufferstream.WriteRange<FlatVertex>(buffervertices);
flatceilingbuffer.Unlock();
bufferstream.Dispose();
// Fill it
DataStream bufferstream = flatceilingbuffer.Lock(0, FlatVertex.Stride * buffervertices.Length, LockFlags.Discard);
bufferstream.WriteRange<FlatVertex>(buffervertices);
flatceilingbuffer.Unlock();
bufferstream.Dispose();
}
}
}

View file

@ -447,26 +447,9 @@ namespace CodeImp.DoomBuilder.Rendering
// This begins a drawing session
public bool StartRendering(bool clear, Color4 backcolor, Surface target, Surface depthbuffer)
{
// When minimized, do not render anything
if(General.MainWindow.WindowState != FormWindowState.Minimized)
// Check if we can render
if(CheckAvailability())
{
// Test the cooperative level
Result coopresult = device.TestCooperativeLevel();
// Check if device must be reset
if(!coopresult.IsSuccess)
{
// Should we reset?
if(coopresult.Name == "D3DERR_DEVICENOTRESET")
{
// Device is lost and must be reset now
Reset();
}
// Impossible to render at this point
return false;
}
// Set rendertarget
device.DepthStencilSurface = depthbuffer;
device.SetRenderTarget(0, target);
@ -514,6 +497,41 @@ namespace CodeImp.DoomBuilder.Rendering
catch(Exception) { }
}
// This checks if we can use the hardware at this moment
public bool CheckAvailability()
{
// When minimized, the hardware is not available
if(General.MainWindow.WindowState != FormWindowState.Minimized)
{
// Test the cooperative level
Result coopresult = device.TestCooperativeLevel();
// Check if device must be reset
if(!coopresult.IsSuccess)
{
// Should we reset?
if(coopresult.Name == "D3DERR_DEVICENOTRESET")
{
// Device is lost and must be reset now
Reset();
}
// Impossible to render at this point
return false;
}
else
{
// Read to go!
return true;
}
}
else
{
// Minimized
return false;
}
}
#endregion
#region ================== Tools