Display GameObjects on a console
RenderArea property on a
ISurface allows you to only display a small section of the surface. This way you can have a giant surface that is bigger than the game window and allows you to scroll around. This is very useful for when you have console that represents the map or play area.
NOTE You may want to review the introduction to game objects article.
Here is an example (click on the screenshot) of a console with a bunch of random characters and colors. The console is 1000x1000 but the view area is only 80x24 and it can be scrolled.
By default the render area is the same size of the backing text surface and no optimizations are made. Once you set the
ISurface.RenderArea property to something custom, optimizations take affect for rendering.
var myConsole = new SadConsole.Console(100,100);
In the example the console is created with a 100 x 100 surface and will render all 100 x 100 cells. Once you change it to be 30 x 30, only that smaller area will actually render.
Reset the render area by simply setting the
RenderArea property to null. This will make the render area match the size of the text surface.
myConsole.TextSurface.RenderArea = null;
Game objects are powerful because they represent independent animated consoles. Even though many of the game objects I use in my games are not animated, I still use them when I want to position characters independently of the console, for example a smiley face character that moves around but does not animate.
Q Hey, if my entity is not going to be animated, why would I want to use this instead of a console?
Good question. The reason is that if you move characters around on the console, you need to remember erase the old position when you move the character to a new position. If you also have background characters (like roads or something) too, you need to not only erase the old position of the character, but restore it to what it was originally.
Think of it like this. If you print a character at
(20, 30) to represent the player and then the player presses the left-arrow key, you need to move the character left by one cell. You do this by printing the character at
(19, 30). However the old location
(20, 30) still has a character there. Now you need to erase the old location so it is blank. Let’s say in this case your player is currently walking through a forest of green and yellow tiles, you’ll need to remember what was there prior to the character and restore it.
Game objects help solve this problem because you position them physically on the screen instead of physically on an individual console.
A problem however appears when your console is not located at
(0, 0) or you’re using a view port. This is because if you position the object at
(1, 1) on the screen, this may not map to where
(1, 1) is on the console.
For example, if you position your console on the screen at
(5, 5) this makes the left-most
(0, 0) cell of the console draw there. If your object is positioned at
(1, 1) it will not appear on the console because the game object position represents where on the screen it is located, not where on a console it should be drawn.
var console = new Console(10, 5);
To fix this, the game object has a property called
RenderOffset which offsets the position on the screen where the game object would be rendered by that amount. To sync any game object’s position with a console use this code:
entity.RenderOffset = console.RelativePosition - console.TextSurface.RenderArea.Location;
console.Positionbecause the relative position takes into account any console parents that may offset where the console is located on the screen.
If you’re using
TextSurface.RenderArea on your console, you need to check and makes sure that the game object is actually located within the view of the console so that it doesn’t appear outside of the render area. Set the game object
IsVisible to hide game objects that are outside of what you’re showing the user. Obviously if your console is exactly the same size of the view area you don’t really have to worry about this.
entity.IsVisible = console.TextSurface.RenderArea.Contains(entity.Position);