Create API for pausing and stopping game engine

This commit is contained in:
2021-03-12 18:36:16 +01:00
parent 73de1f0985
commit f17ce55882
5 changed files with 93 additions and 10 deletions

View File

@@ -27,6 +27,18 @@ public interface Context extends Updatable, Renderable, Disposable {
GUI newGUI();
boolean isRunning();
void close();
boolean isPaused();
void pause();
void resume();
boolean togglePause();
void init(Screen screen, Camera camera);
void input(Screen screen);

View File

@@ -7,6 +7,7 @@ import com.bartlomiejpluta.base.engine.logic.GameLogic;
import com.bartlomiejpluta.base.engine.thread.ThreadManager;
import com.bartlomiejpluta.base.engine.time.ChronoMeter;
import com.bartlomiejpluta.base.engine.ui.ScreenManager;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,8 +32,12 @@ public class DefaultGameEngine implements GameEngine {
private Context context;
@Getter
private boolean running = false;
@Getter
private boolean paused = true;
private void run() {
try {
init();
@@ -62,10 +67,14 @@ public class DefaultGameEngine implements GameEngine {
input();
if (!paused) {
while (accumulator >= step) {
update(dt);
accumulator -= step;
}
} else {
accumulator = 0;
}
render();
}
@@ -105,6 +114,33 @@ public class DefaultGameEngine implements GameEngine {
thread.start();
}
@Override
public void stop() {
log.info("Stopping the engine");
running = false;
}
@Override
public void pause() {
log.info("Pausing the engine");
paused = true;
}
@Override
public void resume() {
log.info("Resuming the engine");
paused = false;
}
@Override
public boolean togglePaused() {
paused = !paused;
log.info("{} the engine", paused ? "Pausing" : "Resuming");
return paused;
}
// TODO
// It is supposed to be moved to the Context so that
// user will be able to choose default window size,

View File

@@ -4,4 +4,16 @@ import com.bartlomiejpluta.base.api.game.context.Context;
public interface GameEngine {
void start(Context context);
void stop();
void pause();
void resume();
boolean isRunning();
boolean isPaused();
boolean togglePaused();
}

View File

@@ -101,6 +101,36 @@ public class DefaultContext implements Context {
return gui;
}
@Override
public boolean isRunning() {
return engine.isRunning();
}
@Override
public void close() {
engine.stop();
}
@Override
public boolean isPaused() {
return engine.isPaused();
}
@Override
public void pause() {
engine.pause();
}
@Override
public void resume() {
engine.resume();
}
@Override
public boolean togglePause() {
return engine.togglePaused();
}
@Override
public void input(Screen screen) {
gameRunner.input(screen);

View File

@@ -81,13 +81,6 @@ public class GLFWScreen implements Screen {
GLFWScreen.this.size.y = height;
});
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
}
});
// Get the resolution of the primary monitor
GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());