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

98 lines
3.5 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 notice %}}
2019-12-01 21:43:31 +00:00
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 to structure how we tell the game engine what to render.
2019-05-22 21:22:29 +00:00
{{% /notice %}}
2019-12-01 21:43:31 +00:00
There are two kinds of renderers: GeneralRenderers and OrderedRenderers.
2019-05-22 21:22:29 +00:00
A GeneralRenderer is a Renderer which reads the game state in order to draw elements to the screen. It also requires a layer, which represents the order in which it will draw to the screen.
2019-12-01 21:43:31 +00:00
If you were using MonoGame, a GeneralRenderer might look like this:
2019-06-13 17:43:08 +00:00
2019-12-01 21:43:31 +00:00
```cs
using System;
using Encompass;
using Microsoft.Xna.Framework;
using SamuraiGunn2.Components;
using SamuraiGunn2.Editor.Components;
using SamuraiGunn2.Helpers;
2019-06-13 17:43:08 +00:00
2019-12-01 21:43:31 +00:00
namespace SamuraiGunn2.Editor.Renderers
{
public class GridRenderer : GeneralRenderer
{
private int gridSize;
private PrimitiveDrawer primitiveDrawer;
2019-06-13 17:43:08 +00:00
2019-12-01 21:43:31 +00:00
public GridRenderer(PrimitiveDrawer primitiveDrawer)
{
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
}
}
```
2019-12-01 21:43:31 +00:00
GeneralRenderers are great for things like UI layers, where we always want a group of particular elements to be drawn at a specific layer regardless of the specifics of the game state.
An OrderedRenderer provides a structure for the common pattern of wanting to draw an individual Component at a specific layer. OrderedRenderers must specify a component that implements IDrawableComponent.
If you were using MonoGame, an OrderedRenderer might look like this:
```cs
using Encompass;
using Microsoft.Xna.Framework.Graphics;
using SamuraiGunn2.Components;
using SamuraiGunn2.Extensions;
using System;
using System.Numerics;
2019-05-22 21:22:29 +00:00
2019-12-01 21:43:31 +00:00
namespace SamuraiGunn2.Renderers
{
public class Texture2DRenderer : OrderedRenderer<Texture2DComponent>
{
private SpriteBatch spriteBatch;
2019-06-13 17:43:08 +00:00
2019-12-01 21:43:31 +00:00
public Texture2DRenderer(SpriteBatch spriteBatch)
{
this.spriteBatch = spriteBatch;
}
2019-06-13 17:43:08 +00:00
2019-12-01 21:43:31 +00:00
public override void Render(Entity entity, Texture2DComponent textureComponent)
{
var transformComponent = GetComponent<TransformComponent>(entity);
2019-06-13 17:43:08 +00:00
2019-12-01 21:43:31 +00:00
spriteBatch.Draw(textureComponent.texture, transformComponent.Position, null, textureComponent.color, transformComponent.Rotation, textureComponent.origin, transformComponent.Scale, SpriteEffects.None, 0);
}
2019-06-13 17:43:08 +00:00
}
}
```
2019-12-01 21:43:31 +00:00
For 2D games, you will need to use layers to be specific about the order in which elements are drawn to the screen. For a 3D game you will probably end up delegating most of the rendering to some kind of scene/camera system.