我试图用RenderTarget2D和ScissorRectangle创建一个可滚动的日志窗口。然而,ScissorRectangle似乎不起作用。这是代码:
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, new RasterizerState() { ScissorTestEnable = true });
Rectangle scissor = new Rectangle(0, Constants.GAME_AREA_HEIGHT, Constants.GAME_AREA_WIDTH, Constants.LOG_HEIGHT);
Rectangle scissorBackup = this.graphicsDevice.ScissorRectangle;
this.graphicsDevice.ScissorRectangle = scissor;
spriteBatch.Draw(logRenderTarget,
new Rectangle(0, Constants.GAME_AREA_HEIGHT, Constants.GAME_AREA_WIDTH, Constants.LOG_HEIGHT),
Color.White
);
this.graphicsDevice.ScissorRectangle = scissorBackup;
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null);
答案 0 :(得分:0)
我决定改用Viewport。但是,似乎MonoGame在调用spriteBatch.End()
之前使用了最后一个设置视口。它似乎与ScissorRectangle相同,所以我可以使它也起作用。我在评论中添加了ScissorRectangle特定的代码。这是我的新班级:
public class LogWriter
{
private GraphicsDevice graphicsDevice;
private Viewport defaultViewport, logViewport;
//private Rectangle scissor, scissorBackup;
public void Init(GraphicsDevice graphicsDevice)
{
this.graphicsDevice = graphicsDevice;
defaultViewport = this.graphicsDevice.Viewport;
logViewport = new Viewport(0, Constants.GAME_AREA_HEIGHT, Constants.GAME_AREA_WIDTH, Constants.LOG_HEIGHT);
//scissor = new Rectangle(0, Constants.GAME_AREA_HEIGHT, Constants.GAME_AREA_WIDTH, Constants.LOG_HEIGHT);
//scissorBackup = this.graphicsDevice.ScissorRectangle;
}
public void WriteLog(SpriteBatch spriteBatch)
{
this.graphicsDevice.Viewport = logViewport;
//this.graphicsDevice.ScissorRectangle = scissor;
spriteBatch.Begin();
//spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, new RasterizerState() { ScissorTestEnable = true });
for (int i = 0; i < Log.Count; i++)
{
spriteBatch.DrawString(Scavenger.AssetManager.Font12,
Log.Entries[i],
new Vector2(
Constants.LOG_MARGIN,
Constants.LOG_MARGIN + i * Scavenger.AssetManager.Font12.LineSpacing),
Color.GreenYellow
);
}
spriteBatch.End();
this.graphicsDevice.Viewport = defaultViewport;
//this.graphicsDevice.ScissorRectangle = scissorBackup;
}
}
在Init()
函数中调用 LoadContent()
。在绘制了所有其他内容之后,在spriteBatch上调用WriteLog()
之后,在Draw()
函数中调用End()
。