gpt4 book ai didi

c# - XNA 在不增加分辨率的情况下调整窗口大小

转载 作者:可可西里 更新时间:2023-11-01 11:09:24 26 4
gpt4 key购买 nike

我想在较大的窗口上创建一个低分辨率的游戏。 (例如 960x540 大小的窗口上的 96x54 分辨率)。

我该怎么做?有没有办法独立于首选的后台缓冲区宽度和高度来调整窗口大小?或者我应该只保留我绘制的低分辨率渲染目标,并在完成最近的纹理采样调整后将其作为全屏四边形绘制在我的窗口上?

提前致谢

小花

最佳答案

我倾向于选择“渲染到纹理”解决方案,这样我就可以在不失真的情况下实现全屏显示。

我用来实现此目的的类通常类似于:

class VirtualScreen
{
public readonly int VirtualWidth;
public readonly int VirtualHeight;
public readonly float VirtualAspectRatio;

private GraphicsDevice graphicsDevice;
private RenderTarget2D screen;

public VirtualScreen(int virtualWidth, int virtualHeight, GraphicsDevice graphicsDevice)
{
VirtualWidth = virtualWidth;
VirtualHeight = virtualHeight;
VirtualAspectRatio = (float)(virtualWidth) / (float)(virtualHeight);

this.graphicsDevice = graphicsDevice;
screen = new RenderTarget2D(graphicsDevice, virtualWidth, virtualHeight, false, graphicsDevice.PresentationParameters.BackBufferFormat, graphicsDevice.PresentationParameters.DepthStencilFormat, graphicsDevice.PresentationParameters.MultiSampleCount, RenderTargetUsage.DiscardContents);
}

private bool areaIsDirty = true;

public void PhysicalResolutionChanged()
{
areaIsDirty = true;
}

private Rectangle area;

public void Update()
{
if (!areaIsDirty)
{
return;
}

areaIsDirty = false;
var physicalWidth = graphicsDevice.Viewport.Width;
var physicalHeight = graphicsDevice.Viewport.Height;
var physicalAspectRatio = graphicsDevice.Viewport.AspectRatio;

if ((int)(physicalAspectRatio * 10) == (int)(VirtualAspectRatio * 10))
{
area = new Rectangle(0, 0, physicalWidth, physicalHeight);
return;
}


if (VirtualAspectRatio > physicalAspectRatio)
{
var scaling = (float)physicalWidth / (float)VirtualWidth;
var width = (float)(VirtualWidth) * scaling;
var height = (float)(VirtualHeight) * scaling;
var borderSize = (int)((physicalHeight - height) / 2);
area = new Rectangle(0, borderSize, (int)width, (int)height);
}
else
{
var scaling = (float)physicalHeight / (float)VirtualHeight;
var width = (float)(VirtualWidth) * scaling;
var height = (float)(VirtualHeight) * scaling;
var borderSize = (int)((physicalWidth - width) / 2);
area = new Rectangle(borderSize, 0, (int)width, (int)height);
}
}

public void BeginCapture()
{
graphicsDevice.SetRenderTarget(screen);
}

public void EndCapture()
{
graphicsDevice.SetRenderTarget(null);
}

public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(screen, area, Color.White);
}


}

然后在我的游戏中,初始化看起来像这样:

  VirtualScreen virtualScreen;

protected override void Initialize()
{
virtualScreen = new VirtualScreen(96, 54, GraphicsDevice);
Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged);
Window.AllowUserResizing = true;
base.Initialize();
}

void Window_ClientSizeChanged(object sender, EventArgs e)
{
virtualScreen.PhysicalResolutionChanged();
}

所有重要的更新调用:

protected override void Update(GameTime gameTime)
{
virtualScreen.Update();

base.Update(gameTime);
}

然后是绘画本身:

 protected override void Draw(GameTime gameTime)
{
virtualScreen.BeginCapture();


GraphicsDevice.Clear(Color.CornflowerBlue);
// game rendering happens here...


virtualScreen.EndCapture();

GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
virtualScreen.Draw(spriteBatch);
spriteBatch.End();

base.Draw(gameTime);
}

有了这个,我基本上可以不再关心分辨率,而只专注于游戏。

关于c# - XNA 在不增加分辨率的情况下调整窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7591466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com