gpt4 book ai didi

c# - 在 Windows 窗体中是否有快速操作和缓冲屏幕的方法?

转载 作者:太空狗 更新时间:2023-10-29 20:39:00 25 4
gpt4 key购买 nike

我正在以学习为目的开发游戏,我只想使用 .NET-Framework 和 C# 中的 Windows 窗体项目来制作它。

我想获取“屏幕”(可以显示在窗口上的东西)作为 int[]。修改数组并以缓冲方式将更改后的数组重新应用到“屏幕”(这样它就不会闪烁)。

我目前正在使用一个 Panel,我在上面用 Graphics 绘制了一个 BitmapBitmap 被转换为 int[],然后我可以对其进行修改并重新应用于 Bitmap 并重新绘制。它可以工作,但速度很慢,尤其是因为我必须在每一帧中放大图像,因为我的游戏只有 300x160,而屏幕只有 900x500。

构建:

    // Renders 1 frame
private void Render()
{
// Buffer setup
_bufferedContext = BufferedGraphicsManager.Current;
_buffer = _bufferedContext.Allocate(panel_canvas.CreateGraphics(), new Rectangle(0, 0, _scaledWidth, _scaledHeight));

_screen.clear();

// Get position of player on map
_xScroll = _player._xMap - _screen._width / 2;
_yScroll = _player._yMap - _screen._height / 2;

// Indirectly modifies the int[] '_pixels'
_level.render(_xScroll, _yScroll, _screen);
_player.render(_screen);

// Converts the int[] into a Bitmap (unsafe method is faster)
unsafe
{
fixed (int* intPtr = &_screen._pixels[0])
{
_screenImage = new Bitmap(_trueWidth, _trueHeight, _trueWidth * 4, PixelFormat.Format32bppRgb, new IntPtr(intPtr));
}
}

// Draw generated image on buffer
Graphics g = _buffer.Graphics;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

g.DrawImage(_screenImage, new Rectangle(0, 0, 900, 506));

// Update panel buffered
_buffer.Render();
}

有没有没有外部库的更快的方法来完成这项工作?

最佳答案

我不确定不安全代码,但我知道缓冲图形管理器。我认为你应该为它创建一个类,而不是每次都创建一个新类。以及让所有 Sprite 的宽度和高度在加载时确定而不是缩放它们。这大大加快了我的小型游戏引擎的速度。

class Spritebatch
{
private Graphics Gfx;
private BufferedGraphics bfgfx;
private BufferedGraphicsContext cntxt = BufferedGraphicsManager.Current;

public Spritebatch(Size clientsize, Graphics gfx)
{
cntxt.MaximumBuffer = new Size(clientsize.Width + 1, clientsize.Height + 1);
bfgfx = cntxt.Allocate(gfx, new Rectangle(Point.Empty, clientsize));
Gfx = gfx;
}

public void Begin()
{
bfgfx.Graphics.Clear(Color.Black);
}

public void Draw(Sprite s)
{
bfgfx.Graphics.DrawImageUnscaled(s.Texture, new Rectangle(s.toRec.X - s.rotationOffset.Width,s.toRec.Y - s.rotationOffset.Height,s.toRec.Width,s.toRec.Height));
}

public void drawImage(Bitmap b, Rectangle rec)
{
bfgfx.Graphics.DrawImageUnscaled(b, rec);
}

public void drawImageClipped(Bitmap b, Rectangle rec)
{
bfgfx.Graphics.DrawImageUnscaledAndClipped(b, rec);
}

public void drawRectangle(Pen p, Rectangle rec)
{
bfgfx.Graphics.DrawRectangle(p, rec);
}

public void End()
{
bfgfx.Render(Gfx);
}

}

这是我使用的示例。它被设置为模仿 Xna 中的 Spritebatch。绘制未缩放的图像确实会提高它的速度。同时创建一个缓冲图形和上下文的实例将比每次必须渲染时创建一个新实例更快。所以我建议您更改行 g.DrawImage(_screenImage, new Rectangle(0, 0, 900, 506));到 DrawImageUnscaled(_screenImage, new Rectangle(0, 0, 900, 506));

已编辑:如何在 Sprite 加载时缩放代码的示例

    public Sprite(Bitmap texture, float x, float y, int width, int height)
{
//texture is the image you originally start with.

Bitmap b = new Bitmap(width, height);
// Create a bitmap with the desired width and height
using (Graphics g = Graphics.FromImage(b))
{
g.DrawImage(texture, 0, 0, width, height);
}
// get the graphics from the new image and draw the old image to it
//scaling it to the proper width and height
Texture = b;
//set Texture which is the final picture to the sprite.
//Uppercase Texture is different from lowercase

关于c# - 在 Windows 窗体中是否有快速操作和缓冲屏幕的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31874785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com