📄 standardgame.java
字号:
display.getRenderer().setCamera(camera);
if ((settings.isMusic()) || (settings.isSFX())) {
initSound();
}
} else {
display = DisplaySystem.getDisplaySystem(DummySystemProvider.DUMMY_SYSTEM_IDENTIFIER);
}
}
/**
* The java.awt.Canvas if DISPLAY_CANVAS is the DISPLAY_MODE
*
* @return
* Canvas
*/
public Canvas getCanvas() {
return canvas;
}
protected void initSound() {
AudioSystem.getSystem().getEar().trackOrientation(camera);
AudioSystem.getSystem().getEar().trackPosition(camera);
}
private void displayMins() {
display.setMinDepthBits(settings.getDepthBits());
display.setMinStencilBits(settings.getStencilBits());
display.setMinAlphaBits(settings.getAlphaBits());
display.setMinSamples(settings.getSamples());
}
private void cameraPerspective() {
camera.setFrustumPerspective(45.0f, (float)display.getWidth() / (float)display.getHeight(), 1.0f, 1000.0f);
camera.setParallelProjection(false);
camera.update();
}
private void cameraFrame() {
Vector3f loc = new Vector3f(0.0f, 0.0f, 25.0f);
Vector3f left = new Vector3f(-1.0f, 0.0f, 0.0f);
Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f);
Vector3f dir = new Vector3f(0.0f, 0.0f, -1.0f);
camera.setFrame(loc, left, up, dir);
}
public void resetCamera() {
cameraFrame();
}
protected void initGame() {
// Create the GameStateManager
GameStateManager.create();
}
protected void update(float interpolation) {
// Open the lock up for just a brief second
unlock();
lock();
// Execute updateQueue item
GameTaskQueueManager.getManager().getQueue(GameTaskQueue.UPDATE).execute();
// Update the GameStates
GameStateManager.getInstance().update(interpolation);
if (type == GameType.GRAPHICAL) {
// Update music/sound
if ((settings.isMusic()) || (settings.isSFX())) {
AudioSystem.getSystem().update();
}
}
}
protected void render(float interpolation) {
display.getRenderer().clearBuffers();
// Execute renderQueue item
GameTaskQueueManager.getManager().getQueue(GameTaskQueue.RENDER).execute();
// Render the GameStates
GameStateManager.getInstance().render(interpolation);
}
public void reinit() {
reinitAudio();
reinitVideo();
}
public void reinitAudio() {
if (AudioSystem.isCreated()) {
AudioSystem.getSystem().cleanup();
}
}
public void reinitVideo() {
GameTaskQueueManager.getManager().update(new Callable<Object>() {
public Object call() throws Exception {
displayMins();
display.recreateWindow(settings.getWidth(), settings
.getHeight(), settings.getDepth(), settings
.getFrequency(), settings.isFullscreen());
camera = display.getRenderer().createCamera(display.getWidth(),
display.getHeight());
display.getRenderer().setBackgroundColor(backgroundColor);
if ((settings.isMusic()) || (settings.isSFX())) {
initSound();
}
return null;
}
});
}
public void recreateGraphicalContext() {
reinit();
}
protected void cleanup() {
GameStateManager.getInstance().cleanup();
DisplaySystem.getDisplaySystem().getRenderer().cleanup();
TextureManager.doTextureCleanup();
TextureManager.clearCache();
JoystickInput.destroyIfInitalized();
if (AudioSystem.isCreated()) {
AudioSystem.getSystem().cleanup();
}
}
protected void quit() {
if (display != null) {
display.reset();
display.close();
}
}
/**
* The internally used <code>DisplaySystem</code> for this instance
* of <code>StandardGame</code>
*
* @return
* DisplaySystem
*
* @see DisplaySystem
*/
public DisplaySystem getDisplay() {
return display;
}
/**
* The internally used <code>Camera</code> for this instance of
* <code>StandardGame</code>.
*
* @return
* Camera
*
* @see Camera
*/
public Camera getCamera() {
return camera;
}
/**
* The <code>GameSettings</code> implementation being utilized in
* this instance of <code>StandardGame</code>.
*
* @return
* GameSettings
*
* @see GameSettings
*/
public GameSettings getSettings() {
return settings;
}
/**
* Override the background color defined for this game. The reinit() method
* must be invoked if the game is currently running before this will take effect.
*
* @param backgroundColor
*/
public void setBackgroundColor(ColorRGBA backgroundColor) {
this.backgroundColor = backgroundColor;
}
/**
* Gracefully shutdown the main game loop thread. This is a synonym
* for the finish() method but just sounds better.
*
* @see #finish()
*/
public void shutdown() {
finish();
}
/**
* Will return true if within the main game loop. This is particularly
* useful to determine if the game has finished the initialization but
* will also return false if the game has been terminated.
*
* @return
* boolean
*/
public boolean isStarted() {
return started;
}
/**
* Specify the UncaughtExceptionHandler for circumstances where an exception in the
* OpenGL thread is not captured properly.
*
* @param exceptionHandler
*/
public void setUncaughtExceptionHandler(UncaughtExceptionHandler exceptionHandler) {
this.exceptionHandler = exceptionHandler;
gameThread.setUncaughtExceptionHandler(this.exceptionHandler);
}
/**
* Causes the current thread to wait for an update to occur in the OpenGL thread.
* This can be beneficial if there is work that has to be done in the OpenGL thread
* that needs to be completed before continuing in another thread.
*
* You can chain invocations of this together in order to wait for multiple updates.
*
* @throws InterruptedException
* @throws ExecutionException
*/
public void delayForUpdate() throws InterruptedException, ExecutionException {
Future<Object> f = GameTaskQueueManager.getManager().update(new Callable<Object>() {
public Object call() throws Exception {
return null;
}
});
f.get();
}
/**
* Convenience method to let you know if the thread you're in is the OpenGL thread
*
* @return
* true if, and only if, the current thread is the OpenGL thread
*/
public boolean inGLThread() {
if (Thread.currentThread() == gameThread) {
return true;
}
return false;
}
/**
* Convenience method that will make sure <code>callable</code> is executed in the
* OpenGL thread. If it is already in the OpenGL thread when this method is invoked
* it will be executed and returned immediately. Otherwise, it will be put into the
* GameTaskQueue and executed in the next update. This is a blocking method and will
* wait for the successful return of <code>callable</code> before returning.
*
* @param <T>
* @param callable
* @return result of callable.get()
* @throws Exception
*/
public <T> T executeInGL(Callable<T> callable) throws Exception {
if (inGLThread()) {
return callable.call();
}
Future<T> future = GameTaskQueueManager.getManager().update(callable);
return future.get();
}
/**
* Will wait for a lock at the beginning of the OpenGL update method. Once this method returns the
* OpenGL thread is blocked until the lock is released (via unlock()). If another thread currently
* has a lock or it is currently in the process of an update the calling thread will be blocked until
* the lock is successfully established.
*/
public void lock() {
updateLock.lock();
}
/**
* Used in conjunction with lock() in order to release a previously assigned lock on the OpenGL thread.
* This <b>MUST</b> be executed within the same thread that called lock() in the first place or the lock
* will not be released.
*/
public void unlock() {
updateLock.unlock();
}
public void setIcons( Image[] icons) {
this.icons = icons;
}
}
class DefaultUncaughtExceptionHandler implements UncaughtExceptionHandler {
private static final Logger logger = Logger
.getLogger(DefaultUncaughtExceptionHandler.class.getName());
private StandardGame game;
public DefaultUncaughtExceptionHandler(StandardGame game) {
this.game = game;
}
public void uncaughtException(Thread t, Throwable e) {
logger.log(Level.SEVERE, "Main game loop broken by uncaught exception", e);
game.shutdown();
game.cleanup();
game.quit();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -