Coverage Summary for Class: DesktopLauncher (com.mygdx.game.desktop)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| DesktopLauncher | 0% (0/1) | 0% (0/3) | 0% (0/39) |
1 package com.mygdx.game.desktop; 2 3 import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application; 4 import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration; 5 import com.mygdx.game.PirateGame; 6 7 import java.io.BufferedReader; 8 import java.io.InputStream; 9 import java.io.InputStreamReader; 10 import java.lang.management.ManagementFactory; 11 import java.util.ArrayList; 12 import java.util.List; 13 14 import static com.mygdx.utils.Constants.*; 15 16 public class DesktopLauncher { 17 public static void main(String[] arg) { 18 // Hack around the -XstartOnFirstThread issue with Mac & libgdx 19 if (restartJVM()) { 20 return; 21 } 22 23 INIT_CONSTANTS(); 24 25 Lwjgl3ApplicationConfiguration cfg = new Lwjgl3ApplicationConfiguration(); 26 cfg.setTitle(VIEWPORT_TITLE); 27 28 if (FULLSCREEN) { 29 cfg.setFullscreenMode(Lwjgl3ApplicationConfiguration.getDisplayMode()); 30 } else { 31 cfg.setWindowedMode(VIEWPORT_WIDTH, VIEWPORT_HEIGHT); 32 } 33 34 if (!VSYNC) { 35 cfg.useVsync(false); 36 cfg.setForegroundFPS(0); 37 } 38 39 new Lwjgl3Application(new PirateGame(), cfg); 40 } 41 42 public static boolean restartJVM() { 43 44 String osName = System.getProperty("os.name"); 45 46 // if not a mac return false 47 if (!osName.startsWith("Mac") && !osName.startsWith("Darwin")) { 48 return false; 49 } 50 51 // get current jvm process pid 52 String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; 53 // get environment variable on whether XstartOnFirstThread is enabled 54 String env = System.getenv("JAVA_STARTED_ON_FIRST_THREAD_" + pid); 55 56 // if environment variable is "1" then XstartOnFirstThread is enabled 57 if (env != null && env.equals("1")) { 58 return false; 59 } 60 61 // restart jvm with -XstartOnFirstThread 62 String separator = System.getProperty("file.separator"); 63 String classpath = System.getProperty("java.class.path"); 64 String mainClass = System.getenv("JAVA_MAIN_CLASS_" + pid); 65 String jvmPath = System.getProperty("java.home") + separator + "bin" + separator + "java"; 66 67 List<String> inputArguments = ManagementFactory.getRuntimeMXBean().getInputArguments(); 68 69 ArrayList<String> jvmArgs = new ArrayList<String>(); 70 71 jvmArgs.add(jvmPath); 72 jvmArgs.add("-XstartOnFirstThread"); 73 jvmArgs.addAll(inputArguments); 74 jvmArgs.add("-cp"); 75 jvmArgs.add(classpath); 76 jvmArgs.add(mainClass); 77 78 try { 79 ProcessBuilder processBuilder = new ProcessBuilder(jvmArgs); 80 processBuilder.redirectErrorStream(true); 81 Process process = processBuilder.start(); 82 83 InputStream is = process.getInputStream(); 84 InputStreamReader isr = new InputStreamReader(is); 85 BufferedReader br = new BufferedReader(isr); 86 String line; 87 88 while ((line = br.readLine()) != null) { 89 System.out.println(line); 90 } 91 92 process.waitFor(); 93 } catch (Exception e) { 94 e.printStackTrace(); 95 } 96 97 return true; 98 } 99 }