如何在 SDL 中更改 window 的分辨率?一切都太大了

How do I change the resolution of a window in SDL? Everything is too large

我正在用 SDL 制作游戏并监督了决议。一切(精灵)都太大了,我想知道我是否可以提高分辨率,使一切变小而不改变每个精灵或修改表面的创建方式?这是我正在处理的事情:

从 SDL 2.0.0 开始,您可以使用函数 SDL_RenderSetLogicalSize to set a device independent resolution for rendering.

这样,您可以使用所需的目标分辨率设置渲染器,并像使用该分辨率一样绘制所有内容。

我引用官方documentation:

This is nice in that you can change the logical rendering size to achieve various effects, but the primary use is this: instead of trying to make the system work with your rendering size, we can now make your rendering size work with the system. On my 1920x1200 monitor, this app thinks it's talking to a 640x480 resolution now, but SDL is using the GPU to scale it up to use all those pixels. Note that 640x480 and 1920x1200 aren't the same aspect ratio: SDL takes care of that, too, scaling as much as possible and letterboxing the difference.

请注意,您可以控制 bunch of hints 的缩放方式,例如 SDL_HINT_RENDER_SCALE_QUALITY

是的,你可以提高分辨率。如果您使用全屏模式,它会使您的精灵看起来更小,就像您调整显示器分辨率时一样。当您处于 Window 模式时,window 会变大,但精灵看起来是一样的。

您也可以使用 @skypjack 提到的虚拟分辨率。一种方法是使用SDL_RenderSetLogicalSize(),另一种方法是使用渲染目标。

您还可以在线调整精灵的大小(指定小于源的目标矩形),或 off-line(使用批处理图像处理工具,如 ACDSee 或 Photoshop)。

最后你会有更多 space 来填补。

我的建议是,当您处理图形时,请尝试在脑海中设定一个目标分辨率,例如 1280x720 或 800x600。然后围绕这个决议设计所有资产。最后,您将拥有可以很好地组合在一起的资产,而无需诉诸编程技巧。然后如果你 运行 进入不同分辨率的设备,你可以使用我上面提到的 'virtual resolution' 来修复它。还有一些技巧,如 safe-frame 或信箱也需要,但让我们把它作为一个高级问题:-)。