From 714d8f1d85c7b10d53e1e9d8a6bae648f16b81e1 Mon Sep 17 00:00:00 2001 From: Benjamin Hess Date: Sat, 17 Mar 2012 15:08:38 +0100 Subject: [PATCH 1/2] OpenGl texture list changed to dictionary for O(1) checks if texture exists --- Vocaluxe/Lib/Draw/COpenGL.cs | 81 ++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 46 deletions(-) diff --git a/Vocaluxe/Lib/Draw/COpenGL.cs b/Vocaluxe/Lib/Draw/COpenGL.cs index f12be76d..558bf654 100644 --- a/Vocaluxe/Lib/Draw/COpenGL.cs +++ b/Vocaluxe/Lib/Draw/COpenGL.cs @@ -46,7 +46,7 @@ namespace Vocaluxe.Lib.Draw private SClientRect _restore; private bool _fullscreen = false; - private List _Textures; + private Dictionary _Textures; private Queue _IDs; private List _Queque; @@ -65,7 +65,7 @@ namespace Vocaluxe.Lib.Draw { this.Icon = new System.Drawing.Icon(Path.Combine(System.Environment.CurrentDirectory, CSettings.sIcon)); - _Textures = new List(); + _Textures = new Dictionary(); _Queque = new List(); _IDs = new Queue(1000000); @@ -448,7 +448,9 @@ namespace Vocaluxe.Lib.Draw CSettings.bFullScreen = true; EnterFullScreen(); } - + + double draw_time = 0; + UInt64 draws = 0; while (_Run) { Application.DoEvents(); @@ -456,12 +458,14 @@ namespace Vocaluxe.Lib.Draw if (_Run) { ClearScreen(); - + double start = CTime.GetMilliseconds(); _Run = _Run && CGraphics.Draw(); + draw_time += CTime.GetMilliseconds() - start; + draws += 1; _Run = CGraphics.UpdateGameLogic(_Keys, _Mouse); control.SwapBuffers(); - + if ((CSettings.bFullScreen && !_fullscreen) || (!CSettings.bFullScreen && _fullscreen)) ToggleFullScreen(); @@ -469,11 +473,12 @@ namespace Vocaluxe.Lib.Draw if (CTime.IsRunning()) delay = (int)Math.Floor(CConfig.CalcCycleTime() - CTime.GetMilliseconds()); - + if (delay >= 1) System.Threading.Thread.Sleep(delay); CTime.CalculateFPS(); + CLog.LogError("drawTime:" + (draw_time / draws).ToString() + " " + draws.ToString()); CTime.Restart(); } else @@ -483,11 +488,13 @@ namespace Vocaluxe.Lib.Draw 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 tex); + RemoveTexture(ref textures[i]); } + return true; } @@ -554,7 +561,7 @@ namespace Vocaluxe.Lib.Draw lock (MutexTexture) { texture.index = _IDs.Dequeue(); - _Textures.Add(texture); + _Textures[texture.index] = texture; } return texture; @@ -758,7 +765,7 @@ namespace Vocaluxe.Lib.Draw lock (MutexTexture) { texture.index = _IDs.Dequeue(); - _Textures.Add(texture); + _Textures[texture.index] = texture; } return texture; @@ -822,7 +829,7 @@ namespace Vocaluxe.Lib.Draw lock (MutexTexture) { texture.index = _IDs.Dequeue(); - _Textures.Add(texture); + _Textures[texture.index] = texture; } return texture; @@ -887,7 +894,7 @@ namespace Vocaluxe.Lib.Draw lock (MutexTexture) { texture.index = _IDs.Dequeue(); - _Textures.Add(texture); + _Textures[texture.index] = texture; } return texture; @@ -907,7 +914,7 @@ namespace Vocaluxe.Lib.Draw texture.index = _IDs.Dequeue(); queque.ID = texture.index; _Queque.Add(queque); - _Textures.Add(texture); + _Textures[texture.index] = texture; } return texture; @@ -1027,21 +1034,13 @@ namespace Vocaluxe.Lib.Draw { lock (MutexTexture) { - for (int i = 0; i < _Textures.Count; i++) - { - if (_Textures[i].index == Texture.index) - { - _IDs.Enqueue(Texture.index); - - GL.DeleteTexture(Texture.ID); - if (Texture.PBO > 0) - GL.DeleteBuffers(1, ref Texture.PBO); - _Textures.RemoveAt(i); - Texture.index = -1; - Texture.ID = -1; - break; - } - } + _IDs.Enqueue(Texture.index); + GL.DeleteTexture(Texture.ID); + if (Texture.PBO > 0) + GL.DeleteBuffers(1, ref Texture.PBO); + _Textures.Remove(Texture.index); + Texture.index = -1; + Texture.ID = -1; } } } @@ -1050,16 +1049,12 @@ namespace Vocaluxe.Lib.Draw { 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[i]; - return true; - } - break; + Texture = _Textures[Texture.index]; + return true; } } } @@ -1338,15 +1333,9 @@ namespace Vocaluxe.Lib.Draw STextureQueque q = _Queque[0]; STexture texture = new STexture(-1); - int index = -1; - for (int i = 0; i < _Textures.Count; i++) + if (_Textures.ContainsKey(q.ID)) { - if (_Textures[i].index == q.ID) - { - texture = _Textures[i]; - index = i; - break; - } + texture = _Textures[q.ID]; } if (texture.index < 1) @@ -1396,7 +1385,7 @@ namespace Vocaluxe.Lib.Draw texture.rect = new SRectF(0f, 0f, texture.width, texture.height, 0f); texture.TexturePath = String.Empty; - _Textures[index] = texture; + _Textures[texture.index] = texture; q.data = null; _Queque.RemoveAt(0); } From ac5b57770ec7076b03b1401a8e726a0abfff70eb Mon Sep 17 00:00:00 2001 From: Benjamin Hess Date: Sat, 17 Mar 2012 15:20:45 +0100 Subject: [PATCH 2/2] removed debug output from last commit --- Vocaluxe/Lib/Draw/COpenGL.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Vocaluxe/Lib/Draw/COpenGL.cs b/Vocaluxe/Lib/Draw/COpenGL.cs index 558bf654..49d99366 100644 --- a/Vocaluxe/Lib/Draw/COpenGL.cs +++ b/Vocaluxe/Lib/Draw/COpenGL.cs @@ -449,8 +449,6 @@ namespace Vocaluxe.Lib.Draw EnterFullScreen(); } - double draw_time = 0; - UInt64 draws = 0; while (_Run) { Application.DoEvents(); @@ -458,10 +456,7 @@ namespace Vocaluxe.Lib.Draw if (_Run) { ClearScreen(); - double start = CTime.GetMilliseconds(); _Run = _Run && CGraphics.Draw(); - draw_time += CTime.GetMilliseconds() - start; - draws += 1; _Run = CGraphics.UpdateGameLogic(_Keys, _Mouse); control.SwapBuffers(); @@ -478,7 +473,6 @@ namespace Vocaluxe.Lib.Draw System.Threading.Thread.Sleep(delay); CTime.CalculateFPS(); - CLog.LogError("drawTime:" + (draw_time / draws).ToString() + " " + draws.ToString()); CTime.Restart(); } else