HEAVILY optimize the game

This commit is contained in:
Michael 2017-07-06 18:25:19 -04:00
parent 38774ba793
commit ca339b2661
7 changed files with 80 additions and 49 deletions

View file

@ -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<PanelButtonData> PanelButtons = new List<PanelButtonData>();
@ -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;

View file

@ -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

View file

@ -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);

View file

@ -139,45 +139,48 @@ namespace ShiftOS.Frontend.GraphicsSubsystem
}
}
public static List<TextCache> StringCaches = new List<TextCache>();
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<byte>(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; }
}
}

View file

@ -80,6 +80,10 @@
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="TrueTypeSharp, Version=1.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TrueTypeSharp.1.0.5\lib\net20\TrueTypeSharp.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Icon.ico" />

View file

@ -98,6 +98,8 @@ namespace ShiftOS.Frontend
}
private double timeSinceLastPurge = 0;
private Texture2D MouseTexture = null;
/// <summary>
@ -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

View file

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
<package id="TrueTypeSharp" version="1.0.5" targetFramework="net45" />
</packages>