@@ -16,6 +16,11 @@ namespace BizHawk.Client.Common
1616{
1717 public sealed class GuiApi : IGuiApi
1818 {
19+ private static Bitmap /*?*/ field = null ;
20+
21+ private static Bitmap NullGraphicsBitmap
22+ => field ??= new ( 1 , 1 ) ;
23+
1924 private static readonly StringFormat PixelTextFormat = new ( StringFormat . GenericTypographic )
2025 {
2126 FormatFlags = StringFormatFlags . MeasureTrailingSpaces ,
@@ -32,8 +37,6 @@ public sealed class GuiApi : IGuiApi
3237
3338 private readonly Dictionary < string , Bitmap > _imageCache = new ( ) ;
3439
35- private readonly Bitmap _nullGraphicsBitmap = new ( 1 , 1 ) ;
36-
3740 private CompositingMode _compositingMode = CompositingMode . SourceOver ;
3841
3942 private Color ? _defaultBackground ;
@@ -46,7 +49,7 @@ public sealed class GuiApi : IGuiApi
4649
4750 private ( int Left , int Top , int Right , int Bottom ) _padding = ( 0 , 0 , 0 , 0 ) ;
4851
49- private readonly FontFamily [ ] _pixelFonts ;
52+ private FontFamily [ ] /*?*/ _pixelFonts = null ;
5053
5154 private DisplaySurfaceID ? _usingSurfaceID ;
5255 public bool HasGUISurface => true ;
@@ -59,12 +62,14 @@ public GuiApi(
5962 _dialogController = dialogController ;
6063 LogCallback = logCallback ;
6164 _displayManager = displayManager ;
62- _pixelFonts = ( new [ ]
65+ }
66+
67+ private IReadOnlyList < FontFamily > PixelFonts
68+ => _pixelFonts ??= ( new [ ]
6369 {
6470 "fceux" ,
6571 "gens" ,
6672 } ) . Select ( s => _displayManager . CustomFonts . Families . First ( f => f . Name . EqualsIgnoreCase ( s ) ) ) . ToArray ( ) ;
67- }
6873
6974 private I2DRenderer Get2DRenderer ( DisplaySurfaceID ? surfaceID )
7075 {
@@ -310,7 +315,7 @@ public void DrawImage(string path, int x, int y, int? width = null, int? height
310315 public void ClearImageCache ( )
311316 {
312317 _imageCache . Clear ( ) ;
313- _displayManager . ClearApiHawkTextureCache ( ) ;
318+ _displayManager ? . ClearApiHawkTextureCache ( ) ;
314319 }
315320
316321 public void DrawImageRegion ( Image img , int source_x , int source_y , int source_width , int source_height , int dest_x , int dest_y , int ? dest_width = null , int ? dest_height = null , DisplaySurfaceID ? surfaceID = null )
@@ -432,7 +437,7 @@ public void DrawString(int x, int y, string message, Color? forecolor = null, Co
432437 _ => FontStyle . Regular ,
433438 } ;
434439
435- using var g = Graphics . FromImage ( _nullGraphicsBitmap ) ;
440+ using var g = Graphics . FromImage ( NullGraphicsBitmap ) ;
436441
437442 // The text isn't written out using GenericTypographic, so measuring it using GenericTypographic seemed to make it worse.
438443 // And writing it out with GenericTypographic just made it uglier. :p
@@ -527,8 +532,8 @@ public void PixelText(
527532 break ;
528533 }
529534
530- using var g = Graphics . FromImage ( _nullGraphicsBitmap ) ;
531- var font = new Font ( _pixelFonts [ index ] , 8 , FontStyle . Regular , GraphicsUnit . Pixel ) ;
535+ using var g = Graphics . FromImage ( NullGraphicsBitmap ) ;
536+ Font font = new ( PixelFonts [ index ] , 8 , FontStyle . Regular , GraphicsUnit . Pixel ) ;
532537 var sizeOfText = g . MeasureString ( message , font , width : 0 , PixelTextFormat ) . ToSize ( ) ;
533538
534539 var r = Get2DRenderer ( surfaceID ) ;
0 commit comments