如何在 XNA 中通过调整窗口大小来缩放texture2d

发布于 2024-10-20 03:06:35 字数 874 浏览 2 评论 0原文

我正在为学校项目开发 UI,并且我尝试了类似的方法来缩放此处列出的纹理,但问题是:

我们的项目以 1440 x 900 开发,因此我制作了自己的图像适合该屏幕分辨率。当我们必须在课堂上演示我们的项目时,投影仪只能渲染到 1024 x 768,因此,屏幕上的许多内容都丢失了。我添加了窗口调整大小功能,并且我正在像这样进行缩放。我有自己的类,名为“button”,它有一个 2d 纹理,以及一个由 Button(Texture2d img, float width, float height) 构造的 Vector2 位置。

我的想法是将图像的位置设置为窗口宽度和高度的可缩放百分比,因此我尝试将 img 的位置设置为 0-1 之间的数字,然后乘以窗口宽度和高度保持一切都正确缩放。

(这段代码不是正确的语法,我只是想传达这一点)

Button button = new Button(texture, .01, .01 );
int height = graphicsdevice.viewport.height * button.position.Y;
int width = graphicsdevice.viewport.width * button.position.X;

Rectangle rect = new Rectangle(0,0,width, height);

sprite.being()
sprite.draw (button.img, rect, color.white);
sprite.end

当我去绘制它并通过拖动鼠标来调整窗口大小时,它最终不会缩放任何东西。如果我一开始就以不同的缓冲区高度和缓冲区宽度进行硬编码,则无论分辨率如何,图像都会保持相同的大小,只是分辨率越小,图像看起来就越像素化。

设计我的程序以允许动态texture2d缩放的最佳方法是什么?

I'm developing an UI for a project for school, and I've tried similar methods to scaling my texture as listed here, but here is the issue:

Our project is developed at 1440 x 900, so I've made my own images that fit that screen resolution. When we have to demo our project in class, the projector can only render up to 1024 x 768, thus, many things on the screen goes missing. I have added window resizing capabilities, and I'm doing my scaling like this. I have my own class called "button" which has a texture 2d, and a Vector2 position contruscted by Button(Texture2d img, float width, float height).

My idea is to set the position of the image to a scalable % of the window width and height, so I'm attempting to set the position of the img to a number between 0-1 and then multiply by the window width and height to keep everything scaled properly.

(this code is not the proper syntax, i'm just trying to convey the point)

Button button = new Button(texture, .01, .01 );
int height = graphicsdevice.viewport.height * button.position.Y;
int width = graphicsdevice.viewport.width * button.position.X;

Rectangle rect = new Rectangle(0,0,width, height);

sprite.being()
sprite.draw (button.img, rect, color.white);
sprite.end

it doesn't end up scaling anything when i go to draw it and resize the window by dragging the mouse around. if i hard code in a different bufferheight and bufferwidth to begin with, the image stays around the same size regardless of resolution, except that the smaller the resolution is, the more pixelated the image looks.

what is the best way to design my program to allow for dynamic texture2d scaling?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天 2024-10-27 03:06:35

正如 Hannesh 所说,如果你全屏运行它,就不会有这些问题。然而,您的做法也存在一个根本问题。您必须使用精灵的大小,而不是使用精灵的位置(在调整窗口大小期间根本不会改变精灵的位置)。我经常使用 Sprite 类中名为 Scale 的属性来执行此操作。因此,您不应将精灵的位置限制在 0 和 1 之间,而应该将精灵的 Size 属性限制在 0 和 1 之间。然后,当您重新缩放窗口时,它也会重新缩放精灵。

在我看来,更好的方法是使用默认分辨率,在您的情况下为 1440 x 900。然后,如果重新缩放窗口,只需将所有精灵的缩放因子乘以新屏幕尺寸与旧屏幕尺寸的比率。每次调整大小只需要 1 次乘法,而不是每次更新一次乘法(这就是您的方法将要做的,因为每次更新您都必须从固定的 0-1 值转换为实际比例)。

此外,您在手动重新调整精灵大小期间注意到的效果是正常的。将图像重新缩放到任意尺寸会导致渲染图像出现伪影,因为图形设备不知道在大多数尺寸下要做什么。解决这个问题的一个好方法是在开发过程中使用填充艺术,然后以正确的分辨率创建最终的艺术作品。显然这不适用于您的情况,因为您正在将窗口大小调整为任意大小,但在游戏中您通常只能切换到某些固定分辨率。

As Hannesh said, if you run it in fullscreen you won't have these problems. However, you also have a fundamental problem with the way you are doing this. Instead of using the position of the sprite, which will not change at all during window resize, you must use the size of the sprite. I often do this using a property called Scale in my Sprite class. So instead of clamping the position of the sprite between 0 and 1, you should be clamping the Size property of the sprite between 0 and 1. Then as you rescale the window it will rescale the sprites.

In my opinion, a better way to do this is to have a default resolution, in your case 1440 x 900. Then, if the window is rescaled, just multiply all sprites' scaling factors by the ratio of the new screensize to the old screensize. This takes only 1 multiplication per resize, instead of a multiplication per update (which is what your method will do, because you have to convert from the clamped 0-1 value to the real scale every update).

Also, the effects you noticed during manual rescale of the sprites is normal. Rescaling images to arbitrary sizes causes artifacts in the rendered image because the graphics device doesn't know what to do at most sizes. A good way to get around this is by using filler art during the development process and then create the final art in the correct resolution(s). Obviously this doesn't apply in your situation because you are resizing a window to arbitrary size, but in games you will usually only be able to switch to certain fixed resolutions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文