Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using Capsicum;
  5. using Capsicum.Interfaces;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using OctoGhast.DataStructures.Entity;
  9. using OctoGhast.Spatial;
  10. using RenderLike;
  11.  
  12. namespace OctoGhast.Renderer {
  13. public class RenderSystem : IExecutableSystem, IInitializableSystem, IPoolBinding {
  14. private Pool _pool;
  15. private Group _renderableGroup;
  16.  
  17. public event EventHandler OnRenderFrame;
  18.  
  19. private Size Size { get; set; }
  20. public Surface Surface { get; }
  21.  
  22. public RenderTarget2D RenderTarget { get; private set; }
  23.  
  24. public RenderSystem(RLConsole console, Size size) {
  25. Size = size;
  26. Surface = console.CreateSurface(Size.Width, Size.Height);
  27. }
  28.  
  29. public void Execute() {
  30. // Scoop everything that is Renderable in our pool.
  31. var renderList = _renderableGroup.Invoke();
  32. // Render everything to the framebuffer
  33. // Currently this renders /everything/ and doesn't do any depth or culling.
  34. foreach (var renderable in _renderableGroup.Invoke()) {
  35. var position = renderable.TryGetComponent<PositionComponent>()?.Position;
  36. var glyph = renderable.TryGetComponent<RenderableComponent>()?.Glyph;
  37.  
  38. if (glyph != null && position != null)
  39. Surface.SetChar((int) position?.X, (int) position?.Y, (char) glyph.Icon);
  40.  
  41. Debug.Assert(glyph == null && position == null,
  42. "glyph == null && position == null\nFound renderable group component that is not renderable");
  43. }
  44.  
  45. // TODO: Lighting system
  46. // TODO: Render unlit scene
  47. // TODO: Blend in lighting map
  48. // Notify subscribers that a frame is available for consumption (?)
  49.  
  50. RenderTarget = RenderTarget.RenderSurface(Surface);
  51. OnRenderFrame?.Invoke(this, new EventArgs());
  52. }
  53.  
  54. public void Initialize() {
  55. // Setup the renderable group
  56. _pool.RegisterGroup("RenderableGroup", entities => entities.Where(s => s.IsRenderable()));
  57. _renderableGroup = _pool.GetGroup("RenderableGroup");
  58. }
  59.  
  60. public void BindPool(Pool pool) {
  61. _pool = pool;
  62. }
  63. }
  64.  
  65. public static class RenderTargetExtensions {
  66. public static RenderTarget2D RenderSurface(this RenderTarget2D target, Surface surface) {
  67. return surface.RenderSurface(target);
  68. }
  69. }
  70.  
  71. public static class RenderableExtensions {
  72. /// <summary>
  73. /// For an entity to be renderable, it needs to have a RenderableComponent, a TransformComponent and
  74. /// a PositionComponent
  75. /// </summary>
  76. /// <param name="entity"></param>
  77. /// <returns></returns>
  78. public static bool IsRenderable(this Capsicum.Entity entity) {
  79. return entity.HasComponent<RenderableComponent>()
  80. && entity.HasComponent<TransformComponent>()
  81. && entity.HasComponent<PositionComponent>();
  82. }
  83. }
  84.  
  85. public class RenderableComponent : IComponent {
  86. public IGlyph Glyph { get; set; }
  87. }
  88.  
  89. public class TransformComponent : IComponent {
  90. public TransformComponent(Vector3 transformVec) {
  91. TransformVec = transformVec;
  92. }
  93.  
  94. public Vector3 TransformVec { get; }
  95. }
  96.  
  97. public class PositionComponent : IComponent {
  98. public Vector3 Position { get; }
  99.  
  100. public PositionComponent(Vector3 position) {
  101. Position = position;
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement