From 0735aae74fbc60fcd2e981ff15888cb8c8495c91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Wed, 4 Aug 2021 17:22:49 +0200 Subject: [PATCH] Added OrthographicCameraController --- .../src/OrthographicCameraController.bf | 122 ++++++++++++++++++ Sandbox/BeefProj.toml | 2 +- Sandbox/src/SandboxApp.bf | 61 ++------- Sandbox/src/SandboxApp2D.bf | 4 +- 4 files changed, 136 insertions(+), 53 deletions(-) create mode 100644 GlitchyEngine/src/OrthographicCameraController.bf diff --git a/GlitchyEngine/src/OrthographicCameraController.bf b/GlitchyEngine/src/OrthographicCameraController.bf new file mode 100644 index 0000000..5c7ab1d --- /dev/null +++ b/GlitchyEngine/src/OrthographicCameraController.bf @@ -0,0 +1,122 @@ +using GlitchyEngine.Renderer; +using GlitchyEngine.Events; +using GlitchyEngine.Math; +using System; + +namespace GlitchyEngine +{ + /// A simple controller for an orthographic camera + public class OrthographicCameraController + { + private OrthographicCamera _camera ~ delete _; + private float _aspectRatio; + private float _zoomLevel = 1.0f; + + private bool _rotation; + + private Vector3 _cameraPosition; + private float _cameraRotation; + private float _cameraTranslationSpeed = 1.0f; + private float _cameraRotationSpeed = 1.0f; + + public OrthographicCamera Camera => _camera; + + public this(float aspectRatio, bool rotation = false) + { + _aspectRatio = aspectRatio; + _rotation = rotation; + + _camera = new OrthographicCamera(); + _camera.NearPlane = -10f; + _camera.FarPlane = 10f; + + UpdateCamera(); + } + + public void Update(GameTime gameTime) + { + if(Application.Get().Window.IsActive) + { + Vector3 movement = .(); + + if(Input.IsKeyPressed(Key.W)) + { + movement.Y += 1; + } + if(Input.IsKeyPressed(Key.S)) + { + movement.Y -= 1; + } + + if(Input.IsKeyPressed(Key.A)) + { + movement.X -= 1; + } + if(Input.IsKeyPressed(Key.D)) + { + movement.X += 1; + } + + if(movement != .Zero) + movement.Normalize(); + + movement *= (float)(gameTime.FrameTime.TotalSeconds) * _cameraTranslationSpeed; + + _cameraPosition += movement; + + if(_rotation) + { + if(Input.IsKeyPressed(Key.Q)) + { + _cameraRotation += (float)(gameTime.FrameTime.TotalSeconds) * _cameraRotationSpeed; + } + if(Input.IsKeyPressed(Key.E)) + { + _cameraRotation -= (float)(gameTime.FrameTime.TotalSeconds) * _cameraRotationSpeed; + } + } + + UpdateCamera(); + } + } + + public void OnEvent(Event e) + { + EventDispatcher dispatcher = EventDispatcher(e); + dispatcher.Dispatch(scope => OnMouseScrolled); + dispatcher.Dispatch(scope => OnWindowResized); + } + + private bool OnMouseScrolled(MouseScrolledEvent e) + { + _zoomLevel -= e.YOffset * 0.25f; + + _zoomLevel = Math.Max(_zoomLevel, 0.25f); + + UpdateCamera(); + + return false; + } + + private bool OnWindowResized(WindowResizeEvent e) + { + _aspectRatio = (float)e.Width / (float)e.Height; + + UpdateCamera(); + + return false; + } + + private void UpdateCamera() + { + _camera.Left = -_aspectRatio * _zoomLevel; + _camera.Right = _aspectRatio * _zoomLevel; + _camera.Top = _zoomLevel; + _camera.Bottom = -_zoomLevel; + _camera.Rotation = .(_camera.Rotation.X, _camera.Rotation.Y, _cameraRotation); + _camera.Position = _cameraPosition; + + _camera.Update(); + } + } +} diff --git a/Sandbox/BeefProj.toml b/Sandbox/BeefProj.toml index 44941ce..004aa4a 100644 --- a/Sandbox/BeefProj.toml +++ b/Sandbox/BeefProj.toml @@ -5,7 +5,7 @@ Dependencies = {GlitchyEngine = "*", corlib = "*", LodePng = "*"} Name = "Sandbox" TargetType = "BeefGUIApplication" StartupObject = "GlitchyEngine.Program" -ProcessorMacros = ["SANDBOX_2D"] +ProcessorMacros = ["!SANDBOX_2D"] [Configs.Debug.Win64] CLibType = "DynamicDebug" diff --git a/Sandbox/src/SandboxApp.bf b/Sandbox/src/SandboxApp.bf index eb08cd1..a3e5307 100644 --- a/Sandbox/src/SandboxApp.bf +++ b/Sandbox/src/SandboxApp.bf @@ -70,6 +70,8 @@ namespace Sandbox EcsWorld _world = new EcsWorld() ~ delete _; + OrthographicCameraController _cameraController ~ delete _; + private Vector3 CircleCoord(float angle) { return .(Math.Cos(angle), Math.Sin(angle), 0); @@ -157,7 +159,7 @@ namespace Sandbox } // Create rasterizer state - GlitchyEngine.Renderer.RasterizerStateDescription rsDesc = .(.Solid, .Back, true); + RasterizerStateDescription rsDesc = .(.Solid, .Back, true); _rasterizerState = new RasterizerState(_context, rsDesc); _texture = new Texture2D(_context, "content/Textures/Checkerboard.dds"); @@ -180,9 +182,10 @@ namespace Sandbox _opaqueBlendState = new BlendState(_context, .Default); InitEcs(); - } - Entity _cameraEntity; + _cameraController = new OrthographicCameraController(Application.Get().Window.Context.SwapChain.BackbufferViewport.Width / + Application.Get().Window.Context.SwapChain.BackbufferViewport.Height); + } void InitEcs() { @@ -190,16 +193,6 @@ namespace Sandbox _world.Register(); _world.Register(); - // Create camera entity - _cameraEntity = _world.NewEntity(); - var cameraTransform = _world.AssignComponent(_cameraEntity); - var camera = _world.AssignComponent(_cameraEntity); - *cameraTransform = TransformComponent(); - camera.NearPlane = 0.1f; - camera.FarPlane = 10.0f; - camera.FovY = Math.PI_f / 4; - cameraTransform.Position = .(0, -1, -5); - for(int x < 20) for(int y < 20) { @@ -215,42 +208,8 @@ namespace Sandbox public override void Update(GameTime gameTime) { - var cameraTransform = _world.GetComponent(_cameraEntity); - - if(Application.Get().Window.IsActive) - { - Vector2 movement = .(); - - if(Input.IsKeyPressed(Key.W)) - { - movement.Y += 1; - } - if(Input.IsKeyPressed(Key.S)) - { - movement.Y -= 1; - } - - if(Input.IsKeyPressed(Key.A)) - { - movement.X -= 1; - } - if(Input.IsKeyPressed(Key.D)) - { - movement.X += 1; - } - - if(movement != .Zero) - movement.Normalize(); - - movement *= (float)(gameTime.FrameTime.TotalSeconds); - - cameraTransform.Position += .(movement, 0); - cameraTransform.Update(); - } - - var camera = _world.GetComponent(_cameraEntity); - camera.Aspect = Application.Get().Window.Context.SwapChain.BackbufferViewport.Width / - Application.Get().Window.Context.SwapChain.BackbufferViewport.Height; + + _cameraController.Update(gameTime); RenderCommand.Clear(null, .(0.2f, 0.2f, 0.2f)); @@ -262,7 +221,7 @@ namespace Sandbox _context.SetViewport(_context.SwapChain.BackbufferViewport); - Renderer.BeginScene(_world, _cameraEntity); + Renderer.BeginScene(_cameraController.Camera); _opaqueBlendState.Bind(); @@ -312,6 +271,8 @@ namespace Sandbox public override void OnEvent(Event event) { + _cameraController.OnEvent(event); + EventDispatcher dispatcher = EventDispatcher(event); dispatcher.Dispatch(scope (e) => OnImGuiRender(e)); diff --git a/Sandbox/src/SandboxApp2D.bf b/Sandbox/src/SandboxApp2D.bf index 8983a77..ec35adc 100644 --- a/Sandbox/src/SandboxApp2D.bf +++ b/Sandbox/src/SandboxApp2D.bf @@ -283,11 +283,11 @@ namespace Sandbox if(i == 1) { - Renderer2D.Draw(_texture, 2 * x, 2 * y, 1, 1, .(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256))); + Renderer2D.Draw(_texture, 2 * x, 2 * y, 1, 1, .(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256)), 10); } else if(i == 2) { - Renderer2D.Draw(_ge_logo, 2 * x, 2 * y, 1, 1, .(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256))); + Renderer2D.Draw(_ge_logo, 2 * x, 2 * y, 1, 1, .(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256)), 10); } }