Skip to content

Instantly share code, notes, and snippets.

@punmechanic
Last active August 29, 2015 14:05
Show Gist options
  • Save punmechanic/cf7205f40df27e7bc964 to your computer and use it in GitHub Desktop.
Save punmechanic/cf7205f40df27e7bc964 to your computer and use it in GitHub Desktop.
// The main game class, instantiated from Main, takes a scheduler and a display window
// The scheduler supplied should be where anything asynchronous should be executed for
// the game, or any kind of repeatable task. Example - game tick.
// The display window actually displays stuff.
// I was hoping for the injected Scheduler to potentially run on the current thread.
// At the point of inception the Game will have been created on the Main thread, thus,
// the Scheduler would run on the Main thread and prevent the program from immediately
// returning.
public class Game implements Disposable {
private final DisplayWindow _display;
private final Disposable _displayUpdates;
private final Scheduler _scheduler;
/**
* Create the Game.
* @param displayService - the service to use for the Display
* @param width - the initial width of the display
* @param height - the initial height of the display
*/
public Game(Scheduler gameScheduler, DisplayWindow displayWindow) {
_display = displayWindow;
_scheduler = gameScheduler;
// schedule display to update @ 60 fps
int frameInterval = (int) ((1f / displayWindow.getTargetFramesPerSecond()) * 1000f);
}
public void dispose() {
_scheduler.dispose();
_display.dispose();
_displayUpdates.dispose();
}
}
/**
* An interface that schedules tasks.
* @author Dan
*
*/
public interface Scheduler extends Disposable {
/**
* Schedule a task to be run indefinitely until the token is disposed.
* @param task The task itself. The passed argument is the actual difference between the last repeat and this repeat.
* @param interval The interval between each task run.
* @returns A token that can be disposed to cancel the task prematurely
*/
Disposable schedule(Runnable task, int interval);
/**
* Schedule a single-fire function. The return value of the function
* is returned through the surfaced Future.
* @param runnable
* @return
*/
<T> Future<T> schedule(Callable<T> runnable);
}
public interface Disposable {
/**
* Dispose of the object, releasing any resources that this object owns.
*/
void dispose();
}
/**
* A scheduler that uses new threads to create tasks.
* @author Dan
*
*/
public class BasicScheduler implements Scheduler, Disposable {
private final ScheduledExecutorService _executor;
/**
* Create the BasicScheduler with a single thread executor.
*/
public BasicScheduler() {
this(Executors.newSingleThreadScheduledExecutor());
}
/**
* Create the BasicScheduler with the given ExecutorService.
* @param executorService
*/
public BasicScheduler(ScheduledExecutorService executorService) {
_executor = executorService;
}
public Disposable schedule(Runnable task, int interval) {
ScheduledFuture<?> future = _executor.scheduleWithFixedDelay(task, 0, interval, TimeUnit.MILLISECONDS);
return new RunnableDisposable(() -> future.cancel(true));
}
@Override
public void dispose() {
_executor.shutdownNow();
}
@Override
public <T> Future<T> schedule(Callable<T> runnable) {
return _executor.submit(runnable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment