Skip to content

Commit

Permalink
Add Window events, Add Boundingboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
MrScautHD committed Aug 25, 2024
1 parent 7750ec8 commit 2a5c8e8
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 360 deletions.
78 changes: 4 additions & 74 deletions src/Bliss.Test/Game.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using System.Numerics;
using Bliss.CSharp;
using Bliss.CSharp.Colors;
using Bliss.CSharp.Interact;
using Bliss.CSharp.Interact.Gamepads;
using Bliss.CSharp.Interact.Mice;
using Bliss.CSharp.Logging;
using Bliss.CSharp.Rendering;
using Bliss.CSharp.Textures;
using Bliss.CSharp.Windowing;
using Veldrid;
using Veldrid.Sdl2;
Expand All @@ -20,9 +14,7 @@ public class Game : Disposable {

public Window Window { get; private set; }
public GraphicsDevice GraphicsDevice { get; private set; }

public CommandList CommandList { get; private set; }
public Graphics Graphics { get; private set; }

private double _fixedFrameRate;

Expand Down Expand Up @@ -60,16 +52,13 @@ public void Run() {
Logger.Info("Initialize command list...");
this.CommandList = this.GraphicsDevice.ResourceFactory.CreateCommandList();

Logger.Info("Initialize graphics...");
this.Graphics = new Graphics(this.GraphicsDevice, this.CommandList);

Logger.Info("Initialize input...");
Input.Init(this.Window);

this.Init();

Logger.Info("Start main loops...");
while (Window.Exists) {
while (this.Window.Exists) {
if (this.GetTargetFps() != 0 && Time.Timer.Elapsed.TotalSeconds <= this._fixedFrameRate) {
continue;
}
Expand All @@ -87,11 +76,7 @@ public void Run() {
this._fixedUpdateTimer -= this._fixedUpdateTimeStep;
}

//this.Graphics.BeginDrawing();
//this.Graphics.ClearBackground(0, Color.DarkGray);
this.Draw(this.Graphics);
//this.Graphics.EndDrawing();

this.Draw(this.GraphicsDevice, this.CommandList);
Input.End();
}

Expand All @@ -101,68 +86,13 @@ public void Run() {

protected virtual void Init() { }

protected virtual void Update() {
/*if (Input.IsMouseButtonPressed(MouseButton.Right)) {
Logger.Error("Right mouse button is pressed!");
}
if (Input.IsMouseButtonDown(MouseButton.Left)) {
Logger.Error("Left mouse button is down!");
}
if (Input.IsMouseButtonReleased(MouseButton.Right)) {
Logger.Error("Right mouse button is released!");
}
if (Input.IsMouseMoving(out Vector2 pos)) {
Logger.Error("Mouse moves: " + pos);
}
if (Input.IsMouseScrolling(out float wheelDelta)) {
Logger.Error("Mouse is scrolling: " + wheelDelta);
}
if (Input.IsKeyDown(Key.A)) {
Logger.Error("Key A is down");
}
if (Input.IsKeyPressed(Key.D)) {
Logger.Error("Key D is pressed");
}
if (Input.IsKeyReleased(Key.D)) {
Logger.Error("Key D is released");
}*/

//if (Input.IsGamepadAvailable(0)) {
// //Logger.Warn(Input.GetGamepadName(0));
// Input.GetGamepadName(0);
//}

if (Input.IsGamepadAvailable(0)) {
if (Input.IsGamepadButtonPressed(0, GamepadButton.A)) {
Input.SetGamepadRumble(0, 0xFFFF, 0xFFFF, 1000);
}
}

if (Input.IsGamepadAvailable(1)) {
if (Input.IsGamepadButtonPressed(1, GamepadButton.A)) {
Input.SetGamepadRumble(1, 0xFFFF, 0xFFFF, 1000);
}
}

if (Input.IsFileDragDropped(out string path)) {
Logger.Warn(path);
}
}
protected virtual void Update() { }

protected virtual void AfterUpdate() { }

protected virtual void FixedUpdate() { }

protected virtual void Draw(Graphics graphics) {

}
protected virtual void Draw(GraphicsDevice graphicsDevice, CommandList commandList) { }

protected virtual void OnClose() { }

Expand Down
2 changes: 1 addition & 1 deletion src/Bliss/CSharp/Colors/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public Color(byte r, byte g, byte b, byte a) {
/// Converts the color to an RgbaFloat value.
/// </summary>
/// <returns>A new instance of the RgbaFloat struct representing the color.</returns>
public readonly RgbaFloat ToRgbaFloat() {
public RgbaFloat ToRgbaFloat() {
return new RgbaFloat(this.R / 255.0F, this.G / 255.0F, this.B / 255.0F, this.A / 255.0F);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Bliss/CSharp/Disposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace Bliss.CSharp;

public abstract class Disposable : IDisposable {

/// <summary>
/// Indicates whether the object has been disposed.
/// </summary>
/// <value>True if the object has been disposed; otherwise, false.</value>
public bool HasDisposed { get; private set; }

/// <summary>
Expand Down
26 changes: 26 additions & 0 deletions src/Bliss/CSharp/Geometry/Box/BoundingBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Numerics;

namespace Bliss.CSharp.Geometry.Box;

public struct BoundingBox {

/// <summary>
/// Minimum box-corner.
/// </summary>
public Vector3 Min;

/// <summary>
/// Maximum box-corner.
/// </summary>
public Vector3 Max;

/// <summary>
/// Initializes a new instance of the <see cref="BoundingBox"/> struct with the specified minimum and maximum vectors.
/// </summary>
/// <param name="min">The minimum vector defining one corner of the bounding box.</param>
/// <param name="max">The maximum vector defining the opposite corner of the bounding box.</param>
public BoundingBox(Vector3 min, Vector3 max) {
this.Min = min;
this.Max = max;
}
}
33 changes: 33 additions & 0 deletions src/Bliss/CSharp/Geometry/Box/OrientedBoundingBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Numerics;

namespace Bliss.CSharp.Geometry.Box;

public struct OrientedBoundingBox {

/// <summary>
/// Minimum box-corner.
/// </summary>
public Vector3 Min;

/// <summary>
/// Maximum box-corner.
/// </summary>
public Vector3 Max;

/// <summary>
/// Box rotation.
/// </summary>
public Quaternion Rotation;

/// <summary>
/// Initializes a new instance of the <see cref="OrientedBoundingBox"/> struct with the specified minimum and maximum vectors and rotation.
/// </summary>
/// <param name="min">The minimum vector defining one corner of the bounding box.</param>
/// <param name="max">The maximum vector defining the opposite corner of the bounding box.</param>
/// <param name="rotation">The rotation of the bounding box as a quaternion.</param>
public OrientedBoundingBox(Vector3 min, Vector3 max, Quaternion rotation) {
this.Min = min;
this.Max = max;
this.Rotation = rotation;
}
}
28 changes: 14 additions & 14 deletions src/Bliss/CSharp/Interact/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,26 @@ public static void Init(Window window) {
_mouseButtonsDown = new List<MouseButton>();
_mouseButtonsReleased = new List<MouseButton>();

Window.Sdl2Window.MouseWheel += OnMouseWheel;
Window.Sdl2Window.MouseMove += OnMouseMove;
Window.Sdl2Window.MouseDown += OnMouseDown;
Window.Sdl2Window.MouseUp += OnMouseUp;
Window.MouseWheel += OnMouseWheel;
Window.MouseMove += OnMouseMove;
Window.MouseDown += OnMouseDown;
Window.MouseUp += OnMouseUp;

// Keyboard
_keyboardKeysPressed = new List<Key>();
_keyboardKeysDown = new List<Key>();
_keyboardKeysReleased = new List<Key>();

Window.Sdl2Window.KeyDown += OnKeyDown;
Window.Sdl2Window.KeyUp += OnKeyUp;
Window.KeyDown += OnKeyDown;
Window.KeyUp += OnKeyUp;

// Gamepads
_gamepads = new Dictionary<int, Gamepad>();

// Other
_filesDragDropped = new List<string>();

Window.Sdl2Window.DragDrop += OnDragDrop;
Window.DragDrop += OnDragDrop;
}

/// <summary>
Expand Down Expand Up @@ -492,14 +492,14 @@ public static void Destroy() {
// Mouse
SetMouseCursor(MouseCursor.Default); // To free the Cursor.

Window.Sdl2Window.MouseWheel -= OnMouseWheel;
Window.Sdl2Window.MouseMove -= OnMouseMove;
Window.Sdl2Window.MouseDown -= OnMouseDown;
Window.Sdl2Window.MouseUp -= OnMouseUp;
Window.MouseWheel -= OnMouseWheel;
Window.MouseMove -= OnMouseMove;
Window.MouseDown -= OnMouseDown;
Window.MouseUp -= OnMouseUp;

// Keyboard
Window.Sdl2Window.KeyDown -= OnKeyDown;
Window.Sdl2Window.KeyUp -= OnKeyUp;
Window.KeyDown -= OnKeyDown;
Window.KeyUp -= OnKeyUp;

// Gamepad
foreach (Gamepad gamepad in _gamepads.Values) {
Expand All @@ -509,7 +509,7 @@ public static void Destroy() {
_gamepads.Clear();

// Other
Window.Sdl2Window.DragDrop -= OnDragDrop;
Window.DragDrop -= OnDragDrop;

// Event
Sdl2Events.Unsubscribe(ProcessEvent);
Expand Down
Loading

0 comments on commit 2a5c8e8

Please sign in to comment.