CSPspEmu.Gui.CommonGuiDisplayOpengl.GetTexVram C# (CSharp) Method

GetTexVram() private method

private GetTexVram ( Action Action ) : void
Action Action
return void
        private void GetTexVram(Action<TexturePair> Action)
        {
            if (TexVram == null)
            {
                TexVram = GLTexture.Create().SetFormat(TextureFormat.RGBA).SetSize(1, 1);
            }

            //Console.WriteLine(TexVram);

            TexVram.Bind();

            if (BufferGraphics == null)
            {
                BufferGraphics = Graphics.FromImage(Buffer);
                //BufferGraphics.Clear(Color.Red);
                BufferGraphics.Clear(Color.Black);
            }

            //if (PspDisplayForm.Singleton.WindowState == FormWindowState.Minimized)
            //{
            //	return;
            //}
            //
            //if (!PspDisplayForm.Singleton.EnableRefreshing)
            //{
            //	return;
            //}

            if (IGuiWindowInfo.EnableRefreshing)
            {
                try
                {
                    int Width = 512;
                    int Height = 272;
                    var FrameAddress = PspDisplay.CurrentInfo.FrameAddress;
                    byte* FrameBuffer = null;
                    byte* DepthBuffer = null;
                    try
                    {
                        FrameBuffer = (byte*)Memory.PspAddressToPointerSafe(
                            FrameAddress,
                            PixelFormatDecoder.GetPixelsSize(PspDisplay.CurrentInfo.PixelFormat, Width * Height)
                        );
                    }
                    catch (Exception Exception)
                    {
                        Console.Error.WriteLine(Exception);
                    }

                    //Console.Error.WriteLine("FrameBuffer == 0x{0:X}!!", (long)FrameBuffer);

                    if (FrameBuffer == null)
                    {
                        //Console.Error.WriteLine("FrameBuffer == null!!");
                    }

                    //Console.WriteLine("{0:X}", Address);

                    var Hash = PixelFormatDecoder.Hash(
                        PspDisplay.CurrentInfo.PixelFormat,
                        (void*)FrameBuffer,
                        Width, Height
                    );

                    if (Hash != LastHash)
                    {
                        LastHash = Hash;
                        Buffer.LockBitsUnlock(System.Drawing.Imaging.PixelFormat.Format32bppArgb, (BitmapData) =>
                        {
                            var Count = Width * Height;
                            fixed (OutputPixel* BitmapDataDecodePtr = BitmapDataDecode)
                            {
                                var BitmapDataPtr = (BGRA*)BitmapData.Scan0.ToPointer();

                                //var LastRow = (FrameBuffer + 512 * 260 * 4 + 4 * 10);
                                //Console.WriteLine("{0},{1},{2},{3}", LastRow[0], LastRow[1], LastRow[2], LastRow[3]);

                                if (FrameBuffer == null)
                                {
                                    if (OldFrameBuffer != null)
                                    {
                                        Console.Error.WriteLine("FrameBuffer == null");
                                    }
                                }
                                else if (BitmapDataPtr == null)
                                {
                                    Console.Error.WriteLine("BitmapDataPtr == null");
                                }
                                else
                                {
                                    PixelFormatDecoder.Decode(
                                        PspDisplay.CurrentInfo.PixelFormat,
                                        (void*)FrameBuffer,
                                        BitmapDataDecodePtr,
                                        Width, Height
                                    );
                                }

                                // Converts the decoded data to Window's format.
                                for (int n = 0; n < Count; n++)
                                {
                                    BitmapDataPtr[n].R = BitmapDataDecodePtr[n].B;
                                    BitmapDataPtr[n].G = BitmapDataDecodePtr[n].G;
                                    BitmapDataPtr[n].B = BitmapDataDecodePtr[n].R;
                                    BitmapDataPtr[n].A = 0xFF;
                                }

                                OldFrameBuffer = FrameBuffer;

                                GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 512, 272, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, BitmapDataPtr);
                                TextureVerticalFlip = true;
                            }
                        });
                    }
                    //else
                    {
                        //Console.WriteLine("Display not updated!");
                    }
                }
                catch (Exception Exception)
                {
                    Console.Error.WriteLine(Exception);
                }
            }

            Action(new TexturePair() { Color = TexVram, Depth = GLTexture.Wrap(0) });
            return;
        }