11package forge .app ;
22
33import com .badlogic .gdx .ApplicationListener ;
4+ import com .badlogic .gdx .Graphics .DisplayMode ;
45import com .badlogic .gdx .backends .lwjgl3 .Lwjgl3Application ;
56import com .badlogic .gdx .backends .lwjgl3 .Lwjgl3ApplicationConfiguration ;
67import com .badlogic .gdx .backends .lwjgl3 .Lwjgl3Clipboard ;
@@ -33,7 +34,6 @@ public GameLauncher(final String versionString, final String[] args) {
3334 Configuration .STACK_SIZE .set (1024 );
3435 Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration ();
3536 HWInfo hw = null ;
36- int totalRAM = 0 ;
3737 try {
3838 SystemInfo si = new SystemInfo ();
3939 // Device Info
@@ -51,56 +51,64 @@ public GameLauncher(final String versionString, final String[] args) {
5151 os .setVersion (si .getOperatingSystem ().getVersionInfo ().getVersion ());
5252 os .setBuild (si .getOperatingSystem ().getVersionInfo ().getBuildNumber ());
5353 os .setRawDescription (si .getOperatingSystem () + " x" + si .getOperatingSystem ().getBitness ());
54- totalRAM = Math .round (si .getHardware ().getMemory ().getTotal () / 1024f / 1024f );
5554 hw = new HWInfo (device , os , false );
5655 } catch (Exception e ) {
5756 e .printStackTrace ();
5857 }
5958
6059 // Retrieve command line parameters
61- int windowHeight = 0 ;
62- int windowWidth = 0 ;
63- boolean isPortrait = false ;
64- boolean manualWindowSize = false ;
60+ Integer widthArg = null ;
61+ Integer heightArg = null ;
62+ boolean portraitArg = false ;
63+ boolean landscapeArg = false ;
6564 for (String arg : args ) {
66- if (arg .startsWith ("width=" )) {
67- windowWidth = Integer .parseInt (arg .substring (6 ));
68- manualWindowSize = true ;
69- }
70- if (arg .startsWith ("height=" )) {
71- windowHeight = Integer .parseInt (arg .substring (7 ));
72- manualWindowSize = true ;
73- }
74- if (arg .equalsIgnoreCase ("portrait" )) {
75- isPortrait = true ;
76- }
65+ if (arg .startsWith ("width=" )) widthArg = Integer .parseInt (arg .substring (6 ));
66+ else if (arg .startsWith ("height=" )) heightArg = Integer .parseInt (arg .substring (7 ));
67+ else if (arg .equalsIgnoreCase ("portrait" )) portraitArg = true ;
68+ else if (arg .equalsIgnoreCase ("landscape" )) landscapeArg = true ;
7769 }
7870
79- // Check if the manually supplied window size indicates portrait mode
80- if ((windowHeight > 0 ) && (windowWidth > 0 )) {
81- if (windowHeight > windowWidth ) {
82- isPortrait = true ;
83- }
84- }
71+ boolean hasBothDims = widthArg != null && heightArg != null ;
72+ // Only disable desktop auto-orientation when the user *really* overrides it
73+ boolean overrideOrientation = portraitArg || landscapeArg || hasBothDims ;
74+ Forge .setDesktopAutoOrientation (!overrideOrientation );
75+
76+ // Determine desired portrait/landscape only if we are overriding orientation.
77+ boolean isPortrait = false ;
78+ if (portraitArg ) isPortrait = true ;
79+ else if (landscapeArg ) isPortrait = false ;
80+ else if (hasBothDims ) isPortrait = heightArg > widthArg ;
8581
8682 ApplicationListener start = Forge .getApp (hw , new Lwjgl3Clipboard (), new Main .DesktopAdapter (switchOrientationFile ),
87- assetsDir , false , isPortrait , totalRAM , false , 0 );
83+ assetsDir , false , isPortrait , false , 0 );
84+
85+ // Initialize window size
86+ int windowWidth , windowHeight ;
8887
89- // If no manual window size is supplied, use the configured one (and adjust for portrait mode if needed)
90- if (!manualWindowSize ) {
88+ if (widthArg != null || heightArg != null ) {
89+ float aspect = getPrimaryScreenAspect ();
90+
91+ // If explicit portrait/landscape requested, coerce aspect direction
92+ if (portraitArg && aspect > 1f ) aspect = 1f / aspect ;
93+ if (landscapeArg && aspect < 1f ) aspect = 1f / aspect ;
94+
95+ if (widthArg != null && heightArg == null ) {
96+ windowWidth = widthArg ;
97+ windowHeight = Math .max (1 , Math .round (windowWidth / aspect ));
98+ } else if (heightArg != null && widthArg == null ) {
99+ windowHeight = heightArg ;
100+ windowWidth = Math .max (1 , Math .round (windowHeight * aspect ));
101+ } else { // both provided
102+ windowWidth = widthArg ;
103+ windowHeight = heightArg ;
104+ }
105+ } else {
91106 windowWidth = Config .instance ().getSettingData ().width ;
92107 windowHeight = Config .instance ().getSettingData ().height ;
93- if (isPortrait && (windowHeight < windowWidth )) {
94- // swap width/height
95- int tmp = windowHeight ;
96- windowHeight = windowWidth ;
97- windowWidth = tmp ;
98- } else if (!isPortrait && (windowWidth < windowHeight )) {
99- // swap width/height
100- int tmp = windowHeight ;
101- windowHeight = windowWidth ;
102- windowWidth = tmp ;
103- }
108+ }
109+ // If user explicitly overrode orientation, normalize by swapping
110+ if (overrideOrientation && isPortrait == windowHeight < windowWidth ) {
111+ int tmp = windowHeight ; windowHeight = windowWidth ; windowWidth = tmp ;
104112 }
105113
106114 if (Config .instance ().getSettingData ().fullScreen ) {
@@ -138,4 +146,10 @@ public void focusLost() {
138146
139147 new Lwjgl3Application (start , config );
140148 }
149+
150+ private static float getPrimaryScreenAspect () {
151+ DisplayMode dm = Lwjgl3ApplicationConfiguration .getDisplayMode ();
152+ if (dm == null || dm .height == 0 ) return 16f / 9f ; // sane fallback
153+ return (float ) dm .width / (float ) dm .height ; // width/height
154+ }
141155}
0 commit comments