/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.anzix.fsz;
import com.ardor3d.framework.CanvasRenderer;
import com.ardor3d.framework.FrameHandler;
import com.ardor3d.framework.NativeCanvas;
import com.ardor3d.util.ContextGarbageCollector;
import net.anzix.fsz.eventbus.Event;
import net.anzix.fsz.eventbus.EventListener;
import net.anzix.fsz.eventbus.event.Exit;
/**
*
* @author elek
*/
public class MainThread implements Runnable, EventListener {
private FrameHandler _frameHandler;
private NativeCanvas canvas;
private boolean exit;
public MainThread(FrameHandler _frameHandler, NativeCanvas canvas) {
this._frameHandler = _frameHandler;
this.canvas = canvas;
}
public void run() {
try {
_frameHandler.init();
while (!exit) {
_frameHandler.updateFrame();
Thread.yield();
}
// grab the graphics context so cleanup will work out.
final CanvasRenderer cr = canvas.getCanvasRenderer();
cr.makeCurrentContext();
ContextGarbageCollector.doFinalCleanup(cr.getRenderer());
canvas.close();
cr.releaseCurrentContext();
System.exit(0);
} catch (final Throwable t) {
System.err.println("Throwable caught in MainThread - exiting");
t.printStackTrace(System.err);
}
}
public void onEvent(Event event) {
if (event instanceof Exit){
exit = true;
}
}
}