Skip to content

Commit

Permalink
Refactoring Application internals to use an event queue instead of ca…
Browse files Browse the repository at this point in the history
…llbacks for Input
  • Loading branch information
NoelFB committed Feb 10, 2024
1 parent f6e4e4d commit 4b12fbc
Show file tree
Hide file tree
Showing 7 changed files with 466 additions and 353 deletions.
101 changes: 57 additions & 44 deletions Framework/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public static class App
private static TimeSpan accumulator;
private static string title = string.Empty;
private static Platform.FosterFlags flags =
Platform.FosterFlags.RESIZABLE |
Platform.FosterFlags.VSYNC |
Platform.FosterFlags.MOUSE_VISIBLE;
Platform.FosterFlags.Resizable |
Platform.FosterFlags.Vsync |
Platform.FosterFlags.MouseVisible;

/// <summary>
/// Foster Version Number
Expand Down Expand Up @@ -183,11 +183,11 @@ public static Point2 SizeInPixels
/// </summary>
public static bool Fullscreen
{
get => flags.Has(Platform.FosterFlags.FULLSCREEN);
get => flags.Has(Platform.FosterFlags.Fullscreen);
set
{
if (value) flags |= Platform.FosterFlags.FULLSCREEN;
else flags &= ~Platform.FosterFlags.FULLSCREEN;
if (value) flags |= Platform.FosterFlags.Fullscreen;
else flags &= ~Platform.FosterFlags.Fullscreen;
Platform.FosterSetFlags(flags);
}
}
Expand All @@ -197,11 +197,11 @@ public static bool Fullscreen
/// </summary>
public static bool Resizable
{
get => flags.Has(Platform.FosterFlags.RESIZABLE);
get => flags.Has(Platform.FosterFlags.Resizable);
set
{
if (value) flags |= Platform.FosterFlags.RESIZABLE;
else flags &= ~Platform.FosterFlags.RESIZABLE;
if (value) flags |= Platform.FosterFlags.Resizable;
else flags &= ~Platform.FosterFlags.Resizable;
Platform.FosterSetFlags(flags);
}
}
Expand All @@ -211,11 +211,11 @@ public static bool Resizable
/// </summary>
public static bool VSync
{
get => flags.Has(Platform.FosterFlags.VSYNC);
get => flags.Has(Platform.FosterFlags.Vsync);
set
{
if (value) flags |= Platform.FosterFlags.VSYNC;
else flags &= ~Platform.FosterFlags.VSYNC;
if (value) flags |= Platform.FosterFlags.Vsync;
else flags &= ~Platform.FosterFlags.Vsync;
Platform.FosterSetFlags(flags);
}
}
Expand All @@ -225,11 +225,11 @@ public static bool VSync
/// </summary>
public static bool MouseVisible
{
get => flags.Has(Platform.FosterFlags.MOUSE_VISIBLE);
get => flags.Has(Platform.FosterFlags.MouseVisible);
set
{
if (value) flags |= Platform.FosterFlags.MOUSE_VISIBLE;
else flags &= ~Platform.FosterFlags.MOUSE_VISIBLE;
if (value) flags |= Platform.FosterFlags.MouseVisible;
else flags &= ~Platform.FosterFlags.MouseVisible;
Platform.FosterSetFlags(flags);
}
}
Expand Down Expand Up @@ -292,10 +292,10 @@ public static void Run(string applicationName, int width, int height, bool fulls
MainThreadID = Thread.CurrentThread.ManagedThreadId;

if (fullscreen)
App.flags |= Platform.FosterFlags.FULLSCREEN;
flags |= Platform.FosterFlags.Fullscreen;

App.title = applicationName;
App.Name = applicationName;
title = applicationName;
Name = applicationName;
var name = Platform.ToUTF8(applicationName);

Platform.FosterStartup(new()
Expand All @@ -305,23 +305,7 @@ public static void Run(string applicationName, int width, int height, bool fulls
width = width,
height = height,
renderer = renderer,
flags = App.flags,
onText = Input.OnText,
onKey = Input.OnKey,
onMouseButton = Input.OnMouseButton,
onMouseMove = Input.OnMouseMove,
onMouseWheel = Input.OnMouseWheel,
onControllerConnect = Input.OnControllerConnect,
onControllerDisconnect = Input.OnControllerDisconnect,
onControllerButton = Input.OnControllerButton,
onControllerAxis = Input.OnControllerAxis,
onExitRequest = () =>
{
if (OnExitRequested != null)
OnExitRequested();
else
Exit();
}
flags = flags,
});

if(Platform.FosterIsRunning() == 0)
Expand All @@ -339,7 +323,7 @@ public static void Run(string applicationName, int width, int height, bool fulls
timer.Restart();

// poll events once, so input has controller state before Startup
Platform.FosterPollEvents();
PollEvents();
Input.Step();

// register & startup all modules in order
Expand Down Expand Up @@ -377,6 +361,16 @@ public static void Run(string applicationName, int width, int height, bool fulls
Exiting = false;
}

/// <summary>
/// Notifies the Application to Exit.
/// The Application may finish the current frame before exiting.
/// </summary>
public static void Exit()
{
if (Running)
Exiting = true;
}

private static void Tick()
{
static void Update(TimeSpan delta)
Expand All @@ -386,7 +380,7 @@ static void Update(TimeSpan delta)

Graphics.Resources.DeleteRequested();
Input.Step();
Platform.FosterPollEvents();
PollEvents();
FramePool.NextFrame();

for (int i = 0; i < modules.Count; i ++)
Expand Down Expand Up @@ -442,13 +436,32 @@ static void Update(TimeSpan delta)
Platform.FosterEndFrame();
}

/// <summary>
/// Notifies the Application to Exit.
/// The Application may finish the current frame before exiting.
/// </summary>
public static void Exit()
private static void PollEvents()
{
if (Running)
Exiting = true;
while (Platform.FosterPollEvents(out var ev) != 0)
{
switch (ev.EventType)
{
case Platform.FosterEventType.None:
break;
case Platform.FosterEventType.ExitRequested:
if (OnExitRequested != null)
OnExitRequested();
else
Exit();
break;
case Platform.FosterEventType.KeyboardInput:
case Platform.FosterEventType.KeyboardKey:
case Platform.FosterEventType.MouseButton:
case Platform.FosterEventType.MouseMove:
case Platform.FosterEventType.MouseWheel:
case Platform.FosterEventType.ControllerConnect:
case Platform.FosterEventType.ControllerDisconnect:
case Platform.FosterEventType.ControllerButton:
case Platform.FosterEventType.ControllerAxis:
Input.OnFosterEvent(ev);
break;
}
}
}
}
Loading

0 comments on commit 4b12fbc

Please sign in to comment.