这段代码是如何工作的以及它在做什么? (GDI,C#)

时间:2016-11-19 04:05:19

标签: c# gdi

我目前正在关注youtube上的教程来制作2D游戏引擎。我以前从未对GDI做过任何事情,但在C#和C ++中都对OpenGL有很好的了解。

但是,我不太明白这里发生了什么,并且会喜欢解释。

以下是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace GameEngine
{
    public class GraphicsEngine
    {
        /// <summary>
        /// A reference to the game window
        /// </summary>
        private Window window;

        /// <summary>
        /// Responsible for rendering graphics to the screen
        /// </summary>
        private Graphics screenBuffer;

        /// <summary>
        /// Responsible for sending the back buffer bitmap to the screen buffer.
        /// </summary>
        private Graphics backBuffer;

        /// <summary>
        /// Responsible for drawing graphics into memory
        /// </summary>
        private Bitmap backBufferBitmap;

        public GraphicsEngine(Window window)
        {
            this.window = window;
        }

        public void Initialize()
        {
            this.screenBuffer = window.CreateGraphics();
            this.backBufferBitmap = new Bitmap(window.Width, window.Height);
        }

        public void Update()
        {
            try
            {
                // copy back buffer to screen buffer
                screenBuffer = Graphics.FromImage(backBufferBitmap);

                // draw the back buffer to the screen
                backBuffer = window.CreateGraphics();
                backBuffer.DrawImage(backBufferBitmap, 0, 0, window.Width, window.Height);

                // clear the buffer
                Clear(Color.Wheat);
            }
            catch (System.ObjectDisposedException)
            {
                Environment.Exit(0);
            }
        }

        private void Clear(Color color)
        {
            screenBuffer.Clear(color);
        }
    }
}

所以,据我所知。初始化时,screenBuffer对象设置为windows图形对象。然后backBufferBitmap设置窗口的宽度和高度,并且基本上存储在内存中以便以后绘制。如果我错了,请告诉我。

然而,更新方法......对我来说没有意义......

0 个答案:

没有答案