Skip to content

Commit

Permalink
Add UpdateCameraOnResize processor
Browse files Browse the repository at this point in the history
- GFX initialization moved to Game.OnLoad
- Renderer processor doesn't update Camera anymore
- Rename MainCamera => FullscreenCamera
- Add Universe.Has method
  • Loading branch information
copygirl committed Jan 2, 2020
1 parent 3532ef2 commit e2fe201
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Immersion/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Program()
protected override void OnLoad()
{
base.OnLoad();
var (mainCamera, _) = GetAll<MainCamera>().First();
var (mainCamera, _) = GetAll<FullscreenCamera>().First();
Set(mainCamera, (Transform)Matrix4x4.CreateTranslation(0, 26, 0));

var meshLoader = Processors.GetOrThrow<MeshLoader>();
Expand Down
10 changes: 5 additions & 5 deletions src/gaemstone.Client/Components/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 10 additions & 7 deletions src/gaemstone.Client/Game.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Drawing;
using System.IO;
using System.Numerics;
Expand Down Expand Up @@ -30,7 +31,7 @@ public Game()

Components.AddStore(new PackedArrayStore<Transform>());
Components.AddStore(new PackedArrayStore<Camera>());
Components.AddStore(new PackedArrayStore<MainCamera>());
Components.AddStore(new PackedArrayStore<FullscreenCamera>());
Components.AddStore(new PackedArrayStore<IndexedMesh>());
Components.AddStore(new PackedArrayStore<Texture>());
}
Expand All @@ -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<Renderer>();
Processors.Start<MeshLoader>();
Processors.Start<UpdateCameraOnResize>();
Processors.Start<CameraController>();

var mainCamera = Entities.New();
Set(mainCamera, (Transform)Matrix4x4.Identity);
Set(mainCamera, FullscreenCamera.Default3D);
}

protected virtual void OnClosing()
Expand Down
8 changes: 2 additions & 6 deletions src/gaemstone.Client/Graphics/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Renderer : IProcessor
{
private Game _game = null!;
private IComponentStore<Camera> _cameraStore = null!;
private IComponentStore<MainCamera> _mainCameraStore = null!;
private IComponentStore<FullscreenCamera> _mainCameraStore = null!;
private IComponentStore<Transform> _transformStore = null!;
private IComponentStore<IndexedMesh> _meshStore = null!;
private IComponentStore<Texture> _textureStore = null!;
Expand All @@ -28,18 +28,14 @@ public void OnLoad(Universe universe)
{
_game = (Game)universe;
_cameraStore = universe.Components.GetStore<Camera>();
_mainCameraStore = universe.Components.GetStore<MainCamera>();
_mainCameraStore = universe.Components.GetStore<FullscreenCamera>();
_transformStore = universe.Components.GetStore<Transform>();
_meshStore = universe.Components.GetStore<IndexedMesh>();
_textureStore = universe.Components.GetStore<Texture>();

_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");

Expand Down
2 changes: 1 addition & 1 deletion src/gaemstone.Client/Processors/CameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void OnKeyUp(IKeyboard keyboard, Key key, int code)

public void OnUpdate(double delta)
{
var (mainCamera, _) = _game.GetAll<MainCamera>().First();
var (mainCamera, _) = _game.GetAll<FullscreenCamera>().First();
var transform = _game.Get<Transform>(mainCamera);

var xMovement = -_mouseMoved.X * (float)delta / 100;
Expand Down
50 changes: 50 additions & 0 deletions src/gaemstone.Client/Processors/UpdateCameraOnResize.cs
Original file line number Diff line number Diff line change
@@ -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<FullscreenCamera>()) {
// Only update cameras when _update is true, or
// entity doesn't yet have a Camera component.
if (!_update && _game.Has<Camera>(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)
});
}
}
}
}
7 changes: 7 additions & 0 deletions src/gaemstone.Common/ECS/Universe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public T Get<T>(Entity entity)
return store.Get(entity.ID);
}

public bool Has<T>(Entity entity)
{
if (!Entities.IsAlive(entity))
throw new ArgumentException($"{entity} is not alive");
return Components.GetStore<T>()?.TryGet(entity.ID, out _) ?? false;
}

public void Set<T>(Entity entity, T value)
{
if (!Entities.IsAlive(entity))
Expand Down

0 comments on commit e2fe201

Please sign in to comment.