gpt4 book ai didi

c# - 如何在 MonoGame 中租用纹理?

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:36 25 4
gpt4 key购买 nike

所以..到目前为止,我的尝试是这样的,游戏没有拉伸(stretch):

enter image description here这是我的做法:

我声明了这个变量。

RenderTarget2D renderTarget;

在我的 game1 构造函数中,我有这个:我在这里也尝试了实际的屏幕分辨率,同样的事情。

graphics.PreferredBackBufferWidth = 96;
graphics.PreferredBackBufferHeight = 160;

在我的初始化中,我有这个:

renderTarget = new RenderTarget2D(GraphicsDevice, GraphicsDevice.PresentationParameters.BackBufferWidth,
GraphicsDevice.PresentationParameters.BackBufferHeight, false,
GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);

我这里有这个功能:

protected void DrawSceneToTexture(RenderTarget2D renderTarget)
{
// Set the render target
GraphicsDevice.SetRenderTarget(renderTarget);

GraphicsDevice.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true };

// Draw the scene
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.PointClamp,
DepthStencilState.None, RasterizerState.CullCounterClockwise);
switch (gameState)
{
case GameStates.Menu: { menuState.Draw(spriteBatch); break; }
case GameStates.Playing: { playingState.Draw(spriteBatch); break; }
case GameStates.GameOver: { gameOverState.Draw(spriteBatch); break; }
}
spriteBatch.End();

// Drop the render target
GraphicsDevice.SetRenderTarget(null);
}

我的绘制方法如下所示:

DrawSceneToTexture(renderTarget);

GraphicsDevice.Clear(Color.Black);

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
SamplerState.LinearClamp, DepthStencilState.Default,
RasterizerState.CullNone);

spriteBatch.Draw(renderTarget, new Rectangle(0, 0, actualScreenWidth, actualScreenHeight), Color.White);

spriteBatch.End();

base.Draw(gameTime);

最佳答案

如果您查看 PreferredBackBufferWidth 的文档它说:

If you request a back-buffer resolution that is not supported by the output device, the XNA Framework automatically selects the highest resolution supported by the output device. For example, if a graphics back buffer with a resolution of 1920×1080 (1080p or 1080i) is created and displayed on a device with 480i resolution, the back buffer automatically is resized to 480i.

所以,我怀疑发生的事情是,当您设置 RenderTarget2D 时,GraphicsDevice.PresentationParameters.BackBufferWidth 的值不是 96。

尝试直接设置渲染目标宽度和高度,看看是否能解决您的问题。

renderTarget = new RenderTarget2D(GraphicsDevice, 96, 160, false,
GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);

如果您只是想缩放渲染以适合屏幕,您可以通过删除所有 DrawSceneToTexture 内容并在 SpriteBatch.Begin 中设置比例来简化您的生活。方法,像这样:

var scaleX = (float)actualScreenHeight / (float)virtualHeight;
var scaleY = (float)actualScreenWidth / (float)virtualWidth;
var transformMatrix = Matrix.CreateScale(new Vector3(scaleX, scaleY, 1));

spriteBatch.Begin (sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, transformMatrix);

关于c# - 如何在 MonoGame 中租用纹理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18497211/

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