Created a functional mainloop

This commit is contained in:
jsrobson10 2019-07-09 18:32:35 +10:00
parent f3fbe8315b
commit 964d6a706c
7 changed files with 301 additions and 0 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>

95
src/mainloop/Tests.java Executable file
View File

@ -0,0 +1,95 @@
package mainloop;
import mainloop.event.IMainloopEvent;
import mainloop.manager.MainloopManager;
import mainloop.task.IMainloopTask;
public class Tests
{
public static long timer = 0;
public static void main(String[] args)
{
MainloopManager ml = new MainloopManager(new IMainloopEvent() {
@Override
public void onStart() {
System.out.println("Started");
}
@Override
public void onLate() {
}
@Override
public void onEarly() {
}
@Override
public void onClose() {
System.out.println("Closed");
}
});
ml.register(new IMainloopTask() {
@Override
public void MainLoopUpdate() {
System.out.print(timer);
System.out.print(" ");
System.out.print(System.currentTimeMillis());
System.out.println();
//try {Thread.sleep(100);} catch(Exception e) {}
timer++;
}
@Override
public boolean MainLoopRepeat() {
return true;
}
@Override
public boolean MainLoopDelay(long delay) {
return delay > 1000;
}
});
ml.register(new IMainloopTask() {
@Override
public void MainLoopUpdate() {
//ml.stop();
}
@Override
public boolean MainLoopRepeat() {
return true;
}
@Override
public boolean MainLoopDelay(long delay) {
return delay > 1000;
}
});
ml.register(new IMainloopTask() {
@Override
public void MainLoopUpdate() {
System.out.println("Hello, World!");
}
@Override
public boolean MainLoopRepeat() {
return false;
}
@Override
public boolean MainLoopDelay(long delay) {
return delay > 10000;
}
});
ml.start();
}
}

View File

@ -0,0 +1,9 @@
package mainloop.event;
public interface IMainloopEvent
{
public void onEarly();
public void onLate();
public void onClose();
public void onStart();
}

View File

@ -0,0 +1,79 @@
package mainloop.manager;
import java.util.ArrayList;
import java.util.Iterator;
import mainloop.event.IMainloopEvent;
import mainloop.task.IMainloopTask;
import mainloop.task.MainloopTask;
import mainloop.time.MainloopTimer;
public class MainloopManager
{
protected ArrayList<MainloopTask> tasks;
protected IMainloopEvent handler;
protected boolean running = false;
protected MainloopTimer timer;
public MainloopManager(IMainloopEvent handler)
{
// Initialize the variables
this.handler = handler;
this.tasks = new ArrayList<MainloopTask>();
this.timer = new MainloopTimer(handler);
}
public void start()
{
// Set running to true
this.running = true;
// Call the on start function
this.handler.onStart();
// Loop while the running flag is true
while(this.running)
{
// Sleep until the timer is on time
this.timer.sleep();
// Update the processes
this.update();
}
}
public void stop()
{
// Call the on close function
this.handler.onClose();
// Set the running flag to false
this.running = false;
}
public void update()
{
// Loop over the tasks
for(Iterator<MainloopTask> i=tasks.iterator();i.hasNext();)
{
// Get the task
MainloopTask task = (MainloopTask) i.next();
// Update the task
task.update();
// Should the task be removed
if(task.shouldRemove())
{
// Remove the task
i.remove();
}
}
}
public void register(IMainloopTask task) {
tasks.add(new MainloopTask(task));
}
}

View File

@ -0,0 +1,8 @@
package mainloop.task;
public interface IMainloopTask
{
public void MainLoopUpdate();
public boolean MainLoopDelay(long delay);
public boolean MainLoopRepeat();
}

View File

@ -0,0 +1,59 @@
package mainloop.task;
public class MainloopTask
{
protected long timer;
protected IMainloopTask task;
protected boolean remove;
public MainloopTask(IMainloopTask task)
{
// Initialize all the variables
this.task = task;
this.timer = 1;
this.remove = false;
}
public void update()
{
// Increase the timer
timer += 1;
if(!this.remove)
{
// Is it time for the function to update
if(task.MainLoopDelay(this.timer))
{
// Set the remove flag to true
this.remove = true;
// Execute the function
task.MainLoopUpdate();
}
}
}
public boolean shouldRemove()
{
// Is the remove flag set
if(this.remove)
{
// Does this task repeat
if(task.MainLoopRepeat())
{
// Set the flags to false and reset the timer
this.remove = false;
this.timer = 1;
}
else
{
// Send back true
return true;
}
}
// Send back false by default
return false;
}
}

View File

@ -0,0 +1,50 @@
package mainloop.time;
import mainloop.event.IMainloopEvent;
public class MainloopTimer
{
protected long millis_now;
protected long millis_count;
IMainloopEvent handler;
public MainloopTimer(IMainloopEvent handler)
{
// Initialize the variables
this.millis_now = System.currentTimeMillis();
this.millis_count = this.millis_now;
this.handler = handler;
}
public void sleep()
{
// Get the current millis and update the millis count
this.millis_now = System.currentTimeMillis();
this.millis_count += 1;
// Is the timer late
if(this.millis_now > this.millis_count)
{
// Call the late handler
handler.onLate();
}
// Is the timer early
else
{
// Call the early handler
handler.onEarly();
// Loop until the timer is on time
while(System.currentTimeMillis() <= this.millis_count)
{
// Sleep for 1 millisecond
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}