From e2fe201b4ddaaf792bacf336faece708022f24cd Mon Sep 17 00:00:00 2001 From: copygirl Date: Thu, 2 Jan 2020 17:14:11 +0100 Subject: [PATCH] Add UpdateCameraOnResize processor - GFX initialization moved to Game.OnLoad - Renderer processor doesn't update Camera anymore - Rename MainCamera => FullscreenCamera - Add Universe.Has method --- src/Immersion/Program.cs | 2 +- src/gaemstone.Client/Components/Camera.cs | 10 ++-- src/gaemstone.Client/Game.cs | 17 ++++--- src/gaemstone.Client/Graphics/Renderer.cs | 8 +-- .../Processors/CameraController.cs | 2 +- .../Processors/UpdateCameraOnResize.cs | 50 +++++++++++++++++++ src/gaemstone.Common/ECS/Universe.cs | 7 +++ 7 files changed, 76 insertions(+), 20 deletions(-) create mode 100644 src/gaemstone.Client/Processors/UpdateCameraOnResize.cs diff --git a/src/Immersion/Program.cs b/src/Immersion/Program.cs index cbb2db3..e75192f 100644 --- a/src/Immersion/Program.cs +++ b/src/Immersion/Program.cs @@ -34,7 +34,7 @@ public Program() protected override void OnLoad() { base.OnLoad(); - var (mainCamera, _) = GetAll().First(); + var (mainCamera, _) = GetAll().First(); Set(mainCamera, (Transform)Matrix4x4.CreateTranslation(0, 26, 0)); var meshLoader = Processors.GetOrThrow(); diff --git a/src/gaemstone.Client/Components/Camera.cs b/src/gaemstone.Client/Components/Camera.cs index 5efe615..cf23ec1 100644 --- a/src/gaemstone.Client/Components/Camera.cs +++ b/src/gaemstone.Client/Components/Camera.cs @@ -9,12 +9,12 @@ public struct Camera public Matrix4x4 Matrix; } - public struct MainCamera + public struct FullscreenCamera { - public static readonly MainCamera Default2D - = new MainCamera { NearPlane = -100.0F, FarPlane = 100.0F }; - public static readonly MainCamera Default3D - = new MainCamera { FieldOfView = 80.0F, NearPlane = 0.1F, FarPlane = 100.0F }; + public static readonly FullscreenCamera Default2D + = new FullscreenCamera { NearPlane = -100.0F, FarPlane = 100.0F }; + public static readonly FullscreenCamera Default3D + = new FullscreenCamera { FieldOfView = 80.0F, NearPlane = 0.1F, FarPlane = 100.0F }; public float FieldOfView; public float NearPlane, FarPlane; diff --git a/src/gaemstone.Client/Game.cs b/src/gaemstone.Client/Game.cs index eeb073e..0e8ccb9 100644 --- a/src/gaemstone.Client/Game.cs +++ b/src/gaemstone.Client/Game.cs @@ -1,3 +1,4 @@ +using System; using System.Drawing; using System.IO; using System.Numerics; @@ -30,7 +31,7 @@ public Game() Components.AddStore(new PackedArrayStore()); Components.AddStore(new PackedArrayStore()); - Components.AddStore(new PackedArrayStore()); + Components.AddStore(new PackedArrayStore()); Components.AddStore(new PackedArrayStore()); Components.AddStore(new PackedArrayStore()); } @@ -43,16 +44,18 @@ public void Run() protected virtual void OnLoad() { - var mainCamera = Entities.New(); - Set(mainCamera, (Transform)Matrix4x4.Identity); - Set(mainCamera, MainCamera.Default3D); + GFX.Initialize(); + GFX.OnDebugOutput += (source, type, id, severity, message) => + Console.WriteLine($"[GLDebug] [{severity}] {type}/{id}: {message}"); - // TODO: This currently has to sit exactly here. - // Renderer requires MainCamera, and it initializes GFX, - // which is required for MeshLoader to create meshes. Processors.Start(); Processors.Start(); + Processors.Start(); Processors.Start(); + + var mainCamera = Entities.New(); + Set(mainCamera, (Transform)Matrix4x4.Identity); + Set(mainCamera, FullscreenCamera.Default3D); } protected virtual void OnClosing() diff --git a/src/gaemstone.Client/Graphics/Renderer.cs b/src/gaemstone.Client/Graphics/Renderer.cs index dcc5397..79d6b75 100644 --- a/src/gaemstone.Client/Graphics/Renderer.cs +++ b/src/gaemstone.Client/Graphics/Renderer.cs @@ -14,7 +14,7 @@ public class Renderer : IProcessor { private Game _game = null!; private IComponentStore _cameraStore = null!; - private IComponentStore _mainCameraStore = null!; + private IComponentStore _mainCameraStore = null!; private IComponentStore _transformStore = null!; private IComponentStore _meshStore = null!; private IComponentStore _textureStore = null!; @@ -28,7 +28,7 @@ public void OnLoad(Universe universe) { _game = (Game)universe; _cameraStore = universe.Components.GetStore(); - _mainCameraStore = universe.Components.GetStore(); + _mainCameraStore = universe.Components.GetStore(); _transformStore = universe.Components.GetStore(); _meshStore = universe.Components.GetStore(); _textureStore = universe.Components.GetStore(); @@ -36,10 +36,6 @@ public void OnLoad(Universe universe) _game.Window.Resize += OnWindowResize; _game.Window.Render += OnWindowRender; - GFX.Initialize(); - GFX.OnDebugOutput += (source, type, id, severity, message) => - Console.WriteLine($"[GLDebug] [{severity}] {type}/{id}: {message}"); - var vertexShaderSource = _game.GetResourceAsString("default.vs.glsl"); var fragmentShaderSource = _game.GetResourceAsString("default.fs.glsl"); diff --git a/src/gaemstone.Client/Processors/CameraController.cs b/src/gaemstone.Client/Processors/CameraController.cs index fb87326..ae729db 100644 --- a/src/gaemstone.Client/Processors/CameraController.cs +++ b/src/gaemstone.Client/Processors/CameraController.cs @@ -100,7 +100,7 @@ private void OnKeyUp(IKeyboard keyboard, Key key, int code) public void OnUpdate(double delta) { - var (mainCamera, _) = _game.GetAll().First(); + var (mainCamera, _) = _game.GetAll().First(); var transform = _game.Get(mainCamera); var xMovement = -_mouseMoved.X * (float)delta / 100; diff --git a/src/gaemstone.Client/Processors/UpdateCameraOnResize.cs b/src/gaemstone.Client/Processors/UpdateCameraOnResize.cs new file mode 100644 index 0000000..efa7a75 --- /dev/null +++ b/src/gaemstone.Client/Processors/UpdateCameraOnResize.cs @@ -0,0 +1,50 @@ +using System; +using System.Drawing; +using System.Numerics; +using gaemstone.Client.Components; +using gaemstone.Common.ECS; +using gaemstone.Common.ECS.Processors; + +namespace gaemstone.Client.Graphics +{ + public class UpdateCameraOnResize + : IProcessor + { + private Game _game = null!; + private bool _update; + + public void OnLoad(Universe universe) + { + _game = (Game)universe; + _game.Window.Resize += OnWindowResize; + _update = true; + } + + public void OnUnload() + => _game.Window.Resize -= OnWindowResize; + + public void OnWindowResize(Size size) + => _update = true; + + public void OnUpdate(double delta) + { + var size = _game.Window.Size; + foreach (var (entity, fsCamera) in _game.GetAll()) { + // Only update cameras when _update is true, or + // entity doesn't yet have a Camera component. + if (!_update && _game.Has(entity)) continue; + + _game.Set(entity, new Camera { + Viewport = new Rectangle(Point.Empty, size), + Matrix = fsCamera.IsOrthographic + ? Matrix4x4.CreateOrthographic(size.Width, size.Height, + fsCamera.NearPlane, fsCamera.FarPlane) + : Matrix4x4.CreatePerspectiveFieldOfView( + fsCamera.FieldOfView * MathF.PI / 180, // Degrees => Radians + (float)size.Width / size.Height, // Aspect Ratio + fsCamera.NearPlane, fsCamera.FarPlane) + }); + } + } + } +} diff --git a/src/gaemstone.Common/ECS/Universe.cs b/src/gaemstone.Common/ECS/Universe.cs index ab10315..50d6b63 100644 --- a/src/gaemstone.Common/ECS/Universe.cs +++ b/src/gaemstone.Common/ECS/Universe.cs @@ -26,6 +26,13 @@ public T Get(Entity entity) return store.Get(entity.ID); } + public bool Has(Entity entity) + { + if (!Entities.IsAlive(entity)) + throw new ArgumentException($"{entity} is not alive"); + return Components.GetStore()?.TryGet(entity.ID, out _) ?? false; + } + public void Set(Entity entity, T value) { if (!Entities.IsAlive(entity))