2017-01-17 5 views
0

RenderTarget2D 및 ScissorRectangle이있는 스크롤 가능한 로그 창을 만들려고합니다. 그러나 ScissorRectangle은 효과가없는 것 같습니다. 코드는 다음과 같습니다.MonoGame - ScissorRectangle not working

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); 
+1

나는 가위 사각형이 'SpriteBatch.End' 호출 중에 * only * 적용된다는 것이 확실하다. 위의 코드에서 너무 일찍 재설정하고 있습니다. – craftworkgames

답변

0

뷰포트를 사용하기로 결정했습니다. 그러나 MonoGame은 spriteBatch.End() 호출 전에 마지막으로 설정된 Viewport를 사용합니다. 그것은 또한 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() 함수를 호출한다. WriteLog()Draw() 함수에서 End()이 호출 된 후에 spriteBatch에서 호출됩니다.