XNA窗口错误缩放,用窗口调整大小固定

时间:2014-02-09 10:50:09

标签: xna window scale

所以我使用此代码以任何分辨率处理渲染:

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。
  4. 我被困了,似乎问题出在XNA的某个地方,任何人都可以帮助我吗?

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

1 个答案:

答案 0 :(得分:0)

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

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