testing building some core systems

- serialization
- chunks
- parralelfor test
This commit is contained in:
max
2026-04-24 19:21:03 +02:00
parent 5eaf3547dc
commit fec2cd8d24
15 changed files with 999 additions and 98 deletions
+31 -59
View File
@@ -16,7 +16,7 @@ public static class Engine
public static bool VSync { get; set; }
public static GraphicsDevice GraphicsDevice { get; private set; }
public static AudioDevice AudioDevice { get; private set; }
//public static AudioDevice AudioDevice { get; private set; }
public static Window MainWindow { get; private set; }
public static TimeSpan Timestep { get; private set; }
@@ -44,37 +44,32 @@ public static class Engine
private const string WindowTitle = "Nerfed";
//..
public static void Run(string[] args)
{
public static void Run(string[] args) {
Timestep = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / TargetTimestep);
gameTimer = Stopwatch.StartNew();
SetFrameLimiter(new FrameLimiterSettings(FrameLimiterMode.Capped, MaxFps));
for (int i = 0; i < previousSleepTimes.Length; i += 1)
{
for(int i = 0; i < previousSleepTimes.Length; i += 1) {
previousSleepTimes[i] = TimeSpan.FromMilliseconds(1);
}
if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_TIMER | SDL.SDL_INIT_GAMECONTROLLER) < 0)
{
if(SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_TIMER | SDL.SDL_INIT_GAMECONTROLLER) < 0) {
throw new Exception("Failed to init SDL");
}
GraphicsDevice = new GraphicsDevice(BackendFlags.All);
GraphicsDevice.LoadDefaultPipelines();
MainWindow = new Window(GraphicsDevice, new WindowCreateInfo(WindowTitle, WindowWidth, WindowHeight, ScreenMode.Windowed));
if (!GraphicsDevice.ClaimWindow(MainWindow, SwapchainComposition.SDR, VSync ? PresentMode.VSync : PresentMode.Mailbox))
{
if(!GraphicsDevice.ClaimWindow(MainWindow, SwapchainComposition.SDR, VSync ? PresentMode.VSync : PresentMode.Mailbox)) {
throw new Exception("Failed to claim window");
}
AudioDevice = new AudioDevice();
//AudioDevice = new AudioDevice();
OnInitialize?.Invoke();
while (!quit)
{
while(!quit) {
Tick();
}
@@ -83,40 +78,33 @@ public static class Engine
GraphicsDevice.UnclaimWindow(MainWindow);
MainWindow.Dispose();
GraphicsDevice.Dispose();
AudioDevice.Dispose();
//AudioDevice.Dispose();
SDL.SDL_Quit();
}
/// <summary>
/// Updates the frame limiter settings.
/// </summary>
public static void SetFrameLimiter(FrameLimiterSettings settings)
{
public static void SetFrameLimiter(FrameLimiterSettings settings) {
framerateCapped = settings.Mode == FrameLimiterMode.Capped;
if (framerateCapped)
{
if(framerateCapped) {
framerateCapTimeSpan = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / settings.Cap);
}
else
{
} else {
framerateCapTimeSpan = TimeSpan.Zero;
}
}
public static void Quit()
{
public static void Quit() {
quit = true;
}
private static void Tick()
{
private static void Tick() {
Profiler.BeginFrame();
AdvanceElapsedTime();
if (framerateCapped)
{
if(framerateCapped) {
Profiler.BeginSample("framerateCapped");
/* We want to wait until the framerate cap,
@@ -124,8 +112,7 @@ public static class Engine
* seeing how long we actually slept for lets us estimate the worst case
* sleep precision so we don't oversleep the next frame.
*/
while (accumulatedDrawTime + worstCaseSleepPrecision < framerateCapTimeSpan)
{
while(accumulatedDrawTime + worstCaseSleepPrecision < framerateCapTimeSpan) {
Thread.Sleep(1);
TimeSpan timeAdvancedSinceSleeping = AdvanceElapsedTime();
UpdateEstimatedSleepPrecision(timeAdvancedSinceSleeping);
@@ -136,8 +123,7 @@ public static class Engine
* SpinWait(1) works by pausing the thread for very short intervals, so it is
* an efficient and time-accurate way to wait out the rest of the time.
*/
while (accumulatedDrawTime < framerateCapTimeSpan)
{
while(accumulatedDrawTime < framerateCapTimeSpan) {
Thread.SpinWait(1);
AdvanceElapsedTime();
}
@@ -146,15 +132,12 @@ public static class Engine
}
// Do not let any step take longer than our maximum.
if (accumulatedUpdateTime > MaxDeltaTime)
{
if(accumulatedUpdateTime > MaxDeltaTime) {
accumulatedUpdateTime = MaxDeltaTime;
}
if (!quit)
{
while (accumulatedUpdateTime >= Timestep)
{
if(!quit) {
while(accumulatedUpdateTime >= Timestep) {
Profiler.BeginSample("Update");
Keyboard.Update();
Mouse.Update();
@@ -167,7 +150,7 @@ public static class Engine
OnUpdate?.Invoke();
Profiler.EndSample();
AudioDevice.WakeThread();
//AudioDevice.WakeThread();
accumulatedUpdateTime -= Timestep;
Profiler.EndSample();
}
@@ -185,8 +168,7 @@ public static class Engine
Profiler.EndFrame();
}
private static TimeSpan AdvanceElapsedTime()
{
private static TimeSpan AdvanceElapsedTime() {
long currentTicks = gameTimer.Elapsed.Ticks;
TimeSpan timeAdvanced = TimeSpan.FromTicks(currentTicks - previousTicks);
accumulatedUpdateTime += timeAdvanced;
@@ -195,12 +177,9 @@ public static class Engine
return timeAdvanced;
}
private static void ProcessSDLEvents()
{
while (SDL.SDL_PollEvent(out SDL.SDL_Event ev) == 1)
{
switch (ev.type)
{
private static void ProcessSDLEvents() {
while(SDL.SDL_PollEvent(out SDL.SDL_Event ev) == 1) {
switch(ev.type) {
case SDL.SDL_EventType.SDL_QUIT:
Quit();
break;
@@ -236,16 +215,14 @@ public static class Engine
/* To calculate the sleep precision of the OS, we take the worst case
* time spent sleeping over the results of previous requests to sleep 1ms.
*/
private static void UpdateEstimatedSleepPrecision(TimeSpan timeSpentSleeping)
{
private static void UpdateEstimatedSleepPrecision(TimeSpan timeSpentSleeping) {
/* It is unlikely that the scheduler will actually be more imprecise than
* 4ms and we don't want to get wrecked by a single long sleep so we cap this
* value at 4ms for sanity.
*/
TimeSpan upperTimeBound = TimeSpan.FromMilliseconds(4);
if (timeSpentSleeping > upperTimeBound)
{
if(timeSpentSleeping > upperTimeBound) {
timeSpentSleeping = upperTimeBound;
}
@@ -254,17 +231,12 @@ public static class Engine
* is if we either 1) just got a new worst case, or 2) the worst case was
* the oldest entry on the list.
*/
if (timeSpentSleeping >= worstCaseSleepPrecision)
{
if(timeSpentSleeping >= worstCaseSleepPrecision) {
worstCaseSleepPrecision = timeSpentSleeping;
}
else if (previousSleepTimes[sleepTimeIndex] == worstCaseSleepPrecision)
{
} else if(previousSleepTimes[sleepTimeIndex] == worstCaseSleepPrecision) {
TimeSpan maxSleepTime = TimeSpan.MinValue;
for (int i = 0; i < previousSleepTimes.Length; i++)
{
if (previousSleepTimes[i] > maxSleepTime)
{
for(int i = 0; i < previousSleepTimes.Length; i++) {
if(previousSleepTimes[i] > maxSleepTime) {
maxSleepTime = previousSleepTimes[i];
}
}