diff --git a/2DGAMELIB/_2DGAMELIB/FrameTimeCounter.cs b/2DGAMELIB/_2DGAMELIB/FrameTimeCounter.cs new file mode 100644 index 0000000..5d9976e --- /dev/null +++ b/2DGAMELIB/_2DGAMELIB/FrameTimeCounter.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; + +namespace _2DGAMELIB +{ + internal class FrameTimeCounter + { + private readonly Stopwatch sw = Stopwatch.StartNew(); + private long lastTicks; + public double FrameMs { get; private set; } + + public void Frame() + { + long now = sw.ElapsedTicks; + + if (lastTicks != 0) + { + FrameMs = (now - lastTicks) * 1000.0 / Stopwatch.Frequency; + } + + lastTicks = now; + } + } +} diff --git a/2DGAMELIB/_2DGAMELIB/ModeEventDispatcher.cs b/2DGAMELIB/_2DGAMELIB/ModeEventDispatcher.cs index 9a73924..ab46825 100644 --- a/2DGAMELIB/_2DGAMELIB/ModeEventDispatcher.cs +++ b/2DGAMELIB/_2DGAMELIB/ModeEventDispatcher.cs @@ -294,6 +294,10 @@ namespace _2DGAMELIB baseControl.SetTitle(UITitle); Modes[mode].Setting(); + FrameTimeCounter FTC = new FrameTimeCounter(); + RealFpsCounter RFC = new RealFpsCounter(); + + /* Action action = delegate { if (FPSF.Value > 1.0) @@ -311,6 +315,28 @@ namespace _2DGAMELIB baseControl.SetBitmap(Display); }; + */ + + Action action = () => + { + if (FPSF.Value > 1.0) + Modes[mode].Draw(FPSF); + + baseControl.SetBitmap(Display); + + FTC.Frame(); + RFC.Frame(); + + if (ShowFPS) + { + baseControl.SetTitle( + UITitle + + " - FPS: " + RFC.Value.ToString("F1") + + " | Frame: " + FTC.FrameMs.ToString("F2") + " ms" + ); + } + }; + while (Drive) { diff --git a/2DGAMELIB/_2DGAMELIB/Par.cs b/2DGAMELIB/_2DGAMELIB/Par.cs index 841babf..b4b7739 100644 --- a/2DGAMELIB/_2DGAMELIB/Par.cs +++ b/2DGAMELIB/_2DGAMELIB/Par.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; +using System.Diagnostics; namespace _2DGAMELIB { @@ -11,7 +12,20 @@ namespace _2DGAMELIB [Serializable] public class Par { - private Pars parent; + //FOR TESTS + public static long TCalc; + public static long TCalcH; + public static long TFill; + public static long TOutline; + public static long THitFill; + + public static int NCalc; + public static int NCalcH; + public static int NFill; + public static int NOutline; + public static int NHitFill; + + private Pars parent; public string Tag = ""; @@ -645,7 +659,11 @@ namespace _2DGAMELIB mv = Position * Unit - bp; - Path.Reset(); + double a = System.Math.PI * Angle / 180.0; + M11 = System.Math.Cos(a); + M12 = System.Math.Sin(a); + + Path.Reset(); OutlinePath.Reset(); foreach (Out item in op) { @@ -654,7 +672,7 @@ namespace _2DGAMELIB { p.X = item.ps[i].X * usx; p.Y = item.ps[i].Y * usy; - p = Rotate(Angle, p) + mv; + p = Rotate(ref p) + mv; points[i].X = (float)p.X; points[i].Y = (float)p.Y; @@ -677,11 +695,8 @@ namespace _2DGAMELIB } } - private Vector2D Rotate(double angle, Vector2D p) + private Vector2D Rotate(ref Vector2D p) { - double M11 = System.Math.Cos(System.Math.PI * angle / 180.0); - double M12 = System.Math.Sin(System.Math.PI * angle / 180.0); - p.X -= bp.X; p.Y -= bp.Y; @@ -694,33 +709,45 @@ namespace _2DGAMELIB return p; } - public void Draw(double Unit, Graphics Graphics) - { - if (Dra) - { - if (Edit) - { - Calculation(Unit); - Edit = false; - } - if (pen != null && (EditP || EditPS)) - { - pen.Width = (float)(Unit * penWidth * positionSize); - EditP = false; - EditPS = false; - } - if (brush != null) - { - Graphics.FillPath(brush, Path); - } - if (pen != null) - { - Graphics.DrawPath(pen, OutlinePath); - } - } - } + public void Draw(double Unit, Graphics Graphics) + { + if (Dra) + { + if (Edit) + { + long t0 = Stopwatch.GetTimestamp(); + Calculation(Unit); + TCalc += Stopwatch.GetTimestamp() - t0; + NCalc++; + Edit = false; + } - private void CalculationH(double Unit) + if (pen != null && (EditP || EditPS)) + { + pen.Width = (float)(Unit * penWidth * positionSize); + EditP = false; + EditPS = false; + } + + if (brush != null) + { + long t0 = Stopwatch.GetTimestamp(); + Graphics.FillPath(brush, Path); + TFill += Stopwatch.GetTimestamp() - t0; + NFill++; + } + + if (pen != null) + { + long t0 = Stopwatch.GetTimestamp(); + Graphics.DrawPath(pen, OutlinePath); + TOutline += Stopwatch.GetTimestamp() - t0; + NOutline++; + } + } + } + + private void CalculationH(double Unit) { ush = Unit * Size; usxh = ush * SizeX; @@ -786,20 +813,27 @@ namespace _2DGAMELIB ph.Y = vh.Y + bph.Y; } - public void DrawH(double Unit, Graphics Graphics) - { - if (Hit) - { - if (EditH) - { - CalculationH(Unit); - EditH = false; - } - Graphics.FillPath(HitBrush, gph); - } - } + public void DrawH(double Unit, Graphics Graphics) + { + if (Hit) + { + if (EditH) + { + long t0 = Stopwatch.GetTimestamp(); + CalculationH(Unit); + TCalcH += Stopwatch.GetTimestamp() - t0; + NCalcH++; + EditH = false; + } - public void SetJointP(int Index, Par Par) + long t1 = Stopwatch.GetTimestamp(); + Graphics.FillPath(HitBrush, gph); + THitFill += Stopwatch.GetTimestamp() - t1; + NHitFill++; + } + } + + public void SetJointP(int Index, Par Par) { if (Index < jp.Count) { diff --git a/2DGAMELIB/_2DGAMELIB/RealFpsCounter.cs b/2DGAMELIB/_2DGAMELIB/RealFpsCounter.cs new file mode 100644 index 0000000..f6147ad --- /dev/null +++ b/2DGAMELIB/_2DGAMELIB/RealFpsCounter.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; + +namespace _2DGAMELIB +{ + internal class RealFpsCounter + { + private readonly Stopwatch sw = Stopwatch.StartNew(); + private int frames; + private long lastTicks; + public double Value { get; private set; } + + public void Frame() + { + frames++; + + long now = sw.ElapsedTicks; + long delta = now - lastTicks; + + if (delta >= Stopwatch.Frequency) + { + Value = frames / (delta / (double)Stopwatch.Frequency); + frames = 0; + lastTicks = now; + } + } + } +} diff --git a/2DGAMELIB/_2DGAMELIB/RenderArea.cs b/2DGAMELIB/_2DGAMELIB/RenderArea.cs index 3fe5683..a2ab858 100644 --- a/2DGAMELIB/_2DGAMELIB/RenderArea.cs +++ b/2DGAMELIB/_2DGAMELIB/RenderArea.cs @@ -19,6 +19,8 @@ namespace _2DGAMELIB protected double displayUnitScale; protected double hitUnitScale; + private int DrawCals = 0; + protected Size WH = System.Drawing.Size.Empty; protected Size WHH = System.Drawing.Size.Empty; protected Size WHA = System.Drawing.Size.Empty; @@ -38,6 +40,7 @@ namespace _2DGAMELIB Setting(Unit, XRatio, YRatio, Size, DisMag, HitMag); } + public RenderArea(ModeEventDispatcher Med, bool Hit) { if (Hit) @@ -64,10 +67,11 @@ namespace _2DGAMELIB displayGraphics.SmoothingMode = SmoothingMode.None; - displayGraphics.PixelOffsetMode = PixelOffsetMode.HighSpeed; + displayGraphics.PixelOffsetMode = PixelOffsetMode.None; displayGraphics.InterpolationMode = InterpolationMode.NearestNeighbor; - //needed for text or smthn - displayGraphics.CompositingMode = CompositingMode.SourceOver; + //needed for text or smthn + displayGraphics.CompositingMode = CompositingMode.SourceOver; + displayGraphics.CompositingQuality = CompositingQuality.HighSpeed; } private void Setting(double Unit, double XRatio, double YRatio, double Size, double DisMag, double HitMag) { @@ -81,9 +85,10 @@ namespace _2DGAMELIB hitGraphics.SmoothingMode = SmoothingMode.None; - hitGraphics.PixelOffsetMode = PixelOffsetMode.HighSpeed; + hitGraphics.PixelOffsetMode = PixelOffsetMode.None; hitGraphics.InterpolationMode = InterpolationMode.NearestNeighbor; hitGraphics.CompositingMode = CompositingMode.SourceOver; + hitGraphics.CompositingQuality = CompositingQuality.HighSpeed; } public Vector2D GetPosition() @@ -93,35 +98,44 @@ namespace _2DGAMELIB public void Draw(Par Par) { - Par.Draw(displayUnitScale, displayGraphics); - if (hitGraphics != null) - { - Par.DrawH(hitUnitScale, hitGraphics); - } + Par.Draw(displayUnitScale, displayGraphics); + if (hitGraphics != null) + { + Par.DrawH(hitUnitScale, hitGraphics); + } } public void Draw(ParT ParT) { - ParT.Draw(displayUnitScale, displayGraphics); - if (hitGraphics != null) - { - ParT.DrawH(hitUnitScale, hitGraphics); - } + ParT.Draw(displayUnitScale, displayGraphics); + if (hitGraphics != null) + { + ParT.DrawH(hitUnitScale, hitGraphics); + } } public void Draw(Pars Pars) { - Pars.Draw(displayUnitScale, displayGraphics); - if (hitGraphics != null) - { - Pars.DrawH(hitUnitScale, hitGraphics); - } + Pars.Draw(displayUnitScale, displayGraphics); + if (hitGraphics != null) + { + Pars.DrawH(hitUnitScale, hitGraphics); + + } + } public void Draw(RenderArea Are) - { - Vector2D p = Are.GetPosition(); - DisplayGraphics.DrawImage(Are.DisplayLayer, (float)(p.X * Are.displayUnitScale), (float)(p.Y * Are.displayUnitScale), Are.WHA.Width, Are.WHA.Height); + { + var p = Are.GetPosition(); + int x = (int)(p.X * Are.displayUnitScale); + int y = (int)(p.Y * Are.displayUnitScale); + + if (Are.DisplayLayer.Width == Are.WHA.Width && Are.DisplayLayer.Height == Are.WHA.Height) + DisplayGraphics.DrawImageUnscaled(Are.DisplayLayer, x, y); + else + DisplayGraphics.DrawImage(Are.DisplayLayer, x, y, Are.WHA.Width, Are.WHA.Height); + if (Are.hitGraphics != null && HitGraphics != null) { HitGraphics.DrawImage(Are.HitLayer, (int)(p.X * Are.hitUnitScale), (int)(p.Y * Are.hitUnitScale), Are.WHH.Width, Are.WHH.Height); @@ -132,7 +146,7 @@ namespace _2DGAMELIB { Vector2D p = GetPosition(); GD.DrawImage(DisplayLayer, (int)(p.X * unitScale), (int)(p.Y * unitScale), WH.Width, WH.Height); - } + } public void DrawTo(Graphics displayGraphics, Graphics hitGraphics) { @@ -142,7 +156,7 @@ namespace _2DGAMELIB { hitGraphics.DrawImage(HitLayer, (int)(p.X * hitUnitScale), (int)(p.Y * hitUnitScale), WHH.Width, WHH.Height); } - } + } public void Clear() { diff --git a/2DGAMELIB/_2DGAMELIB/WPFImage.cs b/2DGAMELIB/_2DGAMELIB/WPFImage.cs index b8b7c2f..5eb5f61 100644 --- a/2DGAMELIB/_2DGAMELIB/WPFImage.cs +++ b/2DGAMELIB/_2DGAMELIB/WPFImage.cs @@ -165,6 +165,7 @@ namespace _2DGAMELIB Glfw.SetWindowUserPointer(window, GCHandle.ToIntPtr(handle)); Glfw.MakeContextCurrent(window); + Glfw.SwapInterval(0); gl = Silk.NET.OpenGL.GL.GetApi(Glfw.GetProcAddress);