xna - 在 XNA 中缩放整个屏幕

标签 xna xna-4.0 resolution-independence

使用 XNA,我正在尝试制作一个冒险游戏引擎,让您制作看起来像是从 90 年代初开始的游戏,例如《触手之日》和《山姆与麦克斯上路》。因此,我希望游戏实际运行在 320x240(我知道,它可能应该是 320x200,但是嘘),但它应该根据用户设置进行放大。

它现在工作得还不错,但我遇到了一些问题,我实际上希望它看起来比现在更像素化。

这就是我现在正在做的事情:

在游戏初始化中:

    public Game() {
        graphics = new GraphicsDeviceManager(this);
        graphics.PreferredBackBufferWidth = 640;
        graphics.PreferredBackBufferHeight = 480;
        graphics.PreferMultiSampling = false;

        Scale = graphics.PreferredBackBufferWidth / 320;
    }

Scale 是一个公共(public)静态变量,我可以随时检查它以查看我的游戏相对于 320x240 应该缩放多少。

在我的绘图功能中:
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone, null, Matrix.CreateScale(Game.Scale));

这样,所有内容都以 320x240 绘制并放大以适应当前分辨率(默认为 640x480)。当然,我会通过数学运算将鼠标的实际坐标转换为 320x240 坐标,等等。

现在,这很棒,但现在我要开始缩放我的 Sprite ,让它们走到远处等等。

看看下面的图片。左上图是游戏以 640x480 运行时的屏幕截图。它右边的图像是它“应该”的样子,320x240。最下面一行的图像只是被放大到 300% 的最上面一行(在 Photoshop 中,而不是在引擎中),所以你可以看到我在说什么。

image showing the difference in scaling as the resolution changes

在 640x480 图像中,您可以看到不同的“线条粗细”;较粗的线条是它的实际外观(一个像素 = 2x2,因为它以 640x480 运行),但较细的线条(1x1 像素)也会出现,当它们不应该出现时,由于缩放(见右边的图像)。

基本上我正在尝试模拟 320x240 显示器,但使用 XNA 将其放大到任何分辨率,并且矩阵转换并没有起到作用。有什么办法可以做到这一点吗?

最佳答案

将 native 分辨率中的所有内容渲染到 RenderTarget 而不是后台缓冲区:

        SpriteBatch targetBatch = new SpriteBatch(GraphicsDevice);
        RenderTarget2D target = new RenderTarget2D(GraphicsDevice, 320, 240);
        GraphicsDevice.SetRenderTarget(target);

        //perform draw calls

然后将此目标(您的整个屏幕)渲染到后台缓冲区:
        //set rendering back to the back buffer
        GraphicsDevice.SetRenderTarget(null);

        //render target to back buffer
        targetBatch.Begin();
        targetBatch.Draw(target, new Rectangle(0, 0, GraphicsDevice.DisplayMode.Width, GraphicsDevice.DisplayMode.Height), Color.White);
        targetBatch.End();

关于xna - 在 XNA 中缩放整个屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7602852/

相关文章:

.net - 缩放整个 WPF 窗口

android - 我如何在 Android 中显示此图像?

xna - Spritebatch.Begin() 变换矩阵

c# - 为技能使用添加冷却时间

c# - 在带有简单着色器的 XNA 4.0 中使 Sprite 变白(alpha 问题)

c# - xna 4.0 需要特殊混合

c#-4.0 - 如何使用C# XNA定时器?

android - 您可以在 Android 上运行 XNA 游戏吗?

c# - 有哪些关于 XNA 性能的书籍、文章、博客文章?