mirror of
https://github.com/MaddyThorson/StrawberryBF.git
synced 2025-01-19 05:28:27 +08:00
Replaced SDLApp code in Game, no longer extends it
This commit is contained in:
parent
12c10909c9
commit
0a709b5788
@ -8,7 +8,7 @@ namespace Strawberry
|
||||
private float value;
|
||||
|
||||
public Action OnComplete ~ delete _;
|
||||
public bool DestroyOnComplete;
|
||||
public bool RemoveOnComplete;
|
||||
|
||||
public this()
|
||||
: base(false, false)
|
||||
@ -21,7 +21,7 @@ namespace Strawberry
|
||||
{
|
||||
Value = value;
|
||||
OnComplete = onComplete;
|
||||
DestroyOnComplete = destroyOnComplete;
|
||||
RemoveOnComplete = destroyOnComplete;
|
||||
}
|
||||
|
||||
public float Value
|
||||
@ -49,7 +49,7 @@ namespace Strawberry
|
||||
Active = false;
|
||||
|
||||
OnComplete?.Invoke();
|
||||
if (DestroyOnComplete)
|
||||
if (RemoveOnComplete)
|
||||
RemoveSelf();
|
||||
}
|
||||
}
|
||||
|
121
src/Game.bf
121
src/Game.bf
@ -1,6 +1,10 @@
|
||||
using SDL2;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
|
||||
namespace Strawberry
|
||||
{
|
||||
@ -9,14 +13,25 @@ namespace Strawberry
|
||||
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 switchToScene;
|
||||
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.Window* window;
|
||||
private SDL.Surface* screen;
|
||||
private int32 updateCounter;
|
||||
|
||||
public this(String windowTitle, int32 width, int32 height)
|
||||
: base()
|
||||
@ -24,9 +39,9 @@ namespace Strawberry
|
||||
Game = this;
|
||||
VirtualInputs = new List<VirtualInput>();
|
||||
|
||||
mTitle.Set(windowTitle);
|
||||
mWidth = width;
|
||||
mHeight = height;
|
||||
Title = windowTitle;
|
||||
Width = width;
|
||||
Height = height;
|
||||
|
||||
screenRect = SDL.Rect(0, 0, width, height);
|
||||
}
|
||||
@ -44,14 +59,82 @@ namespace Strawberry
|
||||
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
|
||||
if (switchToScene != scene)
|
||||
@ -62,9 +145,6 @@ namespace Strawberry
|
||||
scene.Started();
|
||||
}
|
||||
|
||||
for (var i in VirtualInputs)
|
||||
i.Update();
|
||||
|
||||
if (scene != null)
|
||||
scene.Update();
|
||||
|
||||
@ -72,15 +152,18 @@ namespace Strawberry
|
||||
Time.Elapsed += Time.Delta;
|
||||
}
|
||||
|
||||
public override void Draw()
|
||||
public void Render()
|
||||
{
|
||||
base.Draw();
|
||||
|
||||
SDL2.SDL.SetRenderDrawColor(mRenderer, 0, 0, 0, 255);
|
||||
SDL2.SDL.RenderFillRect(mRenderer, &screenRect);
|
||||
|
||||
SDL.SetRenderDrawColor(Renderer, 0, 0, 0, 255);
|
||||
SDL.RenderClear(Renderer);
|
||||
if (Scene != null)
|
||||
Scene.Draw();
|
||||
SDL.RenderPresent(Renderer);
|
||||
}
|
||||
|
||||
public virtual void Draw()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Scene Scene
|
||||
|
24
src/Input/Input.bf
Normal file
24
src/Input/Input.bf
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -141,9 +141,9 @@ namespace Strawberry
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
if (Game.IsKeyDown(PositiveKeycode))
|
||||
if (Input.KeyCheck(PositiveKeycode))
|
||||
{
|
||||
if (Game.IsKeyDown(NegativeKeycode))
|
||||
if (Input.KeyCheck(NegativeKeycode))
|
||||
{
|
||||
switch (OverlapBehavior)
|
||||
{
|
||||
@ -170,7 +170,7 @@ namespace Strawberry
|
||||
value = 1;
|
||||
}
|
||||
}
|
||||
else if (Game.IsKeyDown(NegativeKeycode))
|
||||
else if (Input.KeyCheck(NegativeKeycode))
|
||||
{
|
||||
turned = false;
|
||||
value = -1;
|
||||
|
@ -129,7 +129,7 @@ namespace Strawberry
|
||||
{
|
||||
get
|
||||
{
|
||||
return Game.IsKeyDown(Keycode);
|
||||
return Input.KeyCheck(Keycode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ namespace Strawberry
|
||||
for (var a in riders)
|
||||
{
|
||||
a.MoveExactX(amount);
|
||||
a.Pushed += Point.UnitX * amount;
|
||||
a.MovedByGeometry += Point.UnitX * amount;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -47,7 +47,7 @@ namespace Strawberry
|
||||
{
|
||||
let move = (Top + amount) - a.Bottom;
|
||||
a.MoveExactY(move);
|
||||
a.Pushed += Point.UnitY * move;
|
||||
a.MovedByGeometry += Point.UnitY * move;
|
||||
}
|
||||
}
|
||||
Y += amount;
|
||||
@ -59,7 +59,7 @@ namespace Strawberry
|
||||
for (var a in riders)
|
||||
{
|
||||
a.MoveExactY(amount);
|
||||
a.Pushed += Point.UnitY * amount;
|
||||
a.MovedByGeometry += Point.UnitY * amount;
|
||||
}
|
||||
|
||||
Collidable = true;
|
||||
|
@ -45,13 +45,13 @@ namespace Strawberry
|
||||
else
|
||||
move = Left - a.Right;
|
||||
a.MoveExactX(move, scope => a.Squish, this);
|
||||
a.Pushed += Point.UnitX * move;
|
||||
a.MovedByGeometry += Point.UnitX * move;
|
||||
}
|
||||
else if (riders.Contains(a))
|
||||
{
|
||||
//Carry
|
||||
a.MoveExactX(amount);
|
||||
a.Pushed += Point.UnitX * amount;
|
||||
a.MovedByGeometry += Point.UnitX * amount;
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,13 +81,13 @@ namespace Strawberry
|
||||
else
|
||||
move = Top - a.Bottom;
|
||||
a.MoveExactY(move, scope => a.Squish, this);
|
||||
a.Pushed += Point.UnitY * move;
|
||||
a.MovedByGeometry += Point.UnitY * move;
|
||||
}
|
||||
else if (riders.Contains(a))
|
||||
{
|
||||
//Carry
|
||||
a.MoveExactY(amount);
|
||||
a.Pushed += Point.UnitY * amount;
|
||||
a.MovedByGeometry += Point.UnitY * amount;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,8 +4,8 @@ namespace Strawberry
|
||||
{
|
||||
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.RenderFillRect(Game.mRenderer, &SDL2.SDL.Rect((int32)x, (int32)y, (int32)w, (int32)h));
|
||||
SDL2.SDL.SetRenderDrawColor(Game.Renderer, color.r, color.g, color.b, color.a);
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user