2020-05-05 11:50:38 +08:00
|
|
|
using SDL2;
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
2020-05-07 12:13:34 +08:00
|
|
|
using System.Reflection;
|
|
|
|
using System.IO;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Threading;
|
2021-02-17 14:56:49 +08:00
|
|
|
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
|
|
|
{
|
2020-05-07 12:13:34 +08:00
|
|
|
public readonly List<VirtualInput> VirtualInputs;
|
|
|
|
|
2020-05-05 11:50:38 +08:00
|
|
|
private Scene scene;
|
|
|
|
private Scene switchToScene;
|
|
|
|
private bool updating;
|
2020-05-07 12:13:34 +08:00
|
|
|
|
2020-08-17 13:00:13 +08:00
|
|
|
public Batcher Batcher { get; private set; }
|
2021-02-18 14:11:52 +08:00
|
|
|
public bool DebugOverlay = false;
|
2020-05-07 12:13:34 +08:00
|
|
|
|
2020-05-07 12:54:41 +08:00
|
|
|
private bool* keyboardState;
|
2020-05-07 12:13:34 +08:00
|
|
|
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;
|
2020-05-22 12:24:04 +08:00
|
|
|
|
2021-02-21 13:17:17 +08:00
|
|
|
Batcher = PlatformLayer.CreateBatcher();
|
2020-05-27 11:30:16 +08:00
|
|
|
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();
|
2020-08-17 13:00:13 +08:00
|
|
|
delete Batcher;
|
|
|
|
|
2020-05-05 11:50:38 +08:00
|
|
|
Game = null;
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:06:00 +08:00
|
|
|
protected override void Update()
|
2020-05-07 12:13:34 +08:00
|
|
|
{
|
|
|
|
//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
|
2021-02-17 14:56:49 +08:00
|
|
|
PlatformLayer.GameRenderBegin();
|
2020-08-17 13:00:13 +08:00
|
|
|
Draw();
|
2021-02-17 14:56:49 +08:00
|
|
|
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();
|
2020-05-07 12:13:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Draw()
|
|
|
|
{
|
2020-08-17 13:00:13 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|