recrownedgtk/RecrownedGTK/Game/GameEngine.cs

45 lines
1.3 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 GameEngine(IState initialState) : base() {
this.state = initialState;
}
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);
}
}
}