Example #1
0
        public void Show()
        {
            if (RegisterClass() == 0)
            {
                return;
            }

            if (Create() == 0)
            {
                return;
            }

            GlobalOffscreenBuffer = CreateBackBuffer(WindowWidth, WindowHeight);
            var sw         = Stopwatch.StartNew();
            var frameCount = 0;
            var fps        = 0.0f;

            Win32.MSG Msg = new Win32.MSG();
            while (IsRunning)
            {
                // TODO Frame Rate Limiting
                while (Win32.PeekMessage(out Msg, hWnd, 0, 0, 1))
                {
                    if (Msg.message == WM_QUIT)
                    {
                        IsRunning = false;
                    }

                    // Update Messages
                    {
                        Win32.TranslateMessage(ref Msg);
                        Win32.DispatchMessage(ref Msg);
                    }
                }

                // Render
                {
                    Update();
                    Render(GlobalOffscreenBuffer);

                    // Paint
                    IntPtr hDC        = Win32.GetDC(hWnd);
                    var    dimensions = GetWindowDimensions(hWnd);

                    Win32.StretchDIBits(hDC, 0, 0, WindowWidth, WindowHeight, 0, 0, dimensions.Width, GlobalOffscreenBuffer.Height, GlobalOffscreenBuffer.Memory, ref GlobalOffscreenBuffer.Info, 0, (uint)TernaryRasterOperations.SRCCOPY);
                    ReleaseDC(hWnd, hDC);
                }

                frameCount++;
                fps = frameCount / (sw.ElapsedMilliseconds * 0.001f);

                // TODO Do some sleeping to maintain fps limits
                SetWindowText(hWnd, "Fps:" + fps);
            }
        }
Example #2
0
        private static OffscreenBuffer CreateBackBuffer(int width, int height)
        {
            var buffer = new OffscreenBuffer();

            buffer.Info.Init();                    // Populates BiSize
            buffer.Info.biWidth         = width;
            buffer.Info.biHeight        = -height; // TopDown
            buffer.Info.biPlanes        = 1;
            buffer.Info.biBitCount      = 32;
            buffer.Info.biCompression   = BitmapCompressionMode.BI_RGB;
            buffer.Info.biXPelsPerMeter = 0;
            buffer.Info.biYPelsPerMeter = 0;
            buffer.Info.biClrUsed       = 0;
            buffer.Info.biClrImportant  = 0;

            buffer.Width  = width;
            buffer.Height = height;
            buffer.bpp    = 4;
            buffer.pitch  = buffer.Width * buffer.bpp;

            buffer.Memory = new byte[buffer.Width * buffer.Height * buffer.bpp];

            return(buffer);
        }
Example #3
0
 public virtual void Render(OffscreenBuffer buffer)
 {
     // Noop
 }