From a882312b5483a195444ff607a363fe04fbecbcba Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 27 Jan 2016 21:02:58 +1100 Subject: [PATCH] Fix crashing of launcher after minimising in classic mode. --- Launcher2/Gui/Drawer2DExt.cs | 20 ++++++- .../Gui/Screens/ClassiCubeServersScreen.cs | 23 +++++--- Launcher2/Gui/Screens/DirectConnectScreen.cs | 7 ++- Launcher2/Gui/Screens/LauncherInputScreen.cs | 10 ++-- Launcher2/Gui/Screens/LauncherScreen.cs | 28 +++++----- .../Gui/Screens/MainScreen.Classicube.cs | 20 +++---- Launcher2/Gui/Screens/MainScreen.cs | 4 +- Launcher2/LauncherWindow.Background.cs | 55 +++++++++++-------- 8 files changed, 97 insertions(+), 70 deletions(-) diff --git a/Launcher2/Gui/Drawer2DExt.cs b/Launcher2/Gui/Drawer2DExt.cs index ef5c0de3d..367a990c7 100644 --- a/Launcher2/Gui/Drawer2DExt.cs +++ b/Launcher2/Gui/Drawer2DExt.cs @@ -39,9 +39,9 @@ namespace Launcher2 { public unsafe static void DrawNoise( FastBitmap dst, Rectangle dstRect, FastColour col, int variation ) { int dstWidth = dstRect.Width, dstHeight = dstRect.Height; - int dstX = dstRect.X, dstY = dstRect.Y; - + int dstX = dstRect.X, dstY = dstRect.Y; if( dstX >= dst.Width || dstY >= dst.Height ) return; + dstWidth = Math.Min( dstX + dstWidth, dst.Width ) - dstX; dstHeight = Math.Min( dstY + dstHeight, dst.Height ) - dstY; const int alpha = 255 << 24; @@ -58,6 +58,22 @@ namespace Launcher2 { } } + public unsafe static void FastClear( FastBitmap dst, Rectangle dstRect, FastColour col ) { + int dstWidth = dstRect.Width, dstHeight = dstRect.Height; + int dstX = dstRect.X, dstY = dstRect.Y; + if( dstX >= dst.Width || dstY >= dst.Height ) return; + + dstWidth = Math.Min( dstX + dstWidth, dst.Width ) - dstX; + dstHeight = Math.Min( dstY + dstHeight, dst.Height ) - dstY; + int pixel = col.ToArgb(); + + for( int yy = 0; yy < dstHeight; yy++ ) { + int* row = dst.GetRowPtr( dstY + yy ); + for( int xx = 0; xx < dstWidth; xx++ ) + row[dstX + xx] = pixel; + } + } + static float Noise( int x, int y ) { int n = x + y * 57; n = (n << 13) ^ n; diff --git a/Launcher2/Gui/Screens/ClassiCubeServersScreen.cs b/Launcher2/Gui/Screens/ClassiCubeServersScreen.cs index 5cfe18caf..e1f0ae98c 100644 --- a/Launcher2/Gui/Screens/ClassiCubeServersScreen.cs +++ b/Launcher2/Gui/Screens/ClassiCubeServersScreen.cs @@ -84,9 +84,9 @@ namespace Launcher2 { } public override void Resize() { + DrawBackground(); using( drawer ) { drawer.SetBitmap( game.Framebuffer ); - game.ClearArea( 0, 0, game.Width, tableY ); LauncherTableWidget table = (LauncherTableWidget)widgets[tableIndex]; if( table != null ) @@ -111,14 +111,23 @@ namespace Launcher2 { MakeTableWidget(); } + void DrawBackground() { + using( FastBitmap dst = new FastBitmap( game.Framebuffer, true ) ) { + game.ClearArea( 0, 0, game.Width, tableY, dst ); + int tableHeight = Math.Max( game.Height - tableY - 50, 1 ); + Rectangle rec = new Rectangle( tableX, tableY, game.Width - tableX, tableHeight ); + + if( !game.ClassicMode ) { + FastColour col = LauncherTableWidget.backGridCol; + Drawer2DExt.FastClear( dst, rec, col ); + } else { + game.ClearArea( rec.X, rec.Y, rec.Width, rec.Height, dst ); + } + } + } + void MakeTableWidget() { int tableHeight = Math.Max( game.Height - tableY - 50, 1 ); - FastColour col = LauncherTableWidget.backGridCol; - if( !game.ClassicMode ) - drawer.Clear( col, tableX, tableY, game.Width - tableX, tableHeight ); - else - game.ClearArea( tableX, tableY, game.Width - tableX, tableHeight ); - if( widgets[tableIndex] != null ) { LauncherTableWidget table = (LauncherTableWidget)widgets[tableIndex]; table.Height = tableHeight; diff --git a/Launcher2/Gui/Screens/DirectConnectScreen.cs b/Launcher2/Gui/Screens/DirectConnectScreen.cs index 95a45fe11..46839a163 100644 --- a/Launcher2/Gui/Screens/DirectConnectScreen.cs +++ b/Launcher2/Gui/Screens/DirectConnectScreen.cs @@ -51,10 +51,11 @@ namespace Launcher2 { } void SetStatus( string text ) { + LauncherLabelWidget widget = (LauncherLabelWidget)widgets[5]; + game.ClearArea( widget.X, widget.Y, widget.Width, widget.Height ); + using( drawer ) { - drawer.SetBitmap( game.Framebuffer ); - LauncherLabelWidget widget = (LauncherLabelWidget)widgets[5]; - game.ClearArea( widget.X, widget.Y, widget.Width, widget.Height ); + drawer.SetBitmap( game.Framebuffer ); widget.DrawAt( drawer, text, inputFont, Anchor.Centre, Anchor.Centre, 0, 100 ); Dirty = true; } diff --git a/Launcher2/Gui/Screens/LauncherInputScreen.cs b/Launcher2/Gui/Screens/LauncherInputScreen.cs index 96c744e3a..011bd9647 100644 --- a/Launcher2/Gui/Screens/LauncherInputScreen.cs +++ b/Launcher2/Gui/Screens/LauncherInputScreen.cs @@ -57,8 +57,8 @@ namespace Launcher2 { HandleTab(); } if( lastInput == null ) { - if( e.Key == Key.Escape ) - game.SetScreen( new MainScreen( game ) ); + if( e.Key == Key.Escape ) + game.SetScreen( new MainScreen( game ) ); return; } @@ -113,11 +113,11 @@ namespace Launcher2 { } protected virtual void RedrawLastInput() { + if( lastInput.Width > lastInput.ButtonWidth ) + game.ClearArea( lastInput.X, lastInput.Y, lastInput.Width + 1, lastInput.Height + 1 ); + using( drawer ) { drawer.SetBitmap( game.Framebuffer ); - if( lastInput.Width > lastInput.ButtonWidth ) - game.ClearArea( lastInput.X, lastInput.Y, - lastInput.Width + 1, lastInput.Height + 1 ); lastInput.Redraw( drawer, lastInput.Text, inputFont, inputHintFont ); Dirty = true; } diff --git a/Launcher2/Gui/Screens/LauncherScreen.cs b/Launcher2/Gui/Screens/LauncherScreen.cs index 8fb36daf9..80f91f6c0 100644 --- a/Launcher2/Gui/Screens/LauncherScreen.cs +++ b/Launcher2/Gui/Screens/LauncherScreen.cs @@ -44,22 +44,16 @@ namespace Launcher2 { e.X < widget.X + widget.Width && e.Y < widget.Y + widget.Height ) { if( selectedWidget == widget ) return; - using( drawer ) { - drawer.SetBitmap( game.Framebuffer ); - if( selectedWidget != null ) - UnselectWidget( selectedWidget ); - SelectWidget( widget ); - } + if( selectedWidget != null ) + UnselectWidget( selectedWidget ); + SelectWidget( widget ); selectedWidget = widget; return; } } if( selectedWidget == null ) return; - using( drawer ) { - drawer.SetBitmap( game.Framebuffer ); - UnselectWidget( selectedWidget ); - } + UnselectWidget( selectedWidget ); selectedWidget = null; } @@ -70,7 +64,10 @@ namespace Launcher2 { LauncherButtonWidget button = widget as LauncherButtonWidget; if( button != null ) { button.Active = false; - button.Redraw( drawer, button.Text, buttonFont ); + using( drawer ) { + drawer.SetBitmap( game.Framebuffer ); + button.Redraw( drawer, button.Text, buttonFont ); + } Dirty = true; } } @@ -80,7 +77,10 @@ namespace Launcher2 { LauncherButtonWidget button = widget as LauncherButtonWidget; if( button != null ) { button.Active = true; - button.Redraw( drawer, button.Text, buttonFont ); + using( drawer ) { + drawer.SetBitmap( game.Framebuffer ); + button.Redraw( drawer, button.Text, buttonFont ); + } Dirty = true; } } @@ -92,7 +92,7 @@ namespace Launcher2 { if( lastClicked != null && lastClicked != selectedWidget ) WidgetUnclicked( lastClicked ); if( selectedWidget != null && selectedWidget.OnClick != null ) - selectedWidget.OnClick( e.X, e.Y ); + selectedWidget.OnClick( e.X, e.Y ); lastClicked = selectedWidget; } @@ -107,7 +107,7 @@ namespace Launcher2 { tabDown = true; int index = lastClicked == null ? -1 : Array.IndexOf( widgets, lastClicked ); - int dir = (game.Window.Keyboard[Key.ShiftLeft] + int dir = (game.Window.Keyboard[Key.ShiftLeft] || game.Window.Keyboard[Key.ShiftRight]) ? -1 : 1; index += dir; Utils.Clamp( ref index, 0, widgets.Length - 1); diff --git a/Launcher2/Gui/Screens/MainScreen.Classicube.cs b/Launcher2/Gui/Screens/MainScreen.Classicube.cs index 8a32c6f5a..fca366ba4 100644 --- a/Launcher2/Gui/Screens/MainScreen.Classicube.cs +++ b/Launcher2/Gui/Screens/MainScreen.Classicube.cs @@ -52,25 +52,19 @@ namespace Launcher2 { string lastStatus; void SetStatus( string text ) { - using( drawer ) { - drawer.SetBitmap( game.Framebuffer ); - SetStatusNoLock( text ); - } - } - - void SetStatusNoLock( string text ) { lastStatus = text; LauncherLabelWidget widget = (LauncherLabelWidget)widgets[3]; game.ClearArea( widget.X, widget.Y, widget.Width, widget.Height ); - widget.DrawAt( drawer, text, inputFont, Anchor.Centre, Anchor.Centre, 0, 20 ); + using( drawer ) { + drawer.SetBitmap( game.Framebuffer ); + widget.DrawAt( drawer, text, inputFont, Anchor.Centre, Anchor.Centre, 0, 20 ); + } Dirty = true; } bool HasServers { - get { - return !(game.Session.Servers == null || game.Session.Servers.Count == 0 ); - } + get { return game.Session.Servers != null && game.Session.Servers.Count != 0; } } bool signingIn; @@ -83,7 +77,7 @@ namespace Launcher2 { } if( signingIn ) return; UpdateSignInInfo( Get( 0 ), Get( 1 ) ); - + LauncherBooleanWidget booleanWidget = widgets[skipSSLIndex] as LauncherBooleanWidget; if( booleanWidget != null && booleanWidget.Value ) { ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; @@ -123,7 +117,7 @@ namespace Launcher2 { SetStatus( text ); using( drawer ) { drawer.SetBitmap( game.Framebuffer ); - widgetIndex = 9; + widgetIndex = 9; MakeSSLSkipValidationBoolean(); MakeSSLSkipValidationLabel(); } diff --git a/Launcher2/Gui/Screens/MainScreen.cs b/Launcher2/Gui/Screens/MainScreen.cs index d3b3c67a2..cd45f1d4c 100644 --- a/Launcher2/Gui/Screens/MainScreen.cs +++ b/Launcher2/Gui/Screens/MainScreen.cs @@ -107,13 +107,13 @@ namespace Launcher2 { base.SelectWidget( widget ); if( signingIn || !resumeValid || widget != widgets[4] ) return; const string format = "&eResume to {0}:{1}, as {2}"; - SetStatusNoLock( String.Format( format, resumeIp, resumePort, resumeUser ) ); + SetStatus( String.Format( format, resumeIp, resumePort, resumeUser ) ); } protected override void UnselectWidget( LauncherWidget widget ) { base.UnselectWidget( widget ); if( signingIn || !resumeValid || widget != widgets[4] ) return; - SetStatusNoLock( "" ); + SetStatus( "" ); } public override void Dispose() { diff --git a/Launcher2/LauncherWindow.Background.cs b/Launcher2/LauncherWindow.Background.cs index 361c877b2..024a7d02e 100644 --- a/Launcher2/LauncherWindow.Background.cs +++ b/Launcher2/LauncherWindow.Background.cs @@ -60,6 +60,16 @@ namespace Launcher2 { Framebuffer.Dispose(); Framebuffer = new Bitmap( Width, Height ); } + + if( ClassicMode ) { + using( FastBitmap dst = new FastBitmap( Framebuffer, true ) ) { + ClearTile( 0, 0, Width, 48, elemSize, 128, 64, dst ); + ClearTile( 0, 48, Width, Height - 48, 0, 96, 96, dst ); + } + } else { + ClearArea( 0, 0, Width, Height ); + } + using( IDrawer2D drawer = Drawer ) { drawer.SetBitmap( Framebuffer ); @@ -67,49 +77,46 @@ namespace Launcher2 { DrawTextArgs args = new DrawTextArgs( "&eClassical&fSharp", logoFont, false ); Size size = drawer.MeasureChatSize( ref args ); int xStart = Width / 2 - size.Width / 2; - if( ClassicMode ) - ClearTile( 0, 0, Width, 48, elemSize, 128, 64 ); - else - ClearArea( 0, 0, Width, Height ); args.Text = "&0Classical&0Sharp"; drawer.DrawChatText( ref args, xStart + 4, 8 + 4 ); args.Text = "&eClassical&fSharp"; drawer.DrawChatText( ref args, xStart, 8 ); drawer.UseBitmappedChat = false; - if( ClassicMode ) - ClearTile( 0, 48, Width, Height - 48, 0, 96, 96 ); } Dirty = true; } public void ClearArea( int x, int y, int width, int height ) { + using( FastBitmap dst = new FastBitmap( Framebuffer, true ) ) + ClearArea( x, y, width, height, dst ); + } + + public void ClearArea( int x, int y, int width, int height, FastBitmap dst ) { if( ClassicMode ) { - ClearTile( x, y, width, height, 0, 96, 96 ); + ClearTile( x, y, width, height, 0, 96, 96, dst ); } else { FastColour col = LauncherSkin.BackgroundCol; - using( FastBitmap dst = new FastBitmap( Framebuffer, true ) ) - Drawer2DExt.DrawNoise( dst, new Rectangle( x, y, width, height ), col, 6 ); + Drawer2DExt.DrawNoise( dst, new Rectangle( x, y, width, height ), col, 6 ); } } - void ClearTile( int x, int y, int width, int height, int srcX, byte scaleA, byte scaleB ) { + void ClearTile( int x, int y, int width, int height, int srcX, + byte scaleA, byte scaleB, FastBitmap dst ) { if( x >= Width || y >= Height ) return; - using( FastBitmap dst = new FastBitmap( Framebuffer, true ) ) { - Rectangle srcRect = new Rectangle( srcX, 0, elemSize, elemSize ); - const int tileSize = 48; - Size size = new Size( tileSize, tileSize ); - int xOrig = x, xMax = x + width, yMax = y + height; + Rectangle srcRect = new Rectangle( srcX, 0, elemSize, elemSize ); + const int tileSize = 48; + Size size = new Size( tileSize, tileSize ); + int xOrig = x, xMax = x + width, yMax = y + height; + + for( ; y < yMax; y += tileSize ) + for( x = xOrig; x < xMax; x += tileSize ) + { + int x2 = Math.Min( x + tileSize, Math.Min( xMax, Width ) ); + int y2 = Math.Min( y + tileSize, Math.Min( yMax, Height ) ); - for( ; y < yMax; y += tileSize ) - for( x = xOrig; x < xMax; x += tileSize ) - { - int x2 = Math.Min( x + tileSize, Math.Min( xMax, Width ) ); - int y2 = Math.Min( y + tileSize, Math.Min( yMax, Height ) ); - - Rectangle dstRect = new Rectangle( x, y, x2 - x, y2 - y ); - Drawer2DExt.DrawScaledPixels( terrainPixels, dst, size, srcRect, dstRect, scaleA, scaleB ); - } + Rectangle dstRect = new Rectangle( x, y, x2 - x, y2 - y ); + Drawer2DExt.DrawScaledPixels( terrainPixels, dst, size, srcRect, dstRect, scaleA, scaleB ); } } }