From bfd044544a5f8749a550bea75dd1f81a25a467e1 Mon Sep 17 00:00:00 2001 From: Maddy Thorson Date: Thu, 18 Feb 2021 23:06:00 -0800 Subject: [PATCH] Restructuring - added modules --- src/Assets/Assets.bf | 2 +- src/Input/Input.bf | 7 +- src/JSON/JSON.bf | 10 ++- src/Modules/Editor.bf | 26 +++++++ src/{Core => Modules}/Game.bf | 79 +++------------------ src/Modules/Module.bf | 69 ++++++++++++++++++ src/PlatformLayer/PlatformLayer.bf | 16 ++++- src/PlatformLayer/SDL2/SDL2PlatformLayer.bf | 13 ++-- src/Static/Engine.bf | 41 +++++++++++ 9 files changed, 182 insertions(+), 81 deletions(-) create mode 100644 src/Modules/Editor.bf rename src/{Core => Modules}/Game.bf (57%) create mode 100644 src/Modules/Module.bf create mode 100644 src/Static/Engine.bf diff --git a/src/Assets/Assets.bf b/src/Assets/Assets.bf index 1285743..852ac54 100644 --- a/src/Assets/Assets.bf +++ b/src/Assets/Assets.bf @@ -10,7 +10,7 @@ namespace Strawberry static public Dictionary Sprites { get; private set; } #if DEBUG - static public readonly String Root = "../../../src/assets/"; + static public readonly String Root = "src/assets/"; #else static public readonly String Root = "assets/"; #endif diff --git a/src/Input/Input.bf b/src/Input/Input.bf index fee1703..ef70bc5 100644 --- a/src/Input/Input.bf +++ b/src/Input/Input.bf @@ -5,17 +5,18 @@ namespace Strawberry { static public class Input { - + public const int GamepadLimit = 4; + static private bool[] previousKeyboard; static private float[] lastKeypressTimes; - static private void Init() + static private void Startup() { previousKeyboard = new bool[Keys.Count]; lastKeypressTimes = new float[Keys.Count]; } - static private void Dispose() + static private void Shutdown() { delete previousKeyboard; delete lastKeypressTimes; diff --git a/src/JSON/JSON.bf b/src/JSON/JSON.bf index 628be1a..4864583 100644 --- a/src/JSON/JSON.bf +++ b/src/JSON/JSON.bf @@ -396,7 +396,15 @@ namespace Strawberry static public JSON FromFile(String filePath) { FileStream stream = scope FileStream(); - stream.Open(filePath, .Read); + + let str = scope String(); + Directory.GetCurrentDirectory(str); + Calc.Log(str); + + + + if (stream.Open(filePath, .Read) case .Err) + Runtime.FatalError("Unable to open FileStream"); let json = FromStream(stream); stream.Close(); diff --git a/src/Modules/Editor.bf b/src/Modules/Editor.bf new file mode 100644 index 0000000..f97065b --- /dev/null +++ b/src/Modules/Editor.bf @@ -0,0 +1,26 @@ +namespace Strawberry +{ + public abstract class Editor : Module + { + public this(PlatformLayer platformLayer) + : base(platformLayer) + { + + } + + protected override void Update() + { + + } + + protected override void Render() + { + PlatformLayer.EditorRenderBegin(); + UI(); + PlatformLayer.EditorRenderEnd(); + PlatformLayer.RenderEnd(); + } + + protected abstract void UI(); + } +} diff --git a/src/Core/Game.bf b/src/Modules/Game.bf similarity index 57% rename from src/Core/Game.bf rename to src/Modules/Game.bf index 97b9029..5ae4330 100644 --- a/src/Core/Game.bf +++ b/src/Modules/Game.bf @@ -14,53 +14,29 @@ namespace Strawberry static public Game Game; } - public abstract class Game + public abstract class Game : Module { public readonly List VirtualInputs; - public readonly String Title; - public readonly int Width; - public readonly int Height; - public readonly int WindowScale; - public readonly int GamepadLimit; private Scene scene; private Scene switchToScene; private bool updating; - public PlatformLayer PlatformLayer { get; private set; } public Batcher Batcher { get; private set; } public Color ClearColor = .Black; public bool DebugOverlay = false; private bool* keyboardState; private int32 updateCounter; + private float msCounter; - public this(PlatformLayer platformLayer, String windowTitle, int32 width, int32 height, int32 windowScale, int gamepadLimit) - : base() + public this(PlatformLayer platformLayer) + : base(platformLayer) { Game = this; - PlatformLayer = platformLayer; - Title = windowTitle; - Width = width; - Height = height; - WindowScale = windowScale; - GamepadLimit = gamepadLimit; - - String exePath = scope .(); - Environment.GetExecutableFilePath(exePath); - String exeDir = scope .(); - Path.GetDirectoryPath(exePath, exeDir); - Directory.SetCurrentDirectory(exeDir); - - platformLayer.UpdateScreenMatrix(); - platformLayer.Init(); Batcher = platformLayer.CreateBatcher(); - VirtualInputs = new List(); - Input.[Friend]Init(); - - Tracker.[Friend]BuildAssignmentLists(); Assets.LoadAll(); } @@ -82,50 +58,12 @@ namespace Strawberry } Assets.DisposeAll(); - Input.[Friend]Dispose(); - delete Batcher; Game = null; } - public void Run() - { - float msCounter = 0; - uint32 prevTick = 0; - while (true) - { - if (PlatformLayer.Closed()) - return; - - uint32 tick = PlatformLayer.Ticks; - msCounter += (tick - prevTick); - - if (Time.FixedTimestep) - { - Time.RawDelta = Time.TargetDeltaTime; - while (msCounter >= Time.TargetMilliseconds) - { - msCounter -= Time.TargetMilliseconds; - PlatformLayer.UpdateInput(); - Update(); - Input.AfterUpdate(); - } - } - else - { - Time.RawDelta = (tick - prevTick) / 1000f; - PlatformLayer.UpdateInput(); - Update(); - Input.AfterUpdate(); - } - - Render(); - prevTick = tick; - } - } - - public virtual void Update() + protected override void Update() { //Input for (var i in VirtualInputs) @@ -159,7 +97,7 @@ namespace Strawberry } } - private void Render() + protected override void Render() { PlatformLayer.GameRenderBegin(); Draw(); @@ -200,5 +138,10 @@ namespace Strawberry { } + + public virtual Editor CreateEditor() + { + return null; + } } } diff --git a/src/Modules/Module.bf b/src/Modules/Module.bf new file mode 100644 index 0000000..b762ba8 --- /dev/null +++ b/src/Modules/Module.bf @@ -0,0 +1,69 @@ +namespace Strawberry +{ + public abstract class Module + { + public enum RunResults { Quit, Swap } + + public PlatformLayer PlatformLayer { get; private set; } + + private float msCounter; + private uint32 prevTicks; + + public this(PlatformLayer platformLayer) + { + PlatformLayer = platformLayer; + } + + public Module Run() + { + while (true) + { + let tick = PlatformLayer.Ticks; + msCounter += tick - prevTicks; + + // update + if (Time.FixedTimestep) + { + Time.RawDelta = Time.TargetDeltaTime; + while (msCounter >= Time.TargetMilliseconds) + { + msCounter -= Time.TargetMilliseconds; + PlatformLayer.UpdateInput(); + Update(); + Input.AfterUpdate(); + } + } + else + { + Time.RawDelta = (tick - prevTicks) / 1000f; + PlatformLayer.UpdateInput(); + Update(); + Input.AfterUpdate(); + } + + // render + Render(); + + // exit or swap to another module + if (PlatformLayer.Closed()) + return null; + else if (Input.KeyPressed(.F1)) + { + let swapTo = CreateSwapModule(); + if (swapTo != null) + return swapTo; + } + + prevTicks = tick; + } + } + + protected abstract void Update(); + protected abstract void Render(); + + public virtual Module CreateSwapModule() + { + return null; + } + } +} diff --git a/src/PlatformLayer/PlatformLayer.bf b/src/PlatformLayer/PlatformLayer.bf index 4e6011b..9379a00 100644 --- a/src/PlatformLayer/PlatformLayer.bf +++ b/src/PlatformLayer/PlatformLayer.bf @@ -3,16 +3,28 @@ namespace Strawberry { public abstract class PlatformLayer { + public readonly String Title; + public readonly int ScreenWidth; + public readonly int ScreenHeight; + public readonly int WindowScale; + public Mat4x4 ScreenMatrix { get; private set; } + public this(String title, int screenWidth, int screenHeight, int windowScale) + { + Title = title; + ScreenWidth = screenWidth; + ScreenHeight = screenHeight; + WindowScale = windowScale; + } + public void UpdateScreenMatrix() { - ScreenMatrix = Mat4x4.CreateOrthographic(Game.Width, Game.Height * 0.5f, 0, 1) + ScreenMatrix = Mat4x4.CreateOrthographic(ScreenWidth, ScreenHeight * 0.5f, 0, 1) * Mat4x4.CreateScale(.(1, -1, 1)) * Mat4x4.CreateTranslation(.(-1, 1, 0)); } - public abstract void Init(); public abstract bool Closed(); // Returns whether the game window has been closed //Rendering diff --git a/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf b/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf index c7c3438..3b8bee7 100644 --- a/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf +++ b/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf @@ -23,15 +23,16 @@ namespace Strawberry.SDL2 private SDL.SDL_GLContext glContext; private uint glProgram; - public override void Init() + public this(String title, int screenWidth, int screenHeight, int windowScale) + : base(title, screenWidth, screenHeight, windowScale) { SDL.Version version; SDL.GetVersion(out version); - Calc.Log("Init SDL Version {0}.{1}.{2}", version.major, version.minor, version.patch); + Calc.Log("SDL Version {0}.{1}.{2}", version.major, version.minor, version.patch); { SDL.InitFlag init = .Video | .Events | .Audio | .Timer; - if (Game.GamepadLimit > 0) + if (Input.GamepadLimit > 0) init |= .GameController; if (SDL.Init(init) != 0) @@ -48,8 +49,8 @@ namespace Strawberry.SDL2 //Graphics { - screenRect = SDL.Rect(0, 0, (int32)(Game.Width * Game.WindowScale), (int32)(Game.Height * Game.WindowScale)); - window = SDL.CreateWindow(Game.Title, .Centered, .Centered, screenRect.w, screenRect.h, .Shown | .OpenGL); + screenRect = SDL.Rect(0, 0, (int32)(ScreenWidth * WindowScale), (int32)(ScreenHeight * WindowScale)); + window = SDL.CreateWindow(Title, .Centered, .Centered, screenRect.w, screenRect.h, .Shown | .OpenGL); renderer = SDL.CreateRenderer(window, -1, .Accelerated); screen = SDL.GetWindowSurface(window); SDLImage.Init(.PNG | .JPG); @@ -125,7 +126,7 @@ namespace Strawberry.SDL2 //Input { keyboard = SDL.GetKeyboardState(null); - gamepads = new SDL.SDL_GameController*[Game.GamepadLimit]; + gamepads = new SDL.SDL_GameController*[Input.GamepadLimit]; for (let i < gamepads.Count) gamepads[i] = SDL.GameControllerOpen((int32)i); } diff --git a/src/Static/Engine.bf b/src/Static/Engine.bf new file mode 100644 index 0000000..699f688 --- /dev/null +++ b/src/Static/Engine.bf @@ -0,0 +1,41 @@ +using System; +namespace Strawberry +{ + static public class Engine + { + static public void Run(Module module) + { + Startup(); + + Module current = module; + while (true) + { + let newModule = current.Run(); + + if (newModule != null) + { + delete current; + current = newModule; + } + else + { + delete current; + break; + } + } + + Shutdown(); + } + + static private void Startup() + { + Input.[Friend]Startup(); + Tracker.[Friend]BuildAssignmentLists(); + } + + static private void Shutdown() + { + Input.[Friend]Shutdown(); + } + } +}