1

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?

4

1 回答 1

0

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

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

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

于 2011-03-03T03:53:40.983 回答