如何在MonoGame/XNA中快速绘制2D像素? [英] How to draw on the fly 2D pixel-by-pixel in MonoGame/XNA?
本文介绍了如何在MonoGame/XNA中快速绘制2D像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在XNA/MonoGame中逐个像素快速绘制,但只能找到this。问题是,问题的核心不是如何真正地逐个像素地绘制,而是管理资源和更新东西。
因为我找不到任何简单而简短的答案,并且想知道如何做一段时间,所以我将分享我偶然发现的一个片段(参见答案)。
注意:如果任何人有更好的方法或替代方法,请随时发布或评论!
推荐答案
为您的纹理和大小声明一个类级别变量:
Texture2D Tex, Tex1, Tex2;
const int WIDTH = 32;
const int HEIGHT = 32;
Color[] TexSource = new Color[WIDTH * HEIGHT];
初始化LoadContent()
中的变量
int radius = 5;
point center = new Point(WIDTH >> 2, HEIGHT >> 2);
Tex = new Texture2D(GraphicsDevice, WIDTH, HEIGHT);
for(int x = 0 ; x < WIDTH; x++)
for(int y = 0 ; y < HEIGHT; y++)
if(Math.Sqrt((x - center.x)* (x - center.x)) < radius && Math.Sqrt((y - center.y) * (y - center.y) < radius))
TexSource[x + y * WIDTH] = Color.Red;
else
TexSource[x + y * WIDTH] = Color.White;
Tex = SetData<Color>(TexSource);
//The resulting texture is a red circle on a white background.
//If you want to draw on top of an existing texture Tex1 as Tex2:
Tex1 = Content.Load<Texture2D>(Tex1Name);
Tex2 = new Texture2D(GraphicsDevice, Tex1.Width, Tex1.Height);
TexSource = Tex1.GetData<Color>();
// Draw a white on the image, we will assume tex1 is square for this task
for(int x = 0 ; x < Tex1.Width; x++)
for(int y = 0 ; y < Tex1.Width; y++)
if(x==y) TexSource[x + y * Tex1.Width] = Color.White;
您可以在Update()
但是,永远不要在Draw()
中创建或修改纹理。
SpriteBatch.Draw()
调用预期GraphicsDevice
缓冲区将包含所有纹理数据(通过扩展到GPU缓冲区),此时仍在发生的任何更改(来自更新且IsRunningSlowly
=TRUE)都将在呈现时导致撕裂。
GraphicsDevice.Textures[0] = null;
的解决方法会阻止绘制调用和游戏,直到传输到GPU完成,从而降低整个游戏循环的速度。
这篇关于如何在MonoGame/XNA中快速绘制2D像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文