Change To OpenGL
This commit is contained in:
@@ -1,266 +0,0 @@
|
||||
using SFML.System;
|
||||
using SFML.Window;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.Core
|
||||
{
|
||||
internal struct JoystickMapping
|
||||
{
|
||||
public uint joysticnumber;
|
||||
public uint buttonnumber;
|
||||
}
|
||||
|
||||
public static class InputMapper
|
||||
{
|
||||
private static Dictionary<Keyboard.Key, KeyboardHandler> KeyboardMapping = new Dictionary<Keyboard.Key, KeyboardHandler>();
|
||||
private static Dictionary<JoystickMapping, JoystickButtonHandler> JoystickButtonMapping = new Dictionary<JoystickMapping, JoystickButtonHandler>();
|
||||
private static Dictionary<Mouse.Button, MouseHandler> MouseButtonMapping = new Dictionary<Mouse.Button, MouseHandler>();
|
||||
|
||||
private static List<(Keyboard.Key, Keyboard.Key)> Keyremaps = new List<(Keyboard.Key, Keyboard.Key)>();
|
||||
private static List<(Mouse.Button, Mouse.Button)> MouseButtonremaps = new List<(Mouse.Button, Mouse.Button)>();
|
||||
private static List<(uint, uint, uint)> JoystickButtonremaps = new List<(uint, uint, uint)>();
|
||||
|
||||
public delegate void KeyboardHandler(Keyboard.Key key);
|
||||
public delegate void MouseHandler(Mouse.Button button);
|
||||
public delegate void JoystickButtonHandler(uint JoystickId, uint ButtonNumber);
|
||||
public delegate void JoystickAxisHandler(uint JoystickId, float x, float y, float z, float r, float u, float v);
|
||||
public delegate void JoystickHatHandler(uint JoystickId, float PovX, float PovY);
|
||||
public delegate void MouseCursorPositionhandler(Vector2i mousePosition);
|
||||
|
||||
public static event JoystickAxisHandler? JoystickAxisEvent;
|
||||
public static event JoystickHatHandler? JoystickHatEvent;
|
||||
public static event MouseCursorPositionhandler? MouseCursorPositionEvent;
|
||||
|
||||
public static void Map(KeyboardHandler Method, Keyboard.Key key)
|
||||
{
|
||||
KeyboardMapping.Add(key, Method);
|
||||
}
|
||||
|
||||
public static void Map(MouseHandler Method, Mouse.Button button)
|
||||
{
|
||||
MouseButtonMapping.Add(button, Method);
|
||||
}
|
||||
|
||||
public static void Map(JoystickButtonHandler Method, uint joysticknumber, uint buttonnumber)
|
||||
{
|
||||
JoystickMapping mapping = new JoystickMapping { joysticnumber = joysticknumber, buttonnumber = buttonnumber };
|
||||
JoystickButtonMapping.Add(mapping, Method);
|
||||
}
|
||||
|
||||
public static void Remap(Keyboard.Key key1, Keyboard.Key key2)
|
||||
{
|
||||
if (KeyboardMapping.ContainsKey(key2)) throw new ArgumentException("Key2 already exists");
|
||||
if (!KeyboardMapping.ContainsKey(key1)) throw new ArgumentException("Key1 not existing");
|
||||
var keyboardHandler = KeyboardMapping[key1];
|
||||
KeyboardMapping.Remove(key1);
|
||||
KeyboardMapping.Add(key2, keyboardHandler);
|
||||
if (!Keyremaps.Contains((key1, key2)))
|
||||
Keyremaps.Add((key1, key2));
|
||||
}
|
||||
|
||||
public static void Remap(Mouse.Button button1, Mouse.Button button2)
|
||||
{
|
||||
if (MouseButtonMapping.ContainsKey(button2)) throw new ArgumentException("Button2 already exists");
|
||||
if (!MouseButtonMapping.ContainsKey(button1)) throw new ArgumentException("Button1 not existing");
|
||||
var mouseHandler = MouseButtonMapping[button1];
|
||||
MouseButtonMapping.Remove(button1);
|
||||
MouseButtonMapping.Add(button2, mouseHandler);
|
||||
if (!MouseButtonremaps.Contains((button1, button2)))
|
||||
MouseButtonremaps.Add((button1, button2));
|
||||
}
|
||||
|
||||
public static void Remap(uint joysticknumber, uint buttonnumber1, uint buttonnumber2)
|
||||
{
|
||||
JoystickMapping mappOld = new JoystickMapping { joysticnumber = joysticknumber, buttonnumber = buttonnumber1 };
|
||||
JoystickMapping mappNew = new JoystickMapping { joysticnumber = joysticknumber, buttonnumber = buttonnumber2 };
|
||||
|
||||
if (JoystickButtonMapping.ContainsKey(mappNew)) throw new ArgumentException("Button2 already exists");
|
||||
if (!JoystickButtonMapping.ContainsKey(mappOld)) throw new ArgumentException("Button1 not existing");
|
||||
|
||||
var joystickHandler = JoystickButtonMapping[mappOld];
|
||||
JoystickButtonMapping.Remove(mappOld);
|
||||
JoystickButtonMapping.Add(mappNew, joystickHandler);
|
||||
|
||||
if (!JoystickButtonremaps.Contains((joysticknumber, buttonnumber1, buttonnumber2)))
|
||||
JoystickButtonremaps.Add((joysticknumber, buttonnumber1, buttonnumber2));
|
||||
}
|
||||
|
||||
public static void ClearRemappingLog()
|
||||
{
|
||||
JoystickButtonremaps.Clear();
|
||||
MouseButtonremaps.Clear();
|
||||
Keyremaps.Clear();
|
||||
}
|
||||
|
||||
public static async Task UpdateOrSafeRemapsAsync(string filepath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filepath)) throw new ArgumentException(nameof(filepath));
|
||||
|
||||
if (File.Exists(filepath))
|
||||
{
|
||||
await UpdateRemapsAsync(filepath);
|
||||
}
|
||||
else
|
||||
{
|
||||
string directoryName = Path.GetDirectoryName(filepath);
|
||||
if (directoryName != null) Directory.CreateDirectory(directoryName);
|
||||
await SafeRemapsAsync(new FileStream(filepath, FileMode.Create));
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task SafeRemapsAsync(FileStream fileStream)
|
||||
{
|
||||
using (StreamWriter writer = new StreamWriter(fileStream))
|
||||
{
|
||||
writer.WriteLine("<KeyMappings>");
|
||||
foreach (var keyRemap in Keyremaps)
|
||||
{
|
||||
writer.WriteLine($"{keyRemap.Item1};{keyRemap.Item2}");
|
||||
}
|
||||
writer.WriteLine("</KeyMappings>");
|
||||
writer.WriteLine("<MouseMappings>");
|
||||
foreach (var mouseRemap in MouseButtonremaps)
|
||||
{
|
||||
writer.WriteLine($"{mouseRemap.Item1};{mouseRemap.Item2}");
|
||||
}
|
||||
writer.WriteLine("</MouseMappings>");
|
||||
writer.WriteLine("<JoystickMappings>");
|
||||
foreach (var joyRemap in JoystickButtonremaps)
|
||||
{
|
||||
writer.WriteLine($"{joyRemap.Item1};{joyRemap.Item2};{joyRemap.Item3}");
|
||||
}
|
||||
writer.WriteLine("</JoystickMappings>");
|
||||
writer.Flush();
|
||||
}
|
||||
await fileStream.FlushAsync();
|
||||
fileStream.Close();
|
||||
}
|
||||
|
||||
private static async Task UpdateRemapsAsync(string filepath)
|
||||
{
|
||||
string Filecontent = null;
|
||||
using (StreamReader reader = new StreamReader(filepath))
|
||||
{
|
||||
Filecontent = await reader.ReadToEndAsync();
|
||||
}
|
||||
|
||||
string[] keyRemaps = ExtractMappings(Filecontent, "<KeyMappings>", "</KeyMappings>");
|
||||
string[] mouseButtonRemaps = ExtractMappings(Filecontent, "<MouseMappings>", "</MouseMappings>");
|
||||
string[] joyButtonRemaps = ExtractMappings(Filecontent, "<JoystickMappings>", "</JoystickMappings>");
|
||||
|
||||
Keyremaps.InsertRange(0, keyRemaps.Select(k => ParseKeyMapping(k)).ToArray());
|
||||
MouseButtonremaps.InsertRange(0, mouseButtonRemaps.Select(m => ParseMouseMapping(m)).ToArray());
|
||||
JoystickButtonremaps.InsertRange(0, joyButtonRemaps.Select(j => ParseJoystickMapping(j)).ToArray());
|
||||
|
||||
using (var fileStream = new FileStream(filepath, FileMode.Create))
|
||||
{
|
||||
await SafeRemapsAsync(fileStream);
|
||||
}
|
||||
}
|
||||
|
||||
private static string[] ExtractMappings(string content, string startTag, string endTag)
|
||||
{
|
||||
int start = content.IndexOf(startTag) + startTag.Length;
|
||||
int end = content.IndexOf(endTag);
|
||||
if (start < 0 || end < 0)
|
||||
return new string[0]; // Falls das Tag nicht gefunden wurde
|
||||
|
||||
string mappings = content.Substring(start, end - start);
|
||||
return mappings.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
|
||||
}
|
||||
|
||||
private static (Keyboard.Key, Keyboard.Key) ParseKeyMapping(string keyMapping)
|
||||
{
|
||||
var keys = keyMapping.Split(";");
|
||||
return (Enum.Parse<Keyboard.Key>(keys[0]), Enum.Parse<Keyboard.Key>(keys[1]));
|
||||
}
|
||||
|
||||
private static (Mouse.Button, Mouse.Button) ParseMouseMapping(string mouseMapping)
|
||||
{
|
||||
var buttons = mouseMapping.Split(";");
|
||||
return (Enum.Parse<Mouse.Button>(buttons[0]), Enum.Parse<Mouse.Button>(buttons[1]));
|
||||
}
|
||||
|
||||
private static (uint, uint, uint) ParseJoystickMapping(string joystickMapping)
|
||||
{
|
||||
var joyButtons = joystickMapping.Split(";");
|
||||
return (uint.Parse(joyButtons[0]), uint.Parse(joyButtons[1]), uint.Parse(joyButtons[2]));
|
||||
}
|
||||
|
||||
public static void revertLastKeyRemap()
|
||||
{
|
||||
if (Keyremaps.Any())
|
||||
{
|
||||
var lastKeyRemap = Keyremaps.Last();
|
||||
Keyremaps.Remove(lastKeyRemap);
|
||||
KeyboardMapping[lastKeyRemap.Item2] = KeyboardMapping[lastKeyRemap.Item1];
|
||||
}
|
||||
}
|
||||
|
||||
public static void revertLastMouseButtonRemap()
|
||||
{
|
||||
if (MouseButtonremaps.Any())
|
||||
{
|
||||
var lastMouseButtonRemap = MouseButtonremaps.Last();
|
||||
MouseButtonremaps.Remove(lastMouseButtonRemap);
|
||||
MouseButtonMapping[lastMouseButtonRemap.Item2] = MouseButtonMapping[lastMouseButtonRemap.Item1];
|
||||
}
|
||||
}
|
||||
|
||||
public static void revertLastJoystickButtonRemap()
|
||||
{
|
||||
if (JoystickButtonremaps.Any())
|
||||
{
|
||||
var lastJoystickButtonRemap = JoystickButtonremaps.Last();
|
||||
JoystickButtonremaps.Remove(lastJoystickButtonRemap);
|
||||
|
||||
var mappOld = new JoystickMapping { joysticnumber = lastJoystickButtonRemap.Item1, buttonnumber = lastJoystickButtonRemap.Item2 };
|
||||
var mappNew = new JoystickMapping { joysticnumber = lastJoystickButtonRemap.Item1, buttonnumber = lastJoystickButtonRemap.Item3 };
|
||||
|
||||
JoystickButtonMapping[mappNew] = JoystickButtonMapping[mappOld];
|
||||
JoystickButtonMapping.Remove(mappOld);
|
||||
}
|
||||
}
|
||||
|
||||
public static void HandleKeyboardInput(Keyboard.Key key)
|
||||
{
|
||||
if (KeyboardMapping.ContainsKey(key))
|
||||
{
|
||||
KeyboardMapping[key](key); // Event auslösen
|
||||
}
|
||||
}
|
||||
|
||||
public static void HandleMouseInput(Mouse.Button button)
|
||||
{
|
||||
if (MouseButtonMapping.ContainsKey(button))
|
||||
{
|
||||
MouseButtonMapping[button](button);
|
||||
}
|
||||
}
|
||||
|
||||
public static void HandleJoystickButtonInput(uint joystickId, uint buttonNumber)
|
||||
{
|
||||
var joymap = new JoystickMapping { buttonnumber = buttonNumber, joysticnumber = joystickId };
|
||||
if (JoystickButtonMapping.ContainsKey(joymap))
|
||||
{
|
||||
JoystickButtonMapping[joymap](joystickId, buttonNumber);
|
||||
}
|
||||
}
|
||||
|
||||
public static void HandleMouseMovement(Vector2i mouseposition)
|
||||
{
|
||||
MouseCursorPositionEvent?.Invoke(mouseposition);
|
||||
}
|
||||
|
||||
public static void HandleJoytickMove(uint JoystickId, float x, float y, float z, float r, float u, float v, float PovX, float PovY)
|
||||
{
|
||||
JoystickAxisEvent?.Invoke(JoystickId, x, y, z, r, u, v);
|
||||
JoystickHatEvent?.Invoke(JoystickId, PovX, PovY);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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