From ca339b2661ee565d55191d58ed40c2303f77c0b4 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 6 Jul 2017 18:25:19 -0400 Subject: [PATCH] HEAVILY optimize the game --- ShiftOS.Frontend/Desktop/Desktop.cs | 14 +++- ShiftOS.Frontend/Desktop/WindowManager.cs | 31 ++++----- ShiftOS.Frontend/GUI/Button.cs | 2 +- .../GraphicsSubsystem/GraphicsContext.cs | 65 +++++++++++-------- ShiftOS.Frontend/ShiftOS.Frontend.csproj | 4 ++ ShiftOS.Frontend/ShiftOS.cs | 12 +++- ShiftOS.Frontend/packages.config | 1 + 7 files changed, 80 insertions(+), 49 deletions(-) diff --git a/ShiftOS.Frontend/Desktop/Desktop.cs b/ShiftOS.Frontend/Desktop/Desktop.cs index af79c9c..46baf7f 100644 --- a/ShiftOS.Frontend/Desktop/Desktop.cs +++ b/ShiftOS.Frontend/Desktop/Desktop.cs @@ -110,6 +110,8 @@ namespace ShiftOS.Frontend.Desktop { } + private string dateTimeString = ""; + protected override void OnLayout() { SendToBack(); @@ -117,7 +119,14 @@ namespace ShiftOS.Frontend.Desktop Y = 0; Width = GetSize().Width; Height = GetSize().Height; - Invalidate(); + var now = DateTime.Now.TimeOfDay; + var newDateTimeString = $"{now.Hours}:{now.Minutes}:{now.Seconds}"; + if(newDateTimeString != dateTimeString) + { + dateTimeString = newDateTimeString; + Invalidate(); + } + } private List PanelButtons = new List(); @@ -158,8 +167,7 @@ namespace ShiftOS.Frontend.Desktop var panelClockRight = LoadedSkin.DesktopPanelClockFromRight; var panelClockTextColor = LoadedSkin.DesktopPanelClockColor.ToMonoColor(); - var dateTimeString = DateTime.Now.TimeOfDay.ToString(); - + var measure = gfx.MeasureString(dateTimeString, LoadedSkin.DesktopPanelClockFont); int panelclockleft = Width - (int)measure.X; diff --git a/ShiftOS.Frontend/Desktop/WindowManager.cs b/ShiftOS.Frontend/Desktop/WindowManager.cs index d89cbc2..22c61bf 100644 --- a/ShiftOS.Frontend/Desktop/WindowManager.cs +++ b/ShiftOS.Frontend/Desktop/WindowManager.cs @@ -322,14 +322,13 @@ namespace ShiftOS.Frontend.Desktop //Draw close button if(Shiftorium.UpgradeInstalled("close_button")) { - var closebuttoncolor = LoadedSkin.CloseButtonColor; var closebuttonsize = LoadedSkin.CloseButtonSize; var closebuttonright = LoadedSkin.CloseButtonFromSide; if (LoadedSkin.TitleButtonPosition == 0) closebuttonright = new Point(Width - closebuttonsize.Width - closebuttonright.X, closebuttonright.Y); if (!UIManager.SkinTextures.ContainsKey("closebutton")) { - gfx.DrawRectangle(closebuttonright.X, closebuttonright.Y, closebuttonsize.Width, closebuttonsize.Height, closebuttoncolor.ToMonoColor()); + gfx.DrawRectangle(closebuttonright.X, closebuttonright.Y, closebuttonsize.Width, closebuttonsize.Height, UIManager.SkinTextures["CloseButtonColor"]); } else { @@ -339,7 +338,6 @@ namespace ShiftOS.Frontend.Desktop //Draw maximize button if (Shiftorium.UpgradeInstalled("maximize_button")) { - var closebuttoncolor = LoadedSkin.MaximizeButtonColor; var closebuttonsize = LoadedSkin.MaximizeButtonSize; var closebuttonright = LoadedSkin.MaximizeButtonFromSide; if (LoadedSkin.TitleButtonPosition == 0) @@ -347,7 +345,7 @@ namespace ShiftOS.Frontend.Desktop if (!UIManager.SkinTextures.ContainsKey("maximizebutton")) { - gfx.DrawRectangle(closebuttonright.X, closebuttonright.Y, closebuttonsize.Width, closebuttonsize.Height, closebuttoncolor.ToMonoColor()); + gfx.DrawRectangle(closebuttonright.X, closebuttonright.Y, closebuttonsize.Width, closebuttonsize.Height, UIManager.SkinTextures["MaximizeButtonColor"]); } else { @@ -357,14 +355,13 @@ namespace ShiftOS.Frontend.Desktop //Draw minimize button if (Shiftorium.UpgradeInstalled("minimize_button")) { - var closebuttoncolor = LoadedSkin.MinimizeButtonColor; var closebuttonsize = LoadedSkin.MinimizeButtonSize; var closebuttonright = LoadedSkin.MinimizeButtonFromSide; if (LoadedSkin.TitleButtonPosition == 0) closebuttonright = new Point(Width - closebuttonsize.Width - closebuttonright.X, closebuttonright.Y); if (!UIManager.SkinTextures.ContainsKey("minimizebutton")) { - gfx.DrawRectangle(closebuttonright.X, closebuttonright.Y, closebuttonsize.Width, closebuttonsize.Height, closebuttoncolor.ToMonoColor()); + gfx.DrawRectangle(closebuttonright.X, closebuttonright.Y, closebuttonsize.Width, closebuttonsize.Height, UIManager.SkinTextures["MinimizeButtonColor"]); } else { @@ -388,18 +385,18 @@ namespace ShiftOS.Frontend.Desktop int bottomwidth = Width - bottomlocx - rightborderwidth; int brightlocx = Width - rightborderwidth; - var borderleftcolor = (ContainsFocusedControl || IsFocusedControl) ? LoadedSkin.BorderLeftBackground : LoadedSkin.BorderInactiveLeftBackground; - var borderrightcolor = (ContainsFocusedControl || IsFocusedControl) ? LoadedSkin.BorderRightBackground : LoadedSkin.BorderInactiveRightBackground; - var borderbottomcolor = (ContainsFocusedControl || IsFocusedControl) ? LoadedSkin.BorderBottomBackground : LoadedSkin.BorderInactiveBottomBackground; - var borderbleftcolor = (ContainsFocusedControl || IsFocusedControl) ? LoadedSkin.BorderBottomLeftBackground : LoadedSkin.BorderInactiveBottomLeftBackground; - var borderbrightcolor = (ContainsFocusedControl || IsFocusedControl) ? LoadedSkin.BorderBottomRightBackground : LoadedSkin.BorderInactiveBottomRightBackground; + var borderleftcolor = (ContainsFocusedControl || IsFocusedControl) ? UIManager.SkinTextures["BorderLeftBackground"] : UIManager.SkinTextures["BorderInactiveLeftBackground"]; + var borderrightcolor = (ContainsFocusedControl || IsFocusedControl) ? UIManager.SkinTextures["BorderRightBackground"] : UIManager.SkinTextures["BorderInactiveRightBackground"]; + var borderbottomcolor = (ContainsFocusedControl || IsFocusedControl) ? UIManager.SkinTextures["BorderBottomBackground"] : UIManager.SkinTextures["BorderInactiveBottomBackground"]; + var borderbleftcolor = (ContainsFocusedControl || IsFocusedControl) ? UIManager.SkinTextures["BorderBottomLeftBackground"] : UIManager.SkinTextures["BorderInactiveBottomLeftBackground"]; + var borderbrightcolor = (ContainsFocusedControl || IsFocusedControl) ? UIManager.SkinTextures["BorderBottomRightBackground"] : UIManager.SkinTextures["BorderInactiveBottomRightBackground"]; //draw border corners //BOTTOM LEFT if (!UIManager.SkinTextures.ContainsKey("bottomlborder")) { - gfx.DrawRectangle(0, bottomlocy, leftborderwidth, bottomborderwidth, borderbleftcolor.ToMonoColor()); + gfx.DrawRectangle(0, bottomlocy, leftborderwidth, bottomborderwidth, borderbleftcolor); } else { @@ -409,7 +406,7 @@ namespace ShiftOS.Frontend.Desktop //BOTTOM RIGHT if (!UIManager.SkinTextures.ContainsKey("bottomrborder")) { - gfx.DrawRectangle(brightlocx, bottomlocy, rightborderwidth, bottomborderwidth, borderbrightcolor.ToMonoColor()); + gfx.DrawRectangle(brightlocx, bottomlocy, rightborderwidth, bottomborderwidth, borderbrightcolor); } else { @@ -419,7 +416,7 @@ namespace ShiftOS.Frontend.Desktop //BOTTOM if (!UIManager.SkinTextures.ContainsKey("bottomborder")) { - gfx.DrawRectangle(leftborderwidth, bottomlocy, bottomwidth, bottomborderwidth, borderbottomcolor.ToMonoColor()); + gfx.DrawRectangle(leftborderwidth, bottomlocy, bottomwidth, bottomborderwidth, borderbottomcolor); } else { @@ -429,7 +426,7 @@ namespace ShiftOS.Frontend.Desktop //LEFT if (!UIManager.SkinTextures.ContainsKey("leftborder")) { - gfx.DrawRectangle(0, titleheight, leftborderwidth, Height - titleheight - bottomborderwidth, borderleftcolor.ToMonoColor()); + gfx.DrawRectangle(0, titleheight, leftborderwidth, Height - titleheight - bottomborderwidth, borderleftcolor); } else { @@ -439,7 +436,7 @@ namespace ShiftOS.Frontend.Desktop //RIGHT if (!UIManager.SkinTextures.ContainsKey("rightborder")) { - gfx.DrawRectangle(brightlocx, titleheight, rightborderwidth, Height - titleheight - bottomborderwidth, borderrightcolor.ToMonoColor()); + gfx.DrawRectangle(brightlocx, titleheight, rightborderwidth, Height - titleheight - bottomborderwidth, borderrightcolor); } else { @@ -448,7 +445,7 @@ namespace ShiftOS.Frontend.Desktop } - gfx.DrawRectangle(leftborderwidth, titleheight, Width - leftborderwidth - rightborderwidth, Height - titleheight - bottomborderwidth, LoadedSkin.ControlColor.ToMonoColor()); + gfx.DrawRectangle(leftborderwidth, titleheight, Width - leftborderwidth - rightborderwidth, Height - titleheight - bottomborderwidth, UIManager.SkinTextures["ControlColor"]); //So here's what we're gonna do now. //Now that we have a titlebar and window borders... //We're going to composite the hosted window diff --git a/ShiftOS.Frontend/GUI/Button.cs b/ShiftOS.Frontend/GUI/Button.cs index 0a0376c..c2e55b9 100644 --- a/ShiftOS.Frontend/GUI/Button.cs +++ b/ShiftOS.Frontend/GUI/Button.cs @@ -42,7 +42,7 @@ namespace ShiftOS.Frontend.GUI if (MouseLeftDown) bgCol = UIManager.SkinTextures["ButtonPressedColor"]; - gfx.DrawRectangle(0, 0, Width, Height, fgCol); + gfx.DrawRectangle(0, 0, Width, Height, UIManager.SkinTextures["ControlTextColor"]); gfx.DrawRectangle(1, 1, Width - 2, Height - 2, bgCol); var measure = gfx.MeasureString(Text, Font); diff --git a/ShiftOS.Frontend/GraphicsSubsystem/GraphicsContext.cs b/ShiftOS.Frontend/GraphicsSubsystem/GraphicsContext.cs index 867d18a..772abc4 100644 --- a/ShiftOS.Frontend/GraphicsSubsystem/GraphicsContext.cs +++ b/ShiftOS.Frontend/GraphicsSubsystem/GraphicsContext.cs @@ -139,45 +139,48 @@ namespace ShiftOS.Frontend.GraphicsSubsystem } } + public static List StringCaches = new List(); + public void DrawString(string text, int x, int y, Color color, System.Drawing.Font font, int wrapWidth = 0) { x += _startx; y += _starty; - Vector2 measure; - if (wrapWidth == 0) - measure = MeasureString(text, font); - else - measure = MeasureString(text, font, wrapWidth); - using (var bmp = new System.Drawing.Bitmap((int)measure.X, (int)measure.Y)) + var fontcache = StringCaches.FirstOrDefault(z => z.Text == text && z.FontFamily == font&&z.WrapWidth == wrapWidth); + if (fontcache == null) { - using (var gfx = System.Drawing.Graphics.FromImage(bmp)) + Vector2 measure; + if (wrapWidth == 0) + measure = MeasureString(text, font); + else + measure = MeasureString(text, font, wrapWidth); + using(var bmp = new System.Drawing.Bitmap((int)measure.X, (int)measure.Y)) { - var textformat = new System.Drawing.StringFormat(System.Drawing.StringFormat.GenericTypographic); - textformat.FormatFlags = System.Drawing.StringFormatFlags.MeasureTrailingSpaces; - textformat.Trimming = System.Drawing.StringTrimming.None; - textformat.FormatFlags |= System.Drawing.StringFormatFlags.NoClip; - - gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel; - gfx.DrawString(text, font, new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B)), 0, 0, textformat); - } - var lck = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); - var data = new byte[Math.Abs(lck.Stride) * lck.Height]; - System.Runtime.InteropServices.Marshal.Copy(lck.Scan0, data, 0, data.Length); - bmp.UnlockBits(lck); - using (var tex2 = new Texture2D(_graphicsDevice, bmp.Width, bmp.Height)) - { - for (int i = 0; i < data.Length; i += 4) + using(var gfx = System.Drawing.Graphics.FromImage(bmp)) { - byte r = data[i]; - byte b = data[i + 2]; - data[i] = b; - data[i + 2] = r; + var sFormat = System.Drawing.StringFormat.GenericTypographic; + sFormat.FormatFlags |= System.Drawing.StringFormatFlags.NoClip; + + gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; + + gfx.DrawString(text, font, System.Drawing.Brushes.White, 0, 0, sFormat); } + var lck = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); + var data = new byte[Math.Abs(lck.Stride) * lck.Height]; + System.Runtime.InteropServices.Marshal.Copy(lck.Scan0, data, 0, data.Length); + var tex2 = new Texture2D(_graphicsDevice, bmp.Width, bmp.Height); tex2.SetData(data); - _spritebatch.Draw(tex2, new Rectangle(x, y, bmp.Width, bmp.Height), Color.White); + fontcache = new TextCache(); + fontcache.Text = text; + fontcache.FontFamily = font; + fontcache.WrapWidth = wrapWidth; + fontcache.Cache = tex2; + StringCaches.Add(fontcache); } } + _spritebatch.Draw(fontcache.Cache, new Rectangle(x, y, fontcache.Cache.Width, fontcache.Cache.Height), color); + + } private float getRotation(float x, float y, float x2, float y2) @@ -192,4 +195,12 @@ namespace ShiftOS.Frontend.GraphicsSubsystem return res; } } + + public class TextCache + { + public string Text { get; set; } + public System.Drawing.Font FontFamily { get; set; } + public Texture2D Cache { get; set; } + public int WrapWidth { get; set; } + } } diff --git a/ShiftOS.Frontend/ShiftOS.Frontend.csproj b/ShiftOS.Frontend/ShiftOS.Frontend.csproj index 17c497e..7cc9513 100644 --- a/ShiftOS.Frontend/ShiftOS.Frontend.csproj +++ b/ShiftOS.Frontend/ShiftOS.Frontend.csproj @@ -80,6 +80,10 @@ + + ..\packages\TrueTypeSharp.1.0.5\lib\net20\TrueTypeSharp.dll + True + diff --git a/ShiftOS.Frontend/ShiftOS.cs b/ShiftOS.Frontend/ShiftOS.cs index 9ad83da..1ab0ad1 100644 --- a/ShiftOS.Frontend/ShiftOS.cs +++ b/ShiftOS.Frontend/ShiftOS.cs @@ -98,6 +98,8 @@ namespace ShiftOS.Frontend } + private double timeSinceLastPurge = 0; + private Texture2D MouseTexture = null; /// @@ -214,6 +216,14 @@ namespace ShiftOS.Frontend //Cause layout update on all elements UIManager.LayoutUpdate(); + timeSinceLastPurge += gameTime.ElapsedGameTime.TotalSeconds; + + if(timeSinceLastPurge > 30) + { + GraphicsContext.StringCaches.Clear(); + timeSinceLastPurge = 0; + } + base.Update(gameTime); } @@ -252,7 +262,7 @@ namespace ShiftOS.Frontend { var gfxContext = new GraphicsContext(GraphicsDevice.GraphicsDevice, spriteBatch, 0, 0, GraphicsDevice.PreferredBackBufferWidth, GraphicsDevice.PreferredBackBufferHeight); var color = Color.White; - double fps = 1 / gameTime.ElapsedGameTime.TotalSeconds; + double fps = Math.Round(1 / gameTime.ElapsedGameTime.TotalSeconds); if (fps <= 20) color = Color.Red; gfxContext.DrawString($@"ShiftOS 1.0 Beta 4 diff --git a/ShiftOS.Frontend/packages.config b/ShiftOS.Frontend/packages.config index ee51c23..223a8d3 100644 --- a/ShiftOS.Frontend/packages.config +++ b/ShiftOS.Frontend/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file