diff options
| author | Michael <[email protected]> | 2017-07-05 08:43:35 -0400 |
|---|---|---|
| committer | Michael <[email protected]> | 2017-07-05 08:43:35 -0400 |
| commit | 4f41f51267e04d752d3d438f70f06a7a37975f5d (patch) | |
| tree | 22036cb834d9915d44c527da55adfdab58a3d529 /ShiftOS.Frontend/Apps | |
| parent | 2adb8859edb95921e8f6d3cbb41fdc349825d6f8 (diff) | |
| download | shiftos_thereturn-4f41f51267e04d752d3d438f70f06a7a37975f5d.tar.gz shiftos_thereturn-4f41f51267e04d752d3d438f70f06a7a37975f5d.tar.bz2 shiftos_thereturn-4f41f51267e04d752d3d438f70f06a7a37975f5d.zip | |
HEAVILY optimize skins
Diffstat (limited to 'ShiftOS.Frontend/Apps')
| -rw-r--r-- | ShiftOS.Frontend/Apps/Pong.cs | 171 | ||||
| -rw-r--r-- | ShiftOS.Frontend/Apps/Terminal.cs | 48 |
2 files changed, 196 insertions, 23 deletions
diff --git a/ShiftOS.Frontend/Apps/Pong.cs b/ShiftOS.Frontend/Apps/Pong.cs new file mode 100644 index 0000000..595f8ce --- /dev/null +++ b/ShiftOS.Frontend/Apps/Pong.cs @@ -0,0 +1,171 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Xna.Framework; +using ShiftOS.Engine; +using ShiftOS.Frontend.GraphicsSubsystem; + +namespace ShiftOS.Frontend.Apps +{ + [Launcher("{TITLE_PONG}", true, "al_pong", "{AL_GAMES}")] + [WinOpen("{WO_PONG}")] + [DefaultTitle("{TITLE_PONG}")] + [DefaultIcon("iconPong")] + public class Pong : GUI.Control, IShiftOSWindow + { + public Pong() + { + Width = 720; + Height = 480; + MouseMove += (loc) => + { + double _y = linear(loc.Y, 0, Height, -1, 1); + if(_y != playerY) + { + playerY = _y; + Invalidate(); + } + }; + } + + #region Private variables + private double ballX = 0.0f; + private double ballY = 0.0f; + private double aiBallX = 0.0f; + private double aiBallY = 0.0f; + private double speedFactor = 0.0125; + private double xVel = 1; + private double yVel = 1; + private double aiXVel = 1; + private double aiYVel = 1; + private int paddleWidth; + private long codepointsToEarn = 0; + private int level = 1; + private double playerY = 0.0; + private double opponentY = 0.0; + private int secondsleft = 60; + bool doAi = true; + bool doBallCalc = true; + private string header = ""; + private string counter = ""; + #endregion + + #region Control behaviour overrides + + protected override void OnPaint(GraphicsContext gfx) + { + //This is where we'll dump the winforms painting code + //By now, Layout() would have calculated the game's state + + paddleWidth = Width / 30; + double ballXLocal = linear(ballX, -1.0, 1.0, 0, Width); + double ballYLocal = linear(ballY, -1.0, 1.0, 0, Height); + + ballXLocal -= ((double)paddleWidth / 2); + ballYLocal -= ((double)paddleWidth / 2); + + double aiballXLocal = linear(aiBallX, -1.0, 1.0, 0, Width); + double aiballYLocal = linear(aiBallY, -1.0, 1.0, 0, Height); + + aiballXLocal -= ((double)paddleWidth / 2); + aiballYLocal -= ((double)paddleWidth / 2); + + + gfx.Clear(SkinEngine.LoadedSkin.ControlColor.ToMonoColor()); + + //draw the ball + if (doBallCalc) + { + gfx.DrawRectangle((int)ballXLocal, (int)ballYLocal, paddleWidth, paddleWidth, SkinEngine.LoadedSkin.ControlTextColor.ToMonoColor()); + } + double playerYLocal = linear(playerY, -1.0, 1.0, 0, Height); + double opponentYLocal = linear(opponentY, -1.0, 1.0, 0, Height); + + int paddleHeight = Height / 5; + + int paddleStart = paddleWidth; + + //draw player paddle + gfx.DrawRectangle(paddleWidth, (int)playerYLocal - (paddleHeight / 2), paddleWidth, paddleHeight, SkinEngine.LoadedSkin.ControlTextColor.ToMonoColor()); + + + //draw opponent + gfx.DrawRectangle(Width - (paddleWidth*2), (int)opponentYLocal - (paddleHeight / 2), paddleWidth, paddleHeight, SkinEngine.LoadedSkin.ControlTextColor.ToMonoColor()); + + string cp_text = Localization.Parse("{PONG_STATUSCP}", new Dictionary<string, string> + { + ["%cp"] = codepointsToEarn.ToString() + }); + + var tSize = gfx.MeasureString(cp_text, SkinEngine.LoadedSkin.Header3Font); + + var tLoc = new Vector2((Width - (int)tSize.X) / 2, + (Height - (int)tSize.Y) + + ); + + gfx.DrawString(cp_text, (int)tLoc.X, (int)tLoc.Y, SkinEngine.LoadedSkin.ControlTextColor.ToMonoColor(), SkinEngine.LoadedSkin.Header3Font); + + tSize = gfx.MeasureString(counter, SkinEngine.LoadedSkin.Header2Font); + + tLoc = new Vector2((Width - (int)tSize.X) / 2, + (Height - (int)tSize.Y) / 2 + + ); + gfx.DrawString(counter, (int)tLoc.X, (int)tLoc.Y, SkinEngine.LoadedSkin.ControlTextColor.ToMonoColor(), SkinEngine.LoadedSkin.Header2Font); + tSize = gfx.MeasureString(header, SkinEngine.LoadedSkin.Header2Font); + + tLoc = new Vector2((Width - (int)tSize.X) / 2, + (Height - (int)tSize.Y) / 4 + + ); + gfx.DrawString(header, (int)tLoc.X, (int)tLoc.Y, SkinEngine.LoadedSkin.ControlTextColor.ToMonoColor(), SkinEngine.LoadedSkin.Header2Font); + + string l = Localization.Parse("{PONG_STATUSLEVEL}", new Dictionary<string, string> + { + ["%level"] = level.ToString(), + ["%time"] = secondsleft.ToString() + }); + tSize = gfx.MeasureString(l, SkinEngine.LoadedSkin.Header3Font); + + tLoc = new Vector2((Width - (int)tSize.X) / 2, + (tSize.Y) + ); + gfx.DrawString(l, (int)tLoc.X, (int)tLoc.Y, SkinEngine.LoadedSkin.ControlTextColor.ToMonoColor(), SkinEngine.LoadedSkin.Header3Font); + + + } + + #endregion + + + static public double linear(double x, double x0, double x1, double y0, double y1) + { + if ((x1 - x0) == 0) + { + return (y0 + y1) / 2; + } + return y0 + (x - x0) * (y1 - y0) / (x1 - x0); + } + + public void OnLoad() + { + doBallCalc = true; + } + + public void OnSkinLoad() + { + } + + public bool OnUnload() + { + return true; + } + + public void OnUpgrade() + { + } + } +} diff --git a/ShiftOS.Frontend/Apps/Terminal.cs b/ShiftOS.Frontend/Apps/Terminal.cs index a5d465e..67c7f7f 100644 --- a/ShiftOS.Frontend/Apps/Terminal.cs +++ b/ShiftOS.Frontend/Apps/Terminal.cs @@ -144,21 +144,19 @@ namespace ShiftOS.Frontend.Apps if(!string.IsNullOrEmpty(Text)) using (var gfx = Graphics.FromImage(new Bitmap(1, 1))) { - var cursorpos = GetPointAtIndex(gfx); - var caretSize = gfx.SmartMeasureString(Text.ToString(), LoadedSkin.TerminalFont, Width - 4); - float initial = (((float)Math.Floor(caretSize.Height)) + cursorpos.Y) - _vertOffset; - if (initial < 0) - { - float difference = initial - Height; - _vertOffset = initial + difference; - } - if (initial > Height) - { - float difference = initial - Height; - _vertOffset = initial - difference; + var textsize = gfx.SmartMeasureString(Text, LoadedSkin.TerminalFont, Width); + float initial = textsize.Height - _vertOffset; + if(initial > Height) + { + float difference = Height - initial; + _vertOffset = initial - difference; + } + else if(initial < 0) + { + float difference = Height - initial; + _vertOffset = initial + difference; + } } - - } } protected override void OnLayout() @@ -181,7 +179,7 @@ namespace ShiftOS.Frontend.Apps for (int l = 0; l < line; l++) { lineindex += Lines[l].Length; - var stringMeasure = gfx.SmartMeasureString(Lines[l], LoadedSkin.TerminalFont, Width - 4); + var stringMeasure = gfx.SmartMeasureString(Lines[l] == "\r" ? " " : Lines[l], LoadedSkin.TerminalFont, Width - 4); vertMeasure += (int)stringMeasure.Height; } @@ -320,7 +318,14 @@ namespace ShiftOS.Frontend.Apps //Draw the caret. if (IsFocusedControl) { -// gfx.FillRectangle(new SolidBrush(LoadedSkin.TerminalForeColorCC.ToColor()), new RectangleF(new PointF(CaretPosition.X, CaretPosition.Y - _vertOffset), new SizeF(2, CaretSize.Height))); + PointF cursorPos; + using (var cgfx = System.Drawing.Graphics.FromImage(new System.Drawing.Bitmap(1, 1))) + { + cursorPos = GetPointAtIndex(cgfx); + + } + var cursorSize = gfx.MeasureString(Text[Index-1].ToString(), LoadedSkin.TerminalFont); + gfx.DrawRectangle((int)cursorPos.X, (int)cursorPos.Y - (int)_vertOffset, (int)cursorSize.X, (int)cursorSize.Y, LoadedSkin.TerminalForeColorCC.ToColor().ToMonoColor()); }//Draw the text @@ -382,19 +387,16 @@ namespace ShiftOS.Frontend.Apps var textformat = new StringFormat(StringFormat.GenericTypographic); textformat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; textformat.Trimming = StringTrimming.None; + textformat.FormatFlags |= StringFormatFlags.NoClip; + + gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; var measure = gfx.MeasureString(s, font, width, textformat); return new SizeF((float)Math.Ceiling(measure.Width), (float)Math.Ceiling(measure.Height)); } public static SizeF SmartMeasureString(this Graphics gfx, string s, Font font) { - if (string.IsNullOrEmpty(s)) - s = " "; - var textformat = new StringFormat(StringFormat.GenericTypographic); - textformat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; - textformat.Trimming = StringTrimming.None; - var measure = gfx.MeasureString(s, font, int.MaxValue, textformat); - return new SizeF((float)Math.Ceiling(measure.Width), (float)Math.Floor(measure.Height)); + return SmartMeasureString(gfx, s, font, int.MaxValue); } } |
