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

标签 c# android monogame

所以..到目前为止,我的尝试是这样的,游戏没有拉伸(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/

相关文章:

c# - .net中异步调用和异步io调用的区别

c# - 将 DisplayFormatAttribute.ConvertEmptyStringToNull 的默认值设置为 false

java - 如何从 android 中的 firebase 数据库中的 uid 获取/检索多个数据?

c# - 使用SoundEffectInstance作为背景音乐可以接受吗?

.net - 无法在代码中加载刚刚定义的类

c# - 在 C# 中发送未实现方法的惯用方式

android - shrinkResources true 不能用于 Instant Apps 功能?

java - 如何使用 Uri.Builder 将数组参数添加到 url?

c# - 查找两个列表中的属性差异