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

60 lines
1.9 KiB
Markdown
Raw Normal View History

2019-05-22 21:22:29 +00:00
---
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.
2019-05-22 21:22:29 +00:00
{{% /notice %}}
2021-05-01 19:07:59 +00:00
If you were using FNA, a Renderer might look like this:
2019-12-01 21:43:31 +00:00
```cs
using System;
using Encompass;
using Microsoft.Xna.Framework;
2020-07-11 00:06:40 +00:00
using MyGame.Components;
using MyGame.Messages;
2019-06-13 17:43:08 +00:00
2020-07-11 00:06:40 +00:00
namespace MyGame.Renderers
2019-12-01 21:43:31 +00:00
{
2021-05-01 19:07:59 +00:00
public class GridRenderer : Renderer
2019-12-01 21:43:31 +00:00
{
private int gridSize;
private PrimitiveDrawer primitiveDrawer;
2019-06-13 17:43:08 +00:00
2021-05-01 19:07:59 +00:00
public Renderer(PrimitiveDrawer primitiveDrawer)
2019-12-01 21:43:31 +00:00
{
this.primitiveDrawer = primitiveDrawer;
this.gridSize = 16;
}
2019-06-13 17:43:08 +00:00
2019-12-01 21:43:31 +00:00
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);
}
}
2019-06-13 17:43:08 +00:00
}
}
```
2021-05-01 19:07:59 +00:00
This Renderer will draw a rectangle at the position of the mouse on the screen if an `EditorModeComponent` exists in the world.
2019-06-13 17:43:08 +00:00
2021-05-01 19:07:59 +00:00
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.