如何缩放屏幕像素? [英] How to scale on-screen pixels?

查看:35
本文介绍了如何缩放屏幕像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 2D Jump&Run 引擎,生成了 320x224 (320x240) 的图像.为了保持老派的像素"感觉,我想根据用户的分辨率将结果图像缩放 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 的回答关于将过滤模式更改为 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 示例,但是 Bloom Postprocess 示例使用渲染目标,然后对其应用模糊着色器.您可以简单地完全忽略着色器并进行放大.

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.

这篇关于如何缩放屏幕像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆