-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Game loop implemented, user input handler fixed and improved
- Loading branch information
Showing
10 changed files
with
199 additions
and
63 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/com/gmail/vitalatron/game/exec/AbstractTimedLoop.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.gmail.vitalatron.game.exec; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
public abstract class AbstractTimedLoop implements TimedLoop { | ||
protected volatile int delay = 50; | ||
protected volatile boolean paused = false; | ||
protected volatile boolean stopped = false; | ||
protected List<GameTask> tasks = new CopyOnWriteArrayList<>(); | ||
|
||
@Override | ||
public synchronized void setTimeout(int milliseconds) { | ||
if (milliseconds <= 0) { | ||
throw new IllegalArgumentException("Loop timeout should be > 0"); | ||
} | ||
delay = milliseconds; | ||
} | ||
|
||
@Override | ||
public synchronized void setIterationsPerSecond(int ips) { | ||
if (ips <= 0) { | ||
throw new IllegalArgumentException("IPS should be > 0"); | ||
} | ||
if (ips > 1000) { | ||
throw new IllegalArgumentException("> 1000ips!? UMAD?"); | ||
} | ||
delay = 1000 / ips; | ||
} | ||
|
||
@Override | ||
public void removeAllTasks() { | ||
tasks.clear(); | ||
} | ||
|
||
@Override | ||
public void removeTask(GameTask task) { | ||
tasks.remove(task); | ||
} | ||
|
||
@Override | ||
public void addTask(GameTask task) { | ||
tasks.add(task); | ||
} | ||
|
||
@Override | ||
public synchronized void setPaused(boolean paused) { | ||
this.paused = paused; | ||
} | ||
|
||
@Override | ||
public synchronized void stop() { | ||
stopped = true; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/gmail/vitalatron/game/exec/ExecutorGameLoop.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.gmail.vitalatron.game.exec; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Executors; | ||
|
||
public class ExecutorGameLoop extends AbstractTimedLoop { | ||
protected Executor executor = Executors.newSingleThreadExecutor(); | ||
protected long lastExecutionNanoTime; | ||
|
||
protected Runnable loop = new Runnable() { | ||
@Override | ||
public void run() { | ||
while (!stopped) { | ||
long t = System.nanoTime(); | ||
double delta = ((t - lastExecutionNanoTime) / 1000000) / (double) delay; | ||
|
||
for (GameTask task : tasks) { | ||
task.execute(delta); | ||
} | ||
|
||
lastExecutionNanoTime = System.nanoTime(); | ||
t = (lastExecutionNanoTime - t) / 1000000; | ||
|
||
try { | ||
Thread.sleep(delay - t); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
public ExecutorGameLoop() { | ||
lastExecutionNanoTime = System.nanoTime(); | ||
executor.execute(loop); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
package com.gmail.vitalatron.game.exec; | ||
|
||
public abstract class GameTask implements Runnable { | ||
public interface GameTask { | ||
|
||
public abstract void execute(double delta); | ||
|
||
@Override | ||
public void run() { | ||
this.execute(1D); // FIXME | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
src/main/java/com/gmail/vitalatron/game/exec/ScheduledExecutorGameLoop.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
package com.gmail.vitalatron.game.exec; | ||
|
||
public interface TimedLoop { | ||
public static final int DEFAULT_DELAY = 33; // 30 fps | ||
|
||
public void addTask(GameTask task); | ||
public void removeTask(GameTask task); | ||
public void removeAllTasks(); | ||
|
||
public void setTimeout(int milliseconds); | ||
public void setFps(int fps); | ||
public void setIterationsPerSecond(int fps); | ||
|
||
public void setPaused(boolean paused); | ||
public void stop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/com/gmail/vitalatron/game/meh/core/InputHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.gmail.vitalatron.game.meh.core; | ||
|
||
import com.gmail.vitalatron.game.input.KeyboardButton; | ||
import com.gmail.vitalatron.game.input.UserInputAdapter; | ||
|
||
public class InputHandler extends UserInputAdapter { | ||
|
||
@Override | ||
public void keyReleased(KeyboardButton button) { | ||
System.out.println(button); | ||
} | ||
} |
18 changes: 15 additions & 3 deletions
18
src/main/java/com/gmail/vitalatron/game/meh/core/MehGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters