Removed Subrepository
This commit is contained in:
111
C#/OpenGLTutorial/GameLoop/Game.cs
Normal file
111
C#/OpenGLTutorial/GameLoop/Game.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System.IO;
|
||||
using GLFW;
|
||||
using OpenGLTutorial.Rendering.Display;
|
||||
|
||||
namespace OpenGLTutorial.GameLoop {
|
||||
public abstract class Game {
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the Game and starts the GameLoop
|
||||
/// </summary>
|
||||
/// <param name="width">The initial width of the game window</param>
|
||||
/// <param name="height">The initial height of the game window</param>
|
||||
/// <param name="title">The title of the game window</param>
|
||||
/// <param name="vsync">Sync the game FPS to the Monitor FPS</param>
|
||||
public void Run(int width, int height, string title, bool vsync = false) {
|
||||
Initialize();
|
||||
|
||||
DisplayManager.CreateWindow(width, height, title, vsync);
|
||||
|
||||
LoadContent();
|
||||
|
||||
while (!DisplayManager.Window.IsClosing) {
|
||||
GameTime.DeltaTime = (float)Glfw.Time - GameTime.TotalElapsedSeconds;
|
||||
GameTime.TotalElapsedSeconds = (float)Glfw.Time;
|
||||
|
||||
Glfw.PollEvents();
|
||||
Input.Update();
|
||||
|
||||
Update();
|
||||
Render();
|
||||
|
||||
Input.MouseScroll = 0.0f;
|
||||
}
|
||||
|
||||
Destroy();
|
||||
DisplayManager.Window.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets called at the very start of the GameLoop
|
||||
/// </summary>
|
||||
protected abstract void Initialize();
|
||||
|
||||
/// <summary>
|
||||
/// Gets called after the GLFW Window inititalizes
|
||||
/// </summary>
|
||||
protected abstract void LoadContent();
|
||||
|
||||
/// <summary>
|
||||
/// Gets called every frame before Render
|
||||
/// </summary>
|
||||
protected abstract void Update();
|
||||
|
||||
/// <summary>
|
||||
/// Gets called every frame after Update
|
||||
/// </summary>
|
||||
protected abstract void Render();
|
||||
|
||||
/// <summary>
|
||||
/// Gets called right before the Game ends
|
||||
/// </summary>
|
||||
protected abstract void Destroy();
|
||||
|
||||
/// <summary>
|
||||
/// Copys a folder to the Game output (DEBUG ONLY)
|
||||
/// </summary>
|
||||
/// <param name="folder">The folder to copy</param>
|
||||
protected void DEBUG_CopyFolderToOutput(string folder) {
|
||||
#if DEBUG
|
||||
var info = new DirectoryInfo(folder);
|
||||
info.CopyTo(Directory.GetCurrentDirectory() + "/" + info.Name, true, true);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copys a file to the Game output (DEBUG ONLY)
|
||||
/// </summary>
|
||||
/// <param name="file">The file to copy</param>
|
||||
protected void DEBUG_CopyFileToOutput(string file) {
|
||||
#if DEBUG
|
||||
var info = new FileInfo(file);
|
||||
info.CopyTo(Directory.GetCurrentDirectory() + "/" + info.Name, true);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
public static class DirectoryExtensions {
|
||||
public static void CopyTo(this DirectoryInfo dir, string destinationDir, bool overwrite, bool recursive) {
|
||||
if (overwrite && Directory.Exists(destinationDir))
|
||||
Directory.Delete(destinationDir, true);
|
||||
|
||||
if (!dir.Exists)
|
||||
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
|
||||
|
||||
DirectoryInfo[] dirs = dir.GetDirectories();
|
||||
Directory.CreateDirectory(destinationDir);
|
||||
|
||||
foreach (FileInfo file in dir.GetFiles()) {
|
||||
string targetFilePath = Path.Combine(destinationDir, file.Name);
|
||||
file.CopyTo(targetFilePath);
|
||||
}
|
||||
|
||||
if (recursive) {
|
||||
foreach (DirectoryInfo subDir in dirs) {
|
||||
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
|
||||
CopyTo(subDir, newDestinationDir, true, overwrite);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
15
C#/OpenGLTutorial/GameLoop/GameTime.cs
Normal file
15
C#/OpenGLTutorial/GameLoop/GameTime.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace OpenGLTutorial.GameLoop {
|
||||
public static class GameTime {
|
||||
|
||||
/// <summary>
|
||||
/// The time in seconds that passed between frames
|
||||
/// </summary>
|
||||
public static float DeltaTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The total elapsed time sinse the game started
|
||||
/// </summary>
|
||||
public static float TotalElapsedSeconds { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
173
C#/OpenGLTutorial/GameLoop/Input.cs
Normal file
173
C#/OpenGLTutorial/GameLoop/Input.cs
Normal file
@@ -0,0 +1,173 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using GLFW;
|
||||
|
||||
namespace OpenGLTutorial.GameLoop {
|
||||
public class Input {
|
||||
|
||||
/// <summary>
|
||||
/// Get the state of a key
|
||||
/// Usage: Input.Keys[Keys.W]
|
||||
/// </summary>
|
||||
public static Dict<Keys, bool> Keys { get; } = new (false);
|
||||
|
||||
/// <summary>
|
||||
/// Get the state of a mouse button
|
||||
/// Usage: Input.MouseButtons[MouseButton.Button1]
|
||||
/// </summary>
|
||||
public static Dict<MouseButton, bool> MouseButtons { get; } = new (false);
|
||||
|
||||
/// <summary>
|
||||
/// The screen position of the Mouse
|
||||
/// </summary>
|
||||
public static Point MousePosition { get; set; } = new (0, 0);
|
||||
|
||||
/// <summary>
|
||||
/// The relative scroll offset from last frame
|
||||
/// </summary>
|
||||
public static float MouseScroll { get; set; } = 0.0f;
|
||||
|
||||
/// <summary>
|
||||
/// Get the state of a joystick button
|
||||
/// Usage: Input.JoystickButtons[Joystick.Joystick1][JoystickButton.A]
|
||||
/// </summary>
|
||||
public static Dict<Joystick, Dict<JoystickButton, bool>> JoystickButtons { get; } = new(new Dict<JoystickButton, bool>(false));
|
||||
|
||||
/// <summary>
|
||||
/// Get the state of a joystick axis
|
||||
/// Usage: JoystickAxis[Joystick.Joystick1][JoystickAxis.LeftJoystickX]
|
||||
/// </summary>
|
||||
public static Dict<Joystick, Dict<JoystickAxis, float>> JoystickAxis { get; } = new(new Dict<JoystickAxis, float>(0.0f));
|
||||
|
||||
/// <summary>
|
||||
/// Get the state of the given axis
|
||||
/// </summary>
|
||||
/// <param name="axis">The axis</param>
|
||||
/// <param name="joystick">(OPTIONAL) The joystick to use</param>
|
||||
/// <returns>A value between -1 and 1</returns>
|
||||
/// <exception cref="GLFW.Exception">Throws an exception if the given axis does not exist</exception>
|
||||
public static float GetAxis(Axis axis, Joystick joystick = Joystick.Joystick1) {
|
||||
switch (axis) {
|
||||
case Axis.Horizontal:
|
||||
float x = JoystickAxis[joystick][GameLoop.JoystickAxis.LeftJoystickX];
|
||||
if (Keys[GLFW.Keys.A]) x += -1.0f;
|
||||
if (Keys[GLFW.Keys.D]) x += 1.0f;
|
||||
return Math.Clamp(x, -1, 1);
|
||||
|
||||
case Axis.Vertical:
|
||||
float y = JoystickAxis[joystick][GameLoop.JoystickAxis.LeftJoystickY];
|
||||
if (Keys[GLFW.Keys.S]) y += -1.0f;
|
||||
if (Keys[GLFW.Keys.W]) y += 1.0f;
|
||||
return Math.Clamp(y, -1, 1);
|
||||
|
||||
case Axis.Junp:
|
||||
float jump = JoystickButtons[joystick][JoystickButton.A] ? 1.0f : 0.0f;
|
||||
if (Keys[GLFW.Keys.Space]) jump = 1.0f;
|
||||
return jump;
|
||||
|
||||
default:
|
||||
throw new System.Exception("Unexpected Token: '" + axis + "'. That Axis does not exist");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SYSTEM INTERNAL
|
||||
/// </summary>
|
||||
public static void Update() {
|
||||
UpdateJoystick(Joystick.Joystick1);
|
||||
UpdateJoystick(Joystick.Joystick2);
|
||||
UpdateJoystick(Joystick.Joystick3);
|
||||
UpdateJoystick(Joystick.Joystick4);
|
||||
UpdateJoystick(Joystick.Joystick5);
|
||||
UpdateJoystick(Joystick.Joystick6);
|
||||
UpdateJoystick(Joystick.Joystick7);
|
||||
UpdateJoystick(Joystick.Joystick8);
|
||||
UpdateJoystick(Joystick.Joystick9);
|
||||
UpdateJoystick(Joystick.Joystick10);
|
||||
UpdateJoystick(Joystick.Joystick11);
|
||||
UpdateJoystick(Joystick.Joystick12);
|
||||
UpdateJoystick(Joystick.Joystick13);
|
||||
UpdateJoystick(Joystick.Joystick14);
|
||||
UpdateJoystick(Joystick.Joystick15);
|
||||
UpdateJoystick(Joystick.Joystick16);
|
||||
}
|
||||
|
||||
private static void UpdateJoystick(Joystick joystick) {
|
||||
if (!Glfw.JoystickPresent(joystick)) return;
|
||||
float[] axis = Glfw.GetJoystickAxes(joystick);
|
||||
JoystickAxis[joystick][GameLoop.JoystickAxis.LeftJoystickX] = ClampAxis(axis[0]);
|
||||
JoystickAxis[joystick][GameLoop.JoystickAxis.LeftJoystickY] = -ClampAxis(axis[1]);
|
||||
JoystickAxis[joystick][GameLoop.JoystickAxis.RightJoystickX] = ClampAxis(axis[2]);
|
||||
JoystickAxis[joystick][GameLoop.JoystickAxis.RightJoystickY] = -ClampAxis(axis[3]);
|
||||
JoystickAxis[joystick][GameLoop.JoystickAxis.LeftTrigger] = ClampAxis(axis[4]);
|
||||
JoystickAxis[joystick][GameLoop.JoystickAxis.RightTrigger] = ClampAxis(axis[5]);
|
||||
|
||||
InputState[] buttons = Glfw.GetJoystickButtons(joystick);
|
||||
JoystickButtons[joystick][JoystickButton.A] = buttons[0] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.B] = buttons[1] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.X] = buttons[2] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.Y] = buttons[3] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.Lb] = buttons[4] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.Rb] = buttons[5] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.Back] = buttons[6] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.Start] = buttons[7] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.Left] = buttons[8] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.Right] = buttons[9] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.DpadUp] = buttons[10] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.DpadRight] = buttons[11] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.DpadDown] = buttons[12] != InputState.Release;
|
||||
JoystickButtons[joystick][JoystickButton.DpadLeft] = buttons[13] != InputState.Release;
|
||||
}
|
||||
|
||||
private static float ClampAxis(float value) {
|
||||
if (value < 0.15f && value > -0.15f)
|
||||
value = 0;
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public class Dict<TKey, TValue> : Dictionary<TKey, TValue> {
|
||||
public TValue DefaultValue { get; set; }
|
||||
|
||||
public Dict(TValue defaultValue) => DefaultValue = defaultValue;
|
||||
|
||||
public new TValue this[TKey key] {
|
||||
get => TryGetValue(key, out TValue t) ? t : DefaultValue;
|
||||
set => base[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public enum JoystickButton {
|
||||
A,
|
||||
B,
|
||||
X,
|
||||
Y,
|
||||
Start,
|
||||
Back,
|
||||
Lb,
|
||||
Rb,
|
||||
Left,
|
||||
Right,
|
||||
DpadUp,
|
||||
DpadDown,
|
||||
DpadRight,
|
||||
DpadLeft
|
||||
}
|
||||
|
||||
public enum JoystickAxis {
|
||||
LeftJoystickX,
|
||||
LeftJoystickY,
|
||||
RightJoystickX,
|
||||
RightJoystickY,
|
||||
LeftTrigger,
|
||||
RightTrigger
|
||||
}
|
||||
|
||||
public enum Axis {
|
||||
Horizontal,
|
||||
Vertical,
|
||||
Junp
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user