This is my code to initiate FSEM and paint the canvas; the superclass makes periodic calls to the render and paint methods:
Code
protected void setupCanvas() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
hardwareRenderer = ge.getDefaultScreenDevice();
setUndecorated(true);
setBackground(Color.WHITE);
setIgnoreRepaint(true);
setResizable(false);
if (!hardwareRenderer.isFullScreenSupported()) {
System.err.println("Hardware Compatibility Error: Full Screen Exclusive Mode not supported.");
System.exit(0);
}
hardwareRenderer.setFullScreenWindow(this);
super.setGameSize(getSize());
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
createBufferStrategy(bufferCount); // Asynchronous dispatch
}
});
}
catch (Exception e) {
System.err.println("Buffer strategy creation error.");
System.exit(0);
}
// Allow dispatch to complete - yes I KNOW this is a shoddy cop-out
try {Thread.sleep(500);} catch (InterruptedException e) {}
bufferStrategy = getBufferStrategy();
pack();
setPaused(true);
startGame();
}
public void renderGame() {
try {
Graphics g = bufferStrategy.getDrawGraphics();
drawGame(g);
g.dispose();
}
catch (Exception e) {
System.err.println("Render-time graphics error!");
e.printStackTrace();
terminate();
}
}
public void paintScreen() {
try {
if (!bufferStrategy.contentsLost())
bufferStrategy.show();
Toolkit.getDefaultToolkit().sync();
}
catch (Exception e) {
System.err.println("Paint-time graphics error!");
e.printStackTrace();
terminate();
}
}
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
hardwareRenderer = ge.getDefaultScreenDevice();
setUndecorated(true);
setBackground(Color.WHITE);
setIgnoreRepaint(true);
setResizable(false);
if (!hardwareRenderer.isFullScreenSupported()) {
System.err.println("Hardware Compatibility Error: Full Screen Exclusive Mode not supported.");
System.exit(0);
}
hardwareRenderer.setFullScreenWindow(this);
super.setGameSize(getSize());
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
createBufferStrategy(bufferCount); // Asynchronous dispatch
}
});
}
catch (Exception e) {
System.err.println("Buffer strategy creation error.");
System.exit(0);
}
// Allow dispatch to complete - yes I KNOW this is a shoddy cop-out
try {Thread.sleep(500);} catch (InterruptedException e) {}
bufferStrategy = getBufferStrategy();
pack();
setPaused(true);
startGame();
}
public void renderGame() {
try {
Graphics g = bufferStrategy.getDrawGraphics();
drawGame(g);
g.dispose();
}
catch (Exception e) {
System.err.println("Render-time graphics error!");
e.printStackTrace();
terminate();
}
}
public void paintScreen() {
try {
if (!bufferStrategy.contentsLost())
bufferStrategy.show();
Toolkit.getDefaultToolkit().sync();
}
catch (Exception e) {
System.err.println("Paint-time graphics error!");
e.printStackTrace();
terminate();
}
}
The superclass calls Runtime.getRuntime().addShutdownHook to call the method runBeforeTermination() which is overridden below. Also shown is the code to terminate the program (called if a click a quit button, etc. - the "normal" way to exit) and to restore the screen.
Code
protected void restoreScreen() {
Window w = hardwareRenderer.getFullScreenWindow();
if (w != null)
w.dispose();
hardwareRenderer.setFullScreenWindow(null);
}
public void terminate() {
restoreScreen();
super.terminate();
}
public void runBeforeTermination() {
restoreScreen();
System.out.println("hi");
}
Window w = hardwareRenderer.getFullScreenWindow();
if (w != null)
w.dispose();
hardwareRenderer.setFullScreenWindow(null);
}
public void terminate() {
restoreScreen();
super.terminate();
}
public void runBeforeTermination() {
restoreScreen();
System.out.println("hi");
}
Any time I call terminate() with a call to restoreScreen(), then the shutdown hook runs and "hi" is printed to the console. If I comment out the restoreScreen() or exit via alt+f4, my screen remains the last painted frame and I have to assume nothing is printed to the console (I have to close Eclipse to get my screen back).
I know there are some coding people out there.. is FSEM just not compatible with hooks or something? I see nothing in the API to this effect.
Post has been edited 2 time(s), last time on Jun 3 2008, 6:04 am by DT_Battlekruser. Reason: grammar, spelling, etc.
None.