-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathDesktopLauncher.java
More file actions
287 lines (260 loc) · 9.27 KB
/
DesktopLauncher.java
File metadata and controls
287 lines (260 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.etheller.warsmash.desktop;
import static org.lwjgl.openal.AL10.AL_ORIENTATION;
import static org.lwjgl.openal.AL10.AL_POSITION;
import static org.lwjgl.openal.AL10.alListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.FloatBuffer;
import com.etheller.warsmash.WarsmashGdxMapScreen;
import com.etheller.warsmash.datasources.DataSource;
import org.lwjgl.BufferUtils;
import org.lwjgl.openal.AL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL31;
import org.lwjgl.opengl.GL32;
import org.lwjgl.opengl.GL33;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Graphics.DisplayMode;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.backends.lwjgl.LwjglNativesLoader;
import com.etheller.warsmash.WarsmashGdxFDFTestRenderScreen;
import com.etheller.warsmash.WarsmashGdxMenuScreen;
import com.etheller.warsmash.WarsmashGdxMultiScreenGame;
import com.etheller.warsmash.audio.OpenALSound;
import com.etheller.warsmash.units.DataTable;
import com.etheller.warsmash.units.Element;
import com.etheller.warsmash.util.StringBundle;
import com.etheller.warsmash.util.WarsmashConstants;
import com.etheller.warsmash.viewer5.AudioContext;
import com.etheller.warsmash.viewer5.AudioContext.Listener;
import com.etheller.warsmash.viewer5.AudioDestination;
import com.etheller.warsmash.viewer5.gl.ANGLEInstancedArrays;
import com.etheller.warsmash.viewer5.gl.AudioExtension;
import com.etheller.warsmash.viewer5.gl.DynamicShadowExtension;
import com.etheller.warsmash.viewer5.gl.Extensions;
import com.etheller.warsmash.viewer5.gl.WireframeExtension;
public class DesktopLauncher {
public static void main(final String[] arg) {
System.out.println("You ran it in working directory " + System.getProperty("user.dir"));
final LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.useGL30 = true;
config.gles30ContextMajorVersion = 3;
config.gles30ContextMinorVersion = 3;
// config.samples = 16;
// config.vSyncEnabled = false;
// config.foregroundFPS = 0;
// config.backgroundFPS = 0;
final DisplayMode desktopDisplayMode = LwjglApplicationConfiguration.getDesktopDisplayMode();
config.width = desktopDisplayMode.width;
config.height = desktopDisplayMode.height;
config.fullscreen = true;
String fileToLoad = null;
String iniPath = null;
boolean noLogs = false;
for (int argIndex = 0; argIndex < arg.length; argIndex++) {
if ("-window".equals(arg[argIndex])) {
config.fullscreen = false;
}
else if ("-nolog".equals(arg[argIndex])) {
noLogs = true;
}
else if ((arg.length > (argIndex + 1)) && "-loadfile".equals(arg[argIndex])) {
argIndex++;
fileToLoad = arg[argIndex];
}
else if ((arg.length > (argIndex + 1)) && "-ini".equals(arg[argIndex])) {
argIndex++;
iniPath = arg[argIndex];
}
}
if (!noLogs) {
new File("Logs").mkdir();
try {
System.setOut(new PrintStream(
new FileOutputStream(new File("Logs/" + System.currentTimeMillis() + ".out.log"))));
}
catch (final FileNotFoundException e) {
e.printStackTrace();
}
try {
System.setErr(new PrintStream(
new FileOutputStream(new File("Logs/" + System.currentTimeMillis() + ".err.log"))));
}
catch (final FileNotFoundException e) {
e.printStackTrace();
}
}
loadExtensions();
final DataTable warsmashIni = loadWarsmashIni(iniPath);
// Load icons:
DataSource codebase = WarsmashGdxMapScreen.parseDataSources(warsmashIni);
try {
config.addIcon(codebase.getFile("resources/Icon16.png").getAbsolutePath(), Files.FileType.Internal);
config.addIcon(codebase.getFile("resources/Icon32.png").getAbsolutePath(), Files.FileType.Internal);
config.addIcon(codebase.getFile("resources/Icon64.png").getAbsolutePath(), Files.FileType.Internal);
config.addIcon(codebase.getFile("resources/Icon128.png").getAbsolutePath(), Files.FileType.Internal);
} catch (final IOException e) {
e.printStackTrace();
}
final Element emulatorConstants = warsmashIni.get("Emulator");
if (emulatorConstants == null) {
System.err.println("Missing entry \"Emulator\" in .ini file.");
return;
}
WarsmashConstants.loadConstants(emulatorConstants, warsmashIni);
if (fileToLoad != null) {
System.out.println("About to run loading file: " + fileToLoad);
}
final WarsmashGdxMultiScreenGame warsmashGdxMultiScreenGame = new WarsmashGdxMultiScreenGame();
new LwjglApplication(warsmashGdxMultiScreenGame, config);
final String finalFileToLoad = fileToLoad;
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
if ((finalFileToLoad != null) && finalFileToLoad.toLowerCase().endsWith(".toc")) {
warsmashGdxMultiScreenGame.setScreen(new WarsmashGdxFDFTestRenderScreen(warsmashIni,
warsmashGdxMultiScreenGame, finalFileToLoad));
}
else {
final WarsmashGdxMenuScreen menuScreen = new WarsmashGdxMenuScreen(warsmashIni,
warsmashGdxMultiScreenGame);
warsmashGdxMultiScreenGame.setScreen(menuScreen);
if (finalFileToLoad != null) {
menuScreen.startMap(finalFileToLoad);
}
}
}
});
}
public static DataTable loadWarsmashIni(final String iniPath) {
final DataTable warsmashIni = new DataTable(StringBundle.EMPTY);
try (FileInputStream warsmashIniInputStream = new FileInputStream(iniPath != null ? iniPath : "warsmash.ini")) {
warsmashIni.readTXT(warsmashIniInputStream, true);
}
catch (final FileNotFoundException e) {
throw new RuntimeException(e);
}
catch (final IOException e) {
throw new RuntimeException(e);
}
return warsmashIni;
}
public static DataTable loadWarsmashIni() {
return loadWarsmashIni(null);
}
public static void loadExtensions() {
LwjglNativesLoader.load();
Extensions.angleInstancedArrays = new ANGLEInstancedArrays() {
@Override
public void glVertexAttribDivisorANGLE(final int index, final int divisor) {
GL33.glVertexAttribDivisor(index, divisor);
}
@Override
public void glDrawElementsInstancedANGLE(final int mode, final int count, final int type,
final int indicesOffset, final int instanceCount) {
GL31.glDrawElementsInstanced(mode, count, type, indicesOffset, instanceCount);
}
@Override
public void glDrawArraysInstancedANGLE(final int mode, final int first, final int count,
final int instanceCount) {
GL31.glDrawArraysInstanced(mode, first, count, instanceCount);
}
};
Extensions.dynamicShadowExtension = new DynamicShadowExtension() {
@Override
public void glFramebufferTexture(final int target, final int attachment, final int texture,
final int level) {
GL32.glFramebufferTexture(target, attachment, texture, level);
}
@Override
public void glDrawBuffer(final int mode) {
GL11.glDrawBuffer(mode);
}
};
Extensions.wireframeExtension = new WireframeExtension() {
@Override
public void glPolygonMode(final int face, final int mode) {
GL11.glPolygonMode(face, mode);
}
};
Extensions.audio = new AudioExtension() {
final FloatBuffer orientation = BufferUtils.createFloatBuffer(6).clear();
final FloatBuffer position = BufferUtils.createFloatBuffer(3).clear();
@Override
public float getDuration(final Sound sound) {
if (sound == null) {
return 1;
}
return ((OpenALSound) sound).duration();
}
@Override
public void play(final Sound buffer, final float volume, final float pitch, final float x, final float y,
final float z, final boolean is3dSound, final float maxDistance, final float refDistance,
final boolean looping) {
((OpenALSound) buffer).play(volume, pitch, x, y, z, is3dSound, maxDistance, refDistance, looping);
}
@Override
public AudioContext createContext(final boolean world) {
Listener listener;
if (world && AL.isCreated()) {
listener = new Listener() {
private float x;
private float y;
private float z;
@Override
public void setPosition(final float x, final float y, final float z) {
this.x = x;
this.y = y;
this.z = z;
position.put(0, x);
position.put(1, y);
position.put(2, z);
alListener(AL_POSITION, position);
}
@Override
public float getX() {
return this.x;
}
@Override
public float getY() {
return this.y;
}
@Override
public float getZ() {
return this.z;
}
@Override
public void setOrientation(final float forwardX, final float forwardY, final float forwardZ,
final float upX, final float upY, final float upZ) {
orientation.put(0, forwardX);
orientation.put(1, forwardY);
orientation.put(2, forwardZ);
orientation.put(3, upX);
orientation.put(4, upY);
orientation.put(5, upZ);
alListener(AL_ORIENTATION, orientation);
}
@Override
public boolean is3DSupported() {
return true;
}
};
}
else {
listener = Listener.DO_NOTHING;
}
return new AudioContext(listener, new AudioDestination() {
});
}
};
Extensions.GL_LINE = GL11.GL_LINE;
Extensions.GL_FILL = GL11.GL_FILL;
}
}