-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeway.pde
executable file
·307 lines (237 loc) · 9.58 KB
/
Timeway.pde
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.PrintWriter;
/**
*********************************** Timeway ***********************************
* Your computer is your universe.
*
* Visit https://teojt.github.io/timeway.html for more info on Timeway.
*
* Here's the basics of Timeway's code:
* Timeway is split into "screens", which you can think of as mini-apps.
* The most significant of them all is the Pixel Realm (where all the 3d file
* stuff is). There is also the Editor, and other miscellaneous screens in
* otherscreens.pde.
*
* Screens have their own states and variables which are cleared when the screen is
* no longer in use. All screens use Timeway's engine, which stores the entire
* application's state and provides shared functions for all screens to access
* (e.g. file management, sounds, displaying).
*
*
* Here's a quick rundown of what the other .pde files contain:
*
* pixelrealm_ui: Simply adds UI functionality to the base PixelRealm class since
* having it all in one class would make it bloated and extremely difficult to
* maintain.
*
* screen_startup: Screen that shows the Timeway logo and loading screen and does a
* few startup stuff.
*
* zAndroid and zDesktop: Timeway is designed to work on both Desktop and Android using
* the same codebase. However, there are obviously some big differences between the two,
* and libraries that exist in Android mode simply don't exist for Java (desktop) mode
* and vice verca.
* The solution? Have two scripts for each version.
* When you switch to Android mode, uncomment all the code in zAndroid and comment zDeskstop.
* When you switch to Java mode, uncomment all the code in zDesktop and comment zAndroid.
* Probably the worst system ever, but we like to keep it simple instead of spending ages
* looking for another solution.
*
* Anyways, I have no idea who's reading this or who would delve into the code of Timeway,
* but if you're here reading this, then have fun!
*
*
**/
TWEngine timewayEngine;
boolean sketch_showCrashScreen = false;
String sketch_ERR_LOG_PATH;
// Set to true if you want to show the error log like in an exported build
// rather than throw the error to processing (can be useful if you need more
// error info)
final boolean sketch_FORCE_CRASH_SCREEN = false;
final boolean sketch_MAXIMISE = true;
void settings() {
try {
// TODO... we're disabling graphics acceleration?!
//if (isLinux())
// System.setProperty("jogl.disable.openglcore", "true");
size(displayWidth, displayHeight, P2D);
//size(900, 1800, P2D);
//size(750, 1200, P2D);
smooth(1);
// Ugly, I know. But we're at the lowest level point in the program, so ain't
// much we can do.
final String iconLocation = "data/engine/img/icon.png";
File f = new File(sketchPath()+"/"+iconLocation);
if (f.exists()) {
setDesktopIcon(iconLocation);
}
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
shutdown();
}
}, "Shutdown-thread"));
}
catch (Exception e) {
minimalErrorDialog("A fatal error has occurred: \n"+e.getMessage()+"\n"+e.getStackTrace());
}
}
void shutdown() {
// TODO: Make this much better designed
timewayEngine.stats.save();
timewayEngine.stats.set("last_closed", (int)(System.currentTimeMillis() / 1000L));
if (timewayEngine.currScreen instanceof PixelRealmWithUI) {
PixelRealmWithUI pr = (PixelRealmWithUI)timewayEngine.currScreen;
pr.shutdown();
}
}
void sketch_openErrorLog(String mssg) {
// Write the file
try {
FileWriter myWriter = new FileWriter(sketch_ERR_LOG_PATH);
myWriter.write(mssg);
myWriter.close();
println(mssg);
} catch (IOException e2) {}
openErrorLog();
}
void sketch_openErrorLog(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String sStackTrace = sw.toString();
String errMsg =
"The Timeway engine experienced an unrecoverable problem and had to close the application :(\n\n\n"+
e.getClass().toString()+"\nMessage: \""+
e.getMessage()+"\"\nStack trace:\n"+
sStackTrace;
sketch_openErrorLog(errMsg);
}
// This is all the code you need to set up and start running
// the timeway engine.
void setup() {
hint(DISABLE_OPENGL_ERRORS);
background(0);
if (isAndroid()) {
//orientation(LANDSCAPE);
}
// Are we running in Processing or as an exported application?
File f1 = new File(sketchPath()+"/lib");
sketch_showCrashScreen = f1.exists();
//println("ShowcrashScreen: ", sketch_showCrashScreen);
sketch_ERR_LOG_PATH = sketchPath()+"/data/error_log.txt";
// Create the engine.
timewayEngine = new TWEngine(this);
// Set the starting point for the engine.
timewayEngine.startScreen(new Startup(timewayEngine));
// Engine is ready and going, set title.
surface.setTitle(timewayEngine.getAppName());
// Print a lil information to the terminal as a bonus :P
println("\n***** "+timewayEngine.getAppName()+" "+timewayEngine.getVersion()+" *****");
println("by Teo Taylor\n\n");
// Self-explainatory.
requestAndroidPermissions();
}
// The magical draw() method that runs it all. Be amazed :o
void draw() {
if (timewayEngine == null) {
// This shouldn't ever run but let's have it here to be safe
timewayEngine = new TWEngine(this);
}
else {
// Show error message on crash
if (sketch_showCrashScreen || sketch_FORCE_CRASH_SCREEN) {
try {
// Run Timeway.
timewayEngine.engine();
}
catch (java.lang.OutOfMemoryError outofmem) {
// Woops
sketch_openErrorLog("Timeway has run out of memory.");
exit();
}
catch (Exception e) {
// Open a text document containing the error message
sketch_openErrorLog(e);
// Then shut it all down.
exit();
}
}
// Run Timeway.
else timewayEngine.engine();
}
}
void keyPressed() {
if (timewayEngine != null && timewayEngine.input != null) {
timewayEngine.input.keyboardAction(key, keyCode);
timewayEngine.input.lastKeyPressed = key;
timewayEngine.input.lastKeycodePressed = keyCode;
// Begin the timer. This will automatically increment once it's != 0.
timewayEngine.input.keyHoldCounter = 1;
}
}
void keyReleased() {
// Stop the hold timer. This will no longer increment.
if (timewayEngine != null && timewayEngine.input != null) {
timewayEngine.input.keyHoldCounter = 0;
timewayEngine.input.releaseKeyboardAction(key, keyCode);
}
}
void mouseWheel(MouseEvent event) {
if (timewayEngine != null && timewayEngine.input != null) timewayEngine.input.rawScroll = event.getCount();
//println(event.scrollAmount());
//TODO: ifShiftDown is horizontal scrolling!
//println(event.isShiftDown());
//println(timewayEngine.rawScroll);
}
void outputFileSelected(File selection) {
if (timewayEngine != null) {
timewayEngine.file.outputFileSelected(selection);
}
}
void mouseClicked() {
if (timewayEngine != null && timewayEngine.input != null) timewayEngine.input.clickEventAction();
}
// Because TWEngine is designed to be isolated from the rest of... well, Timeweay,
// there are some things that TWEngine needs to access that are external to the engine,
// and isolating it would mean those external dependancies wouldn't exist.
// These methods handle these external dependencies required by the engine through
// void methods
//@SuppressWarnings("unused")
void twengineRequestEditor(String path) {
timewayEngine.requestScreen(new Editor(timewayEngine, path));
}
void twengineRequestReadonlyEditor(String path) {
timewayEngine.requestScreen(new ReadOnlyEditor(timewayEngine, path));
}
//@SuppressWarnings("unused")
void twengineRequestUpdater(JSONObject json) {
timewayEngine.requestScreen(new Updater(timewayEngine, json));
}
//@SuppressWarnings("unused")
void twengineRequestBenchmarks() {
//timewayEngine.requestScreen(new Updater(timewayEngine, json));
//timewayEngine.requestScreen(new Benchmark(this));
}
@SuppressWarnings("unused")
void twengineRequestSketch(String path) {
}
//@SuppressWarnings("unused")
boolean hasPixelrealm() {
return false;
}
//@SuppressWarnings("unused")
boolean pixelrealmCache() {
boolean cacheHit = false;
cacheHit |= timewayEngine.sound.cacheHit(timewayEngine.DEFAULT_DIR+"/"+PixelRealm.REALM_BGM+".wav");
cacheHit |= timewayEngine.sound.cacheHit(timewayEngine.DEFAULT_DIR+"/"+PixelRealm.REALM_BGM+".ogg");
cacheHit |= timewayEngine.sound.cacheHit(timewayEngine.DEFAULT_DIR+"/"+PixelRealm.REALM_BGM+".mp3");
cacheHit |= timewayEngine.sound.cacheHit(timewayEngine.DEFAULT_DIR+"/"+timewayEngine.file.unhide(PixelRealm.REALM_BGM+".wav"));
cacheHit |= timewayEngine.sound.cacheHit(timewayEngine.DEFAULT_DIR+"/"+timewayEngine.file.unhide(PixelRealm.REALM_BGM+".ogg"));
cacheHit |= timewayEngine.sound.cacheHit(timewayEngine.DEFAULT_DIR+"/"+timewayEngine.file.unhide(PixelRealm.REALM_BGM+".mp3"));
cacheHit |= timewayEngine.sound.cacheHit(timewayEngine.APPPATH+PixelRealm.REALM_BGM_DEFAULT);
cacheHit |= timewayEngine.sound.cacheHit(timewayEngine.APPPATH+PixelRealm.REALM_BGM_DEFAULT_LEGACY);
return cacheHit;
}