How to make my game engine faster (GDI+, C#)

廉价感情. 提交于 2019-12-20 02:38:10

问题


I'm currently building a 2D Game Engine in C#, using GDI. I do know that there are better alternatives to GDI but I'm also so deep into the project I can't turn back now.

So, I currently have a graphics engine, that renders a portion of a bitmap, depending on what my cameras position and windows size is, which has given me a great performance boost. Great!

However, I've noticed that if I place a bunch of game objects, in the same position, I get the same frame rate compared to if I placed the objects in different positions.

So, maybe the graphics engine is rendering parts of the bitmap that don't need to be rendered? and if this is the case how do I fix it?

I believe code isn't really needed, but just to be sure here's what my graphics engine is doing during my main game loop

namespace GameEngine.Graphics
{
    public class GraphicsEngine
    {
        private Window window;
        private View view;

        private System.Drawing.Graphics frontBuffer;
        private System.Drawing.Graphics backBuffer;
        private System.Drawing.Bitmap backBufferBitmap;

        public GraphicsEngine(Window window, View view)
        {
            this.window = window;
            this.view = view;

            frontBuffer = window.CreateGraphics();
            backBufferBitmap = new Bitmap((int)view.Size.X, (int)view.Size.Y);
            backBuffer = System.Drawing.Graphics.FromImage(backBufferBitmap);

            frontBuffer.CompositingMode = CompositingMode.SourceCopy;
            frontBuffer.CompositingQuality = CompositingQuality.AssumeLinear;
            frontBuffer.SmoothingMode = SmoothingMode.None;
            frontBuffer.InterpolationMode = InterpolationMode.NearestNeighbor;  
            frontBuffer.TextRenderingHint = TextRenderingHint.SystemDefault;
            frontBuffer.PixelOffsetMode = PixelOffsetMode.HighSpeed;

            backBuffer.CompositingMode = CompositingMode.SourceOver;
            backBuffer.CompositingQuality = CompositingQuality.HighSpeed;
            backBuffer.SmoothingMode = SmoothingMode.None;
            backBuffer.InterpolationMode = InterpolationMode.Low;
            backBuffer.TextRenderingHint = TextRenderingHint.SystemDefault;
            backBuffer.PixelOffsetMode = PixelOffsetMode.Half;
        }

        public void Render()
        {
            frontBuffer.DrawImage(backBufferBitmap, new Rectangle(0, 0, window.Width, window.Height),
                                                    new Rectangle((int)view.Position.X, (int)view.Position.Y,
                                                    (int)view.Dimensions.X, (int)view.Dimensions.Y), GraphicsUnit.Pixel);
        }
}
}

来源:https://stackoverflow.com/questions/40908035/how-to-make-my-game-engine-faster-gdi-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!