This repository has been archived by the owner on Jan 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 422
/
index.html
319 lines (300 loc) · 9.09 KB
/
index.html
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
308
309
310
311
312
313
314
315
316
317
318
319
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>GBA.js</title>
<link rel="stylesheet" href="resources/main.css">
<script src="js/util.js"></script>
<script src="js/core.js"></script>
<script src="js/arm.js"></script>
<script src="js/thumb.js"></script>
<script src="js/mmu.js"></script>
<script src="js/io.js"></script>
<script src="js/audio.js"></script>
<script src="js/video.js"></script>
<script src="js/video/proxy.js"></script>
<script src="js/video/software.js"></script>
<script src="js/irq.js"></script>
<script src="js/keypad.js"></script>
<script src="js/sio.js"></script>
<script src="js/savedata.js"></script>
<script src="js/gpio.js"></script>
<script src="js/gba.js"></script>
<script src="resources/xhr.js"></script>
<script>
var gba;
var runCommands = [];
var debug = null;
try {
gba = new GameBoyAdvance();
gba.keypad.eatInput = true;
gba.setLogger(function(level, error) {
console.log(error);
gba.pause();
var screen = document.getElementById('screen');
if (screen.getAttribute('class') == 'dead') {
console.log('We appear to have crashed multiple times without reseting.');
return;
}
var crash = document.createElement('img');
crash.setAttribute('id', 'crash');
crash.setAttribute('src', 'resources/crash.png');
screen.parentElement.insertBefore(crash, screen);
screen.setAttribute('class', 'dead');
});
} catch (exception) {
gba = null;
}
window.onload = function() {
if (gba && FileReader) {
var canvas = document.getElementById('screen');
gba.setCanvas(canvas);
gba.logLevel = gba.LOG_ERROR;
loadRom('resources/bios.bin', function(bios) {
gba.setBios(bios);
});
if (!gba.audio.context) {
// Remove the sound box if sound isn't available
var soundbox = document.getElementById('sound');
soundbox.parentElement.removeChild(soundbox);
}
if (window.navigator.appName == 'Microsoft Internet Explorer') {
// Remove the pixelated option if it doesn't work
var pixelatedBox = document.getElementById('pixelated');
pixelatedBox.parentElement.removeChild(pixelatedBox);
}
} else {
var dead = document.getElementById('controls');
dead.parentElement.removeChild(dead);
}
}
function fadeOut(id, nextId, kill) {
var e = document.getElementById(id);
var e2 = document.getElementById(nextId);
if (!e) {
return;
}
var removeSelf = function() {
if (kill) {
e.parentElement.removeChild(e);
} else {
e.setAttribute('class', 'dead');
e.removeEventListener('webkitTransitionEnd', removeSelf);
e.removeEventListener('oTransitionEnd', removeSelf);
e.removeEventListener('transitionend', removeSelf);
}
if (e2) {
e2.setAttribute('class', 'hidden');
setTimeout(function() {
e2.removeAttribute('class');
}, 0);
}
}
e.addEventListener('webkitTransitionEnd', removeSelf, false);
e.addEventListener('oTransitionEnd', removeSelf, false);
e.addEventListener('transitionend', removeSelf, false);
e.setAttribute('class', 'hidden');
}
function run(file) {
var dead = document.getElementById('loader');
dead.value = '';
var load = document.getElementById('select');
load.textContent = 'Loading...';
load.removeAttribute('onclick');
var pause = document.getElementById('pause');
pause.textContent = "PAUSE";
gba.loadRomFromFile(file, function(result) {
if (result) {
for (var i = 0; i < runCommands.length; ++i) {
runCommands[i]();
}
runCommands = [];
fadeOut('preload', 'ingame');
fadeOut('instructions', null, true);
gba.runStable();
} else {
load.textContent = 'FAILED';
setTimeout(function() {
load.textContent = 'SELECT';
load.onclick = function() {
document.getElementById('loader').click();
}
}, 3000);
}
});
}
function reset() {
gba.pause();
gba.reset();
var load = document.getElementById('select');
load.textContent = 'SELECT';
var crash = document.getElementById('crash');
if (crash) {
var context = gba.targetCanvas.getContext('2d');
context.clearRect(0, 0, 480, 320);
gba.video.drawCallback();
crash.parentElement.removeChild(crash);
var canvas = document.getElementById('screen');
canvas.removeAttribute('class');
} else {
lcdFade(gba.context, gba.targetCanvas.getContext('2d'), gba.video.drawCallback);
}
load.onclick = function() {
document.getElementById('loader').click();
}
fadeOut('ingame', 'preload');
}
function uploadSavedataPending(file) {
runCommands.push(function() { gba.loadSavedataFromFile(file) });
}
function togglePause() {
var e = document.getElementById('pause');
if (gba.paused) {
if (debug && debug.gbaCon) {
debug.gbaCon.run();
} else {
gba.runStable();
}
e.textContent = "PAUSE";
} else {
if (debug && debug.gbaCon) {
debug.gbaCon.pause();
} else {
gba.pause();
}
e.textContent = "UNPAUSE";
}
}
function screenshot() {
var canvas = gba.indirectCanvas;
window.open(canvas.toDataURL('image/png'), 'screenshot');
}
function lcdFade(context, target, callback) {
var i = 0;
var drawInterval = setInterval(function() {
i++;
var pixelData = context.getImageData(0, 0, 240, 160);
for (var y = 0; y < 160; ++y) {
for (var x = 0; x < 240; ++x) {
var xDiff = Math.abs(x - 120);
var yDiff = Math.abs(y - 80) * 0.8;
var xFactor = (120 - i - xDiff) / 120;
var yFactor = (80 - i - ((y & 1) * 10) - yDiff + Math.pow(xDiff, 1 / 2)) / 80;
pixelData.data[(x + y * 240) * 4 + 3] *= Math.pow(xFactor, 1 / 3) * Math.pow(yFactor, 1 / 2);
}
}
context.putImageData(pixelData, 0, 0);
target.clearRect(0, 0, 480, 320);
if (i > 40) {
clearInterval(drawInterval);
} else {
callback();
}
}, 50);
}
function setVolume(value) {
gba.audio.masterVolume = Math.pow(2, value) - 1;
}
function setPixelated(pixelated) {
var screen = document.getElementById('screen');
var context = screen.getContext('2d');
if (context.webkitImageSmoothingEnabled) {
context.webkitImageSmoothingEnabled = !pixelated;
} else if (context.mozImageSmoothingEnabled) {
context.mozImageSmoothingEnabled = !pixelated;
} else if (window.navigator.appName != 'Microsoft Internet Explorer') {
if (pixelated) {
screen.setAttribute('width', '240');
screen.setAttribute('height', '160');
} else {
screen.setAttribute('width', '480');
screen.setAttribute('height', '320');
}
if (window.navigator.appName == 'Opera') {
// Ugly hack! Ew!
if (pixelated) {
screen.style.marginTop = '0';
screen.style.marginBottom = '-325px';
} else {
delete screen.style;
}
}
}
}
function enableDebug() {
window.onmessage = function(message) {
if (message.origin != document.domain && (message.origin != 'file://' || document.domain)) {
console.log('Failed XSS');
return;
}
switch (message.data) {
case 'connect':
if (message.source == debug) {
debug.postMessage('connect', document.domain || '*');
}
break;
case 'connected':
break;
case 'disconnect':
if (message.source == debug) {
debug = null;
}
}
}
window.onunload = function() {
if (debug && debug.postMessage) {
debug.postMessage('disconnect', document.domain || '*');
}
}
if (!debug || !debug.postMessage) {
debug = window.open('debugger.html', 'debug');
} else {
debug.postMessage('connect', document.domain || '*');
}
}
document.addEventListener('webkitfullscreenchange', function() {
var canvas = document.getElementById('screen');
if (document.webkitIsFullScreen) {
canvas.setAttribute('height', document.body.offsetHeight);
canvas.setAttribute('width', document.body.offsetHeight / 2 * 3);
canvas.setAttribute('style', 'margin: 0');
} else {
canvas.setAttribute('height', 320);
canvas.setAttribute('width', 480);
canvas.removeAttribute('style');
}
}, false);
</script>
</head>
<body>
<canvas id="screen" width="480" height="320"></canvas>
<section id="controls">
<div id="preload">
<button class="bigbutton" id="select" onclick="document.getElementById('loader').click()">SELECT</button>
<input id="loader" type="file" accept=".gba" onchange="run(this.files[0]);">
<button onclick="document.getElementById('saveloader').click()">Upload Savegame</button>
<input id="saveloader" type="file" onchange="uploadSavedataPending(this.files[0]);">
</div>
<div id="ingame" class="hidden">
<button id="pause" class="bigbutton" onclick="togglePause()">PAUSE</button>
<button class="bigbutton" onclick="reset()">RESET</button>
<button onclick="gba.downloadSavedata()">Download Savegame</button>
<button onclick="screenshot()">Screenshot</button>
<label id="pixelated">
<input type="checkbox" onchange="setPixelated(this.checked)">
<p>Pixelated</p>
</label>
<div id="sound">
<input type="checkbox" checked onchange="gba.audio.masterEnable = this.checked">
<p>Sound</p>
<input type="range" min="0" max="1" value="1" step="any" onchange="setVolume(this.value)" oninput="setVolume(this.value)">
</div>
<p id="openDebug" onclick="enableDebug()">Debugger</p>
</div>
</section>
<ul id="links">
<li><a href="http://github.com/endrift/gbajs/">Fork me on GitHub!</a></li>
<li><a href="https://github.com/endrift/gbajs/wiki/Compatibility-List">Compatibility list</a></li>
</ul>
<footer>© 2012 – 2013 <a href="http://endrift.com/">Jeffrey Pfau</a></footer>
</body>
</html>