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

60 lines
1.9 KiB
Markdown

---
title: "Renderer"
date: 2019-05-22T14:16:06-07:00
weight: 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:
```cs
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()
{
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.