2727using OpenBveApi . Colors ;
2828using OpenBveApi . Hosts ;
2929using OpenBveApi . Objects ;
30+ using Buffer = System . Buffer ;
3031using ButtonState = OpenTK . Input . ButtonState ;
3132using LibRender2 . Menu ;
3233using LibRender2 . Screens ;
@@ -180,7 +181,7 @@ internal static void Main(string[] args)
180181 }
181182
182183 // load route
183- internal static bool LoadRoute ( Bitmap bitmap = null ) {
184+ internal static bool LoadRoute ( byte [ ] textureBytes = null ) {
184185 if ( string . IsNullOrEmpty ( CurrentRouteFile ) )
185186 {
186187 return false ;
@@ -190,7 +191,7 @@ internal static bool LoadRoute(Bitmap bitmap = null) {
190191 try
191192 {
192193 Encoding encoding = TextEncoding . GetSystemEncodingFromFile ( CurrentRouteFile ) ;
193- Loading . Load ( CurrentRouteFile , encoding , bitmap ) ;
194+ Loading . Load ( CurrentRouteFile , encoding , textureBytes ) ;
194195 result = true ;
195196 } catch ( Exception ex ) {
196197 MessageBox . Show ( ex . Message , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Hand ) ;
@@ -419,25 +420,33 @@ internal static void keyDownEvent(object sender, KeyboardKeyEventArgs e)
419420 {
420421 return ;
421422 }
423+ byte [ ] textureBytes = { } ;
422424 if ( CurrentRouteFile != null && CurrentlyLoading == false )
423425 {
424-
425- Bitmap bitmap = null ;
426426 CurrentlyLoading = true ;
427427 Renderer . OptionInterface = false ;
428428 if ( ! Interface . CurrentOptions . LoadingBackground )
429429 {
430430 Renderer . RenderScene ( 0.0 ) ;
431431 currentGameWindow . SwapBuffers ( ) ;
432- bitmap = new Bitmap ( Renderer . Screen . Width , Renderer . Screen . Height , System . Drawing . Imaging . PixelFormat . Format32bppArgb ) ;
433- BitmapData bData = bitmap . LockBits ( new Rectangle ( 0 , 0 , bitmap . Width , bitmap . Height ) , ImageLockMode . ReadWrite , bitmap . PixelFormat ) ;
434- GL . ReadPixels ( 0 , 0 , Renderer . Screen . Width , Renderer . Screen . Height , OpenTK . Graphics . OpenGL . PixelFormat . Bgra , PixelType . UnsignedByte , bData . Scan0 ) ;
435- bitmap . UnlockBits ( bData ) ;
436- bitmap . RotateFlip ( RotateFlipType . RotateNoneFlipY ) ;
432+ textureBytes = new byte [ Renderer . Screen . Width * Renderer . Screen . Height * 4 ] ;
433+ GL . ReadPixels ( 0 , 0 , Renderer . Screen . Width , Renderer . Screen . Height , OpenTK . Graphics . OpenGL . PixelFormat . Rgba , PixelType . UnsignedByte , textureBytes ) ;
434+ // GL.ReadPixels is reversed for what it wants as a texture, so we've got to flip it
435+ byte [ ] tmp = new byte [ Renderer . Screen . Width * 4 ] ; // temp row
436+ int currentLine = 0 ;
437+ while ( currentLine < Renderer . Screen . Height / 2 )
438+ {
439+ int start = currentLine * Renderer . Screen . Width * 4 ;
440+ int flipStart = ( Renderer . Screen . Height - currentLine - 1 ) * Renderer . Screen . Width * 4 ;
441+ Buffer . BlockCopy ( textureBytes , start , tmp , 0 , Renderer . Screen . Width * 4 ) ;
442+ Buffer . BlockCopy ( textureBytes , flipStart , textureBytes , start , Renderer . Screen . Width * 4 ) ;
443+ Buffer . BlockCopy ( tmp , 0 , textureBytes , flipStart , Renderer . Screen . Width * 4 ) ;
444+ currentLine ++ ;
445+ }
437446 }
438447 Renderer . Reset ( ) ;
439448 CameraAlignment a = Renderer . Camera . Alignment ;
440- if ( LoadRoute ( bitmap ) )
449+ if ( LoadRoute ( textureBytes ) )
441450 {
442451 Renderer . Camera . Alignment = a ;
443452 Program . Renderer . CameraTrackFollower . UpdateAbsolute ( - 1.0 , true , false ) ;
@@ -454,10 +463,6 @@ internal static void keyDownEvent(object sender, KeyboardKeyEventArgs e)
454463 }
455464 CurrentlyLoading = false ;
456465 Renderer . OptionInterface = true ;
457- if ( bitmap != null )
458- {
459- bitmap . Dispose ( ) ;
460- }
461466 GCSettings . LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode . CompactOnce ;
462467 GC . Collect ( ) ;
463468
0 commit comments