From e43f5e7b0a77281f5b5b02f86fb6469cec46bd6e Mon Sep 17 00:00:00 2001 From: Flamefire Date: Mon, 6 May 2013 20:18:17 +0200 Subject: [PATCH] Changed usage of CTexture.Index: You only need to check for null as of last commit --- Vocaluxe/Base/CProfiles.cs | 2 +- Vocaluxe/Lib/Draw/CDirect3D.cs | 9 +--- Vocaluxe/Lib/Draw/CDrawWinForm.cs | 54 ++++++++----------- Vocaluxe/Lib/Draw/COpenGL.cs | 6 +-- VocaluxeLib/Draw/CTexture.cs | 3 +- VocaluxeLib/Menu/CButton.cs | 6 +-- VocaluxeLib/Menu/SongMenu/CCategory.cs | 2 +- .../Menu/SongMenu/CSongMenuTileBoard.cs | 6 +-- 8 files changed, 37 insertions(+), 51 deletions(-) diff --git a/Vocaluxe/Base/CProfiles.cs b/Vocaluxe/Base/CProfiles.cs index 12320bd2..aa45b4d2 100644 --- a/Vocaluxe/Base/CProfiles.cs +++ b/Vocaluxe/Base/CProfiles.cs @@ -130,7 +130,7 @@ namespace Vocaluxe.Base { CTexture tex = CDraw.AddTexture(file); - if (tex.Index != -1) + if (tex != null) { SAvatar avatar = new SAvatar {Texture = tex, FileName = Path.GetFileName(file)}; _Avatars.Add(avatar); diff --git a/Vocaluxe/Lib/Draw/CDirect3D.cs b/Vocaluxe/Lib/Draw/CDirect3D.cs index 6d8e5df2..d6c455b8 100644 --- a/Vocaluxe/Lib/Draw/CDirect3D.cs +++ b/Vocaluxe/Lib/Draw/CDirect3D.cs @@ -1002,13 +1002,8 @@ namespace Vocaluxe.Lib.Draw { lock (_MutexTexture) { - if (texture != null && _Textures.ContainsKey(texture.Index)) - { - if (_Textures[texture.Index].ID > 0) - return true; - } + return texture != null && _Textures.ContainsKey(texture.Index); } - return false; } /// @@ -1017,7 +1012,7 @@ namespace Vocaluxe.Lib.Draw /// The texture to be removed public void RemoveTexture(ref CTexture texture) { - if (texture != null && texture.Index >= 0) + if (_TextureExists(texture)) { lock (_MutexTexture) { diff --git a/Vocaluxe/Lib/Draw/CDrawWinForm.cs b/Vocaluxe/Lib/Draw/CDrawWinForm.cs index 065170d3..b13a52c6 100644 --- a/Vocaluxe/Lib/Draw/CDrawWinForm.cs +++ b/Vocaluxe/Lib/Draw/CDrawWinForm.cs @@ -46,8 +46,8 @@ namespace Vocaluxe.Lib.Draw private bool _TopMost; private Rectangle _Bounds; - private readonly List _Textures; - private readonly List _Bitmaps; + private readonly Dictionary _Textures = new Dictionary(); + private readonly List _Bitmaps = new List(); private readonly Color _ClearColor = Color.DarkBlue; @@ -55,9 +55,6 @@ namespace Vocaluxe.Lib.Draw { Icon = new Icon(Path.Combine(Environment.CurrentDirectory, CSettings.Icon)); - _Textures = new List(); - _Bitmaps = new List(); - _Keys = new CKeys(); _Mouse = new CMouse(); ClientSize = new Size(1280, 720); @@ -297,14 +294,14 @@ namespace Vocaluxe.Lib.Draw CTexture texture = new CTexture(_Bitmaps.Count - 1, bmp.Width, bmp.Height); // Add to Texture List - _Textures.Add(texture); + _Textures[texture.Index] = texture; return texture; } public void CopyScreen(ref CTexture texture) { - if (texture.Index == 0 || texture.W2 != GetScreenWidth() || texture.H2 != GetScreenHeight()) + if (!_TextureExists(texture) || texture.W2 != GetScreenWidth() || texture.H2 != GetScreenHeight()) { RemoveTexture(ref texture); texture = CopyScreen(); @@ -341,9 +338,7 @@ namespace Vocaluxe.Lib.Draw CTexture texture = new CTexture(_Bitmaps.Count - 1, bmp.Width, bmp.Height); // Add to Texture List - texture.TexturePath = String.Empty; - - _Textures.Add(texture); + _Textures[texture.Index] = texture; return texture; } @@ -352,7 +347,7 @@ namespace Vocaluxe.Lib.Draw { if (File.Exists(texturePath)) { - foreach (CTexture tex in _Textures) + foreach (CTexture tex in _Textures.Values) { if (tex.TexturePath == texturePath) return tex; @@ -367,19 +362,17 @@ namespace Vocaluxe.Lib.Draw return null; } + private bool _TextureExists(CTexture texture) + { + return texture != null && _Textures.ContainsKey(texture.Index); + } + public void RemoveTexture(ref CTexture texture) { - if (texture != null && texture.Index >= 0) + if (_TextureExists(texture)) { - for (int i = 0; i < _Textures.Count; i++) - { - if (_Textures[i].Index == texture.Index) - { - _Bitmaps[texture.Index].Dispose(); - _Textures.RemoveAt(i); - break; - } - } + _Bitmaps[texture.Index].Dispose(); + _Textures.Remove(texture.Index); } texture = null; } @@ -402,7 +395,7 @@ namespace Vocaluxe.Lib.Draw public bool UpdateTexture(CTexture texture, byte[] data) { - if ((texture.Index >= 0) && (_Textures.Count > 0) && (_Bitmaps.Count > texture.Index)) + if (_TextureExists(texture)) { BitmapData bmpData = _Bitmaps[texture.Index].LockBits(new Rectangle(0, 0, _Bitmaps[texture.Index].Width, _Bitmaps[texture.Index].Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); @@ -415,30 +408,29 @@ namespace Vocaluxe.Lib.Draw public void DrawTexture(CTexture texture) { - if ((texture.Index >= 0) && (_Textures.Count > 0) && (_Bitmaps.Count > texture.Index)) + if (texture != null) DrawTexture(texture, texture.Rect, texture.Color); } public void DrawTexture(CTexture texture, SRectF rect) { - if ((texture.Index >= 0) && (_Textures.Count > 0) && (_Bitmaps.Count > texture.Index)) + if (texture != null) DrawTexture(texture, rect, texture.Color); } public void DrawTexture(CTexture texture, SRectF rect, SColorF color, bool mirrored = false) { - if ((texture.Index >= 0) && (_Textures.Count > 0) && (_Bitmaps.Count > texture.Index)) + if (texture != null) DrawTexture(texture, rect, color, rect, mirrored); } public void DrawTexture(CTexture texture, SRectF rect, SColorF color, SRectF bounds, bool mirrored = false) { - if ((texture.Index >= 0) && (_Textures.Count > 0) && (_Bitmaps.Count > texture.Index)) - { - Bitmap coloredBitmap = _ColorizeBitmap(_Bitmaps[texture.Index], color); - _G.DrawImage(coloredBitmap, new RectangleF(rect.X, rect.Y, rect.W, rect.H)); - coloredBitmap.Dispose(); - } + if (!_TextureExists(texture)) + return; + Bitmap coloredBitmap = _ColorizeBitmap(_Bitmaps[texture.Index], color); + _G.DrawImage(coloredBitmap, new RectangleF(rect.X, rect.Y, rect.W, rect.H)); + coloredBitmap.Dispose(); } public void DrawTexture(CTexture texture, SRectF rect, SColorF color, float begin, float end) {} diff --git a/Vocaluxe/Lib/Draw/COpenGL.cs b/Vocaluxe/Lib/Draw/COpenGL.cs index f3aa39b4..c404b037 100644 --- a/Vocaluxe/Lib/Draw/COpenGL.cs +++ b/Vocaluxe/Lib/Draw/COpenGL.cs @@ -883,7 +883,7 @@ namespace Vocaluxe.Lib.Draw public void RemoveTexture(ref CTexture texture) { - if (texture != null && texture.Index > 0) + if (_TextureExists(texture, false)) { lock (_MutexTexture) { @@ -897,13 +897,13 @@ namespace Vocaluxe.Lib.Draw texture = null; } - private bool _TextureExists(CTexture texture) + private bool _TextureExists(CTexture texture, bool checkIfLoaded = true) { lock (_MutexTexture) { if (texture != null && _Textures.ContainsKey(texture.Index)) { - if (_Textures[texture.Index].ID > 0) + if (!checkIfLoaded || _Textures[texture.Index].ID > 0) return true; } } diff --git a/VocaluxeLib/Draw/CTexture.cs b/VocaluxeLib/Draw/CTexture.cs index a6c48222..c0604f3e 100644 --- a/VocaluxeLib/Draw/CTexture.cs +++ b/VocaluxeLib/Draw/CTexture.cs @@ -1,5 +1,4 @@ -using System; -using System.Drawing; +using System.Drawing; using VocaluxeLib.Menu; namespace VocaluxeLib.Draw diff --git a/VocaluxeLib/Menu/CButton.cs b/VocaluxeLib/Menu/CButton.cs index 3507e0fa..34fa5fa7 100644 --- a/VocaluxeLib/Menu/CButton.cs +++ b/VocaluxeLib/Menu/CButton.cs @@ -285,7 +285,7 @@ namespace VocaluxeLib.Menu if (!Selected && !Pressed || !Enabled) { - texture = Texture.Index != -1 ? Texture : CBase.Theme.GetSkinTexture(_Theme.TextureName, _PartyModeID); + texture = Texture ?? CBase.Theme.GetSkinTexture(_Theme.TextureName, _PartyModeID); CBase.Drawing.DrawTexture(texture, Rect, Color); @@ -299,7 +299,7 @@ namespace VocaluxeLib.Menu } else if (!_IsSelText) { - texture = Texture.Index != -1 ? Texture : CBase.Theme.GetSkinTexture(_Theme.SelTextureName, _PartyModeID); + texture = Texture ?? CBase.Theme.GetSkinTexture(_Theme.SelTextureName, _PartyModeID); CBase.Drawing.DrawTexture(texture, Rect, SelColor); @@ -313,7 +313,7 @@ namespace VocaluxeLib.Menu } else if (_IsSelText) { - texture = SelTexture.Index != -1 ? SelTexture : CBase.Theme.GetSkinTexture(_Theme.SelTextureName, _PartyModeID); + texture = SelTexture ?? CBase.Theme.GetSkinTexture(_Theme.SelTextureName, _PartyModeID); CBase.Drawing.DrawTexture(texture, Rect, SelColor); diff --git a/VocaluxeLib/Menu/SongMenu/CCategory.cs b/VocaluxeLib/Menu/SongMenu/CCategory.cs index f107481f..d94848f0 100644 --- a/VocaluxeLib/Menu/SongMenu/CCategory.cs +++ b/VocaluxeLib/Menu/SongMenu/CCategory.cs @@ -46,7 +46,7 @@ namespace VocaluxeLib.Menu.SongMenu get { return _CoverTextureBig ?? _CoverTextureSmall; } set { - if (value.Index == -1) + if (value == null) return; _CoverTextureBig = value; } diff --git a/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs b/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs index 4cfa78f9..291a8c5b 100644 --- a/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs +++ b/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs @@ -323,7 +323,7 @@ namespace VocaluxeLib.Menu.SongMenu { foreach (CStatic tile in _Tiles) { - if ((tile.Texture.Index != _CoverTexture.Index) && CHelper.IsInBounds(tile.Rect, mouseEvent) && !sel) + if (tile.Texture != _CoverTexture && CHelper.IsInBounds(tile.Rect, mouseEvent) && !sel) { if (mouseEvent.LB || !CBase.Songs.IsInCategory()) { @@ -371,7 +371,7 @@ namespace VocaluxeLib.Menu.SongMenu { foreach (CStatic tile in _Tiles) { - if ((tile.Texture.Index != _CoverTexture.Index) && CHelper.IsInBounds(tile.Rect, mouseEvent)) + if (tile.Texture != _CoverTexture && CHelper.IsInBounds(tile.Rect, mouseEvent)) { _EnterCategory(_PreviewSelected); mouseEvent.Handled = true; @@ -394,7 +394,7 @@ namespace VocaluxeLib.Menu.SongMenu else { // ReSharper disable ConvertIfStatementToConditionalTernaryExpression - if (tile.Texture.Index != _CoverTexture.Index) + if (tile.Texture != _CoverTexture) // ReSharper restore ConvertIfStatementToConditionalTernaryExpression tile.Draw(1f, tile.Rect.Z, EAspect.Crop); else