Fixed a crash when minimizing during resources loading

This commit is contained in:
codeimp 2009-06-16 17:29:00 +00:00
parent 656edc7f13
commit 0a10e5bfef
2 changed files with 104 additions and 54 deletions

View file

@ -342,6 +342,8 @@ namespace CodeImp.DoomBuilder.Map
General.Plugins.OnSectorFloorSurfaceUpdate(this, ref floorvertices); General.Plugins.OnSectorFloorSurfaceUpdate(this, ref floorvertices);
surfaceentry.floorvertices = floorvertices; surfaceentry.floorvertices = floorvertices;
surfaceentry.floortexture = longfloortexname; surfaceentry.floortexture = longfloortexname;
if(surfaceentry.ceilvertices == null)
surfaceentry.ceilvertices = floorvertices;
// Update entry // Update entry
surfaceentry = General.Map.CRenderer2D.Surfaces.UpdateSurfaces(surfaceentry); surfaceentry = General.Map.CRenderer2D.Surfaces.UpdateSurfaces(surfaceentry);
@ -359,6 +361,8 @@ namespace CodeImp.DoomBuilder.Map
General.Plugins.OnSectorCeilingSurfaceUpdate(this, ref ceilvertices); General.Plugins.OnSectorCeilingSurfaceUpdate(this, ref ceilvertices);
surfaceentry.ceilvertices = ceilvertices; surfaceentry.ceilvertices = ceilvertices;
surfaceentry.ceiltexture = longceiltexname; surfaceentry.ceiltexture = longceiltexname;
if(surfaceentry.floorvertices == null)
surfaceentry.floorvertices = ceilvertices;
// Update entry // Update entry
surfaceentry = General.Map.CRenderer2D.Surfaces.UpdateSurfaces(surfaceentry); surfaceentry = General.Map.CRenderer2D.Surfaces.UpdateSurfaces(surfaceentry);

View file

@ -101,8 +101,14 @@ namespace CodeImp.DoomBuilder.Rendering
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets) foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
{ {
// Dispose vertex buffers // Dispose vertex buffers
foreach(VertexBuffer vb in set.Value.buffers) for(int i = 0; i < set.Value.buffers.Count; i++)
vb.Dispose(); {
if(set.Value.buffers[i] != null)
{
set.Value.buffers[i].Dispose();
set.Value.buffers[i] = null;
}
}
} }
sets = null; sets = null;
@ -119,12 +125,19 @@ namespace CodeImp.DoomBuilder.Rendering
resourcesunloaded = true; resourcesunloaded = true;
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets) foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
{ {
foreach(VertexBuffer vb in set.Value.buffers) // Dispose vertex buffers
vb.Dispose(); for(int i = 0; i < set.Value.buffers.Count; i++)
{
set.Value.buffers.Clear(); if(set.Value.buffers[i] != null)
{
set.Value.buffers[i].Dispose();
set.Value.buffers[i] = null;
} }
} }
}
lockedbuffers.Clear();
}
// Called when all resource must be reloaded // Called when all resource must be reloaded
public void ReloadResource() public void ReloadResource()
@ -157,7 +170,7 @@ namespace CodeImp.DoomBuilder.Rendering
bstream.Dispose(); bstream.Dispose();
// Add to list // Add to list
set.Value.buffers.Add(b); set.Value.buffers[i] = b;
} }
} }
@ -235,7 +248,8 @@ namespace CodeImp.DoomBuilder.Rendering
// This ensures there is enough space for a given number of free entries (also adds new bufers if needed) // This ensures there is enough space for a given number of free entries (also adds new bufers if needed)
private void EnsureFreeBufferSpace(SurfaceBufferSet set, int freeentries) private void EnsureFreeBufferSpace(SurfaceBufferSet set, int freeentries)
{ {
DataStream bstream; DataStream bstream = null;
VertexBuffer vb = null;
// Check if we have to add entries // Check if we have to add entries
int addentries = freeentries - set.holes.Count; int addentries = freeentries - set.holes.Count;
@ -260,12 +274,22 @@ namespace CodeImp.DoomBuilder.Rendering
// Calculate the number of vertices that will be // Calculate the number of vertices that will be
int buffernumvertices = bufferentries * verticesperentry; int buffernumvertices = bufferentries * verticesperentry;
if(!resourcesunloaded)
{
// Make the new buffer! // Make the new buffer!
VertexBuffer b = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffernumvertices, vb = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffernumvertices,
Usage.None, VertexFormat.None, Pool.Default); Usage.None, VertexFormat.None, Pool.Default);
// Add it. Also add available entries as holes, because they are not used yet. // Add it.
set.buffers.Add(b); set.buffers.Add(vb);
}
else
{
// We can't make a vertexbuffer right now
set.buffers.Add(null);
}
// Also add available entries as holes, because they are not used yet.
set.buffersizes.Add(buffernumvertices); set.buffersizes.Add(buffernumvertices);
for(int i = 0; i < bufferentries; i++) for(int i = 0; i < bufferentries; i++)
set.holes.Add(new SurfaceEntry(set.numvertices, set.buffers.Count - 1, i * verticesperentry)); set.holes.Add(new SurfaceEntry(set.numvertices, set.buffers.Count - 1, i * verticesperentry));
@ -284,6 +308,8 @@ namespace CodeImp.DoomBuilder.Rendering
bstream.Dispose(); bstream.Dispose();
set.buffers[bufferindex].Tag = null; set.buffers[bufferindex].Tag = null;
} }
if((set.buffers[bufferindex] != null) && !resourcesunloaded)
set.buffers[bufferindex].Dispose(); set.buffers[bufferindex].Dispose();
// Get the entries that are in this buffer only // Get the entries that are in this buffer only
@ -300,18 +326,24 @@ namespace CodeImp.DoomBuilder.Rendering
// Calculate the number of vertices that will be // Calculate the number of vertices that will be
int buffernumvertices = bufferentries * verticesperentry; int buffernumvertices = bufferentries * verticesperentry;
// Make the new buffer! if(!resourcesunloaded)
VertexBuffer b = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffernumvertices, {
// Make the new buffer and lock it
vb = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffernumvertices,
Usage.None, VertexFormat.None, Pool.Default); Usage.None, VertexFormat.None, Pool.Default);
bstream = vb.Lock(0, FlatVertex.Stride * theseentries.Count * verticesperentry, LockFlags.Discard);
}
// Start refilling the buffer with sector geometry // Start refilling the buffer with sector geometry
int vertexoffset = 0; int vertexoffset = 0;
bstream = b.Lock(0, FlatVertex.Stride * theseentries.Count * verticesperentry, LockFlags.Discard);
foreach(SurfaceEntry e in theseentries) foreach(SurfaceEntry e in theseentries)
{
if(!resourcesunloaded)
{ {
// Fill buffer // Fill buffer
bstream.WriteRange(e.floorvertices); bstream.WriteRange(e.floorvertices);
bstream.WriteRange(e.ceilvertices); bstream.WriteRange(e.ceilvertices);
}
// Set the new location in the buffer // Set the new location in the buffer
e.vertexoffset = vertexoffset; e.vertexoffset = vertexoffset;
@ -320,12 +352,20 @@ namespace CodeImp.DoomBuilder.Rendering
vertexoffset += verticesperentry; vertexoffset += verticesperentry;
} }
if(!resourcesunloaded)
{
// Unlock buffer // Unlock buffer
b.Unlock(); vb.Unlock();
bstream.Dispose(); bstream.Dispose();
set.buffers[bufferindex] = vb;
}
else
{
// No vertex buffer at this time, sorry
set.buffers[bufferindex] = null;
}
// Set the new buffer and add available entries as holes, because they are not used yet. // Set the new buffer and add available entries as holes, because they are not used yet.
set.buffers[bufferindex] = b;
set.buffersizes[bufferindex] = buffernumvertices; set.buffersizes[bufferindex] = buffernumvertices;
set.holes.Clear(); set.holes.Clear();
for(int i = 0; i < bufferentries - theseentries.Count; i++) for(int i = 0; i < bufferentries - theseentries.Count; i++)
@ -374,6 +414,8 @@ namespace CodeImp.DoomBuilder.Rendering
entry = nentry; entry = nentry;
} }
if(!resourcesunloaded)
{
// Lock the buffer // Lock the buffer
DataStream bstream; DataStream bstream;
VertexBuffer vb = set.buffers[entry.bufferindex]; VertexBuffer vb = set.buffers[entry.bufferindex];
@ -393,6 +435,7 @@ namespace CodeImp.DoomBuilder.Rendering
bstream.WriteRange(entry.floorvertices); bstream.WriteRange(entry.floorvertices);
bstream.WriteRange(entry.ceilvertices); bstream.WriteRange(entry.ceilvertices);
} }
}
return entry; return entry;
} }
@ -413,6 +456,8 @@ namespace CodeImp.DoomBuilder.Rendering
// This unlocks the locked buffers // This unlocks the locked buffers
public void UnlockBuffers() public void UnlockBuffers()
{
if(!resourcesunloaded)
{ {
foreach(VertexBuffer vb in lockedbuffers) foreach(VertexBuffer vb in lockedbuffers)
{ {
@ -428,6 +473,7 @@ namespace CodeImp.DoomBuilder.Rendering
// Clear list // Clear list
lockedbuffers = new List<VertexBuffer>(); lockedbuffers = new List<VertexBuffer>();
} }
}
// This gets or creates a set for a specific number of vertices // This gets or creates a set for a specific number of vertices
private SurfaceBufferSet GetSet(int numvertices) private SurfaceBufferSet GetSet(int numvertices)