Basic structure for game engine and state system.

Untested.
This commit is contained in:
Harrison Deng 2020-04-17 22:12:57 -05:00
parent 326378bfd1
commit 01b900f368
3 changed files with 65 additions and 20 deletions

View File

@ -0,0 +1,49 @@
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System;
using System.ComponentModel;
using RecrownedGTK.Persistence;
using RecrownedGTK.AssetsSystem;
namespace RecrownedGTK.Game {
public sealed class GameEngine : GameWindow {
private IState state;
public readonly Preferences preferences;
public readonly AssetManager assets;
public GameEngine(IState initialState, Preferences preferences, AssetManager assets) : base() {
this.state = initialState;
this.assets = assets;
this.preferences = preferences;
}
protected override void OnUpdateFrame(FrameEventArgs e) {
state.Update(e.Time);
IState potentialState = null;
if ((potentialState = state.ChangeState()) != null) {
if (potentialState.Shown(this)) {
state.Hidden();
state = potentialState;
}
}
base.OnUpdateFrame(e);
}
protected override void OnRenderFrame(FrameEventArgs e) {
state.Render(e.Time);
SwapBuffers();
base.OnRenderFrame(e);
}
protected override void OnClosed(EventArgs e) {
if (state.CloseRequested()) {
((CancelEventArgs) e).Cancel = true;
}
base.OnClosed(e);
}
protected override void OnResize(EventArgs e) {
state.WindowSizeUpdate(Width, Height);
GL.Viewport(0, 0, Width, Height);
base.OnResize(e);
}
}
}

View File

@ -0,0 +1,16 @@
using OpenTK.Input;
namespace RecrownedGTK.Game {
public interface IState
{
bool Shown(GameEngine gameManager);
void Update(double time);
void Render(double time);
void Hidden();
void WindowSizeUpdate(int width, int height);
bool CloseRequested();
IState ChangeState();
}
}

View File

@ -1,20 +0,0 @@
using OpenTK;
namespace RecrownedGTK.Game {
public class GameManager : GameWindow {
public GameManager() : base() {
//TODO Implement interface that calls the users created game files.
}
protected override void OnUpdateFrame(FrameEventArgs e) {
base.OnUpdateFrame(e);
}
protected override void OnRenderFrame(FrameEventArgs e) {
base.OnRenderFrame(e);
}
protected override void OnClosed(System.EventArgs e) {
base.OnClosed(e);
}
}
}