StrawberryBF/src/Modules/Game.bf

141 lines
2.2 KiB
Brainfuck
Raw Normal View History

2020-05-05 11:50:38 +08:00
using SDL2;
using System;
using System.Collections;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Threading;
using ImGui;
2020-05-05 11:50:38 +08:00
2020-05-05 12:01:32 +08:00
namespace Strawberry
2020-05-05 11:50:38 +08:00
{
static
{
static public Game Game;
}
2021-02-19 15:06:00 +08:00
public abstract class Game : Module
2020-05-05 11:50:38 +08:00
{
public readonly List<VirtualInput> VirtualInputs;
2020-05-05 11:50:38 +08:00
private Scene scene;
private Scene switchToScene;
private bool updating;
public Batcher Batcher { get; private set; }
2021-02-18 14:11:52 +08:00
public bool DebugOverlay = false;
2020-05-07 12:54:41 +08:00
private bool* keyboardState;
private int32 updateCounter;
2021-02-19 15:06:00 +08:00
private float msCounter;
2020-05-05 11:50:38 +08:00
2021-02-21 13:17:17 +08:00
public this()
2020-05-05 11:50:38 +08:00
{
Game = this;
2021-02-21 13:17:17 +08:00
Batcher = PlatformLayer.CreateBatcher();
VirtualInputs = new List<VirtualInput>();
2020-06-15 11:48:01 +08:00
Assets.LoadAll();
2020-05-05 11:50:38 +08:00
}
public ~this()
{
if (scene != null)
delete scene;
if (switchToScene != scene && switchToScene != null)
delete switchToScene;
2020-05-08 04:48:03 +08:00
{
let list = scope List<VirtualInput>();
for (var i in VirtualInputs)
list.Add(i);
for (var i in list)
delete i;
delete VirtualInputs;
}
2020-05-05 11:50:38 +08:00
2020-06-15 11:48:01 +08:00
Assets.DisposeAll();
delete Batcher;
2020-05-05 11:50:38 +08:00
Game = null;
}
2021-02-19 15:06:00 +08:00
protected override void Update()
{
//Input
for (var i in VirtualInputs)
i.Update();
2020-05-05 11:50:38 +08:00
2021-02-18 14:11:52 +08:00
//Debug Overlay
if (Input.KeyPressed(.Grave))
DebugOverlay = !DebugOverlay;
2020-05-05 11:50:38 +08:00
//Switch scenes
if (switchToScene != scene)
{
if (scene != null)
delete scene;
scene = switchToScene;
scene.Started();
}
2020-05-08 10:10:54 +08:00
if (Time.Freeze > 0)
Time.Freeze -= Time.RawDelta;
else
{
if (scene != null)
scene.Update();
}
2020-05-05 11:50:38 +08:00
}
2021-02-19 15:06:00 +08:00
protected override void Render()
2020-05-05 11:50:38 +08:00
{
2021-02-22 04:48:53 +08:00
PlatformLayer.RenderBegin();
// game
PlatformLayer.GameRenderBegin();
Draw();
PlatformLayer.GameRenderEnd();
2021-02-18 14:11:52 +08:00
2021-02-22 04:48:53 +08:00
// debug overlay
2021-02-18 14:11:52 +08:00
if (DebugOverlay)
{
2021-02-21 11:13:31 +08:00
PlatformLayer.ImGuiRenderBegin();
2021-02-22 04:48:53 +08:00
ImGui.PushID("DebugOverlay");
2021-02-18 14:11:52 +08:00
DebugOverlay();
2021-02-22 04:48:53 +08:00
ImGui.PopID();
2021-02-21 11:13:31 +08:00
PlatformLayer.ImGuiRenderEnd();
2021-02-18 14:11:52 +08:00
}
2020-08-04 10:09:45 +08:00
PlatformLayer.RenderEnd();
}
public virtual void Draw()
{
Scene?.Draw();
Batcher.Draw();
2020-05-05 11:50:38 +08:00
}
public Scene Scene
{
get
{
return scene;
}
set
{
if (switchToScene != scene && switchToScene != null)
delete switchToScene;
switchToScene = value;
}
}
2021-02-18 14:11:52 +08:00
public virtual void DebugOverlay()
{
}
2020-05-05 11:50:38 +08:00
}
}