encompass-cs-docs/content/concepts/renderer.md

1.9 KiB

title date weight
Renderer 2019-05-22T14:16:06-07:00 30

A Renderer is responsible for reading the game state and telling the game engine what to draw to the screen.

{{% notice note %}} Remember: Encompass isn't a game engine and it doesn't have a rendering system. So Renderers aren't actually doing the rendering, it is just a way of structuring how we tell the game engine what to render. {{% /notice %}}

If you were using FNA, a Renderer might look like this:

using System;
using Encompass;
using Microsoft.Xna.Framework;
using MyGame.Components;
using MyGame.Messages;

namespace MyGame.Renderers
{
    public class GridRenderer : Renderer
    {
        private int gridSize;
        private PrimitiveDrawer primitiveDrawer;

        public Renderer(PrimitiveDrawer primitiveDrawer)
        {
            this.primitiveDrawer = primitiveDrawer;
            this.gridSize = 16;
        }

        public override void Render(double dt, double alpha)
        {
            if (SomeComponent<EditorModeComponent>() && SomeComponent<MouseComponent>())
            {
                var entity = ReadEntity<MouseComponent>();
                var transformComponent = GetComponent<TransformComponent>(entity);

                Rectangle rectangle = new Rectangle
                {
                    X = (transformComponent.Position.X / gridSize) * gridSize,
                    Y = (transformComponent.Position.Y / gridSize) * gridSize,
                    Width = gridSize,
                    Height = gridSize
                };

                primitiveDrawer.DrawBorder(rectangle, 0, new System.Numerics.Vector2(1, 1), Color.White, 1);
            }
        }
    }
}

This Renderer will draw a rectangle at the position of the mouse on the screen if an EditorModeComponent exists in the world.

It's your job to figure out how to interpret your game's data into rendering. Many games will only have one big Renderer that just renders everything.