我正在写一个基于图块的自上而下的2D游戏,希望当玩家移动时图块在屏幕上“滚动”。出于性能方面的考虑,我想将图像从上一帧“移位”,并且只为该帧的新区域绘制单个图块。
目前,游戏每帧都重新绘制所有图块(图块,即10像素* 10像素彩色块的全屏),这不能产生令人满意的帧速率。为了避免这种情况,我计划在绘制完每一帧后(例如保存到位图),然后考虑到玩家的移动方式将其重新插入下一帧,但是我还没有找到一种方法来使用SlimDX进行“保存”。
我目前的方法似乎更有希望,它有2个Direct2D.BitmapRenderTarget
(在窗口渲染目标上用CreateCompatibleRenderTarget()
创建),它们在“当前”和“上一个”之间切换角色目标。来自“上一个”目标图像的图像将被绘制到“当前”目标上,然后将新的图块绘制到“当前”目标上,然后将“当前”目标绘制到窗口上。我现在遇到的问题是,将“上一个”图像绘制到“当前”目标上会导致Direct2DException,我相信由于它是从窗口目标创建的,因此与其他BitmapRenderTarget不兼容。
这是上面使用2个BitmapRenderTargets描述的方法的代码:
public void draw(RenderTarget target) { //target is a WindowRenderTarget
BitmapRenderTarget currentBuffer;
BitmapRenderTarget previousBuffer;
int[] distance; //the distance the player has moved [0]=x, [1]=y
previousBuffer.BeginDraw();
if(backgroundBuffer.Bitmap != null) {
currentBuffer.DrawBitmap(previousBuffer.Bitmap, //draw the previous frame onto the current frame
new Rectangle( //target rectangle
new Point(
-1*distance[0]*getZoomLevel(),//xpos of previous frame image
-1*distance[1]*getZoomLevel()//ypos of previous frame image
),
currentBuffer.Size.ToSize() //the size of the image
),
1,//opacity
InterpolationMode.Linear,
new Rectangle( //source rectangle (covers whole image)
new Point(0,0),
previousBuffer.Size.ToSize()
)
);
}
drawChangeToTarget(currentBuffer); //draw the new tiles onto the current Buffer
currentBuffer.EndDraw(); //throws Direct2DException here
target.DrawBitmap(currentBuffer.Bitmap, //draw the result to the screen
new Rectangle ( //destination rectangle
new Point (0,0),
target.Size.ToSize()
),
1, //opacity
InterpolationMode.Linear,
new Rectangle( //target rectangle
new Point(0,0),
currentBuffer.Size.ToSize()
)
);
}