在 XNA 中缩放整个屏幕 [英] Scaling entire screen in XNA

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

问题描述

使用 XNA,我正在尝试制作一个冒险游戏引擎,让您制作看起来像是从 90 年代初期掉下来的游戏,例如触手之日Sam &Max Hit the Road.因此,我希望游戏实际以 320x240 运行(我知道,它可能应该是 320x200,但嘘),但它应该根据用户设置进行扩展.

Using XNA, I'm trying to make an adventure game engine that lets you make games that look like they fell out of the early 90s, like Day of the Tentacle and Sam & Max Hit the Road. Thus, I want the game to actually run at 320x240 (I know, it should probably be 320x200, but shh), but it should scale up depending on user settings.

它现在还可以,但我遇到了一些问题,我实际上希望它看起来更多像素化.

It works kind of okay right now, but I'm running into some problems where I actually want it to look more pixellated that it currently does.

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

Here's what I'm doing right now:

在游戏初始化中:

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

        Scale = graphics.PreferredBackBufferWidth / 320;
    }

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

Scale is a public static variable that I can check anytime to see how much I should scale my game relative to 320x240.

在我的绘图函数中:

spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone, null, Matrix.CreateScale(Game.Scale));

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

This way, everything is drawn at 320x240 and blown up to fit the current resolution (640x480 by default). And of course I do math to convert the actual coordinates of the mouse into 320x240 coordinates, and so forth.

现在,这一切都很棒,但现在我要开始缩放我的精灵,让它们走到远处等等.

Now, this is great and all, but now I'm getting to the point where I want to start scaling my sprites, to have them walk into the distance and so forth.

看看下面的图片.左上角的图像是游戏以 640x480 运行时的截图.它右侧的图像是它应该"的样子,尺寸为 320x240.底行图像只是顶行放大到 300%(在 Photoshop 中,而不是在引擎中),因此您可以看到我在说什么.

Look at the images below. The upper-left image is a piece of a screenshot from when the game is running at 640x480. The image to the right of it is how it "should" look, at 320x240. The bottom row of images is just the top row blown up to 300% (in Photoshop, not in-engine) so you can see what I'm talking about.

在 640x480 的图像中,您可以看到不同的线条粗细";较粗的线条是它的真实外观(一个像素 = 2x2,因为它以 640x480 运行),但由于缩放,较细的线条(1x1 像素)也会出现,因为它们不应该出现(请参阅右侧的图像)).

In the 640x480 image, you can see different "line thicknesses;" the thicker lines are how it should really look (one pixel = 2x2, because this is running at 640x480), but the thinner lines (1x1 pixel) also appear, when they shouldn't, due to scaling (see the images on the right).

基本上我试图模拟一个 320x240 的显示器,但使用 XNA 将其放大到任何分辨率,而矩阵变换并没有起到作用.有什么办法可以让我这样做吗?

Basically I'm trying to emulate a 320x240 display but blown up to any resolution using XNA, and matrix transformations aren't doing the trick. Is there any way I could go about doing this?

推荐答案

以原始分辨率将所有内容渲染到 RenderTarget 而不是后台缓冲区:

Render everything in the native resolution to a RenderTarget instead of the back buffer:

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

        //perform draw calls

然后将此目标(您的整个屏幕)渲染到后台缓冲区:

Then render this target (your whole screen) to the back buffer:

        //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 中缩放整个屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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