Viewport

What is a viewport?

A viewport represents a part of the screen that you can see, while the content behind the viewport might be much larger. For example, you cannot show the entire world in a 100×80 window screen. Hence, we can use a viewport as a camera to show us what we can see, and when we move the viewport around it can show us more of the world.

Creating a viewport

// Load an 8x8 font
Game.Instance.LoadFont("Fonts/thick_square_8x8.font");

// The main world console 
var worldConsole = new SadConsole.Console(100, 100);
worldConsole.Font = Game.Instance.Fonts["ThickSquare8"];

// Define a viewport starting at x0 y0, with width and height of 25
// The view position is the same as the worldConsole's position
worldConsole.Surface.View = new Rectangle(0, 0, 25, 25);

// Draw a border around the world
var shapeParameters = ShapeParameters.CreateFilled(new ColoredGlyph(Color.White, Color.Cyan), new ColoredGlyph(Color.White, Color.Black));

worldConsole.Surface.DrawBox(new Rectangle(0, 0, worldConsole.Width, worldConsole.Height), shapeParameters);
worldConsole.Surface[1, 1].Background = Color.Red;

// Add the console as a child, all rendering is handled by the parent console
Game.Instance.StartingConsole.Children.Add(worldConsole);

This will give you an effect that looks like this:

When you move the viewport one cell to the right as such:

worldConsole.Surface.View = new Rectangle(1, 0, 25, 25);

You will see that the view shifts

To move the actual position of where the view itself is rendered, you need to move the world console’s position.

worldConsole.Position = new Point(5, 5);

Pretty simple, you can use this to create bigger worlds, while only rendering a portion of it.

Thats it for viewport!

Leave a comment

Design a site like this with WordPress.com
Get started