2019-08-06 16:33:49 +02:00
|
|
|
using Coroutine;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
2019-08-07 22:25:33 +02:00
|
|
|
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;
|
2019-08-07 22:25:33 +02:00
|
|
|
public static InputHandler Input => instance.InputHandler;
|
2019-08-06 16:33:49 +02:00
|
|
|
|
|
|
|
public readonly GraphicsDeviceManager GraphicsDeviceManager;
|
2019-08-07 22:25:33 +02:00
|
|
|
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
|
|
|
|
2019-09-05 14:27:18 +02:00
|
|
|
public GenericCallback OnLoadContent;
|
|
|
|
public TimeCallback OnUpdate;
|
|
|
|
public TimeCallback OnDraw;
|
|
|
|
|
2019-08-06 16:33:49 +02:00
|
|
|
public MlemGame(int windowWidth = 1280, int windowHeight = 720, bool vsync = false, bool allowResizing = true, string contentDir = "Content") {
|
|
|
|
instance = this;
|
|
|
|
this.GraphicsDeviceManager = new GraphicsDeviceManager(this) {
|
|
|
|
PreferredBackBufferWidth = windowWidth,
|
|
|
|
PreferredBackBufferHeight = windowHeight,
|
|
|
|
SynchronizeWithVerticalRetrace = vsync
|
|
|
|
};
|
|
|
|
this.Content.RootDirectory = contentDir;
|
|
|
|
this.Window.AllowUserResizing = allowResizing;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadContent() {
|
|
|
|
this.SpriteBatch = new SpriteBatch(this.GraphicsDevice);
|
2020-02-01 21:16:10 +01:00
|
|
|
this.InputHandler = new InputHandler();
|
2019-12-05 17:52:25 +01:00
|
|
|
this.Components.Add(this.InputHandler);
|
2020-02-01 21:16:10 +01:00
|
|
|
this.UiSystem = new UiSystem(this.Window, this.GraphicsDevice, new UntexturedStyle(this.SpriteBatch), this.InputHandler);
|
2019-12-05 17:52:25 +01:00
|
|
|
this.Components.Add(this.UiSystem);
|
2019-09-05 14:27:18 +02:00
|
|
|
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);
|
2019-09-05 14:27:18 +02:00
|
|
|
this.OnUpdate?.Invoke(this, gameTime);
|
2020-01-30 00:55:02 +01:00
|
|
|
CoroutineHandler.Tick(gameTime.ElapsedGameTime.TotalSeconds);
|
2019-08-07 22:25:33 +02:00
|
|
|
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) {
|
2019-08-15 14:59:15 +02:00
|
|
|
this.UiSystem.DrawEarly(gameTime, this.SpriteBatch);
|
|
|
|
this.DoDraw(gameTime);
|
2019-08-09 23:43:50 +02:00
|
|
|
this.UiSystem.Draw(gameTime, this.SpriteBatch);
|
2019-09-05 14:27:18 +02:00
|
|
|
this.OnDraw?.Invoke(this, gameTime);
|
2019-08-07 22:25:33 +02:00
|
|
|
CoroutineHandler.RaiseEvent(CoroutineEvents.Draw);
|
2019-08-06 16:33:49 +02:00
|
|
|
}
|
|
|
|
|
2019-08-15 14:59:15 +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);
|
|
|
|
}
|
|
|
|
|
2019-09-05 14:27:18 +02:00
|
|
|
public delegate void GenericCallback(MlemGame game);
|
|
|
|
|
|
|
|
public delegate void TimeCallback(MlemGame game, GameTime time);
|
|
|
|
|
2019-08-06 16:33:49 +02:00
|
|
|
}
|
|
|
|
}
|