Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Linq;
- using Capsicum;
- using Capsicum.Interfaces;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using OctoGhast.DataStructures.Entity;
- using OctoGhast.Spatial;
- using RenderLike;
- namespace OctoGhast.Renderer {
- public class RenderSystem : IExecutableSystem, IInitializableSystem, IPoolBinding {
- private Pool _pool;
- private Group _renderableGroup;
- public event EventHandler OnRenderFrame;
- private Size Size { get; set; }
- public Surface Surface { get; }
- public RenderTarget2D RenderTarget { get; private set; }
- public RenderSystem(RLConsole console, Size size) {
- Size = size;
- Surface = console.CreateSurface(Size.Width, Size.Height);
- }
- public void Execute() {
- // Scoop everything that is Renderable in our pool.
- var renderList = _renderableGroup.Invoke();
- // Render everything to the framebuffer
- // Currently this renders /everything/ and doesn't do any depth or culling.
- foreach (var renderable in _renderableGroup.Invoke()) {
- var position = renderable.TryGetComponent<PositionComponent>()?.Position;
- var glyph = renderable.TryGetComponent<RenderableComponent>()?.Glyph;
- if (glyph != null && position != null)
- Surface.SetChar((int) position?.X, (int) position?.Y, (char) glyph.Icon);
- Debug.Assert(glyph == null && position == null,
- "glyph == null && position == null\nFound renderable group component that is not renderable");
- }
- // TODO: Lighting system
- // TODO: Render unlit scene
- // TODO: Blend in lighting map
- // Notify subscribers that a frame is available for consumption (?)
- RenderTarget = RenderTarget.RenderSurface(Surface);
- OnRenderFrame?.Invoke(this, new EventArgs());
- }
- public void Initialize() {
- // Setup the renderable group
- _pool.RegisterGroup("RenderableGroup", entities => entities.Where(s => s.IsRenderable()));
- _renderableGroup = _pool.GetGroup("RenderableGroup");
- }
- public void BindPool(Pool pool) {
- _pool = pool;
- }
- }
- public static class RenderTargetExtensions {
- public static RenderTarget2D RenderSurface(this RenderTarget2D target, Surface surface) {
- return surface.RenderSurface(target);
- }
- }
- public static class RenderableExtensions {
- /// <summary>
- /// For an entity to be renderable, it needs to have a RenderableComponent, a TransformComponent and
- /// a PositionComponent
- /// </summary>
- /// <param name="entity"></param>
- /// <returns></returns>
- public static bool IsRenderable(this Capsicum.Entity entity) {
- return entity.HasComponent<RenderableComponent>()
- && entity.HasComponent<TransformComponent>()
- && entity.HasComponent<PositionComponent>();
- }
- }
- public class RenderableComponent : IComponent {
- public IGlyph Glyph { get; set; }
- }
- public class TransformComponent : IComponent {
- public TransformComponent(Vector3 transformVec) {
- TransformVec = transformVec;
- }
- public Vector3 TransformVec { get; }
- }
- public class PositionComponent : IComponent {
- public Vector3 Position { get; }
- public PositionComponent(Vector3 position) {
- Position = position;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement