Manual control when you need to redraw the screen

I am trying to make a different engine game for XNA. I basically port the game from the previous work that I used using the SDL-based roguelike library called libtcod. How can I change the main thing of the XNA template to make the game not redraw the screen in every frame, but instead when I want?

+4
source share
2 answers

The right way to do this is to call GraphicsDevice.Present() when you want to draw the back buffer on the screen.

Now the difficulty is that the Game class automatically calls Present for you (in particular, in Game.EndDraw ), which you do not want to do. Fortunately, Game provides several ways to prevent Present from being called:

A better way would be to override BeginDraw and return it false to prevent the frame from drawing (including preventing Draw and EndDraw from being called):

 protected override bool BeginDraw() { if(readyToDraw) return base.BeginDraw(); else return false; } 

Other alternatives are to call Game.SuppressDraw or override EndDraw so that it does not call base.EndDraw() until you are ready to display the frame on the screen.

Personally, I would just draw every frame.

+5
source share

You do not need to inherit the Game class, this is completely optional. You can write your own class that redraws the screen only when you want. There is some useful code in the class, so you can use Reflector to copy the code, and then change its logic.

+2
source share

All Articles