【问题标题】:XNA window erroneously scaled, fixed with window resizeXNA 窗口错误缩放,通过窗口调整大小修复
【发布时间】:2014-02-09 10:50:08
【问题描述】:

所以我使用这段代码来处理任何分辨率的渲染:

int windowW = GraphicsDevice.Viewport.Width; //Save the current screen size
int windowH = GraphicsDevice.Viewport.Height;
RenderTarget2D screen = new RenderTarget2D(GraphicsDevice, 1024, 768,
    false, GraphicsDevice.DisplayMode.Format, DepthFormat.Depth24, 8, RenderTargetUsage.DiscardContents);
GraphicsDevice.SetRenderTarget(screen);
GraphicsDevice.Clear(Color.Black);

soldrGameHub.Render(gameTime, spriteBatch); //Render all to another RenderTarget

GraphicsDevice.SetRenderTarget(null);

GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();

float scaleX = windowW / 1024f; //The scales on each dimension, to keep the aspect ratio
float scaleY = windowH / 768f;
float scale;
Vector2 position; //and the position to make sure we render in the center

if (scaleX <= scaleY) { //screen taller than wide
    scale = scaleX;
    position = new Vector2(0, (windowH - 768 * scale) / 2f);
}
else {
    scale = scaleY;
    position = new Vector2((windowW - 1024 * scale) / 2f, 0);
}
soldrGameHub.SetMouseOffset((int) position.X, (int) position.Y); //Set the mouse offset
soldrGameHub.SetScale(scale); //and scale to get the right coordinates at any time.

spriteBatch.Draw(screen, position, null, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0); //and finally draw the renderTarget to the screen.

spriteBatch.End();

所以基本上:我假设分辨率为 1024x768,渲染所有内容,渲染到 RenderTarget,最后在屏幕上绘制 RenderTarget。我必须更正鼠标位置(在另一个类中处理)以使用偏移量(以保持活动区域居中)和比例(以填充整个屏幕)。 这在 99% 的情况下都有效,除了一开始,在我调整屏幕大小之前(在经典的 windows 样式中,通过用鼠标拖动右下角)。 所以,如果调整大小,它会起作用,否则不会。

发生的情况是屏幕仅垂直缩放。 (例如,应该是 75x75 的东西是 75x73)结果是,在屏幕底部,鼠标显示得比实际高(你必须点击低于你应该点击的位置) )。这似乎不是鼠标的问题,而是窗口的处理方式(渲染在我不知道的情况下缩放)。 当屏幕为 1024x768(调整大小之前)时,windowW 和 windowH 分别为 1024 和 768,scaleX 和 scaleY 都是 1.0。 真正奇怪的是 如果我调整窗口大小,问题完全消失了。

我尝试过的:

  1. int windowW = GraphicsDevice.Viewport.Width 更改为使用Window.ClientBounds。我得到了另一个高度,但其他方面完全一样的问题。
  2. 调整原始窗口的大小(从 1024x600 开始)。这确实有效,但我担心潜在的问题。
  3. 我使用了截图工具来确保渲染确实存在问题。 75x73 示例实际上取自带有 Paint.NET 的 Snipping Tool。

我被卡住了,似乎问题出在 XNA 的某个地方,有人可以帮助我吗?

TL;DR:代码可能不是那么重要,请阅读文本,或者至少阅读粗体文本。

【问题讨论】:

标签: xna window scale


【解决方案1】:

我最终启动了最大化窗口,这似乎解决了这个问题。

如果有人真的找到了真正的解决方案,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-11
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    相关资源
    最近更新 更多