Replaced SDLApp code in Game, no longer extends it

This commit is contained in:
Matt Thorson 2020-05-06 21:13:34 -07:00
parent 12c10909c9
commit 0a709b5788
8 changed files with 142 additions and 35 deletions

View File

@ -8,7 +8,7 @@ namespace Strawberry
private float value; private float value;
public Action OnComplete ~ delete _; public Action OnComplete ~ delete _;
public bool DestroyOnComplete; public bool RemoveOnComplete;
public this() public this()
: base(false, false) : base(false, false)
@ -21,7 +21,7 @@ namespace Strawberry
{ {
Value = value; Value = value;
OnComplete = onComplete; OnComplete = onComplete;
DestroyOnComplete = destroyOnComplete; RemoveOnComplete = destroyOnComplete;
} }
public float Value public float Value
@ -49,7 +49,7 @@ namespace Strawberry
Active = false; Active = false;
OnComplete?.Invoke(); OnComplete?.Invoke();
if (DestroyOnComplete) if (RemoveOnComplete)
RemoveSelf(); RemoveSelf();
} }
} }

View File

@ -1,6 +1,10 @@
using SDL2; using SDL2;
using System; using System;
using System.Collections; using System.Collections;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace Strawberry namespace Strawberry
{ {
@ -9,14 +13,25 @@ namespace Strawberry
static public Game Game; static public Game Game;
} }
public class Game : SDLApp public class Game
{ {
public List<VirtualInput> VirtualInputs; public readonly List<VirtualInput> VirtualInputs;
public readonly String Title;
public readonly int Width;
public readonly int Height;
private Scene scene; private Scene scene;
private Scene switchToScene; private Scene switchToScene;
private bool updating; private bool updating;
public SDL.Renderer* Renderer { get; private set; }
public bool* KeyboardState { get; private set; }
public bool* PreviousKeyboardState { get; private set; }
private SDL.Rect screenRect; private SDL.Rect screenRect;
private SDL.Window* window;
private SDL.Surface* screen;
private int32 updateCounter;
public this(String windowTitle, int32 width, int32 height) public this(String windowTitle, int32 width, int32 height)
: base() : base()
@ -24,9 +39,9 @@ namespace Strawberry
Game = this; Game = this;
VirtualInputs = new List<VirtualInput>(); VirtualInputs = new List<VirtualInput>();
mTitle.Set(windowTitle); Title = windowTitle;
mWidth = width; Width = width;
mHeight = height; Height = height;
screenRect = SDL.Rect(0, 0, width, height); screenRect = SDL.Rect(0, 0, width, height);
} }
@ -44,14 +59,82 @@ namespace Strawberry
Game = null; Game = null;
} }
public new virtual void Init() public void Run()
{ {
base.Init(); Stopwatch sw = scope .();
sw.Start();
int curPhysTickCount = 0;
while (true)
{
SDL.Event event;
if (SDL.PollEvent(out event) != 0 && event.type == .Quit)
return;
// Fixed 60 Hz update
double msPerTick = 1000 / 60.0;
int newPhysTickCount = (int)(sw.ElapsedMilliseconds / msPerTick);
int addTicks = newPhysTickCount - curPhysTickCount;
if (curPhysTickCount == 0)
{
// Initial render
Render();
}
else
{
PreviousKeyboardState = KeyboardState;
KeyboardState = SDL.GetKeyboardState(null);
addTicks = Math.Min(addTicks, 20); // Limit catchup
if (addTicks > 0)
{
for (int i < addTicks)
{
updateCounter++;
Update();
}
Render();
}
else
Thread.Sleep(1);
}
curPhysTickCount = newPhysTickCount;
}
} }
public override void Update() public virtual void Init()
{ {
base.Update(); String exePath = scope .();
Environment.GetExecutableFilePath(exePath);
String exeDir = scope .();
Path.GetDirectoryPath(exePath, exeDir);
Directory.SetCurrentDirectory(exeDir);
SDL.Init(.Video | .Events | .Audio);
SDL.EventState(.JoyAxisMotion, .Disable);
SDL.EventState(.JoyBallMotion, .Disable);
SDL.EventState(.JoyHatMotion, .Disable);
SDL.EventState(.JoyButtonDown, .Disable);
SDL.EventState(.JoyButtonUp, .Disable);
SDL.EventState(.JoyDeviceAdded, .Disable);
SDL.EventState(.JoyDeviceRemoved, .Disable);
window = SDL.CreateWindow(Title, .Centered, .Centered, screenRect.w, screenRect.h, .Shown);
Renderer = SDL.CreateRenderer(window, -1, .Accelerated);
screen = SDL.GetWindowSurface(window);
SDLImage.Init(.PNG | .JPG);
SDLMixer.OpenAudio(44100, SDLMixer.MIX_DEFAULT_FORMAT, 2, 4096);
SDLTTF.Init();
}
public virtual void Update()
{
//Input
for (var i in VirtualInputs)
i.Update();
//Switch scenes //Switch scenes
if (switchToScene != scene) if (switchToScene != scene)
@ -62,9 +145,6 @@ namespace Strawberry
scene.Started(); scene.Started();
} }
for (var i in VirtualInputs)
i.Update();
if (scene != null) if (scene != null)
scene.Update(); scene.Update();
@ -72,15 +152,18 @@ namespace Strawberry
Time.Elapsed += Time.Delta; Time.Elapsed += Time.Delta;
} }
public override void Draw() public void Render()
{ {
base.Draw(); SDL.SetRenderDrawColor(Renderer, 0, 0, 0, 255);
SDL.RenderClear(Renderer);
SDL2.SDL.SetRenderDrawColor(mRenderer, 0, 0, 0, 255);
SDL2.SDL.RenderFillRect(mRenderer, &screenRect);
if (Scene != null) if (Scene != null)
Scene.Draw(); Scene.Draw();
SDL.RenderPresent(Renderer);
}
public virtual void Draw()
{
} }
public Scene Scene public Scene Scene

24
src/Input/Input.bf Normal file
View File

@ -0,0 +1,24 @@
using SDL2;
namespace Strawberry
{
static public class Input
{
static public bool KeyCheck(SDL.Scancode key)
{
if (Game.KeyboardState == null)
return false;
return Game.KeyboardState[(int)key];
}
static public bool KeyPressed(SDL.Scancode key)
{
return KeyCheck(key) && (Game.PreviousKeyboardState == null || !Game.PreviousKeyboardState[(int)key]);
}
static public bool KeyReleased(SDL.Scancode key)
{
return (Game.PreviousKeyboardState != null && Game.PreviousKeyboardState[(int)key]) && !KeyCheck(key);
}
}
}

View File

@ -141,9 +141,9 @@ namespace Strawberry
public override void Update() public override void Update()
{ {
if (Game.IsKeyDown(PositiveKeycode)) if (Input.KeyCheck(PositiveKeycode))
{ {
if (Game.IsKeyDown(NegativeKeycode)) if (Input.KeyCheck(NegativeKeycode))
{ {
switch (OverlapBehavior) switch (OverlapBehavior)
{ {
@ -170,7 +170,7 @@ namespace Strawberry
value = 1; value = 1;
} }
} }
else if (Game.IsKeyDown(NegativeKeycode)) else if (Input.KeyCheck(NegativeKeycode))
{ {
turned = false; turned = false;
value = -1; value = -1;

View File

@ -129,7 +129,7 @@ namespace Strawberry
{ {
get get
{ {
return Game.IsKeyDown(Keycode); return Input.KeyCheck(Keycode);
} }
} }
} }

View File

@ -26,7 +26,7 @@ namespace Strawberry
for (var a in riders) for (var a in riders)
{ {
a.MoveExactX(amount); a.MoveExactX(amount);
a.Pushed += Point.UnitX * amount; a.MovedByGeometry += Point.UnitX * amount;
} }
} }
else else
@ -47,7 +47,7 @@ namespace Strawberry
{ {
let move = (Top + amount) - a.Bottom; let move = (Top + amount) - a.Bottom;
a.MoveExactY(move); a.MoveExactY(move);
a.Pushed += Point.UnitY * move; a.MovedByGeometry += Point.UnitY * move;
} }
} }
Y += amount; Y += amount;
@ -59,7 +59,7 @@ namespace Strawberry
for (var a in riders) for (var a in riders)
{ {
a.MoveExactY(amount); a.MoveExactY(amount);
a.Pushed += Point.UnitY * amount; a.MovedByGeometry += Point.UnitY * amount;
} }
Collidable = true; Collidable = true;

View File

@ -45,13 +45,13 @@ namespace Strawberry
else else
move = Left - a.Right; move = Left - a.Right;
a.MoveExactX(move, scope => a.Squish, this); a.MoveExactX(move, scope => a.Squish, this);
a.Pushed += Point.UnitX * move; a.MovedByGeometry += Point.UnitX * move;
} }
else if (riders.Contains(a)) else if (riders.Contains(a))
{ {
//Carry //Carry
a.MoveExactX(amount); a.MoveExactX(amount);
a.Pushed += Point.UnitX * amount; a.MovedByGeometry += Point.UnitX * amount;
} }
} }
@ -81,13 +81,13 @@ namespace Strawberry
else else
move = Top - a.Bottom; move = Top - a.Bottom;
a.MoveExactY(move, scope => a.Squish, this); a.MoveExactY(move, scope => a.Squish, this);
a.Pushed += Point.UnitY * move; a.MovedByGeometry += Point.UnitY * move;
} }
else if (riders.Contains(a)) else if (riders.Contains(a))
{ {
//Carry //Carry
a.MoveExactY(amount); a.MoveExactY(amount);
a.Pushed += Point.UnitY * amount; a.MovedByGeometry += Point.UnitY * amount;
} }
} }

View File

@ -4,8 +4,8 @@ namespace Strawberry
{ {
static public void Rect(int x, int y, int w, int h, SDL2.SDL.Color color) static public void Rect(int x, int y, int w, int h, SDL2.SDL.Color color)
{ {
SDL2.SDL.SetRenderDrawColor(Game.mRenderer, color.r, color.g, color.b, color.a); SDL2.SDL.SetRenderDrawColor(Game.Renderer, color.r, color.g, color.b, color.a);
SDL2.SDL.RenderFillRect(Game.mRenderer, &SDL2.SDL.Rect((int32)x, (int32)y, (int32)w, (int32)h)); SDL2.SDL.RenderFillRect(Game.Renderer, &SDL2.SDL.Rect((int32)x, (int32)y, (int32)w, (int32)h));
} }
static public void Rect(Rect rect, SDL2.SDL.Color color) static public void Rect(Rect rect, SDL2.SDL.Color color)