From d2f8af880eef1c3b5c28719996fbd73cffcf6cb8 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Tue, 26 Feb 2013 18:22:18 +0100 Subject: [PATCH] Bugfixes for CDataBase/COpenGL Changed dynamic song loading to not use a thread as it was not threadsafe before --- Vocaluxe/Base/CDataBase.cs | 22 ++++----------- Vocaluxe/Base/CGraphics.cs | 2 +- Vocaluxe/Base/CSongs.cs | 54 ++++++++++++++++++++---------------- Vocaluxe/Lib/Draw/COpenGL.cs | 9 +++++- 4 files changed, 45 insertions(+), 42 deletions(-) diff --git a/Vocaluxe/Base/CDataBase.cs b/Vocaluxe/Base/CDataBase.cs index 6c8ff5f3..0220890c 100644 --- a/Vocaluxe/Base/CDataBase.cs +++ b/Vocaluxe/Base/CDataBase.cs @@ -1011,8 +1011,6 @@ namespace Vocaluxe.Base #region Cover public static bool GetCover(string CoverPath, ref STexture tex, int MaxSize) { - bool result = false; - if (!File.Exists(CoverPath)) { CLog.LogError("Can't find File: " + CoverPath); @@ -1048,10 +1046,11 @@ namespace Vocaluxe.Base if (reader.HasRows) { - result = true; reader.Read(); byte[] data = GetBytes(reader); + reader.Dispose(); tex = CDraw.QuequeTexture(w, h, ref data); + return true; } } else @@ -1120,19 +1119,17 @@ namespace Vocaluxe.Base { reader.Read(); int id = reader.GetInt32(0); + reader.Dispose(); command.CommandText = "INSERT INTO CoverData (CoverID, Data) VALUES (@id, @data)"; command.Parameters.Add("@id", DbType.Int32).Value = id; command.Parameters.Add("@data", System.Data.DbType.Binary, 20).Value = data; command.ExecuteReader(); - result = true; + return true; } } - - if (reader != null) - reader.Dispose(); } - return result; + return false; } public static void CommitCovers() @@ -1284,14 +1281,7 @@ namespace Vocaluxe.Base command.CommandText = "SELECT Data FROM ImageData WHERE ImageID = @id"; command.Parameters.Add("@id",DbType.Int32).Value=id; - try - { - reader = command.ExecuteReader(); - } - catch (Exception) - { - throw; - } + reader = command.ExecuteReader(); if (reader.HasRows) { diff --git a/Vocaluxe/Base/CGraphics.cs b/Vocaluxe/Base/CGraphics.cs index 0d24defd..44ed03e8 100644 --- a/Vocaluxe/Base/CGraphics.cs +++ b/Vocaluxe/Base/CGraphics.cs @@ -324,7 +324,7 @@ namespace Vocaluxe.Base CInput.Update(); if (CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_DYNAMIC && _CurrentScreen != EScreens.ScreenSing) - CSongs.LoadCover(); + CSongs.LoadCover(30, 1); if (CSettings.GameState != EGameState.EditTheme) { diff --git a/Vocaluxe/Base/CSongs.cs b/Vocaluxe/Base/CSongs.cs index 4a3751c5..7ef5a4ba 100644 --- a/Vocaluxe/Base/CSongs.cs +++ b/Vocaluxe/Base/CSongs.cs @@ -43,7 +43,7 @@ namespace Vocaluxe.Base private static bool _SongsLoaded = false; private static bool _CoverLoaded = false; - private static int _CoverLoadIndex = -1; + private static int _CoverLoadIndex = 0; private static int _CatIndex = -1; private static bool _Init = false; private static List _Categories = new List(); @@ -56,8 +56,6 @@ namespace Vocaluxe.Base private static EOffOn _IgnoreArticles = CConfig.IgnoreArticles; private static ESongSorting _SongSorting = CConfig.SongSorting; private static bool _ShowDuetSongs = true; - - private static Thread _CoverLoaderThread = null; public static string SearchFilter { @@ -180,10 +178,10 @@ namespace Vocaluxe.Base if (!SongsLoaded) return -1; - if (_Songs.Count > _CoverLoadIndex + 1) + if (_CoverLoadIndex < _Songs.Count) { - _CoverLoadIndex++; Song = _Songs[_CoverLoadIndex]; + _CoverLoadIndex++; return _CoverLoadIndex; } @@ -191,7 +189,7 @@ namespace Vocaluxe.Base } public static int NumSongsWithCoverLoaded { - get { return _CoverLoadIndex + 1; } + get { return _CoverLoadIndex; } } public static void SetCoverSmall(int SongIndex, STexture Texture) @@ -200,11 +198,7 @@ namespace Vocaluxe.Base return; if (SongIndex < _Songs.Count) - { _Songs[SongIndex].CoverTextureSmall = Texture; - if (SongIndex == _Songs.Count - 1) - _CoverLoaded = true; - } } public static void SetCoverBig(int SongIndex, STexture Texture) @@ -213,11 +207,7 @@ namespace Vocaluxe.Base return; if (SongIndex < _Songs.Count) - { _Songs[SongIndex].CoverTextureBig = Texture; - if (SongIndex == _Songs.Count - 1) - _CoverLoaded = true; - } } @@ -752,32 +742,48 @@ namespace Vocaluxe.Base if (CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_ATSTART) { CLog.StartBenchmark(2, "Load Covers/Notes"); - _LoadCover(); + _LoadCoverAndNotes(); CLog.StopBenchmark(2, "Load Covers/Notes"); } CLog.StopBenchmark(1, "Load Songs "); GC.Collect(); } - public static void LoadCover() + public static void LoadCover(int WaitTime, int NumLoads) { if (CConfig.Renderer == ERenderer.TR_CONFIG_SOFTWARE) - return; + return; //should be removed as soon as the other renderer are ready for queque if (!SongsLoaded || CoverLoaded) return; - if (_CoverLoaderThread == null) + if (!_CoverLoadTimer.IsRunning) { - _CoverLoaderThread = new Thread(new ThreadStart(_LoadCover)); - _CoverLoaderThread.Name = "CoverLoader"; - _CoverLoaderThread.Priority = ThreadPriority.BelowNormal; - _CoverLoaderThread.IsBackground = true; - _CoverLoaderThread.Start(); + _CoverLoadTimer.Reset(); + _CoverLoadTimer.Start(); + } + + if (_CoverLoadTimer.ElapsedMilliseconds >= WaitTime) + { + for (int i = 0; i < NumLoads; i++) + { + CSong song = null; + int n = GetNextSongWithoutCover(ref song); + + if (n < 0) + return; + + song.LoadSmallCover(); + + if (n == NumAllSongs) + CDataBase.CommitCovers(); + } + _CoverLoadTimer.Reset(); + _CoverLoadTimer.Start(); } } - private static void _LoadCover() + private static void _LoadCoverAndNotes() { foreach(CSong song in _Songs) { diff --git a/Vocaluxe/Lib/Draw/COpenGL.cs b/Vocaluxe/Lib/Draw/COpenGL.cs index 8300f10b..3ccfd7a9 100644 --- a/Vocaluxe/Lib/Draw/COpenGL.cs +++ b/Vocaluxe/Lib/Draw/COpenGL.cs @@ -733,7 +733,14 @@ namespace Vocaluxe.Lib.Draw CLog.LogError("Error loading Texture: " + TexturePath); return new STexture(-1); } - return AddTexture(bmp, TexturePath); + try + { + return AddTexture(bmp, TexturePath); + } + finally + { + bmp.Dispose(); + } } CLog.LogError("Can't find File: " + TexturePath); return new STexture(-1);