mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Added OrthographicCameraController
This commit is contained in:
@@ -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<MouseScrolledEvent>(scope => OnMouseScrolled);
|
||||||
|
dispatcher.Dispatch<WindowResizeEvent>(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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ Dependencies = {GlitchyEngine = "*", corlib = "*", LodePng = "*"}
|
|||||||
Name = "Sandbox"
|
Name = "Sandbox"
|
||||||
TargetType = "BeefGUIApplication"
|
TargetType = "BeefGUIApplication"
|
||||||
StartupObject = "GlitchyEngine.Program"
|
StartupObject = "GlitchyEngine.Program"
|
||||||
ProcessorMacros = ["SANDBOX_2D"]
|
ProcessorMacros = ["!SANDBOX_2D"]
|
||||||
|
|
||||||
[Configs.Debug.Win64]
|
[Configs.Debug.Win64]
|
||||||
CLibType = "DynamicDebug"
|
CLibType = "DynamicDebug"
|
||||||
|
|||||||
+10
-49
@@ -70,6 +70,8 @@ namespace Sandbox
|
|||||||
|
|
||||||
EcsWorld _world = new EcsWorld() ~ delete _;
|
EcsWorld _world = new EcsWorld() ~ delete _;
|
||||||
|
|
||||||
|
OrthographicCameraController _cameraController ~ delete _;
|
||||||
|
|
||||||
private Vector3 CircleCoord(float angle)
|
private Vector3 CircleCoord(float angle)
|
||||||
{
|
{
|
||||||
return .(Math.Cos(angle), Math.Sin(angle), 0);
|
return .(Math.Cos(angle), Math.Sin(angle), 0);
|
||||||
@@ -157,7 +159,7 @@ namespace Sandbox
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create rasterizer state
|
// Create rasterizer state
|
||||||
GlitchyEngine.Renderer.RasterizerStateDescription rsDesc = .(.Solid, .Back, true);
|
RasterizerStateDescription rsDesc = .(.Solid, .Back, true);
|
||||||
_rasterizerState = new RasterizerState(_context, rsDesc);
|
_rasterizerState = new RasterizerState(_context, rsDesc);
|
||||||
|
|
||||||
_texture = new Texture2D(_context, "content/Textures/Checkerboard.dds");
|
_texture = new Texture2D(_context, "content/Textures/Checkerboard.dds");
|
||||||
@@ -180,9 +182,10 @@ namespace Sandbox
|
|||||||
_opaqueBlendState = new BlendState(_context, .Default);
|
_opaqueBlendState = new BlendState(_context, .Default);
|
||||||
|
|
||||||
InitEcs();
|
InitEcs();
|
||||||
}
|
|
||||||
|
|
||||||
Entity _cameraEntity;
|
_cameraController = new OrthographicCameraController(Application.Get().Window.Context.SwapChain.BackbufferViewport.Width /
|
||||||
|
Application.Get().Window.Context.SwapChain.BackbufferViewport.Height);
|
||||||
|
}
|
||||||
|
|
||||||
void InitEcs()
|
void InitEcs()
|
||||||
{
|
{
|
||||||
@@ -190,16 +193,6 @@ namespace Sandbox
|
|||||||
_world.Register<MeshComponent>();
|
_world.Register<MeshComponent>();
|
||||||
_world.Register<CameraComponent>();
|
_world.Register<CameraComponent>();
|
||||||
|
|
||||||
// Create camera entity
|
|
||||||
_cameraEntity = _world.NewEntity();
|
|
||||||
var cameraTransform = _world.AssignComponent<TransformComponent>(_cameraEntity);
|
|
||||||
var camera = _world.AssignComponent<CameraComponent>(_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 x < 20)
|
||||||
for(int y < 20)
|
for(int y < 20)
|
||||||
{
|
{
|
||||||
@@ -215,42 +208,8 @@ namespace Sandbox
|
|||||||
|
|
||||||
public override void Update(GameTime gameTime)
|
public override void Update(GameTime gameTime)
|
||||||
{
|
{
|
||||||
var cameraTransform = _world.GetComponent<TransformComponent>(_cameraEntity);
|
|
||||||
|
|
||||||
if(Application.Get().Window.IsActive)
|
_cameraController.Update(gameTime);
|
||||||
{
|
|
||||||
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<CameraComponent>(_cameraEntity);
|
|
||||||
camera.Aspect = Application.Get().Window.Context.SwapChain.BackbufferViewport.Width /
|
|
||||||
Application.Get().Window.Context.SwapChain.BackbufferViewport.Height;
|
|
||||||
|
|
||||||
RenderCommand.Clear(null, .(0.2f, 0.2f, 0.2f));
|
RenderCommand.Clear(null, .(0.2f, 0.2f, 0.2f));
|
||||||
|
|
||||||
@@ -262,7 +221,7 @@ namespace Sandbox
|
|||||||
|
|
||||||
_context.SetViewport(_context.SwapChain.BackbufferViewport);
|
_context.SetViewport(_context.SwapChain.BackbufferViewport);
|
||||||
|
|
||||||
Renderer.BeginScene(_world, _cameraEntity);
|
Renderer.BeginScene(_cameraController.Camera);
|
||||||
|
|
||||||
_opaqueBlendState.Bind();
|
_opaqueBlendState.Bind();
|
||||||
|
|
||||||
@@ -312,6 +271,8 @@ namespace Sandbox
|
|||||||
|
|
||||||
public override void OnEvent(Event event)
|
public override void OnEvent(Event event)
|
||||||
{
|
{
|
||||||
|
_cameraController.OnEvent(event);
|
||||||
|
|
||||||
EventDispatcher dispatcher = EventDispatcher(event);
|
EventDispatcher dispatcher = EventDispatcher(event);
|
||||||
|
|
||||||
dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e));
|
dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e));
|
||||||
|
|||||||
@@ -283,11 +283,11 @@ namespace Sandbox
|
|||||||
|
|
||||||
if(i == 1)
|
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)
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user