-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathappEntry.js
More file actions
235 lines (178 loc) · 7.61 KB
/
Copy pathappEntry.js
File metadata and controls
235 lines (178 loc) · 7.61 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
var VBAGraphics = require("./Graphics");
var VBASound = require("./Sound");
var VBASaves = require("./Saves");
var VBAInput = require("./Input");
var VBAUI = require("./UI");
var isRunning = false;
var isPaused = false;
window.init = function () {
document.querySelector(".pixels").innerHTML = '<canvas width="240" height="160"></canvas>';
window.vbaGraphics = new VBAGraphics(window.gbaninja, document.querySelector("canvas"));
var res = window.vbaGraphics.initScreen();
if (!res) {
window.vbaGraphics = null;
document.querySelector(".pixels").innerHTML = "<p style='margin: 20px;'>You need to enable WebGL</p>";
gtag("event", "webgl_disabled_at_init_1", {});
return;
}
window.vbaGraphics.drawFrame();
window.vbaSound = new VBASound(window.gbaninja);
window.vbaSaves = new VBASaves(window.gbaninja);
window.vbaInput = new VBAInput(window.gbaninja);
window.vbaUI = new VBAUI(document.querySelector(".ui"));
document.querySelector(".pixels").style.display = "none";
if (!qs.exclusive) {
document.querySelector(".ui").style.display = "block";
}
vbaUI.reset();
window.doPerfCalc();
};
window.start = function () {
if (window.isRunning) {
throw new Error("Already started");
}
if (!window.vbaGraphics) {
// webgl is disabled
gtag("event", "webgl_disabled_at_start_1", {});
return;
}
document.querySelector(".pixels").style.display = "block";
document.querySelector(".ui").style.display = "none";
var onResize = window.vbaGraphics.onResize.bind(window.vbaGraphics, window.innerWidth, window.innerHeight);
window.onresize = onResize;
onResize();
VBAInterface.VBA_start();
gtag("event", "run_rom_1", {
event_label: window.vbaSaves.getRomCode() + " " + require("./romCodeToEnglish")(window.vbaSaves.getRomCode()),
});
isRunning = true;
window.focusCheck();
window.doTimestep(window.frameNum + 1);
};
var GBA_CYCLES_PER_SECOND = 16777216;
var TARGET_FRAMERATE = 500;
window.lastFrameTime = window.performance.now();
window.frameTimeout = null;
window.animationFrameRequest = null;
window.frameNum = 1;
window.lastFocusTime = 0;
window.vbaPerf = {};
vbaPerf.deltaTimesThisSecond = [];
vbaPerf.cyclesThisSecond = [];
vbaPerf.renderDeadlineResultsThisSecond = [];
vbaPerf.spareAudioSamplesThisSecond = [];
vbaPerf.audioDeadlineResultsThisSecond = [];
window.doTimestep = function (frameNum, mustRender) {
if (!hasEmuModule()) {
return;
}
if (frameNum !== window.frameNum + 1) {
return;
}
window.frameNum = frameNum;
var currentTime = window.performance.now();
var deltaTime = currentTime - lastFrameTime;
var clampedDeltaTime = Math.min(50, deltaTime);
if (currentTime - window.lastFocusTime > 100 || deltaTime < 0.1) {
window.animationFrameRequest = window.requestAnimationFrame(function () {
window.doTimestep(frameNum + 1);
});
return;
}
lastFrameTime = currentTime;
if (isRunning) {
vbaSaves.checkSaves();
var cyclesToDo = Math.floor(GBA_CYCLES_PER_SECOND / (1000 / clampedDeltaTime));
if (vbaSound.spareSamplesAtLastEvent > 1000) {
cyclesToDo -= Math.floor(Math.min(cyclesToDo * 0.03, GBA_CYCLES_PER_SECOND / 10000));
}
if (vbaSound.spareSamplesAtLastEvent < 700) {
cyclesToDo += Math.floor(Math.min(cyclesToDo * 0.03, GBA_CYCLES_PER_SECOND / 10000));
}
if (!isPaused) {
VBAInterface.VBA_do_cycles(cyclesToDo);
}
vbaPerf.deltaTimesThisSecond.push(deltaTime);
vbaPerf.cyclesThisSecond.push(cyclesToDo);
clearTimeout(window.frameTimeout);
window.frameTimeout = setTimeout(function () {
window.doTimestep(frameNum + 1);
}, 1000 / TARGET_FRAMERATE);
cancelAnimationFrame(window.animationFrameRequest);
window.animationFrameRequest = window.requestAnimationFrame(function () {
window.doTimestep(frameNum + 1);
});
} else if (VBAInterface.VBA_get_emulating()) {
VBAInterface.VBA_stop();
document.querySelector(".pixels").style.display = "none";
document.querySelector(".ui").style.display = "block";
}
};
window.hasRequestedFrameButNotRendered = false;
window.focusCheck = function () {
window.lastFocusTime = window.performance.now();
window.hasRequestedFrameButNotRendered = true;
window.requestAnimationFrame(window.focusCheck);
};
window.perfTimer = null;
window.lastPerfTime = performance.now();
window.doPerfCalc = function () {
clearTimeout(window.perfTimer);
var currentTime = window.performance.now();
var deltaTime = currentTime - lastPerfTime;
window.lastPerfTime = currentTime;
if (hasEmuModule() && window.vbaInput.isKeyDown(vbaInput.bindings.PERF_STATS)) {
document.querySelector(".perf").style.display = "block";
function samplesToMillis (samples) {
return Math.floor(samples / window.vbaSound.getSampleRate() * 1000) + "ms";
}
var romCode = window.vbaSaves.getRomCode();
var sumCycles = vbaPerf.cyclesThisSecond.reduce(function (a, b) { return a + b; }, 0);
var maxAudioSamples = vbaPerf.spareAudioSamplesThisSecond.reduce(function (a, b) { return Math.max(a, b); }, 0);
var minAudioSamples = vbaPerf.spareAudioSamplesThisSecond.reduce(function (a, b) { return Math.min(a, b); }, Infinity);
if (minAudioSamples === Infinity) {
minAudioSamples = 0;
}
var audioDeadlineResults = vbaPerf.audioDeadlineResultsThisSecond.reduce(function (a, b) {
if (b) {
a.hit++;
} else {
a.miss++;
}
return a;
}, {hit: 0, miss: 0});
var renderDeadlineResults = vbaPerf.renderDeadlineResultsThisSecond.reduce(function (a, b) {
if (b) {
a.hit++;
} else {
a.miss++;
}
return a;
}, {hit: 0, miss: 0});
document.querySelector(".perf-game").innerText = (romCode ? (romCode + " ") : "") + require("./romCodeToEnglish")(romCode);
document.querySelector(".perf-timesteps").innerText = Math.round(vbaPerf.cyclesThisSecond.length / (deltaTime / 1000));
document.querySelector(".perf-percentage").innerText = (sumCycles / (GBA_CYCLES_PER_SECOND * (deltaTime / 1000)) * 100).toFixed(1) + "%";
document.querySelector(".perf-audio-lag").innerText = samplesToMillis(minAudioSamples) + " - " + samplesToMillis(maxAudioSamples);
document.querySelector(".perf-audio-deadlines").innerText = audioDeadlineResults.hit + " / " + (audioDeadlineResults.hit + audioDeadlineResults.miss);
document.querySelector(".perf-render-deadlines").innerText = renderDeadlineResults.hit + " / " + (renderDeadlineResults.hit + renderDeadlineResults.miss);
} else {
document.querySelector(".perf").style.display = "none";
}
vbaPerf.cyclesThisSecond.length = 0;
vbaPerf.deltaTimesThisSecond.length = 0;
vbaPerf.renderDeadlineResultsThisSecond.length = 0;
vbaPerf.spareAudioSamplesThisSecond.length = 0;
vbaPerf.audioDeadlineResultsThisSecond.length = 0;
window.perfTimer = setTimeout(window.doPerfCalc, 1000);
};
window.togglePause = function () {
if (!isRunning) {
return;
}
isPaused = !isPaused;
document.querySelector(".ui").style.display = isPaused ? "block" : "none";
window.vbaUI.setPausedState(isPaused);
};
window.scheduleStop = function () {
isRunning = false;
};