如何扩大屏幕上的像素?像素、屏幕上

2023-09-07 12:44:12 作者:命该如此

我写了一个2D跳跃和放大器;运行引擎造成了320x224(320×240)的图像。为了保持老派pixely-feel吧,我想用2个或3或4缩放得到的图像,根据用户的分辨率。

I have written a 2D Jump&Run Engine resulting in a 320x224 (320x240) image. To maintain the old school "pixely"-feel to it, I would like to scale the resulting image by 2 or 3 or 4, according to the resolution of the user.

我不希望以每一个精灵,而由此产生的图像!

I don't want to scale each and every sprite, but the resulting image!

在此先感谢:)

推荐答案

Bob's答案是正确的有关更改过滤模式为 TextureFilter.Point 让事情变得漂亮和像素化。

Bob's answer is correct about changing the filtering mode to TextureFilter.Point to keep things nice and pixelated.

不过,可能是一个比缩放每个精灵(你也不得不扩展每个精灵的位置)是只通过一个矩阵来 SpriteBatch.Begin 更好的方法,像这样:

But possibly a better method than scaling each sprite (as you'd also have to scale the position of each sprite) is to just pass a matrix to SpriteBatch.Begin, like so:

sb.Begin(/* first three parameters */, Matrix.CreateScale(4f));

这会给你你想要的扩展,而无需修改所有的绘制调用。

That will give you the scaling you want without having to modify all your draw calls.

不过值得一提的是,如果使用浮点偏移在你的游戏中,你将最终获得的东西没有对齐到像素边界后,扩展(无论使用哪种方法)。

However it is worth noting that, if you use floating-point offsets in your game, you will end up with things not aligned to pixel boundaries after you scale up (with either method).

有两个解决办法。首先是有一个这样的功能:

There are two solutions to this. The first is to have a function like this:

public static Vector2 Floor(Vector2 v)
{
    return new Vector2((float)Math.Floor(v.X), (float)Math.Floor(v.Y));
}

然后通过该功能,每次通过你的位置,你画一个精灵。虽然这可能无法正常工作,如果你的精灵使用任何旋转或偏移。并再次你会回来的,以修改每一个绘制调用。

And then pass your position through that function every time you draw a sprite. Although this might not work if your sprites use any rotation or offsets. And again you'll be back to modifying every single draw call.

正确的方式做到这一点,如果你想要一个普通的逐点向上扩展你的整个场景,是绘制场景到渲染目标,在原来的大小。然后绘制渲染目标的屏幕,放大(与 TextureFilter.Point )。

The "correct" way to do this, if you want a plain point-wise scale-up of your whole scene, is to draw your scene to a render target at the original size. And then draw your render target to screen, scaled up (with TextureFilter.Point).

您想看看这个函数是 GraphicsDevice.SetRenderTarget 。 这MSDN文章可能是值得一读。如果你在或移动到XNA 4.0,这可能是值得一读。

The function you want to look at is GraphicsDevice.SetRenderTarget. This MSDN article might be worth reading. If you're on or moving to XNA 4.0, this might be worth reading.

我无法找到这个快速更简单的XNA样品,但布鲁姆后处理示例使用的渲染目标,这则模糊着色器适用于。你可以简单地忽略着色器完全,只是做了放大。

I couldn't find a simpler XNA sample for this quickly, but the Bloom Postprocess sample uses a render target that it then applies a blur shader to. You could simply ignore the shader entirely and just do the scale-up.