Change To OpenGL
This commit is contained in:
@@ -1,10 +1,19 @@
|
||||
using Firebird2D.SceneSystem;
|
||||
using SFML.Graphics;
|
||||
using SFML.System;
|
||||
using SFML.Window;
|
||||
using Firebird2D.Datatypes;
|
||||
using Firebird2D.DataypesAndInterfaces.Datatypes;
|
||||
using Firebird2D.DataypesAndInterfaces.EventArguments;
|
||||
using Firebird2D.DataypesAndInterfaces.Interfaces;
|
||||
using Firebird2D.Interfaces;
|
||||
using Firebird2D.Interfaces.Actors;
|
||||
using Firebird2D.Rendering;
|
||||
using Firebird2D.SceneSystem;
|
||||
using Silk.NET.Input;
|
||||
using Silk.NET.Maths;
|
||||
using Silk.NET.OpenGL;
|
||||
using Silk.NET.Windowing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -13,92 +22,88 @@ namespace Firebird2D.Core
|
||||
{
|
||||
public class MainWindow
|
||||
{
|
||||
private RenderWindow renderWindow;
|
||||
private IWindow renderWindow;
|
||||
|
||||
private Clock clock = new Clock();
|
||||
private IInputContext _inputContext;
|
||||
|
||||
private List<uint> joyStickMovement = new List<uint>();
|
||||
private IKeyboard _keyboard;
|
||||
|
||||
public Vector2u mainWindowSize { get; private set; }
|
||||
private GL _gl;
|
||||
|
||||
internal I_Scene? ActiveSecene { set; get; }
|
||||
private FirebirdShader _shader;
|
||||
|
||||
public MainWindow(VideoMode resolution, string GameName, Styles style = Styles.Default)
|
||||
private Matrix4x4 _projection;
|
||||
|
||||
internal I_Scene? ActiveScene { set; get; }
|
||||
|
||||
private IEventBus eventBus;
|
||||
|
||||
private IKeyMapper keyMapper;
|
||||
|
||||
private IPipeline<FirebirdTickArgs> tickPipeline;
|
||||
|
||||
public MainWindow(Game game)
|
||||
{
|
||||
renderWindow = new RenderWindow(resolution, GameName, style);
|
||||
|
||||
renderWindow.Closed += (sender, e) => renderWindow.Close();
|
||||
|
||||
renderWindow.Resized += RenderWindow_Resized;
|
||||
|
||||
mainWindowSize = renderWindow.Size;
|
||||
renderWindow.KeyPressed += RenderWindow_KeyPressed;
|
||||
renderWindow.JoystickButtonPressed += RenderWindow_JoystickButtonPressed;
|
||||
renderWindow.MouseButtonPressed += RenderWindow_MouseButtonPressed;
|
||||
renderWindow.MouseMoved += RenderWindow_MouseMoved;
|
||||
renderWindow.JoystickMoved += RenderWindow_JoystickMoved;
|
||||
eventBus = game.EventBus;
|
||||
keyMapper = game.KeyMapper;
|
||||
}
|
||||
|
||||
private void RenderWindow_JoystickMoved(object? sender, JoystickMoveEventArgs e)
|
||||
public void Run()
|
||||
{
|
||||
if(!joyStickMovement.Contains(e.JoystickId)) joyStickMovement.Add(e.JoystickId);
|
||||
WindowOptions opts = WindowOptions.Default;
|
||||
opts.Size = new Silk.NET.Maths.Vector2D<int>(800, 600);
|
||||
opts.Title = "Testtitle"; // TODO Title einstellbar
|
||||
tickPipeline = eventBus.GetOrBuildPipeline<FirebirdTickArgs>(FirebirdSystemEventIds.Tick, this);
|
||||
|
||||
renderWindow = Window.Create(opts);
|
||||
|
||||
renderWindow.Load += OnLoad;
|
||||
renderWindow.Render += OnRender;
|
||||
renderWindow.Update += OnUpdate;
|
||||
renderWindow.FramebufferResize += OnResize;
|
||||
renderWindow.Run();
|
||||
}
|
||||
|
||||
private void RenderWindow_MouseMoved(object? sender, MouseMoveEventArgs e)
|
||||
private void OnResize(Vector2D<int> newSize)
|
||||
{
|
||||
Vector2i mousposition = new Vector2i(e.X, e.Y);
|
||||
InputMapper.HandleMouseMovement(mousposition);
|
||||
_projection = Matrix4x4.CreateOrthographicOffCenter(0, newSize.X, newSize.Y, 0, -1, 1);
|
||||
}
|
||||
|
||||
private void RenderWindow_MouseButtonPressed(object? sender, MouseButtonEventArgs e)
|
||||
private void OnUpdate(double dt)
|
||||
{
|
||||
InputMapper.HandleMouseInput(e.Button);
|
||||
}
|
||||
|
||||
private void RenderWindow_JoystickButtonPressed(object? sender, JoystickButtonEventArgs e)
|
||||
{
|
||||
InputMapper.HandleJoystickButtonInput(e.JoystickId, e.Button);
|
||||
}
|
||||
|
||||
private void RenderWindow_KeyPressed(object? sender, KeyEventArgs e)
|
||||
{
|
||||
InputMapper.HandleKeyboardInput(e.Code);
|
||||
}
|
||||
|
||||
private void RenderWindow_Resized(object? sender, SizeEventArgs e)
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Gameloop()
|
||||
{
|
||||
while (renderWindow.IsOpen)
|
||||
tickPipeline.SendEvent(this,new FirebirdTickArgs(dt,_shader,_projection));
|
||||
if (ActiveScene != null)
|
||||
{
|
||||
int delteMiliseconds = clock.Restart().AsMilliseconds();
|
||||
|
||||
renderWindow.DispatchEvents();
|
||||
|
||||
renderWindow.Clear();
|
||||
|
||||
if (ActiveSecene != null)
|
||||
{
|
||||
foreach (uint joystickId in joyStickMovement)
|
||||
InputMapper.HandleJoytickMove(joystickId
|
||||
,Joystick.GetAxisPosition(joystickId,Joystick.Axis.X)
|
||||
, Joystick.GetAxisPosition(joystickId, Joystick.Axis.Y)
|
||||
, Joystick.GetAxisPosition(joystickId, Joystick.Axis.Z)
|
||||
, Joystick.GetAxisPosition(joystickId, Joystick.Axis.R)
|
||||
, Joystick.GetAxisPosition(joystickId, Joystick.Axis.U)
|
||||
, Joystick.GetAxisPosition(joystickId, Joystick.Axis.V)
|
||||
, Joystick.GetAxisPosition(joystickId, Joystick.Axis.PovX)
|
||||
, Joystick.GetAxisPosition(joystickId, Joystick.Axis.PovY));
|
||||
ActiveSecene.TickScene(delteMiliseconds);
|
||||
ActiveSecene.RenderScene(renderWindow);
|
||||
}
|
||||
|
||||
renderWindow.Display();
|
||||
ActiveScene.TickScene(dt);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRender(double dt)
|
||||
{
|
||||
_gl.Clear(ClearBufferMask.ColorBufferBit);
|
||||
|
||||
_shader.Use();
|
||||
_shader.SetMatrix4("projection", _projection);
|
||||
|
||||
if (ActiveScene != null)
|
||||
{
|
||||
ActiveScene.RenderScene(_projection, _shader, dt);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnLoad()
|
||||
{
|
||||
_gl = GL.GetApi(renderWindow);
|
||||
|
||||
_gl.ClearColor(0.1f,0.1f,0.1f,1.0f);
|
||||
_gl.Enable(GLEnum.Blend);
|
||||
_gl.BlendFunc(GLEnum.SrcAlpha, GLEnum.OneMinusSrcAlpha);
|
||||
|
||||
_shader = new FirebirdShader(_gl, "shaders/vertex.glsl", "shaders/fragment.glsl");
|
||||
|
||||
_projection = Matrix4x4.CreateOrthographicOffCenter(0, renderWindow.Size.X, renderWindow.Size.Y, 0, -1, 1);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user