Added threaded cover loading to CDirect3D.cs

This commit is contained in:
Stephan Sundermann committed 2012-03-16 12:48:34 +01:00
1 parent 852fa38f61
commit f08e72bd70
2 files changed
+482 -33

No files matched your search

+2 -2
View File
@@ -827,7 +827,7 @@ namespace Vocaluxe.Base
Category = -1;
_SongsLoaded = true;
if (CConfig.Renderer == ERenderer.TR_CONFIG_OPENGL)
if (CConfig.Renderer != ERenderer.TR_CONFIG_SOFTWARE)
{
CLog.StartBenchmark(2, "Load Cover");
for (int i = 0; i < _Songs.Count; i++)
@@ -849,7 +849,7 @@ namespace Vocaluxe.Base
public static void LoadCover(long WaitTime, int NumLoads)
{
if (CConfig.Renderer == ERenderer.TR_CONFIG_OPENGL)
if (CConfig.Renderer != ERenderer.TR_CONFIG_SOFTWARE)
return; //should be removed as soon as the other renderer are ready for queque
if (!SongsLoaded)
+480 -31
View File
@@ -36,6 +36,9 @@ namespace Vocaluxe.Lib.Draw
private List<STexture> _Textures;
private List<Texture> _D3DTextures;
private List<STextureQueque> _Queque;
private Object MutexTexture = new Object();
private VertexBuffer _VertexBuffer;
@@ -46,14 +49,20 @@ namespace Vocaluxe.Lib.Draw
private Bitmap blankMap;
private STexture blankTexture;
private Texture transparentTexture;
#endregion private vars
/// <summary>
/// Creates a new Instance of the CDirect3D Class
/// </summary>
public CDirect3D()
{
this.Icon = new System.Drawing.Icon(Path.Combine(System.Environment.CurrentDirectory, CSettings.sIcon));
_Textures = new List<STexture>();
_D3DTextures = new List<Texture>();
_Queque = new List<STextureQueque>();
_Keys = new CKeys();
_D3D = new Direct3D();
@@ -86,6 +95,8 @@ namespace Vocaluxe.Lib.Draw
_PresentParameters.Multisample = MultisampleType.None;
_PresentParameters.MultisampleQuality = 0;
//Apply antialiasing and check if antialiasing mode is supported
#region Antialiasing
int quality = 0;
if (CConfig.AAMode == EAntiAliasingModes.x0)
{
@@ -132,12 +143,15 @@ namespace Vocaluxe.Lib.Draw
else
CLog.LogError("[Direct3D] This AAMode is not supported by this device or driver, fallback to no AA");
}
#endregion Antialiasing
//Apply the VSync configuration
if (CConfig.VSync == EOffOn.TR_CONFIG_ON)
_PresentParameters.PresentationInterval = PresentInterval.Default;
else
_PresentParameters.PresentationInterval = PresentInterval.Immediate;
//GMA 950 graphics devices can only process vertices in software mode
Capabilities caps = _D3D.GetDeviceCaps(_D3D.Adapters.DefaultAdapter.Adapter, DeviceType.Hardware);
CreateFlags flags = CreateFlags.None;
if ((caps.DeviceCaps & DeviceCaps.HWTransformAndLight) != 0)
@@ -148,13 +162,15 @@ namespace Vocaluxe.Lib.Draw
this.CenterToScreen();
blankMap = new Bitmap(1, 1); //Quick and Dirty, used for DrawColor
//This creates a new white texture and adds it to the texture pool
//This texture is used for the DrawRect method
blankMap = new Bitmap(1, 1);
Graphics g = Graphics.FromImage(blankMap);
g.Clear(Color.White);
g.Dispose();
blankTexture = AddTexture(blankMap);
h = ClientSize.Height;
w = ClientSize.Width;
transparentTexture = new Texture(_Device, 1, 1, 0, Usage.None, Format.A8R8G8B8, Pool.Managed);
blankMap.Dispose();
}
@@ -186,9 +202,12 @@ namespace Vocaluxe.Lib.Draw
RResize();
}
#endregion form events
/// <summary>
/// Resizes the viewport
/// </summary>
private void RResize()
{
// The window was minimized, so restore it to the last known size
if (ClientSize.Width == 0 || ClientSize.Height == 0)
{
ClientSize = _SizeBeforeMinimize;
@@ -202,28 +221,41 @@ namespace Vocaluxe.Lib.Draw
if ((float)w / (float)h > CSettings.GetRenderAspect())
{
//The windows's width is too big
w = (int)Math.Round((float)h * CSettings.GetRenderAspect());
x = (ClientSize.Width - w) / 2;
}
else
{
//The windows's height is too big
h = (int)Math.Round((float)w / CSettings.GetRenderAspect());
y = (ClientSize.Height - h) / 2;
}
//Apply the new sizes to the PresentParameters
_PresentParameters.BackBufferWidth = ClientSize.Width;
_PresentParameters.BackBufferHeight = ClientSize.Height;
ClearScreen();
//To set new PresentParameters the device has to be resetted
Reset();
//All configurations got flushed due to Reset(), so apply them again
Init();
//Set the new Viewport
_Device.Viewport = new Viewport(x, y, w, h);
//Store size so it can get restored after the window gets minimized
_SizeBeforeMinimize = ClientSize;
}
}
/// <summary>
/// Triggers the Fullscreen mode
/// </summary>
private void EnterFullScreen()
{
//This currently not using real fullscreen mode but a borderless window
//Real fullscreen could be gained setting _PresentParameters.Windowed = true
//And calling Reset() and Init() after
_OldSize = this.ClientSize;
this.ClientSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
this.FormBorderStyle = FormBorderStyle.None;
@@ -237,7 +269,9 @@ namespace Vocaluxe.Lib.Draw
CConfig.SaveConfig();
}
/// <summary>
/// Triggers the windowed mode
/// </summary>
private void LeaveFullScreen()
{
this.ClientSize = _OldSize;
@@ -312,7 +346,13 @@ namespace Vocaluxe.Lib.Draw
#region implementation
/// <summary>
/// Inits the Device
/// </summary>
/// <returns>True if it succeeded else false</returns>
public bool Init()
{
if (!_Device.Disposed)
{
this.Text = CSettings.GetFullVersionText();
Matrix translate = Matrix.Translation(new Vector3(-CSettings.iRenderW / 2, CSettings.iRenderH / 2, 0));
@@ -339,7 +379,12 @@ namespace Vocaluxe.Lib.Draw
_Device.SetTextureStageState(0, TextureStage.AlphaOperation, TextureOperation.Modulate);
return true;
}
else
return false;
}
/// <summary>
/// Starts the rendering
/// </summary>
public void MainLoop()
{
_Run = true;
@@ -358,23 +403,33 @@ namespace Vocaluxe.Lib.Draw
if (_Run)
{
//Clear the previous Frame
ClearScreen();
//We want to begin drawing
_Device.BeginScene();
CheckQueque();
_Run = _Run && CGraphics.Draw();
_Run = CGraphics.UpdateGameLogic(_Keys, _Mouse);
//We finished drawing the frame
_Device.EndScene();
_Run = CGraphics.UpdateGameLogic(_Keys, _Mouse);
try
{
//Now push the frame to the Viewport
_Device.Present();
}
catch (Direct3D9Exception)
{
if (_Device.TestCooperativeLevel() == ResultCode.DeviceNotReset) //Do only reset when the device in a resetable state!
//In Direct3D devices can get lost.
//This happens for example when opening the Task Manager in Vista/Windows 7 or if a UAC message is opening
//We need to reset the device to get it to workable state again
//After a reset Init() needs to be called because all data in the Direct3D default pool are lost and need to be recreated
if (_Device.TestCooperativeLevel() == ResultCode.DeviceNotReset)
{
Reset();
Init();
}
}
//Apply fullscreen mode
if ((CSettings.bFullScreen && !_fullscreen) || (!CSettings.bFullScreen && _fullscreen))
{
if (!_fullscreen)
@@ -387,7 +442,7 @@ namespace Vocaluxe.Lib.Draw
if (delay >= 1)
System.Threading.Thread.Sleep(delay);
//Calculate the FPS Rate and restart the timer after a frame
CTime.CalculateFPS();
CTime.Restart();
}
@@ -396,19 +451,27 @@ namespace Vocaluxe.Lib.Draw
}
}
/// <summary>
/// Resets the device, all objects in the Direct3D default pool get flushed and need to be recreated
/// </summary>
public void Reset()
{
//Dispose all objects in the default pool, those need to be recreated
TexturedColoredVertex.GetDeclaration(_Device).Dispose();
_VertexBuffer.Dispose();
_Device.Reset(_PresentParameters);
}
/// <summary>
/// Unloads all Textures and other objects used by Direct3D for rendering
/// </summary>
/// <returns></returns>
public bool Unload()
{
//Dispose all textures
for (int i = 0; i < _D3DTextures.Count; i++)
{
_D3DTextures[i].Dispose();
}
TexturedColoredVertex.GetDeclaration(_Device).Dispose();
_VertexBuffer.Dispose();
@@ -417,21 +480,40 @@ namespace Vocaluxe.Lib.Draw
return true;
}
/// <summary>
/// Gets the current viewport width
/// </summary>
/// <returns>The current viewport width</returns>
public int GetScreenWidth()
{
return _Device.Viewport.Width;
}
/// <summary>
/// Gets the current viewport height
/// </summary>
/// <returns>The current viewport height</returns>
public int GetScreenHeight()
{
return _Device.Viewport.Height;
}
/// <summary>
/// Calculates the bounds for a CText object
/// </summary>
/// <param name="text">The CText object of which the bounds should be calculated for</param>
/// <returns>RectangleF object containing the bounds</returns>
public RectangleF GetTextBounds(CText text)
{
return GetTextBounds(text, text.Height);
}
/// <summary>
/// Calculates the bounds for a CText object
/// </summary>
/// <param name="text">The CText object of which the bounds should be calculated for</param>
/// <param name="Height">The height of the CText object</param>
/// <returns>RectangleF object containing the bounds</returns>
public RectangleF GetTextBounds(CText text, float Height)
{
CFonts.Height = Height;
@@ -442,21 +524,38 @@ namespace Vocaluxe.Lib.Draw
#region Basic Draw Methods
/// <summary>
/// Removes all textures from the screen
/// </summary>
public void ClearScreen()
{
_Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
}
/// <summary>
/// Not implemented yet
/// </summary>
/// <returns></returns>
public STexture CopyScreen()
{
//FIXME
STexture texture = new STexture(-1);
return texture;
}
/// <summary>
/// Not implemented yet
/// </summary>
/// <returns></returns>
public void CopyScreen(ref STexture Texture)
{
//FIXME
return;
}
/// <summary>
/// Creates a Screenshot of the current frame
/// </summary>
public void MakeScreenShot()
{
string file = "Screenshot_";
@@ -466,6 +565,7 @@ namespace Vocaluxe.Lib.Draw
while (File.Exists(Path.Combine(path, file + i.ToString("00000") + ".bmp")))
i++;
//create a surface of the frame
using (Surface surface = _Device.GetBackBuffer(0, 0))
{
Bitmap screen = new Bitmap(Surface.ToStream(surface, ImageFileFormat.Bmp));
@@ -476,29 +576,74 @@ namespace Vocaluxe.Lib.Draw
_Mouse.Visible = true;
}
/// <summary>
/// Draws a line
/// </summary>
/// <param name="a">The alpha value from 0-255</param>
/// <param name="r">The red value from 0-255</param>
/// <param name="g">The red value from 0-255</param>
/// <param name="b">The red value from 0-255</param>
/// <param name="w">The width of the line</param>
/// <param name="x1">The start x-value</param>
/// <param name="y1">The start y-value</param>
/// <param name="x2">The end x-value</param>
/// <param name="y2">The end y-value</param>
public void DrawLine(int a, int r, int g, int b, int w, int x1, int y1, int x2, int y2)
{
SColorF color = new SColorF(a / 255, g / 255, b / 255, a / 255);
DrawTexture(blankTexture, new SRectF(x1, y1, x2, y2, 1), color);
}
/// <summary>
/// Draws a text string
/// </summary>
/// <param name="Text">The text to be drawn</param>
/// <param name="x">The text's x-position</param>
/// <param name="y">The text's y-position</param>
/// <param name="h">The text's height</param>
public void DrawText(string Text, int x, int y, int h)
{
DrawText(Text, x, y, h, 0f);
}
/// <summary>
/// Draws a text string
/// </summary>
/// <param name="Text">The text to be drawn</param>
/// <param name="x">The text's x-position</param>
/// <param name="y">The text's y-position</param>
/// <param name="h">The text's height</param>
/// <param name="z">The text's x-position</param>
public void DrawText(string Text, int x, int y, float h, float z)
{
CFonts.DrawText(Text, h, x, y, z, new SColorF(1f, 1f, 1f, 1f));
}
/// <summary>
/// Draws a colored rectangle
/// </summary>
/// <param name="color">The color in which the rectangle will be drawn in</param>
/// <param name="rect">The coordinates in a SRectF struct</param>
public void DrawColor(SColorF color, SRectF rect)
{
//blankMap = new Bitmap(1, 1);
//Graphics g = Graphics.FromImage(blankMap);
//g.Clear(Color.White);
//g.Dispose();
//blankTexture = AddTexture(blankMap);
//blankMap.Dispose();
DrawTexture(blankTexture, rect, color);
//RemoveTexture(ref blankTexture);
}
#endregion Basic Draw Methods
#region Textures
/// <summary>
/// Adds a texture and stores it in the VRam
/// </summary>
/// <param name="TexturePath">The texture's filepath</param>
/// <returns>A STexture object containing the added texture</returns>
public STexture AddTexture(string TexturePath)
{
if (System.IO.File.Exists(TexturePath))
@@ -519,18 +664,30 @@ namespace Vocaluxe.Lib.Draw
return new STexture(-1);
}
/// <summary>
/// Calculates the next power of two
/// </summary>
/// <param name="n">The value of which the next power of two will be calculated</param>
/// <returns>The next power of two</returns>
private static float NextPowerOfTwo(float n)
{
if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
return (float)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
}
/// <summary>
/// Adds a texture and stores it in the Vram
/// </summary>
/// <param name="bmp">The Bitmap of which the texute will be created from</param>
/// <param name="TexturePath">The path to the texture</param>
/// <returns>A STexture object containing the added texture</returns>
public STexture AddTexture(Bitmap bmp, string TexturePath)
{
STexture texture = new STexture(-1);
if (bmp.Height == 0 || bmp.Width == 0)
return texture;
int MaxSize;
//Apply the right max size
switch (CConfig.TextureQuality)
{
case ETextureQuality.TR_CONFIG_TEXTURE_LOWEST:
@@ -571,10 +728,13 @@ namespace Vocaluxe.Lib.Draw
texture.width = w;
texture.height = h;
//Older graphics card can only handle textures with sizes being powers of two
texture.w2 = (float)NextPowerOfTwo(w);
texture.h2 = (float)NextPowerOfTwo(h);
//Create a new Bitmap with the new sizes
Bitmap bmp2 = new Bitmap((int)texture.w2, (int)texture.h2);
//Scale the texture
Graphics g = Graphics.FromImage(bmp2);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
@@ -584,12 +744,16 @@ namespace Vocaluxe.Lib.Draw
texture.width_ratio = 1f;
texture.height_ratio = 1f;
//Fill the new Bitmap with the texture data
BitmapData bmp_data = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
byte[] Data = new byte[4 * bmp2.Width * bmp2.Height];
Marshal.Copy(bmp_data.Scan0, Data, 0, Data.Length);
bmp2.UnlockBits(bmp_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
Texture t = new Texture(_Device, (int)texture.w2, (int)texture.h2, 0, Usage.AutoGenerateMipMap, Format.A8R8G8B8, Pool.Managed);
//Lock the texture and fill it with the data
DataRectangle rect = t.LockRectangle(0, LockFlags.None);
for (int i = 0; i < Data.Length; )
{
@@ -608,11 +772,19 @@ namespace Vocaluxe.Lib.Draw
texture.color = new SColorF(1f, 1f, 1f, 1f);
texture.rect = new SRectF(0f, 0f, texture.width, texture.height, 0f);
texture.TexturePath = String.Empty;
lock (MutexTexture)
{
_Textures.Add(texture);
}
return texture;
}
/// <summary>
/// Adds a texture and stores it in the Vram
/// </summary>
/// <param name="bmp">The Bitmap of which the texute will be created from</param>
/// <returns>A STexture object containing the added texture</returns>
public STexture AddTexture(Bitmap bmp)
{
return AddTexture(bmp, String.Empty);
@@ -627,7 +799,22 @@ namespace Vocaluxe.Lib.Draw
public STexture QuequeTexture(int W, int H, ref byte[] Data)
{
return AddTexture(W, H, ref Data);
STexture texture = new STexture(-1);
STextureQueque queque = new STextureQueque();
queque.data = Data;
queque.height = H;
queque.width = W;
lock (MutexTexture)
{
_D3DTextures.Add(null); //_D3DTextures.Add(transparentTexture);
texture.index = _D3DTextures.Count - 1;
queque.ID = texture.index;
_Queque.Add(queque);
_Textures.Add(texture);
}
return texture;
}
public STexture AddTexture(int W, int H, ref byte[] Data)
@@ -656,10 +843,20 @@ namespace Vocaluxe.Lib.Draw
texture.color = new SColorF(1f, 1f, 1f, 1f);
texture.rect = new SRectF(0f, 0f, texture.width, texture.height, 0f);
texture.TexturePath = String.Empty;
lock (MutexTexture)
{
_Textures.Add(texture);
}
return texture;
}
/// <summary>
/// Updates the data of a texture
/// </summary>
/// <param name="Texture">The texture to update</param>
/// <param name="Data">A Pointer containing the data of which the texture should be updated</param>
/// <returns>True if succeeded</returns>
public bool UpdateTexture(ref STexture Texture, IntPtr Data)
{
byte[] PointerData = new Byte[4 * (int)Texture.width * (int)Texture.height];
@@ -668,9 +865,17 @@ namespace Vocaluxe.Lib.Draw
return UpdateTexture(ref Texture, ref PointerData);
}
/// <summary>
/// Updates the data of a texture
/// </summary>
/// <param name="Texture">The texture to update</param>
/// <param name="Data">A byte array containing the new texture's data</param>
/// <returns>True if succeeded</returns>
public bool UpdateTexture(ref STexture Texture, ref byte[] Data)
{
if ((Texture.index >= 0) && (_Textures.Count > 0) && (_D3DTextures.Count > Texture.index))
lock (MutexTexture)
{
if ((Texture.index >= 0) && (_Textures.Count > 0) && (_D3DTextures.Count > Texture.index) && _TextureExists(ref Texture))
{
DataRectangle rect = _D3DTextures[Texture.index].LockRectangle(0, LockFlags.None);
for (int i = 0; i < Data.Length; )
@@ -684,26 +889,45 @@ namespace Vocaluxe.Lib.Draw
Texture.height_ratio = Texture.height / NextPowerOfTwo(Texture.height);
Texture.width_ratio = Texture.width / NextPowerOfTwo(Texture.width);
}
return true;
}
else
return false;
}
}
private bool _TextureExists(STexture Texture)
/// <summary>
/// Checks if a texture exists
/// </summary>
/// <param name="Texture">The texture to check</param>
/// <returns>True if the texture exists</returns>
private bool _TextureExists(ref STexture Texture)
{
if ((Texture.index > 0) && (_Textures.Count > 0))
lock (MutexTexture)
{
for (int i = 0; i < _Textures.Count; i++)
{
if (_Textures[i].index == Texture.index)
if (Texture.index == _Textures[i].index)
{
if (_Textures[i].index >= 0)
{
Texture = _Textures[i];
return true;
}
break;
}
}
}
return false;
}
/// <summary>
/// Removes a texture from the Vram
/// </summary>
/// <param name="Texture">The texture to be removed</param>
public void RemoveTexture(ref STexture Texture)
{
lock (MutexTexture)
{
if ((Texture.index >= 0) && (_Textures.Count > 0))
{
@@ -714,47 +938,103 @@ namespace Vocaluxe.Lib.Draw
_D3DTextures[Texture.index].Dispose();
_Textures.RemoveAt(i);
Texture.index = -1;
GC.Collect();
Texture.ID = -1;
break;
}
}
}
}
}
/// <summary>
/// Draws a texture
/// </summary>
/// <param name="Texture">The texture to be drawn</param>
public void DrawTexture(STexture Texture)
{
//lock (MutexTexture)
{
if ((Texture.index >= 0) && (_Textures.Count > 0) && (_D3DTextures.Count > Texture.index))
DrawTexture(Texture, Texture.rect, Texture.color);
}
}
/// <summary>
/// Draws a texture
/// </summary>
/// <param name="Texture">The texture to be drawn</param>
/// <param name="rect">A SRectF struct containing the destination coordinates</param>
public void DrawTexture(STexture Texture, SRectF rect)
{
//lock (MutexTexture)
{
if ((Texture.index >= 0) && (_Textures.Count > 0) && (_D3DTextures.Count > Texture.index))
DrawTexture(Texture, rect, Texture.color, false);
}
}
/// <summary>
/// Draws a texture
/// </summary>
/// <param name="Texture">The texture to be drawn</param>
/// <param name="rect">A SRectF struct containing the destination coordinates</param>
/// <param name="color">A SColorF struct containing a color which the texture will be colored in</param>
public void DrawTexture(STexture Texture, SRectF rect, SColorF color)
{
//lock (MutexTexture)
{
if ((Texture.index >= 0) && (_Textures.Count > 0) && (_D3DTextures.Count > Texture.index))
DrawTexture(Texture, rect, color, false);
}
}
/// <summary>
/// Draws a texture
/// </summary>
/// <param name="Texture">The texture to be drawn</param>
/// <param name="rect">A SRectF struct containing the destination coordinates</param>
/// <param name="color">A SColorF struct containing a color which the texture will be colored in</param>
/// <param name="bounds">A SRectF struct containing which part of the texture should be drawn</param>
public void DrawTexture(STexture Texture, SRectF rect, SColorF color, SRectF bounds)
{
//lock (MutexTexture)
{
if ((Texture.index >= 0) && (_Textures.Count > 0) && (_D3DTextures.Count > Texture.index))
DrawTexture(Texture, rect, color, bounds, false);
}
}
/// <summary>
/// Draws a texture
/// </summary>
/// <param name="Texture">The texture to be drawn</param>
/// <param name="rect">A SRectF struct containing the destination coordinates</param>
/// <param name="color">A SColorF struct containing a color which the texture will be colored in</param>
/// <param name="mirrored">True if the texture should be mirrored</param>
public void DrawTexture(STexture Texture, SRectF rect, SColorF color, bool mirrored)
{
//lock (MutexTexture)
{
if ((Texture.index >= 0) && (_Textures.Count > 0) && (_D3DTextures.Count > Texture.index))
DrawTexture(Texture, rect, color, new SRectF(0, 0, CSettings.iRenderW, CSettings.iRenderH, rect.Z), mirrored);
}
}
/// <summary>
/// Draws a texture
/// </summary>
/// <param name="Texture">The texture to be drawn</param>
/// <param name="rect">A SRectF struct containing the destination coordinates</param>
/// <param name="color">A SColorF struct containing a color which the texture will be colored in</param>
/// <param name="bounds">A SRectF struct containing which part of the texture should be drawn</param>
/// <param name="mirrored">True if the texture should be mirrored</param>
public void DrawTexture(STexture Texture, SRectF rect, SColorF color, SRectF bounds, bool mirrored)
{
if ((Texture.index >= 0) && (_Textures.Count > 0) && (_D3DTextures.Count > Texture.index))
//lock (MutexTexture)
{
if ((Texture.index >= 0) && (_Textures.Count > 0) && (_D3DTextures.Count > Texture.index) && _TextureExists(ref Texture))
{
//Calculate the position
float x1 = (bounds.X - rect.X) / rect.W * Texture.width_ratio;
float x2 = (bounds.X + bounds.W - rect.X) / rect.W * Texture.width_ratio;
float y1 = (bounds.Y - rect.Y) / rect.H * Texture.height_ratio;
@@ -772,6 +1052,7 @@ namespace Vocaluxe.Lib.Draw
if (y2 > Texture.height_ratio)
y2 = Texture.height_ratio;
//Calculate the size
float rx1 = rect.X;
float rx2 = rect.X + rect.W;
float ry1 = rect.Y;
@@ -789,7 +1070,8 @@ namespace Vocaluxe.Lib.Draw
if (ry2 > bounds.Y + bounds.H)
ry2 = bounds.Y + bounds.H;
rx1 -= 0.5f; // Align pixels
//Align the pixels because Direct3D expects the pixels to be the left top corner
rx1 -= 0.5f;
ry1 -= 0.5f;
rx2 -= 0.5f;
ry2 -= 0.5f;
@@ -801,6 +1083,8 @@ namespace Vocaluxe.Lib.Draw
if (color.G >= 1f)
color.G = 1f;
//Apply rotation
//FIXME: Rotated textures get distorted when they hit the border of the window
if (rect.Rotation != 0)
{
rect.Rotation = rect.Rotation * (float)Math.PI / 180;
@@ -812,12 +1096,18 @@ namespace Vocaluxe.Lib.Draw
Matrix rotation = Matrix.RotationZ(-rect.Rotation);
Matrix translationB = Matrix.Translation(centerX, centerY, 0);
//Multiplicate the matrices to get the real world matrix,
//First shift the texture into the center
//Rotate it and shift it back to the origin position
//Apply the originTranslation after
Matrix result = translationA * rotation * translationB * originTranslation;
_Device.SetTransform(TransformState.World, result);
}
Color c = Color.FromArgb((int)(color.A * 255 * CGraphics.GlobalAlpha), (int)(color.R * 255), (int)(color.G * 255), (int)(color.B * 255));
//Create new vertices and write them into a vertexbuffer
DataStream stream = _VertexBuffer.Lock(0, 0, LockFlags.Discard);
if (!mirrored)
{
@@ -839,9 +1129,12 @@ namespace Vocaluxe.Lib.Draw
}
_VertexBuffer.Unlock();
stream.Dispose();
//Set the texture
_Device.SetTexture(0, _D3DTextures[Texture.index]);
//Draw 2 triangles to build a quad
_Device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
//Reapply old matrices if the texture was rotated
if (rect.Rotation != 0)
{
Matrix originTranslation = Matrix.Translation(new Vector3(-CSettings.iRenderW / 2, CSettings.iRenderH / 2, 0));
@@ -849,10 +1142,23 @@ namespace Vocaluxe.Lib.Draw
}
}
}
}
/// <summary>
/// Draws a texture
/// </summary>
/// <param name="Texture">The texture to be drawn</param>
/// <param name="rect">A SRectF struct containing the destination coordinates</param>
/// <param name="color">A SColorF struct containing a color which the texture will be colored in</param>
/// <param name="begin">A Value ranging from 0 to 1 containing the beginning of the texture</param>
/// <param name="end">A Value ranging from 0 to 1 containing the ending of the texture</param>
public void DrawTexture(STexture Texture, SRectF rect, SColorF color, float begin, float end)
{
//lock (MutexTexture)
{
if (_TextureExists(ref Texture))
{
float x1 = 0f + begin * Texture.width_ratio;
float x2 = Texture.width_ratio * end;
float y1 = 0f;
@@ -863,26 +1169,75 @@ namespace Vocaluxe.Lib.Draw
float ry1 = rect.Y;
float ry2 = rect.Y + rect.H;
rx1 -= 0.5f; // Align pixels
//Align the pixels because Direct3D expects the pixels to be the left top corner
rx1 -= 0.5f;
ry1 -= 0.5f;
rx2 -= 0.5f;
ry2 -= 0.5f;
Color c = Color.FromArgb((int)(color.A * 255 * CGraphics.GlobalAlpha), (int)(color.R * 255), (int)(color.G * 255), (int)(color.B * 255));
//Apply rotation
//FIXME: Rotated textures get distorted when they hit the border of the window
if (rect.Rotation != 0)
{
rect.Rotation = rect.Rotation * (float)Math.PI / 180;
float centerX = (rx1 + rx2) / 2f;
float centerY = -(ry1 + ry2) / 2f;
Matrix originTranslation = _Device.GetTransform(TransformState.World);
Matrix translationA = Matrix.Translation(-centerX, -centerY, 0);
Matrix rotation = Matrix.RotationZ(-rect.Rotation);
Matrix translationB = Matrix.Translation(centerX, centerY, 0);
//Multiplicate the matrices to get the real world matrix,
//First shift the texture into the center
//Rotate it and shift it back to the origin position
//Apply the originTranslation after
Matrix result = translationA * rotation * translationB * originTranslation;
_Device.SetTransform(TransformState.World, result);
}
//Create new vertices and write them into a vertexbuffer
DataStream stream = _VertexBuffer.Lock(0, 0, LockFlags.Discard);
stream.WriteRange(new[] {
new TexturedColoredVertex(new Vector3(rx1, ry1*-1f, rect.Z + CGraphics.ZOffset), new Vector2(x1, y1), c.ToArgb()),
new TexturedColoredVertex(new Vector3(rx1, ry2*-1f, rect.Z + CGraphics.ZOffset), new Vector2(x1, y2), c.ToArgb()),
new TexturedColoredVertex(new Vector3(rx2, ry2*-1f, rect.Z + CGraphics.ZOffset), new Vector2(x2, y2), c.ToArgb()),
new TexturedColoredVertex(new Vector3(rx2, ry1*-1f, rect.Z + CGraphics.ZOffset), new Vector2(x2, y1), c.ToArgb()),
new TexturedColoredVertex(new Vector3(rx1, -ry1, rect.Z + CGraphics.ZOffset), new Vector2(x1, y1), c.ToArgb()),
new TexturedColoredVertex(new Vector3(rx1, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x1, y2), c.ToArgb()),
new TexturedColoredVertex(new Vector3(rx2, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x2, y2), c.ToArgb()),
new TexturedColoredVertex(new Vector3(rx2, -ry1, rect.Z + CGraphics.ZOffset), new Vector2(x2, y1), c.ToArgb()),
});
_VertexBuffer.Unlock();
stream.Dispose();
//Set the texture
_Device.SetTexture(0, _D3DTextures[Texture.index]);
//Draw 2 triangles to build a quad
_Device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
//Reapply old matrices if the texture was rotated
if (rect.Rotation != 0)
{
Matrix originTranslation = Matrix.Translation(new Vector3(-CSettings.iRenderW / 2, CSettings.iRenderH / 2, 0));
_Device.SetTransform(TransformState.World, originTranslation);
}
}
}
}
/// <summary>
/// Draws a reflection of a texture
/// </summary>
/// <param name="Texture">The texture of which a reflection should be drawn</param>
/// <param name="rect">A SRectF struct containing the destination coordinates</param>
/// <param name="color">A SColorF struct containing a color which the texture will be colored in</param>
/// <param name="bounds">A SRectF struct containing which part of the texture should be drawn</param>
/// <param name="space">The space between the texture and the reflection</param>
/// <param name="height">The height of the reflection</param>
public void DrawTextureReflection(STexture Texture, SRectF rect, SColorF color, SRectF bounds, float space, float height)
{
//lock (MutexTexture)
{
if (_TextureExists(ref Texture))
{
if (rect.W == 0f || rect.H == 0f || bounds.H == 0f || bounds.W == 0f || color.A == 0f || height <= 0f)
return;
@@ -896,7 +1251,7 @@ namespace Vocaluxe.Lib.Draw
if (height > bounds.H)
height = bounds.H;
if (_TextureExists(Texture))
if (_TextureExists(ref Texture))
{
float x1 = (bounds.X - rect.X) / rect.W * Texture.width_ratio;
float x2 = (bounds.X + bounds.W - rect.X) / rect.W * Texture.width_ratio;
@@ -933,33 +1288,127 @@ namespace Vocaluxe.Lib.Draw
if (ry2 > bounds.Y + bounds.H + space + height)
ry2 = bounds.Y + bounds.H + space + height;
rx1 -= 0.5f; // Align pixels
//Align the pixels because Direct3D expects the pixels to be the left top corner
rx1 -= 0.5f;
ry1 -= 0.5f;
rx2 -= 0.5f;
ry2 -= 0.5f;
DataStream stream = _VertexBuffer.Lock(0, 0, LockFlags.Discard);
//Apply rotation
//FIXME: Rotated textures get distorted when they hit the border of the window
if (rect.Rotation != 0)
{
rect.Rotation = rect.Rotation * (float)Math.PI / 180;
float centerX = (rx1 + rx2) / 2f;
float centerY = -(ry1 + ry2) / 2f;
Matrix originTranslation = _Device.GetTransform(TransformState.World);
Matrix translationA = Matrix.Translation(-centerX, -centerY, 0);
Matrix rotation = Matrix.RotationZ(-rect.Rotation);
Matrix translationB = Matrix.Translation(centerX, centerY, 0);
//Multiplicate the matrices to get the real world matrix,
//First shift the texture into the center
//Rotate it and shift it back to the origin position
//Apply the originTranslation after
Matrix result = translationA * rotation * translationB * originTranslation;
_Device.SetTransform(TransformState.World, result);
}
Color c = Color.FromArgb((int)(color.A * 255 * CGraphics.GlobalAlpha), (int)(color.R * 255), (int)(color.G * 255), (int)(color.B * 255));
Color t = Color.FromArgb(0, (int)(color.R * 255), (int)(color.G * 255), (int)(color.B * 255));
Color transparent = Color.FromArgb(0, (int)(color.R * 255), (int)(color.G * 255), (int)(color.B * 255));
//Create new vertices and write them into a vertexbuffer
DataStream stream = _VertexBuffer.Lock(0, 0, LockFlags.Discard);
stream.WriteRange(new[] {
new TexturedColoredVertex(new Vector3(rx2, -ry1, rect.Z + CGraphics.ZOffset), new Vector2(x2, y2), c.ToArgb()),
new TexturedColoredVertex(new Vector3(rx2, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x2, y1), t.ToArgb()),
new TexturedColoredVertex(new Vector3(rx1, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x1, y1), t.ToArgb()),
new TexturedColoredVertex(new Vector3(rx2, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x2, y1), transparent.ToArgb()),
new TexturedColoredVertex(new Vector3(rx1, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x1, y1), transparent.ToArgb()),
new TexturedColoredVertex(new Vector3(rx1, -ry1, rect.Z + CGraphics.ZOffset), new Vector2(x1, y2), c.ToArgb()),
});
_VertexBuffer.Unlock();
stream.Dispose();
//Set the texture
_Device.SetTexture(0, _D3DTextures[Texture.index]);
//Draw 2 triangles to build a quad
_Device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
//Reapply old matrices if the texture was rotated
if (rect.Rotation != 0)
{
Matrix originTranslation = Matrix.Translation(new Vector3(-CSettings.iRenderW / 2, CSettings.iRenderH / 2, 0));
_Device.SetTransform(TransformState.World, originTranslation);
}
}
}
}
}
private void CheckQueque()
{
lock (MutexTexture)
{
if (_Queque.Count == 0)
return;
STextureQueque q = _Queque[0];
STexture texture = new STexture(-1);
int index = -1;
for (int i = 0; i < _Textures.Count; i++)
{
if (_Textures[i].index == q.ID)
{
texture = _Textures[i];
index = i;
break;
}
}
if (texture.index < 0)
return;
texture.width = q.width;
texture.height = q.height;
texture.w2 = NextPowerOfTwo(texture.width);
texture.h2 = NextPowerOfTwo(texture.height);
texture.width_ratio = texture.width / texture.w2;
texture.height_ratio = texture.height / texture.h2;
Texture t = new Texture(_Device, (int)texture.w2, (int)texture.h2, 0, Usage.AutoGenerateMipMap, Format.A8R8G8B8, Pool.Managed);
DataRectangle rect = t.LockRectangle(0, LockFlags.None);
for (int i = 0; i < q.data.Length; )
{
rect.Data.Write(q.data, i, 4 * q.width);
i += 4 * q.width;
rect.Data.Position = rect.Data.Position - 4 * q.width;
rect.Data.Position += rect.Pitch;
}
t.UnlockRectangle(0);
_D3DTextures[q.ID] = t;
// Add to Texture List
texture.color = new SColorF(1f, 1f, 1f, 1f);
texture.rect = new SRectF(0f, 0f, texture.width, texture.height, 0f);
texture.TexturePath = String.Empty;
_Textures[index] = texture;
_Queque.RemoveAt(0);
}
}
/// <summary>
/// Gets the count of current textures
/// </summary>
/// <returns>The amount of textures</returns>
public int TextureCount()
{
lock (MutexTexture)
{
return _Textures.Count;
}
}
#endregion Textures
#endregion implementation