diff --git a/src/de/teamteamteam/spacescooter/Main.java b/src/de/teamteamteam/spacescooter/Main.java index af7f1a3..fab4942 100644 --- a/src/de/teamteamteam/spacescooter/Main.java +++ b/src/de/teamteamteam/spacescooter/Main.java @@ -1,6 +1,7 @@ package de.teamteamteam.spacescooter; import java.awt.EventQueue; +import java.lang.reflect.InvocationTargetException; import de.teamteamteam.spacescooter.screen.LoadingScreen; import de.teamteamteam.spacescooter.screen.Screen; @@ -23,41 +24,48 @@ public class Main { * @param args Command line arguments. */ public static void main(String[] args) { - GraphicsSettings gs = new GraphicsSettings(); //Get settings - - GameConfig.windowWidth = 800; - GameConfig.windowHeight = 650; - - //Instantiate the GameFrame - final GameFrame gameFrame = new GameFrame(); - - //Initialize SuperScreen and add to GameFrame, so we can call doPaint() on it. - final SuperScreen superScreen = new SuperScreen(null); - //Initialize the GameFrame properly within the AWT EventQueue - EventQueue.invokeLater(new Runnable() { - public void run() { - gameFrame.setSuperScreen(superScreen); - gameFrame.init(); - gameFrame.draw(); //Draw nothing for the first time. - } - }); - - //Initialize GameThread - PaintThread paintThread = new PaintThread(gameFrame); - paintThread.setHz(gs.getRefreshRate()); //This may be set depending on the system graphic settings. - paintThread.start(); - - //Initialize UpdateThread - UpdateThread updateThread = new UpdateThread(); - updateThread.setSuperScreen(superScreen); - updateThread.setHz(100); //This shall remain constant across all systems. - updateThread.start(); + try { + EventQueue.invokeAndWait(new Runnable() { + public void run() { + GraphicsSettings gs = new GraphicsSettings(); //Get settings + + GameConfig.windowWidth = 800; + GameConfig.windowHeight = 650; + + //Instantiate the GameFrame + final GameFrame gameFrame = new GameFrame(); + + //Initialize SuperScreen and add to GameFrame, so we can call doPaint() on it. + final SuperScreen superScreen = new SuperScreen(null); + gameFrame.setSuperScreen(superScreen); - //Set up the LoadingScreen - superScreen.setOverlay(new LoadingScreen(superScreen)); - - //Start loading and everything will follow up. - Loader.load((LoadingScreen) Screen.currentScreen); + //Initialize the gameFrame and trigger a first draw. + gameFrame.init(); + gameFrame.draw(); //Draw nothing for the first time. + + //Initialize GameThread + PaintThread paintThread = new PaintThread(gameFrame); + paintThread.setHz(gs.getRefreshRate()); //This may be set depending on the system graphic settings. + paintThread.start(); + + //Initialize UpdateThread + UpdateThread updateThread = new UpdateThread(); + updateThread.setSuperScreen(superScreen); + updateThread.setHz(100); //This shall remain constant across all systems. + updateThread.start(); + + //Set up the LoadingScreen + superScreen.setOverlay(new LoadingScreen(superScreen)); + + //Start loading and everything will follow up. + Loader.load((LoadingScreen) Screen.currentScreen); + } + }); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } } } diff --git a/src/de/teamteamteam/spacescooter/thread/PaintThread.java b/src/de/teamteamteam/spacescooter/thread/PaintThread.java index 1c4ca09..bf656e4 100644 --- a/src/de/teamteamteam/spacescooter/thread/PaintThread.java +++ b/src/de/teamteamteam/spacescooter/thread/PaintThread.java @@ -1,6 +1,7 @@ package de.teamteamteam.spacescooter.thread; import java.awt.EventQueue; +import java.lang.reflect.InvocationTargetException; import de.teamteamteam.spacescooter.GameFrame; @@ -38,7 +39,13 @@ public class PaintThread extends TimedThread { */ public void work() { //Trigger redrawing the things. Important: AWT-Context needed here! - EventQueue.invokeLater(this.paintRunnable); + try { + EventQueue.invokeAndWait(this.paintRunnable); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } } /** diff --git a/src/de/teamteamteam/spacescooter/thread/TimedThread.java b/src/de/teamteamteam/spacescooter/thread/TimedThread.java index b674f33..e2bd386 100644 --- a/src/de/teamteamteam/spacescooter/thread/TimedThread.java +++ b/src/de/teamteamteam/spacescooter/thread/TimedThread.java @@ -40,6 +40,11 @@ public abstract class TimedThread extends Thread { this.workTime = (workDone - workStart); long timeToWait = this.workInterval - workTime; + //in case we are already running late, just print a warning and carry on! + if(timeToWait < 0) { + System.err.println("[" + this.getName() + "] workTime exceeds workInterval!:" + this.workTime + " > " + this.workInterval); + continue; + } long msToWait = timeToWait / 1000000; // wait using sleep for bigger intervals