recrownedgtk/RecrownedGTK/Game/GameEngine.cs

49 lines
1.6 KiB
C#

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);
}
}
}