我目前正在使用GDI在C#中构建2D游戏引擎。我知道有更好的GDI替代方案,但我也非常深入到我现在无法回头的项目中。
所以,我目前有一个图形引擎,根据我的相机位置和窗口大小呈现位图的一部分,这给了我很大的性能提升。太好了!
然而,我注意到,如果我放置一堆游戏对象,在同一位置,与将对象放置在不同位置相比,我获得相同的帧速率。
那么,也许图形引擎渲染的部分位图不需要渲染?如果是这种情况,我该如何解决?
我认为代码并不是真正需要的,但只是为了确定我的图形引擎在主游戏循环中的作用
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);
}
}
}