diff --git a/Vocaluxe/Lib/Draw/CDirect3D.cs b/Vocaluxe/Lib/Draw/CDirect3D.cs index 1c04f6fd..865cad00 100644 --- a/Vocaluxe/Lib/Draw/CDirect3D.cs +++ b/Vocaluxe/Lib/Draw/CDirect3D.cs @@ -37,7 +37,6 @@ namespace Vocaluxe.Lib.Draw class CDirect3D : RenderForm, IDraw { #region private vars - private const int _MaxID = 100000; private readonly CKeys _Keys; private readonly CMouse _Mouse; @@ -53,9 +52,6 @@ namespace Vocaluxe.Lib.Draw private readonly Dictionary _D3DTextures = new Dictionary(); private readonly Queue _TexturesToLoad = new Queue(); - private readonly Queue _IDs = new Queue(_MaxID); - - private readonly Object _MutexTexture = new Object(); private VertexBuffer _VertexBuffer; private IndexBuffer _IndexBuffer; @@ -76,7 +72,6 @@ namespace Vocaluxe.Lib.Draw private readonly Queue _VerticesTextures = new Queue(); private readonly Queue _VerticesRotationMatrices = new Queue(); - private readonly bool _NonPowerOf2TextureSupported; #endregion private vars /// @@ -86,10 +81,6 @@ namespace Vocaluxe.Lib.Draw { Icon = new Icon(Path.Combine(CSettings.ProgramFolder, CSettings.FileNameIcon)); - //Fill Queue with 100000 IDs - for (int i = 0; i < _MaxID; i++) - _IDs.Enqueue(i); - _Keys = new CKeys(); try { @@ -177,10 +168,10 @@ namespace Vocaluxe.Lib.Draw CreateFlags flags = (caps.DeviceCaps & DeviceCaps.HWTransformAndLight) != 0 ? CreateFlags.HardwareVertexProcessing : CreateFlags.SoftwareVertexProcessing; //Check if Pow2 textures are needed - _NonPowerOf2TextureSupported = true; - _NonPowerOf2TextureSupported &= (caps.TextureCaps & TextureCaps.Pow2) == 0; - _NonPowerOf2TextureSupported &= (caps.TextureCaps & TextureCaps.NonPow2Conditional) == 0; - _NonPowerOf2TextureSupported &= (caps.TextureCaps & TextureCaps.SquareOnly) == 0; + CDrawHelper.NonPowerOf2TextureSupported = true; + CDrawHelper.NonPowerOf2TextureSupported &= (caps.TextureCaps & TextureCaps.Pow2) == 0; + CDrawHelper.NonPowerOf2TextureSupported &= (caps.TextureCaps & TextureCaps.NonPow2Conditional) == 0; + CDrawHelper.NonPowerOf2TextureSupported &= (caps.TextureCaps & TextureCaps.SquareOnly) == 0; try { @@ -689,20 +680,19 @@ namespace Vocaluxe.Lib.Draw /// public CTexture CopyScreen() { - CTexture texture = _GetNewTexture(_W, _H); + CTexture texture = CDrawHelper.GetNewTexture(_W, _H); Surface backbufferSurface = _Device.GetBackBuffer(0, 0); var tex = new Texture(_Device, texture.W2, texture.H2, 0, Usage.AutoGenerateMipMap, Format.A8R8G8B8, Pool.Managed); Surface textureSurface = tex.GetSurfaceLevel(0); Surface.FromSurface(textureSurface, backbufferSurface, Filter.Default, 0, new Rectangle(0, 0, _W, _H), new Rectangle(0, 0, _W, _H)); backbufferSurface.Dispose(); - lock (_MutexTexture) + lock (_D3DTextures) { - texture.ID = _IDs.Dequeue(); _D3DTextures.Add(texture.ID, tex); - - return texture; } + + return texture; } /// @@ -711,8 +701,7 @@ namespace Vocaluxe.Lib.Draw /// The texture in which the frame is copied to public void CopyScreen(ref CTexture texture) { - //Check for actual texture sizes! (W2/H2) as it may be up/downsized compared to OrigSize - if (!_TextureExists(texture) || texture.W2 != GetScreenWidth() || texture.H2 != GetScreenHeight()) + if (!_TextureExists(texture) || texture.DataSize.Width != GetScreenWidth() || texture.DataSize.Height != GetScreenHeight()) { RemoveTexture(ref texture); texture = CopyScreen(); @@ -860,15 +849,15 @@ namespace Vocaluxe.Lib.Draw int w = Math.Min(bmp.Width, maxSize); int h = Math.Min(bmp.Height, maxSize); - w = _CheckForNextPowerOf2(w); - h = _CheckForNextPowerOf2(h); - var texture = new CTexture(bmp.Width, bmp.Height, w, h, true); + CTexture texture = CDrawHelper.GetNewTexture(bmp.Width,bmp.Height,w, h); Bitmap bmp2 = null; byte[] data; try { + w = texture.W2; + h = texture.H2; if (bmp.Width != w || bmp.Height != h) { //Create a new Bitmap with the new sizes @@ -881,6 +870,7 @@ namespace Vocaluxe.Lib.Draw g.DrawImage(bmp, new Rectangle(0, 0, bmp2.Width, bmp2.Height)); } bmp = bmp2; + texture.DataSize = new Size(w, h); } //Fill the new Bitmap with the texture data @@ -895,33 +885,32 @@ namespace Vocaluxe.Lib.Draw bmp2.Dispose(); } - return _AddTexture(texture, w, data); + return _AddTexture(texture, data); } public CTexture AddTexture(int w, int h, byte[] data) { - CTexture texture = _GetNewTexture(w, h); - return _AddTexture(texture, w, data); + CTexture texture = CDrawHelper.GetNewTexture(w, h); + return _AddTexture(texture, data); } - private CTexture _AddTexture(CTexture texture, int w, byte[] data) + private CTexture _AddTexture(CTexture texture, byte[] data) { - Texture t = _CreateTexture(texture.W2, texture.H2, w, data); + Texture t = _CreateTexture(texture, data); - lock (_MutexTexture) + lock (_D3DTextures) { - texture.ID = _IDs.Dequeue(); _D3DTextures.Add(texture.ID, t); } return texture; } - private Texture _CreateTexture(int textureW, int textureH, int w, byte[] data) + private Texture _CreateTexture(CTexture texture, byte[] data) { //Create a new texture in the managed pool, which does not need to be recreated on a lost device //because a copy of the texture is hold in the Ram - var t = new Texture(_Device, textureW, textureH, 0, Usage.AutoGenerateMipMap, Format.A8R8G8B8, Pool.Managed); - _WriteDataToTexture(t, w, data); + var t = new Texture(_Device, texture.W2, texture.H2, 0, Usage.AutoGenerateMipMap, Format.A8R8G8B8, Pool.Managed); + _WriteDataToTexture(t, texture.DataSize.Width, data); return t; } @@ -946,22 +935,15 @@ namespace Vocaluxe.Lib.Draw public CTexture EnqueueTexture(int w, int h, byte[] data) { - CTexture texture = _GetNewTexture(w, h); + CTexture texture = CDrawHelper.GetNewTexture(w, h); - lock (_MutexTexture) + lock (_D3DTextures) { - texture.ID = _IDs.Dequeue(); _D3DTextures.Add(texture.ID, null); - var queue = new STextureQueue(texture.ID, texture.W2, texture.H2, w, h, data); - _TexturesToLoad.Enqueue(queue); + _TexturesToLoad.Enqueue(new STextureQueue(texture, data)); } return texture; } - - private CTexture _GetNewTexture(int w, int h) - { - return new CTexture(w, h, _CheckForNextPowerOf2(w), _CheckForNextPowerOf2(h)); - } #endregion adding #region updating @@ -975,10 +957,17 @@ namespace Vocaluxe.Lib.Draw /// True if succeeded public bool UpdateTexture(CTexture texture, int w, int h, byte[] data) { - //Check if texture exists and extend matches - if (!_TextureExists(texture) || texture.UsedWidth != w || texture.UsedHeight != h) + if (!_TextureExists(texture)) return false; - lock (_MutexTexture) + if (texture.DataSize.Width != w || texture.DataSize.Height != h) + { + if (texture.W2 > w || texture.H2 > h) + return false; // Texture memory to small + if (texture.W2 * 0.9 < w || texture.H2 * 0.9 < h) + return false; // Texture memory to big + texture.DataSize = new Size(w, h); + } + lock (_D3DTextures) { _WriteDataToTexture(_D3DTextures[texture.ID], w, data); } @@ -1003,7 +992,7 @@ namespace Vocaluxe.Lib.Draw /// True if the texture exists private bool _TextureExists(CTexture texture) { - lock (_MutexTexture) + lock (_D3DTextures) { return texture != null && _D3DTextures.ContainsKey(texture.ID); } @@ -1015,15 +1004,16 @@ namespace Vocaluxe.Lib.Draw /// The texture to be removed public void RemoveTexture(ref CTexture texture) { - if (_TextureExists(texture)) - { - lock (_MutexTexture) + if(texture==null) return; + lock (_D3DTextures) { - _D3DTextures[texture.ID].Dispose(); - _D3DTextures.Remove(texture.ID); - _IDs.Enqueue(texture.ID); + Texture t; + if (_D3DTextures.TryGetValue(texture.ID, out t)) + { + if(t!=null) t.Dispose(); + _D3DTextures.Remove(texture.ID); + } } - } texture = null; } @@ -1282,17 +1272,16 @@ namespace Vocaluxe.Lib.Draw private void _CheckQueue() { - lock (_MutexTexture) + lock (_D3DTextures) { while (_TexturesToLoad.Count > 0) { STextureQueue q = _TexturesToLoad.Dequeue(); - Texture t; - if (_D3DTextures.TryGetValue(q.ID, out t) && t != null) + if (!_D3DTextures.ContainsKey(q.Texture.ID)) continue; - t = _CreateTexture(q.TextureWidth, q.TextureHeight, q.DataWidth, q.Data); - _D3DTextures[q.ID] = t; + Texture t = _CreateTexture(q.Texture, q.Data); + _D3DTextures[q.Texture.ID] = t; } } } @@ -1310,20 +1299,6 @@ namespace Vocaluxe.Lib.Draw #endregion implementation #region utility - /// - /// Calculates the next power of two if the device has the POW2 flag set - /// - /// The value of which the next power of two will be calculated - /// The next power of two - private int _CheckForNextPowerOf2(int n) - { - if (_NonPowerOf2TextureSupported) - return n; - if (n < 0) - throw new ArgumentOutOfRangeException("n", "Must be positive."); - return (int)Math.Pow(2, Math.Ceiling(Math.Log(n, 2))); - } - private SlimDX.Matrix _CalculateRotationMatrix(float rot, float rx1, float rx2, float ry1, float ry2) { SlimDX.Matrix originTranslation = SlimDX.Matrix.Translation(new Vector3(-(float)CSettings.RenderW / 2, (float)CSettings.RenderH / 2, 0)); diff --git a/Vocaluxe/Lib/Draw/CDrawHelper.cs b/Vocaluxe/Lib/Draw/CDrawHelper.cs new file mode 100644 index 00000000..12aa802d --- /dev/null +++ b/Vocaluxe/Lib/Draw/CDrawHelper.cs @@ -0,0 +1,43 @@ +using System; +using System.Diagnostics; +using System.Drawing; +using VocaluxeLib.Draw; + +namespace Vocaluxe.Lib.Draw +{ + static class CDrawHelper + { + public static bool NonPowerOf2TextureSupported; + + private static readonly Object _MutexID = new object(); + private static int _NextID; + + /// + /// Calculates the next power of two if the device has the POW2 flag set + /// + /// The value of which the next power of two will be calculated + /// The next power of two + private static int _CheckForNextPowerOf2(int n) + { + if (NonPowerOf2TextureSupported) + return n; + if (n < 0) + throw new ArgumentOutOfRangeException("n", "Must be positive."); + return (int)Math.Pow(2, Math.Ceiling(Math.Log(n, 2))); + } + + public static CTexture GetNewTexture(int origWidth, int origHeight, int dataWidth = 0, int dataHeight = 0) + { + Debug.Assert(origWidth > 0 && origHeight > 0); + Debug.Assert(dataWidth > 0 || dataHeight <= 0); + int id; + lock (_MutexID) + { + id = _NextID++; + } + Size origSize = new Size(origWidth, origHeight); + Size dataSize = (dataHeight > 0) ? new Size(dataWidth, dataHeight) : origSize; + return new CTexture(id, origSize, dataSize, _CheckForNextPowerOf2(dataSize.Width), _CheckForNextPowerOf2(dataSize.Height)); + } + } +} \ No newline at end of file diff --git a/Vocaluxe/Lib/Draw/CDrawWinForm.cs b/Vocaluxe/Lib/Draw/CDrawWinForm.cs index 08720117..13f3ad4e 100644 --- a/Vocaluxe/Lib/Draw/CDrawWinForm.cs +++ b/Vocaluxe/Lib/Draw/CDrawWinForm.cs @@ -332,7 +332,7 @@ namespace Vocaluxe.Lib.Draw private CTexture _GetNewTexture(int w, int h) { - return new CTexture(w, h) {ID = _Bitmaps.Count - 1}; + return new CTexture(_Bitmaps.Count - 1, new Size(w, h), new Size(w, h)); } public CTexture AddTexture(string texturePath) diff --git a/Vocaluxe/Lib/Draw/COpenGL.cs b/Vocaluxe/Lib/Draw/COpenGL.cs index 43ba70c2..3e156504 100644 --- a/Vocaluxe/Lib/Draw/COpenGL.cs +++ b/Vocaluxe/Lib/Draw/COpenGL.cs @@ -63,20 +63,12 @@ namespace Vocaluxe.Lib.Draw struct STextureQueue { - public readonly int ID; - public readonly int TextureWidth; - public readonly int TextureHeight; - public readonly int DataWidth; - public readonly int DataHeight; + public readonly CTexture Texture; public readonly byte[] Data; - public STextureQueue(int id, int textureWidth, int textureHeight, int dataWidth, int dataHeight, byte[] data) + public STextureQueue(CTexture texture, byte[] data) { - ID = id; - TextureWidth = textureWidth; - TextureHeight = textureHeight; - DataWidth = dataWidth; - DataHeight = dataHeight; + Texture = texture; Data = data; } } @@ -84,8 +76,6 @@ namespace Vocaluxe.Lib.Draw class COpenGL : Form, IDraw { #region private vars - private const int _MaxID = 100000; - private readonly CKeys _Keys; private readonly CMouse _Mouse; private bool _Run; @@ -96,11 +86,8 @@ namespace Vocaluxe.Lib.Draw private bool _Fullscreen; private readonly Dictionary _Textures = new Dictionary(); - private readonly Queue _IDs = new Queue(_MaxID); private readonly Queue _TexturesToLoad = new Queue(); - private readonly Object _MutexTexture = new Object(); - private int _H = 1; private int _W = 1; private int _Y; @@ -113,9 +100,6 @@ namespace Vocaluxe.Lib.Draw { Icon = new Icon(Path.Combine(CSettings.ProgramFolder, CSettings.FileNameIcon)); - for (int i = 1; i < _MaxID; i++) - _IDs.Enqueue(i); - //Check AA Mode CConfig.AAMode = (EAntiAliasingModes)_CheckAntiAliasingMode((int)CConfig.AAMode); @@ -161,6 +145,8 @@ namespace Vocaluxe.Lib.Draw ClientSize = new Size(CConfig.ScreenW, CConfig.ScreenH); CenterToScreen(); + + CDrawHelper.NonPowerOf2TextureSupported = false; } #region Helpers @@ -490,7 +476,7 @@ namespace Vocaluxe.Lib.Draw public CTexture CopyScreen() { - CTexture texture = _GetNewTexture(_W, _H); + CTexture texture = CDrawHelper.GetNewTexture(_W, _H); texture.Name = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texture.Name); @@ -504,10 +490,8 @@ namespace Vocaluxe.Lib.Draw GL.BindTexture(TextureTarget.Texture2D, 0); // Add to Texture List - - lock (_MutexTexture) + lock (_Textures) { - texture.ID = _IDs.Dequeue(); _Textures[texture.ID] = texture; } @@ -517,7 +501,7 @@ namespace Vocaluxe.Lib.Draw public void CopyScreen(ref CTexture texture) { //Check for actual texture sizes as it may be downsized compared to OrigSize - if (!_TextureExists(texture) || texture.UsedWidth != GetScreenWidth() || texture.UsedHeight != GetScreenHeight()) + if (!_TextureExists(texture) || texture.DataSize.Width != GetScreenWidth() || texture.DataSize.Height != GetScreenHeight()) { RemoveTexture(ref texture); texture = CopyScreen(); @@ -691,10 +675,9 @@ namespace Vocaluxe.Lib.Draw int w = Math.Min(bmp.Width, maxSize); int h = Math.Min(bmp.Height, maxSize); - w = MathHelper.NextPowerOfTwo(w); - h = MathHelper.NextPowerOfTwo(h); - var texture = new CTexture(bmp.Width, bmp.Height, w, h, true) {Name = GL.GenTexture()}; + CTexture texture = CDrawHelper.GetNewTexture(w, h); + texture.Name = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texture.Name); Bitmap bmp2 = null; @@ -736,9 +719,8 @@ namespace Vocaluxe.Lib.Draw // Add to Texture List texture.TexturePath = String.Empty; - lock (_MutexTexture) + lock (_Textures) { - texture.ID = _IDs.Dequeue(); _Textures[texture.ID] = texture; } @@ -747,20 +729,19 @@ namespace Vocaluxe.Lib.Draw public CTexture AddTexture(int w, int h, byte[] data) { - CTexture texture = _GetNewTexture(w, h); + CTexture texture = CDrawHelper.GetNewTexture(w, h); - _CreateTexture(texture, w, h, data); + _CreateTexture(texture, data); - lock (_MutexTexture) + lock (_Textures) { - texture.ID = _IDs.Dequeue(); _Textures[texture.ID] = texture; } return texture; } - private void _CreateTexture(CTexture texture, int w, int h, byte[] data) + private void _CreateTexture(CTexture texture, byte[] data) { if (_UsePBO) { @@ -782,7 +763,7 @@ namespace Vocaluxe.Lib.Draw GL.BindTexture(TextureTarget.Texture2D, texture.Name); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texture.W2, texture.H2, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); - GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, w, h, PixelFormat.Bgra, PixelType.UnsignedByte, data); + GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, texture.OrigSize.Width, texture.OrigSize.Height, PixelFormat.Bgra, PixelType.UnsignedByte, data); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureParameterName.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureParameterName.ClampToEdge); @@ -795,31 +776,32 @@ namespace Vocaluxe.Lib.Draw public CTexture EnqueueTexture(int w, int h, byte[] data) { - CTexture texture = _GetNewTexture(w, h); + CTexture texture = CDrawHelper.GetNewTexture(w, h); - lock (_MutexTexture) + lock (_Textures) { - texture.ID = _IDs.Dequeue(); - var queue = new STextureQueue(texture.ID, texture.W2, texture.H2, w, h, data); + var queue = new STextureQueue(texture, data); _TexturesToLoad.Enqueue(queue); _Textures[texture.ID] = texture; } return texture; } - - private static CTexture _GetNewTexture(int w, int h) - { - return new CTexture(w, h, MathHelper.NextPowerOfTwo(w), MathHelper.NextPowerOfTwo(h)); - } #endregion adding #region updating public bool UpdateTexture(CTexture texture, int w, int h, byte[] data) { - //Check if texture exists and extend matches - if (!_TextureExists(texture) || texture.UsedWidth != w || texture.UsedHeight != h) + if (!_TextureExists(texture)) return false; + if (texture.DataSize.Width != w || texture.DataSize.Height != h) + { + if (texture.W2 > w || texture.H2 > h) + return false; // Texture memory to small + if (texture.W2 * 0.9 < w || texture.H2 * 0.9 < h) + return false; // Texture memory to big + texture.DataSize = new Size(w, h); + } if (_UsePBO) { GL.BindBuffer(BufferTarget.PixelUnpackBuffer, texture.PBO); @@ -865,27 +847,25 @@ namespace Vocaluxe.Lib.Draw public void RemoveTexture(ref CTexture texture) { - if (_TextureExists(texture, false)) + lock (_Textures) { - lock (_MutexTexture) + if (_TextureExists(texture)) { - _IDs.Enqueue(texture.ID); GL.DeleteTexture(texture.Name); if (texture.PBO > 0) GL.DeleteBuffers(1, ref texture.PBO); - _Textures.Remove(texture.ID); } + _Textures.Remove(texture.ID); } texture = null; } - private bool _TextureExists(CTexture texture, bool checkIfLoaded = true) + private bool _TextureExists(CTexture texture) { - lock (_MutexTexture) + lock (_Textures) { - if (texture != null && _Textures.ContainsKey(texture.ID)) + if (texture != null && _Textures.ContainsKey(texture.ID) && texture.Name>0) { - if (!checkIfLoaded || _Textures[texture.ID].Name > 0) return true; } } @@ -1154,16 +1134,15 @@ namespace Vocaluxe.Lib.Draw private void _CheckQueue() { - lock (_MutexTexture) + lock (_Textures) { while (_TexturesToLoad.Count > 0) { STextureQueue q = _TexturesToLoad.Dequeue(); - CTexture texture; - if (!_Textures.TryGetValue(q.ID, out texture)) + if (!_Textures.ContainsKey(q.Texture.ID)) continue; - _CreateTexture(texture, q.DataWidth, q.DataHeight, q.Data); + _CreateTexture(q.Texture, q.Data); } } } diff --git a/Vocaluxe/Vocaluxe.csproj b/Vocaluxe/Vocaluxe.csproj index b8276467..9f5c71a4 100644 --- a/Vocaluxe/Vocaluxe.csproj +++ b/Vocaluxe/Vocaluxe.csproj @@ -224,6 +224,7 @@ + diff --git a/VocaluxeLib/Draw/CTexture.cs b/VocaluxeLib/Draw/CTexture.cs index 76a03818..e228a4a3 100644 --- a/VocaluxeLib/Draw/CTexture.cs +++ b/VocaluxeLib/Draw/CTexture.cs @@ -21,7 +21,7 @@ namespace VocaluxeLib.Draw { public class CTexture { - public int ID = -1; + public readonly int ID; public int PBO; //Only used by OpenGL (the texture "name" according to the specs) public int Name = -1; @@ -29,14 +29,29 @@ namespace VocaluxeLib.Draw public string TexturePath = ""; /// - /// Original size (e.g. of bmp) + /// Size of original image (e.g. of bmp) /// - public readonly Size OrigSize; + public Size OrigSize; + public float OrigAspect { get { return (float)OrigSize.Width / OrigSize.Height; } } + private Size _DataSize; + /// + /// Size of data used (mostly equal to OrigSize but due to resizing this might change) + /// + public Size DataSize + { + get { return _DataSize; } + set + { + _DataSize = value; + _CalcRatios(); + } + } + /// /// Current size when drawn /// @@ -45,7 +60,6 @@ namespace VocaluxeLib.Draw public SColorF Color = new SColorF(1f, 1f, 1f, 1f); private int _W2, _H2; - private readonly bool _UseFullTexture; /// /// Internal texture width (on device), a power of 2 if necessary /// @@ -55,10 +69,7 @@ namespace VocaluxeLib.Draw set { _W2 = value; - if (_UseFullTexture) - WidthRatio = 1; - else - WidthRatio = (float)OrigSize.Width / _W2; + _CalcRatios(); } } /// @@ -70,10 +81,7 @@ namespace VocaluxeLib.Draw set { _H2 = value; - if (_UseFullTexture) - HeightRatio = 1; - else - HeightRatio = (float)OrigSize.Height / _H2; + _CalcRatios(); } } @@ -86,64 +94,31 @@ namespace VocaluxeLib.Draw /// public float HeightRatio { get; private set; } - /// - /// Actual texture space used in x direction - /// - public int UsedWidth - { - get { return (int)(_W2 * WidthRatio); } - } - - /// - /// Actual texture space used in y direction - /// - public int UsedHeight - { - get { return (int)(_H2 * HeightRatio); } - } - - /* - /// - /// Internal use. Specifies if full texture memory should be used - /// Set to true if you resized the original image to the maximum - /// - public bool UseFullTexture - { - get { return _UseFullTexture; } - set - { - _UseFullTexture = value; - if (value) - { - WidthRatio = 1; - HeightRatio = 1; - } - else - { - WidthRatio = (float)OrigSize.Width / _W2; - HeightRatio = (float)OrigSize.Height / _H2; - } - } - } -*/ - /// /// Creates a new texture reference /// - /// Original width (Bitmap size) - /// Original height (Bitmap size) + /// ID of the texture + /// Original size (Bitmap size) + /// Size of the data used /// Width in video memory /// Height in video memory - /// True if it is using the full size of the video memory (no blank parts due to Pow-2-Issue) - public CTexture(int origWidth, int origHeight, int texWidth = 0, int texHeight = 0, bool useFullTexture = false) + public CTexture(int id, Size origSize, Size dataSize, int texWidth = 0, int texHeight = 0) { - OrigSize = new Size(origWidth, origHeight); - Rect = new SRectF(0f, 0f, origWidth, origHeight, 0f); - _UseFullTexture = useFullTexture; + ID = id; + _DataSize = dataSize; + OrigSize = origSize; + _W2 = (texWidth > 0) ? texWidth : dataSize.Width; + _H2 = (texHeight > 0) ? texHeight : dataSize.Height; - //IMPORTANT: Use setter here to calculate ratios! - W2 = (texWidth > 0) ? texWidth : origWidth; - H2 = (texHeight > 0) ? texHeight : origHeight; + Rect = new SRectF(0f, 0f, origSize.Width, origSize.Height, 0f); + + _CalcRatios(); + } + + private void _CalcRatios() + { + WidthRatio = (float)_DataSize.Width / _W2; + HeightRatio = (float)_DataSize.Height / _H2; } } } \ No newline at end of file diff --git a/VocaluxeLib/Menu/CStatic.cs b/VocaluxeLib/Menu/CStatic.cs index 6d4c2e35..4285a25d 100644 --- a/VocaluxeLib/Menu/CStatic.cs +++ b/VocaluxeLib/Menu/CStatic.cs @@ -192,11 +192,10 @@ namespace VocaluxeLib.Menu public void Draw(EAspect aspect, float scale = 1f, float zModify = 0f, bool forceDraw = false) { - CTexture texture = Texture ?? new CTexture((int)Rect.W, (int)Rect.H); - + CTexture texture = Texture; SRectF bounds = Rect.Scale(scale); SRectF rect; - if (aspect != EAspect.Stretch) + if (aspect != EAspect.Stretch && texture != null) CHelper.SetRect(bounds, out rect, texture.OrigAspect, aspect); else rect = bounds; @@ -205,9 +204,14 @@ namespace VocaluxeLib.Menu var color = new SColorF(Color.R, Color.G, Color.B, Color.A * Alpha); if (Visible || forceDraw || (CBase.Settings.GetProgramState() == EProgramState.EditTheme)) { - CBase.Drawing.DrawTexture(texture, rect, color, bounds); - if (Reflection) - CBase.Drawing.DrawTextureReflection(texture, rect, color, bounds, ReflectionSpace, ReflectionHeight); + if (texture != null) + { + CBase.Drawing.DrawTexture(texture, rect, color, bounds); + if (Reflection) + CBase.Drawing.DrawTextureReflection(texture, rect, color, bounds, ReflectionSpace, ReflectionHeight); + } + else + CBase.Drawing.DrawColor(color, rect); } if (Selected && (CBase.Settings.GetProgramState() == EProgramState.EditTheme))