OpenGl texture list changed to dictionary for O(1) checks if texture exists

This commit is contained in:
Benjamin Hess committed 2012-03-17 15:08:38 +01:00
1 parent 18940c7ed9
commit 714d8f1d85
1 file changed
+35 -46
+35 -46
View File
@@ -46,7 +46,7 @@ namespace Vocaluxe.Lib.Draw
private SClientRect _restore; private SClientRect _restore;
private bool _fullscreen = false; private bool _fullscreen = false;
private List<STexture> _Textures; private Dictionary<int, STexture> _Textures;
private Queue<int> _IDs; private Queue<int> _IDs;
private List<STextureQueque> _Queque; private List<STextureQueque> _Queque;
@@ -65,7 +65,7 @@ namespace Vocaluxe.Lib.Draw
{ {
this.Icon = new System.Drawing.Icon(Path.Combine(System.Environment.CurrentDirectory, CSettings.sIcon)); this.Icon = new System.Drawing.Icon(Path.Combine(System.Environment.CurrentDirectory, CSettings.sIcon));
_Textures = new List<STexture>(); _Textures = new Dictionary<int, STexture>();
_Queque = new List<STextureQueque>(); _Queque = new List<STextureQueque>();
_IDs = new Queue<int>(1000000); _IDs = new Queue<int>(1000000);
@@ -448,7 +448,9 @@ namespace Vocaluxe.Lib.Draw
CSettings.bFullScreen = true; CSettings.bFullScreen = true;
EnterFullScreen(); EnterFullScreen();
} }
double draw_time = 0;
UInt64 draws = 0;
while (_Run) while (_Run)
{ {
Application.DoEvents(); Application.DoEvents();
@@ -456,12 +458,14 @@ namespace Vocaluxe.Lib.Draw
if (_Run) if (_Run)
{ {
ClearScreen(); ClearScreen();
double start = CTime.GetMilliseconds();
_Run = _Run && CGraphics.Draw(); _Run = _Run && CGraphics.Draw();
draw_time += CTime.GetMilliseconds() - start;
draws += 1;
_Run = CGraphics.UpdateGameLogic(_Keys, _Mouse); _Run = CGraphics.UpdateGameLogic(_Keys, _Mouse);
control.SwapBuffers(); control.SwapBuffers();
if ((CSettings.bFullScreen && !_fullscreen) || (!CSettings.bFullScreen && _fullscreen)) if ((CSettings.bFullScreen && !_fullscreen) || (!CSettings.bFullScreen && _fullscreen))
ToggleFullScreen(); ToggleFullScreen();
@@ -469,11 +473,12 @@ namespace Vocaluxe.Lib.Draw
if (CTime.IsRunning()) if (CTime.IsRunning())
delay = (int)Math.Floor(CConfig.CalcCycleTime() - CTime.GetMilliseconds()); delay = (int)Math.Floor(CConfig.CalcCycleTime() - CTime.GetMilliseconds());
if (delay >= 1) if (delay >= 1)
System.Threading.Thread.Sleep(delay); System.Threading.Thread.Sleep(delay);
CTime.CalculateFPS(); CTime.CalculateFPS();
CLog.LogError("drawTime:" + (draw_time / draws).ToString() + " " + draws.ToString());
CTime.Restart(); CTime.Restart();
} }
else else
@@ -483,11 +488,13 @@ namespace Vocaluxe.Lib.Draw
public bool Unload() public bool Unload()
{ {
while (_Textures.Count > 0) STexture[] textures = new STexture[_Textures.Count];
_Textures.Values.CopyTo(textures, 0);
for (int i = 0; i < _Textures.Count; i++ )
{ {
STexture tex = _Textures[0]; RemoveTexture(ref textures[i]);
RemoveTexture(ref tex);
} }
return true; return true;
} }
@@ -554,7 +561,7 @@ namespace Vocaluxe.Lib.Draw
lock (MutexTexture) lock (MutexTexture)
{ {
texture.index = _IDs.Dequeue(); texture.index = _IDs.Dequeue();
_Textures.Add(texture); _Textures[texture.index] = texture;
} }
return texture; return texture;
@@ -758,7 +765,7 @@ namespace Vocaluxe.Lib.Draw
lock (MutexTexture) lock (MutexTexture)
{ {
texture.index = _IDs.Dequeue(); texture.index = _IDs.Dequeue();
_Textures.Add(texture); _Textures[texture.index] = texture;
} }
return texture; return texture;
@@ -822,7 +829,7 @@ namespace Vocaluxe.Lib.Draw
lock (MutexTexture) lock (MutexTexture)
{ {
texture.index = _IDs.Dequeue(); texture.index = _IDs.Dequeue();
_Textures.Add(texture); _Textures[texture.index] = texture;
} }
return texture; return texture;
@@ -887,7 +894,7 @@ namespace Vocaluxe.Lib.Draw
lock (MutexTexture) lock (MutexTexture)
{ {
texture.index = _IDs.Dequeue(); texture.index = _IDs.Dequeue();
_Textures.Add(texture); _Textures[texture.index] = texture;
} }
return texture; return texture;
@@ -907,7 +914,7 @@ namespace Vocaluxe.Lib.Draw
texture.index = _IDs.Dequeue(); texture.index = _IDs.Dequeue();
queque.ID = texture.index; queque.ID = texture.index;
_Queque.Add(queque); _Queque.Add(queque);
_Textures.Add(texture); _Textures[texture.index] = texture;
} }
return texture; return texture;
@@ -1027,21 +1034,13 @@ namespace Vocaluxe.Lib.Draw
{ {
lock (MutexTexture) lock (MutexTexture)
{ {
for (int i = 0; i < _Textures.Count; i++) _IDs.Enqueue(Texture.index);
{ GL.DeleteTexture(Texture.ID);
if (_Textures[i].index == Texture.index) if (Texture.PBO > 0)
{ GL.DeleteBuffers(1, ref Texture.PBO);
_IDs.Enqueue(Texture.index); _Textures.Remove(Texture.index);
Texture.index = -1;
GL.DeleteTexture(Texture.ID); Texture.ID = -1;
if (Texture.PBO > 0)
GL.DeleteBuffers(1, ref Texture.PBO);
_Textures.RemoveAt(i);
Texture.index = -1;
Texture.ID = -1;
break;
}
}
} }
} }
} }
@@ -1050,16 +1049,12 @@ namespace Vocaluxe.Lib.Draw
{ {
lock (MutexTexture) lock (MutexTexture)
{ {
for (int i = 0; i < _Textures.Count; i++) if(_Textures.ContainsKey((Texture.index)))
{ {
if (Texture.index == _Textures[i].index) if(_Textures[Texture.index].ID > 0)
{ {
if (_Textures[i].ID > 0) Texture = _Textures[Texture.index];
{ return true;
Texture = _Textures[i];
return true;
}
break;
} }
} }
} }
@@ -1338,15 +1333,9 @@ namespace Vocaluxe.Lib.Draw
STextureQueque q = _Queque[0]; STextureQueque q = _Queque[0];
STexture texture = new STexture(-1); STexture texture = new STexture(-1);
int index = -1; if (_Textures.ContainsKey(q.ID))
for (int i = 0; i < _Textures.Count; i++)
{ {
if (_Textures[i].index == q.ID) texture = _Textures[q.ID];
{
texture = _Textures[i];
index = i;
break;
}
} }
if (texture.index < 1) if (texture.index < 1)
@@ -1396,7 +1385,7 @@ namespace Vocaluxe.Lib.Draw
texture.rect = new SRectF(0f, 0f, texture.width, texture.height, 0f); texture.rect = new SRectF(0f, 0f, texture.width, texture.height, 0f);
texture.TexturePath = String.Empty; texture.TexturePath = String.Empty;
_Textures[index] = texture; _Textures[texture.index] = texture;
q.data = null; q.data = null;
_Queque.RemoveAt(0); _Queque.RemoveAt(0);
} }