Restructuring - added modules

This commit is contained in:
Maddy Thorson 2021-02-18 23:06:00 -08:00
parent 6c37bb7af5
commit bfd044544a
9 changed files with 182 additions and 81 deletions

View File

@ -10,7 +10,7 @@ namespace Strawberry
static public Dictionary<String, Sprite> 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

View File

@ -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;

View File

@ -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();

26
src/Modules/Editor.bf Normal file
View File

@ -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();
}
}

View File

@ -14,53 +14,29 @@ namespace Strawberry
static public Game Game;
}
public abstract class Game
public abstract class Game : Module
{
public readonly List<VirtualInput> 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<VirtualInput>();
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;
}
}
}

69
src/Modules/Module.bf Normal file
View File

@ -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;
}
}
}

View File

@ -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

View File

@ -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);
}

41
src/Static/Engine.bf Normal file
View File

@ -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();
}
}
}