using System.IO;
using GLFW;
using OpenGLTutorial.Rendering.Display;
namespace OpenGLTutorial.GameLoop {
public abstract class Game {
///
/// Initializes the Game and starts the GameLoop
///
/// The initial width of the game window
/// The initial height of the game window
/// The title of the game window
/// Sync the game FPS to the Monitor FPS
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();
}
///
/// Gets called at the very start of the GameLoop
///
protected abstract void Initialize();
///
/// Gets called after the GLFW Window inititalizes
///
protected abstract void LoadContent();
///
/// Gets called every frame before Render
///
protected abstract void Update();
///
/// Gets called every frame after Update
///
protected abstract void Render();
///
/// Gets called right before the Game ends
///
protected abstract void Destroy();
///
/// Copys a folder to the Game output (DEBUG ONLY)
///
/// The folder to copy
protected void DEBUG_CopyFolderToOutput(string folder) {
#if DEBUG
var info = new DirectoryInfo(folder);
info.CopyTo(Directory.GetCurrentDirectory() + "/" + info.Name, true, true);
#endif
}
///
/// Copys a file to the Game output (DEBUG ONLY)
///
/// The file to copy
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);
}
}
}
}
}