问题
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