private Action<int> CreateUpdateRateSetter(TimedCallback callback) { return (updateRate) => { callback.Fps = updateRate; }; }
public void LooperMultipleCallbacksWork() { long updates = 0; var resetEvent = new ManualResetEvent(false); var gameLoop = new Looper(); var timedCallback1 = new TimedCallback(30, () => { updates++; }); var timedCallback2 = new TimedCallback(60, () => { updates--; if (updates <= -30) { resetEvent.Set(); } }); gameLoop.AddCallback(timedCallback1); gameLoop.AddCallback(timedCallback2); gameLoop.Start(); Assert.True(resetEvent.WaitOne(TimeSpan.FromSeconds(1.01))); gameLoop.Dispose(); // Since timedCallback2 is executing twice as fast as timedCallback1 it will negate the value of updates Assert.Equal(updates, -30); }
public void RemoveCallback(TimedCallback callback) { lock (_updateLock) { _callbacks.Remove(callback); _callbacksModified = true; } }
public void AddCallback(TimedCallback callback) { lock (_updateLock) { _callbacks.Add(callback); _callbacksModified = true; } }
private TimedCallback CreateAndCacheCallback(Game game) { var updateCallback = new TimedCallback { Callback = game.PrepareUpdate }; // Add the callback to the callback cache _callbacks.TryAdd(game.ID, updateCallback); return updateCallback; }
public void LooperSingleCallbackWorks() { long updates = 0; var resetEvent = new ManualResetEvent(false); var gameLoop = new Looper(); var timedCallback = new TimedCallback(30, () => { updates++; if (updates >= 30) { resetEvent.Set(); } }); gameLoop.AddCallback(timedCallback); gameLoop.Start(); Assert.True(resetEvent.WaitOne(TimeSpan.FromSeconds(1.01))); gameLoop.Dispose(); Assert.Equal(updates, 30); }