Create API for pausing and stopping game engine
This commit is contained in:
@@ -27,6 +27,18 @@ public interface Context extends Updatable, Renderable, Disposable {
|
|||||||
|
|
||||||
GUI newGUI();
|
GUI newGUI();
|
||||||
|
|
||||||
|
boolean isRunning();
|
||||||
|
|
||||||
|
void close();
|
||||||
|
|
||||||
|
boolean isPaused();
|
||||||
|
|
||||||
|
void pause();
|
||||||
|
|
||||||
|
void resume();
|
||||||
|
|
||||||
|
boolean togglePause();
|
||||||
|
|
||||||
void init(Screen screen, Camera camera);
|
void init(Screen screen, Camera camera);
|
||||||
|
|
||||||
void input(Screen screen);
|
void input(Screen screen);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import com.bartlomiejpluta.base.engine.logic.GameLogic;
|
|||||||
import com.bartlomiejpluta.base.engine.thread.ThreadManager;
|
import com.bartlomiejpluta.base.engine.thread.ThreadManager;
|
||||||
import com.bartlomiejpluta.base.engine.time.ChronoMeter;
|
import com.bartlomiejpluta.base.engine.time.ChronoMeter;
|
||||||
import com.bartlomiejpluta.base.engine.ui.ScreenManager;
|
import com.bartlomiejpluta.base.engine.ui.ScreenManager;
|
||||||
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -31,8 +32,12 @@ public class DefaultGameEngine implements GameEngine {
|
|||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
|
|
||||||
|
@Getter
|
||||||
private boolean running = false;
|
private boolean running = false;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private boolean paused = true;
|
||||||
|
|
||||||
private void run() {
|
private void run() {
|
||||||
try {
|
try {
|
||||||
init();
|
init();
|
||||||
@@ -62,9 +67,13 @@ public class DefaultGameEngine implements GameEngine {
|
|||||||
|
|
||||||
input();
|
input();
|
||||||
|
|
||||||
while (accumulator >= step) {
|
if (!paused) {
|
||||||
update(dt);
|
while (accumulator >= step) {
|
||||||
accumulator -= step;
|
update(dt);
|
||||||
|
accumulator -= step;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
accumulator = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
render();
|
render();
|
||||||
@@ -105,6 +114,33 @@ public class DefaultGameEngine implements GameEngine {
|
|||||||
thread.start();
|
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
|
// TODO
|
||||||
// It is supposed to be moved to the Context so that
|
// It is supposed to be moved to the Context so that
|
||||||
// user will be able to choose default window size,
|
// user will be able to choose default window size,
|
||||||
|
|||||||
@@ -4,4 +4,16 @@ import com.bartlomiejpluta.base.api.game.context.Context;
|
|||||||
|
|
||||||
public interface GameEngine {
|
public interface GameEngine {
|
||||||
void start(Context context);
|
void start(Context context);
|
||||||
|
|
||||||
|
void stop();
|
||||||
|
|
||||||
|
void pause();
|
||||||
|
|
||||||
|
void resume();
|
||||||
|
|
||||||
|
boolean isRunning();
|
||||||
|
|
||||||
|
boolean isPaused();
|
||||||
|
|
||||||
|
boolean togglePaused();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,6 +101,36 @@ public class DefaultContext implements Context {
|
|||||||
return gui;
|
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
|
@Override
|
||||||
public void input(Screen screen) {
|
public void input(Screen screen) {
|
||||||
gameRunner.input(screen);
|
gameRunner.input(screen);
|
||||||
|
|||||||
@@ -81,13 +81,6 @@ public class GLFWScreen implements Screen {
|
|||||||
GLFWScreen.this.size.y = height;
|
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
|
// Get the resolution of the primary monitor
|
||||||
GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
|
GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user