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

View file

@ -101,8 +101,14 @@ namespace CodeImp.DoomBuilder.Rendering
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
{
// Dispose vertex buffers
foreach(VertexBuffer vb in set.Value.buffers)
vb.Dispose();
for(int i = 0; i < set.Value.buffers.Count; i++)
{
if(set.Value.buffers[i] != null)
{
set.Value.buffers[i].Dispose();
set.Value.buffers[i] = null;
}
}
}
sets = null;
@ -119,12 +125,19 @@ namespace CodeImp.DoomBuilder.Rendering
resourcesunloaded = true;
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
{
foreach(VertexBuffer vb in set.Value.buffers)
vb.Dispose();
set.Value.buffers.Clear();
// Dispose vertex buffers
for(int i = 0; i < set.Value.buffers.Count; i++)
{
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
public void ReloadResource()
@ -157,7 +170,7 @@ namespace CodeImp.DoomBuilder.Rendering
bstream.Dispose();
// 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)
private void EnsureFreeBufferSpace(SurfaceBufferSet set, int freeentries)
{
DataStream bstream;
DataStream bstream = null;
VertexBuffer vb = null;
// Check if we have to add entries
int addentries = freeentries - set.holes.Count;
@ -260,12 +274,22 @@ namespace CodeImp.DoomBuilder.Rendering
// Calculate the number of vertices that will be
int buffernumvertices = bufferentries * verticesperentry;
if(!resourcesunloaded)
{
// 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);
// Add it. Also add available entries as holes, because they are not used yet.
set.buffers.Add(b);
// Add it.
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);
for(int i = 0; i < bufferentries; i++)
set.holes.Add(new SurfaceEntry(set.numvertices, set.buffers.Count - 1, i * verticesperentry));
@ -284,6 +308,8 @@ namespace CodeImp.DoomBuilder.Rendering
bstream.Dispose();
set.buffers[bufferindex].Tag = null;
}
if((set.buffers[bufferindex] != null) && !resourcesunloaded)
set.buffers[bufferindex].Dispose();
// 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
int buffernumvertices = bufferentries * verticesperentry;
// Make the new buffer!
VertexBuffer b = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffernumvertices,
if(!resourcesunloaded)
{
// Make the new buffer and lock it
vb = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffernumvertices,
Usage.None, VertexFormat.None, Pool.Default);
bstream = vb.Lock(0, FlatVertex.Stride * theseentries.Count * verticesperentry, LockFlags.Discard);
}
// Start refilling the buffer with sector geometry
int vertexoffset = 0;
bstream = b.Lock(0, FlatVertex.Stride * theseentries.Count * verticesperentry, LockFlags.Discard);
foreach(SurfaceEntry e in theseentries)
{
if(!resourcesunloaded)
{
// Fill buffer
bstream.WriteRange(e.floorvertices);
bstream.WriteRange(e.ceilvertices);
}
// Set the new location in the buffer
e.vertexoffset = vertexoffset;
@ -320,12 +352,20 @@ namespace CodeImp.DoomBuilder.Rendering
vertexoffset += verticesperentry;
}
if(!resourcesunloaded)
{
// Unlock buffer
b.Unlock();
vb.Unlock();
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.buffers[bufferindex] = b;
set.buffersizes[bufferindex] = buffernumvertices;
set.holes.Clear();
for(int i = 0; i < bufferentries - theseentries.Count; i++)
@ -374,6 +414,8 @@ namespace CodeImp.DoomBuilder.Rendering
entry = nentry;
}
if(!resourcesunloaded)
{
// Lock the buffer
DataStream bstream;
VertexBuffer vb = set.buffers[entry.bufferindex];
@ -393,6 +435,7 @@ namespace CodeImp.DoomBuilder.Rendering
bstream.WriteRange(entry.floorvertices);
bstream.WriteRange(entry.ceilvertices);
}
}
return entry;
}
@ -413,6 +456,8 @@ namespace CodeImp.DoomBuilder.Rendering
// This unlocks the locked buffers
public void UnlockBuffers()
{
if(!resourcesunloaded)
{
foreach(VertexBuffer vb in lockedbuffers)
{
@ -428,6 +473,7 @@ namespace CodeImp.DoomBuilder.Rendering
// Clear list
lockedbuffers = new List<VertexBuffer>();
}
}
// This gets or creates a set for a specific number of vertices
private SurfaceBufferSet GetSet(int numvertices)