1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-01 05:10:50 +01:00
MLEM/MLEM.Startup/MlemGame.cs

77 lines
2.7 KiB
C#
Raw Normal View History

2019-08-06 16:33:49 +02:00
using Coroutine;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MLEM.Input;
2019-08-09 23:43:50 +02:00
using MLEM.Ui;
2019-08-10 21:37:10 +02:00
using MLEM.Ui.Style;
2019-08-06 16:33:49 +02:00
namespace MLEM.Startup {
public class MlemGame : Game {
private static MlemGame instance;
public static InputHandler Input => instance.InputHandler;
2019-08-06 16:33:49 +02:00
public readonly GraphicsDeviceManager GraphicsDeviceManager;
public SpriteBatch SpriteBatch { get; protected set; }
public InputHandler InputHandler { get; protected set; }
2019-08-09 23:43:50 +02:00
public UiSystem UiSystem { get; protected set; }
2019-08-06 16:33:49 +02:00
public GenericCallback OnLoadContent;
public TimeCallback OnUpdate;
public TimeCallback OnDraw;
public MlemGame(int windowWidth = 1280, int windowHeight = 720) {
2019-08-06 16:33:49 +02:00
instance = this;
2019-08-06 16:33:49 +02:00
this.GraphicsDeviceManager = new GraphicsDeviceManager(this) {
PreferredBackBufferWidth = windowWidth,
PreferredBackBufferHeight = windowHeight,
HardwareModeSwitch = false
2019-08-06 16:33:49 +02:00
};
this.Window.AllowUserResizing = true;
this.Content.RootDirectory = "Content";
2019-08-06 16:33:49 +02:00
}
protected override void LoadContent() {
this.SpriteBatch = new SpriteBatch(this.GraphicsDevice);
this.InputHandler = new InputHandler();
this.Components.Add(this.InputHandler);
this.UiSystem = new UiSystem(this.Window, this.GraphicsDevice, new UntexturedStyle(this.SpriteBatch), this.InputHandler);
this.Components.Add(this.UiSystem);
this.OnLoadContent?.Invoke(this);
2019-08-06 16:33:49 +02:00
}
2019-12-05 17:35:24 +01:00
protected sealed override void Update(GameTime gameTime) {
this.DoUpdate(gameTime);
this.OnUpdate?.Invoke(this, gameTime);
CoroutineHandler.Tick(gameTime.ElapsedGameTime.TotalSeconds);
CoroutineHandler.RaiseEvent(CoroutineEvents.Update);
}
2019-08-06 16:33:49 +02:00
2019-12-01 20:25:25 +01:00
protected sealed override void Draw(GameTime gameTime) {
this.UiSystem.DrawEarly(gameTime, this.SpriteBatch);
this.DoDraw(gameTime);
2019-08-09 23:43:50 +02:00
this.UiSystem.Draw(gameTime, this.SpriteBatch);
this.OnDraw?.Invoke(this, gameTime);
CoroutineHandler.RaiseEvent(CoroutineEvents.Draw);
2019-08-06 16:33:49 +02:00
}
protected virtual void DoDraw(GameTime gameTime) {
base.Draw(gameTime);
}
2019-12-05 17:35:24 +01:00
protected virtual void DoUpdate(GameTime gameTime) {
base.Update(gameTime);
}
2019-08-06 16:33:49 +02:00
public static T LoadContent<T>(string name) {
return instance.Content.Load<T>(name);
}
public delegate void GenericCallback(MlemGame game);
public delegate void TimeCallback(MlemGame game, GameTime time);
2019-08-06 16:33:49 +02:00
}
}