diff --git a/Vocaluxe/Lib/Draw/CDirect3D.cs b/Vocaluxe/Lib/Draw/CDirect3D.cs index 3a8989e1..9ed511ae 100644 --- a/Vocaluxe/Lib/Draw/CDirect3D.cs +++ b/Vocaluxe/Lib/Draw/CDirect3D.cs @@ -237,8 +237,6 @@ namespace Vocaluxe.Lib.Draw // ReSharper restore RedundantOverridenMember #endregion resize - #region implementation - #region main stuff /// /// Inits the Device @@ -577,25 +575,18 @@ namespace Vocaluxe.Lib.Draw #endregion Basic Draw Methods #region Textures - private CD3DTexture _CreateTexture(Size dataSize) - { - return new CD3DTexture(_Device, dataSize, _CheckForNextPowerOf2(dataSize.Width), _CheckForNextPowerOf2(dataSize.Height)); - } - - protected override CD3DTexture _CreateTexture(Size dataSize, byte[] data) + protected override CD3DTexture _CreateTexture(Size dataSize) { //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 - CD3DTexture t = _CreateTexture(dataSize); - _WriteDataToTexture(t.D3DTexture, dataSize.Width, data); - return t; + return new CD3DTexture(_Device, dataSize, _CheckForNextPowerOf2(dataSize.Width), _CheckForNextPowerOf2(dataSize.Height)); } - private static void _WriteDataToTexture(Texture t, int w, byte[] data) + protected override void _WriteDataToTexture(CD3DTexture texture, byte[] data) { //Lock the texture and fill it with the data - DataRectangle rect = t.LockRectangle(0, LockFlags.Discard); - int rowWidth = 4 * w; + DataRectangle rect = texture.D3DTexture.LockRectangle(0, LockFlags.Discard); + int rowWidth = 4 * texture.DataSize.Width; if (rowWidth == rect.Pitch) rect.Data.Write(data, 0, data.Length); else @@ -607,7 +598,7 @@ namespace Vocaluxe.Lib.Draw rect.Data.Position = rect.Data.Position - rowWidth + rect.Pitch; } } - t.UnlockRectangle(0); + texture.D3DTexture.UnlockRectangle(0); } /// @@ -631,34 +622,11 @@ namespace Vocaluxe.Lib.Draw return false; // Texture memory to big texture.DataSize = new Size(w, h); } - _WriteDataToTexture(texture.D3DTexture, w, data); + _WriteDataToTexture(texture, data); return true; } #region drawing - /// - /// Draws a texture - /// - /// The texture to be drawn - public void DrawTexture(CTextureRef texture) - { - if (texture == null) - return; - DrawTexture(texture, texture.Rect, texture.Color); - } - - /// - /// Draws a texture - /// - /// The texture to be drawn - /// A SRectF struct containing the destination coordinates - public void DrawTexture(CTextureRef texture, SRectF rect) - { - if (texture == null) - return; - DrawTexture(texture, rect, texture.Color); - } - /// /// Draws a texture /// @@ -666,7 +634,7 @@ namespace Vocaluxe.Lib.Draw /// A SRectF struct containing the destination coordinates /// A SColorF struct containing a color which the texture will be colored in /// True if the texture should be mirrored - public void DrawTexture(CTextureRef textureRef, SRectF rect, SColorF color, bool mirrored = false) + public override void DrawTexture(CTextureRef textureRef, SRectF rect, SColorF color, bool mirrored = false) { if (rect.W < 1 || rect.H < 1) return; @@ -908,8 +876,6 @@ namespace Vocaluxe.Lib.Draw #endregion Textures - #endregion implementation - private static Matrix _CalculateRotationMatrix(float rot, float rx1, float rx2, float ry1, float ry2) { Matrix originTranslation = Matrix.Translation(new Vector3(-(float)CSettings.RenderW / 2, (float)CSettings.RenderH / 2, 0)); diff --git a/Vocaluxe/Lib/Draw/CDrawBase.cs b/Vocaluxe/Lib/Draw/CDrawBase.cs index 9ab6ae89..9fe49f16 100644 --- a/Vocaluxe/Lib/Draw/CDrawBase.cs +++ b/Vocaluxe/Lib/Draw/CDrawBase.cs @@ -15,11 +15,12 @@ namespace Vocaluxe.Lib.Draw { abstract class CDrawBase where TTextureType : CTextureBase, IDisposable { - struct STextureQueue + private struct STextureQueue { public readonly int TextureID; public readonly Size DataSize; public readonly byte[] Data; + public STextureQueue(int textureID, Size dataSize, byte[] data) { TextureID = textureID; @@ -28,7 +29,7 @@ namespace Vocaluxe.Lib.Draw } } - struct STextureCacheEntry + private struct STextureCacheEntry { public TTextureType Texture; /// @@ -85,9 +86,8 @@ namespace Vocaluxe.Lib.Draw //Dispose all textures foreach (TTextureType texture in _Textures.Values) { - if (texture != null && --texture.RefCount<=0) + if (texture != null && --texture.RefCount <= 0) texture.Dispose(); - } _Textures.Clear(); _TextureCache.Clear(); @@ -151,13 +151,34 @@ namespace Vocaluxe.Lib.Draw } #region Textures + protected abstract void _WriteDataToTexture(TTextureType texture, byte[] data); + + protected virtual void _WriteDataToTexture(TTextureType texture, IntPtr data) + { + byte[] dataArray = new byte[4 * texture.DataSize.Width * texture.DataSize.Height]; + Marshal.Copy(data, dataArray, 0, dataArray.Length); + _WriteDataToTexture(texture, dataArray); + } + + /// + /// Factory method to create a texture of the actual type + /// + /// + /// + protected abstract TTextureType _CreateTexture(Size dataSize); + /// /// Creates the texture specified by the reference and fills it with the given data /// /// /// /// - protected abstract TTextureType _CreateTexture(Size dataSize, byte[] data); + protected TTextureType _CreateTexture(Size dataSize, byte[] data) + { + TTextureType texture = _CreateTexture(dataSize); + _WriteDataToTexture(texture, data); + return texture; + } /// /// Creates the texture specified by the reference and fills it with the given data
@@ -166,11 +187,11 @@ namespace Vocaluxe.Lib.Draw /// /// /// - protected virtual TTextureType _CreateTexture(Size dataSize, IntPtr data) + protected TTextureType _CreateTexture(Size dataSize, IntPtr data) { - byte[] dataArray = new byte[4 * dataSize.Width * dataSize.Height]; - Marshal.Copy(data, dataArray, 0, dataArray.Length); - return _CreateTexture(dataSize, dataArray); + TTextureType texture = _CreateTexture(dataSize); + _WriteDataToTexture(texture, data); + return texture; } private void _AddToCache(TTextureType texture, Size origSize, string texturePath) @@ -178,7 +199,7 @@ namespace Vocaluxe.Lib.Draw if (texture != null && !String.IsNullOrEmpty(texturePath)) { texture.TexturePath = texturePath; - STextureCacheEntry cacheEntry=new STextureCacheEntry {OrigSize = origSize, Texture = texture}; + STextureCacheEntry cacheEntry = new STextureCacheEntry {OrigSize = origSize, Texture = texture}; lock (_Textures) { _TextureCache[texturePath] = cacheEntry; @@ -196,9 +217,7 @@ namespace Vocaluxe.Lib.Draw if (cacheEntry.Texture.RefCount <= 0) _TextureCache.Remove(texturePath); else - { return _GetTextureReference(cacheEntry.OrigSize, cacheEntry.Texture); - } } } return null; @@ -280,7 +299,7 @@ namespace Vocaluxe.Lib.Draw break; } - Size origSize=new Size(bmp.Width, bmp.Height); + Size origSize = new Size(bmp.Width, bmp.Height); Bitmap bmp2 = null; TTextureType texture; @@ -307,7 +326,7 @@ namespace Vocaluxe.Lib.Draw //Fill the new Bitmap with the texture data BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); - texture=_CreateTexture(new Size(w,h), bmpData.Scan0); + texture = _CreateTexture(new Size(w, h), bmpData.Scan0); bmp.UnlockBits(bmpData); } finally @@ -317,25 +336,25 @@ namespace Vocaluxe.Lib.Draw } _AddToCache(texture, origSize, texturePath); - return _GetTextureReference(origSize,texture); + return _GetTextureReference(origSize, texture); } public CTextureRef AddTexture(int w, int h, byte[] data) { TTextureType texture = _CreateTexture(new Size(w, h), data); - return _GetTextureReference(w,h, texture); + return _GetTextureReference(w, h, texture); } /// - /// Method used to add a texture to the texture list and return a TextureRef to it
- /// Important: This should be the only function to ever write to _Textures + /// Method used to add a texture to the texture list and return a TextureRef to it
+ /// Important: This should be the only function to ever write to _Textures ///
/// /// /// protected CTextureRef _GetTextureReference(Size origSize, TTextureType texture) { - Debug.Assert(origSize.Width>0 && origSize.Height>0); + Debug.Assert(origSize.Width > 0 && origSize.Height > 0); int id; lock (_MutexID) { @@ -344,7 +363,7 @@ namespace Vocaluxe.Lib.Draw CTextureRef textureRef = new CTextureRef(id, origSize); if (texture != null) texture.RefCount++; - if(texture==null || texture.RefCount==1) + if (texture == null || texture.RefCount == 1) _TextureCount++; lock (_Textures) { @@ -354,8 +373,8 @@ namespace Vocaluxe.Lib.Draw } /// - /// Method used to add a texture to the texture list and return a TextureRef to it
- /// Convenience method + /// Method used to add a texture to the texture list and return a TextureRef to it
+ /// Convenience method ///
/// /// @@ -371,7 +390,7 @@ namespace Vocaluxe.Lib.Draw lock (_Textures) { CTextureRef textureRef = _GetTextureReference(w, h, null); - _TexturesToLoad.Enqueue(new STextureQueue(textureRef.ID, new Size(w,h),data)); + _TexturesToLoad.Enqueue(new STextureQueue(textureRef.ID, new Size(w, h), data)); return textureRef; } } @@ -405,7 +424,7 @@ namespace Vocaluxe.Lib.Draw } lock (_Textures) { - return _Textures.TryGetValue(textureRef.ID, out texture) && texture!=null; + return _Textures.TryGetValue(textureRef.ID, out texture) && texture != null; } } @@ -419,17 +438,18 @@ namespace Vocaluxe.Lib.Draw return; lock (_Textures) { - TTextureType t; - if (_Textures.TryGetValue(texture.ID, out t)) + TTextureType t; + if (_Textures.TryGetValue(texture.ID, out t)) + { + if (t != null && --t.RefCount <= 0) { - if (t != null && --t.RefCount <= 0) - { - if(t.TexturePath!=null) _TextureCache.Remove(t.TexturePath); - t.Dispose(); - _TextureCount--; - } - _Textures.Remove(texture.ID); + if (t.TexturePath != null) + _TextureCache.Remove(t.TexturePath); + t.Dispose(); + _TextureCount--; } + _Textures.Remove(texture.ID); + } texture.ID = -1; } texture = null; @@ -456,6 +476,31 @@ namespace Vocaluxe.Lib.Draw { return _TextureCount; } + + /// + /// Draws a texture + /// + /// The texture to be drawn + public void DrawTexture(CTextureRef texture) + { + if (texture == null) + return; + DrawTexture(texture, texture.Rect, texture.Color); + } + + /// + /// Draws a texture + /// + /// The texture to be drawn + /// A SRectF struct containing the destination coordinates + public void DrawTexture(CTextureRef texture, SRectF rect) + { + if (texture == null) + return; + DrawTexture(texture, rect, texture.Color); + } + + public abstract void DrawTexture(CTextureRef textureRef, SRectF rect, SColorF color, bool mirrored = false); #endregion /// diff --git a/Vocaluxe/Lib/Draw/COpenGL.cs b/Vocaluxe/Lib/Draw/COpenGL.cs index 8e94d484..8cfb9aa4 100644 --- a/Vocaluxe/Lib/Draw/COpenGL.cs +++ b/Vocaluxe/Lib/Draw/COpenGL.cs @@ -273,14 +273,9 @@ namespace Vocaluxe.Lib.Draw //TODO: Check if _W,_H needs to be used or not Size size = new Size(GetScreenWidth(), GetScreenHeight()); COGLTexture texture = _CreateTexture(size); + 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.CopyTexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, 0, 0, size.Width, size.Height); //TODO: Use _X,_Y and _W,_H? - - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); - GL.BindTexture(TextureTarget.Texture2D, 0); return _GetTextureReference(size, texture); @@ -298,9 +293,7 @@ namespace Vocaluxe.Lib.Draw else { GL.BindTexture(TextureTarget.Texture2D, texture.Name); - GL.CopyTexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, 0, 0, GetScreenWidth(), GetScreenHeight()); - GL.BindTexture(TextureTarget.Texture2D, 0); } } @@ -405,53 +398,42 @@ namespace Vocaluxe.Lib.Draw #endregion Basic Draw Methods #region Textures - - #region adding - private COGLTexture _CreateTexture(Size dataSize) + protected override COGLTexture _CreateTexture(Size dataSize) { - return new COGLTexture(GL.GenTexture(), dataSize, _CheckForNextPowerOf2(dataSize.Width), _CheckForNextPowerOf2(dataSize.Height)); - } - - protected override COGLTexture _CreateTexture(Size dataSize, IntPtr data) - { - COGLTexture texture = _CreateTexture(dataSize); - + COGLTexture texture = new COGLTexture(GL.GenTexture(), dataSize, _CheckForNextPowerOf2(dataSize.Width), _CheckForNextPowerOf2(dataSize.Height)); 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, texture.DataSize.Width, texture.DataSize.Height, PixelFormat.Bgra, PixelType.UnsignedByte, data); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); - GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2D); GL.BindTexture(TextureTarget.Texture2D, 0); return texture; } - protected override COGLTexture _CreateTexture(Size dataSize, byte[] data) + protected override void _WriteDataToTexture(COGLTexture texture, byte[] data) + { + GL.BindTexture(TextureTarget.Texture2D, texture.Name); + + GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, texture.DataSize.Width, texture.DataSize.Height, PixelFormat.Bgra, PixelType.UnsignedByte, data); + GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2D); + + GL.BindTexture(TextureTarget.Texture2D, 0); + } + + protected override void _WriteDataToTexture(COGLTexture texture, IntPtr data) { - COGLTexture texture = _CreateTexture(dataSize); - 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, texture.DataSize.Width, texture.DataSize.Height, PixelFormat.Bgra, PixelType.UnsignedByte, data); - - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2D); GL.BindTexture(TextureTarget.Texture2D, 0); - return texture; } - #endregion adding - #region updating public override bool UpdateTexture(CTextureRef textureRef, int w, int h, byte[] data) { COGLTexture texture; @@ -466,34 +448,13 @@ namespace Vocaluxe.Lib.Draw texture.DataSize = new Size(w, h); } - GL.BindTexture(TextureTarget.Texture2D, texture.Name); - - GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, w, h, PixelFormat.Bgra, PixelType.UnsignedByte, data); - - GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2D); - - GL.BindTexture(TextureTarget.Texture2D, 0); + _WriteDataToTexture(texture, data); return true; } - #endregion updating #region drawing - public void DrawTexture(CTextureRef texture) - { - if (texture == null) - return; - DrawTexture(texture, texture.Rect, texture.Color); - } - - public void DrawTexture(CTextureRef texture, SRectF rect) - { - if (texture == null) - return; - DrawTexture(texture, rect, texture.Color); - } - - public void DrawTexture(CTextureRef textureRef, SRectF rect, SColorF color, bool mirrored = false) + public override void DrawTexture(CTextureRef textureRef, SRectF rect, SColorF color, bool mirrored = false) { COGLTexture texture; if (!_GetTexture(textureRef, out texture))