-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
131 lines (109 loc) · 2.84 KB
/
Copy pathapp.js
File metadata and controls
131 lines (109 loc) · 2.84 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
// =============================================================================
// Name: Radroach Races
// Author: Theeohn Megistus
// License: MIT
// Repository: https://github.com/Theeohn/Radroach-Races-3000a
// =============================================================================
// File: APP.JS (main, scene manager)
// Description: This is the main shell for the holotape, a "scene" manager
// that swaps scenes in and out as needed to keep memory usage low.
// =============================================================================
(function () {
const HOLOTAPE_DIR = 'HOLO/RADROACH_RACES/';
let scene = null; // Current scene
let pending = null; // Next scene queued
let loadTimer = null; // Timer for deferred scene switch
const app = {
scenes: {
RACE: 'RACE.JS',
TITLE: 'TITLE.JS',
},
// Go to scene
go: function (file, params) {
pending = { file: file, params: params };
if (!loadTimer) {
// Defer a tick to clear and load scene safely.
loadTimer = setTimeout(loadScene, 0);
}
},
// Garbage collect
gc: function () {
try {
// Garbage collect memory
// https://www.espruino.com/Reference#l_process_memory
process.memory(true); // true = GC
// Defrag memory
// https://www.espruino.com/Reference#l_E_defrag
E.defrag(); // Probably overkill, but safe
} catch (e) {}
},
};
/**
* Draw a scene load error
* @param file The scene file path
* @returns {void} Nothing
*/
function drawError(file) {
h.clear(0);
h.setColor(3)
.setFontMonofonto23()
.setFontAlign(0, 0)
.drawString('UNABLE TO LOAD', 240, 140);
h.setColor(2)
.setFontMonofonto16()
.setFontAlign(0, 0)
.drawString(file, 240, 172);
h.setColor(1)
.setFontMonofonto14()
.setFontAlign(0, 0)
.drawString('Try Reinstalling Holotape.', 240, 204);
h.flip();
Pip.lastFlip = getTime();
}
/**
* Load the next scene and remove the current scene
* @returns {void} Nothing
*/
function loadScene() {
loadTimer = null;
const p = pending;
pending = null;
if (!p) {
return;
}
if (scene) {
scene.remove();
scene = null;
}
app.gc();
try {
// Load scene
scene = eval(fs.readFileSync(HOLOTAPE_DIR + p.file))(app, p.params);
} catch (e) {
drawError(p.file);
return;
}
}
function remove() {
if (loadTimer) {
clearTimeout(loadTimer);
loadTimer = null;
}
pending = null;
if (scene) {
scene.remove();
scene = null;
}
Pip.audioStop();
h.clear();
h.flip();
}
// Start with the title scene
app.go(app.scenes.TITLE);
return {
id: 'radroachraces',
notDefault: true,
fullscreen: true,
remove: remove,
};
});