forked from kzahel/snes9x-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snes9x.js
executable file
·181 lines (147 loc) · 5.3 KB
/
snes9x.js
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
var reload = chrome.runtime.reload;
FS_SIZE = 1024*1024*50 // 50mb?
function gamepads() {
var conn = 'webkitGamepadConnected' // "gamepadconnected"
var disconn = 'webkitGamepadDisconnected' //gamepaddisconnected
window.addEventListener(conn, function(e) {
console.log("gamepadconnected: Gamepad connected at index %d: %s. %d buttons, %d axes.",
e.gamepad.index, e.gamepad.id,
e.gamepad.buttons.length, e.gamepad.axes.length);
});
window.addEventListener(disconn, function(e) {
console.log("gamepaddisconnected: Gamepad disconnected from index %d: %s",
e.gamepad.index, e.gamepad.id);
});
var interval;
if (!('ongamepadconnected' in window)) {
// No gamepad events available, poll instead.
console.log('setup polling for gamepads')
interval = setInterval(pollGamepads, 500);
}
function pollGamepads() {
var gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads : []);
for (var i = 0; i < gamepads.length; i++) {
var gp = gamepads[i];
if (gp) {
var info = "polling: Gamepad connected at index " + gp.index + ": " + gp.id +
". It has " + gp.buttons.length + " buttons and " + gp.axes.length + " axes.";
console.log('found gamepad',info,gp)
//gameLoop();
clearInterval(interval);
}
}
}
}
gamepads()
function renderROM(entry) {
var p = document.createElement('p')
var a = document.createElement('a')
a.href = '#'
a.innerText = entry.name
a.addEventListener('click', function(e) {
startGame(entry.name)
})
p.appendChild(a)
return p
}
function renderROMs() {
var div = document.getElementById('roms')
if (ROMS.length > 0) {
div.innerHTML = '<p>Previously entered ROMs</p>'
} else {
div.innerHTML = ''
}
for (var i=0;i<ROMS.length;i++) {
var p = renderROM(ROMS[i])
div.appendChild(p)
}
}
function maybeLoadROMStartup() {
if (false && window.ROMS.length > 0) {
startGame(window.ROMS[0].name)
}
}
function domContentLoaded() {
window.ROMS = []
webkitRequestFileSystem(window.PERSISTENT, FS_SIZE, function(fs) {
console.log('got pers fs')
var reader = fs.root.createReader()
console.log('reader',reader)
reader.readEntries( function(entries) {
console.log('entries',entries)
for (var i=0; i<entries.length; i++) {
if (entries[i].name != '.snes9x') {
ROMS.push(entries[i])
}
}
renderROMs()
maybeLoadROMStartup()
})
}, function(e) { console.error('error reading FS',e) } )
document.getElementById('romfile').addEventListener(
'change', handleFileSelect, false);
}
function handleFileSelect(evt) {
var file = evt.target.files[0];
var reader = new FileReader();
var result = null;
console.log('reader.readAsArrayBuffer');
reader.onloadend = onReadSuccess;
reader.onerror = errorHandler;
reader.readAsArrayBuffer(file);
function onReadSuccess() {
result = this.result;
console.log('window.webkitRequestFileSystem');
window.webkitRequestFileSystem(
window.PERSISTENT, FS_SIZE, onRequestQuotaSuccess, errorHandler);
}
function onRequestQuotaSuccess(fs) {
console.log('fs.root.getFile');
fs.root.getFile(file.name, {create: true}, onGetFileSuccess, errorHandler);
}
function onGetFileSuccess(fileEntry) {
console.log('fileEntry.createWriter');
fileEntry.createWriter(onCreateWriterSuccess, errorHandler);
}
function onCreateWriterSuccess(writer) {
console.log('writer.write');
writer.onwriteend = onWriteSuccess;
writer.onerror = errorHandler;
var blob = new Blob([result]);
writer.write(blob);
}
function onWriteSuccess() {
console.log('startGame: ' + file.name);
startGame(file.name);
}
}
function errorHandler(error) {
console.log('Error: ' + error);
}
function startGame(filename) {
// Width and height in pixels of the SNES screen.
var snesWidth = 256;
var snesHeight = 239;
var SCALE=2
var nacl = document.createElement('embed');
nacl.setAttribute('id', 'snes9x');
nacl.setAttribute('width', snesWidth * SCALE); // Scale screen by 2x.
nacl.setAttribute('height', snesHeight * SCALE);
nacl.setAttribute('PS_STDOUT','dev/tty');
nacl.setAttribute('PS_TTY_PREFIX','tty:');
nacl.setAttribute('PS_EXIT_MESSAGE','exit');
nacl.setAttribute('src', 'snes9x.nmf');
nacl.setAttribute('type', 'application/x-nacl');
nacl.setAttribute('ARG0', 'snes9x');
nacl.setAttribute('ARG1', filename);
nacl.setAttribute('ARG2', '-v1'); // Use "blocky" image scaling.
// Remove previous embed element.
document.getElementById('listener').innerHTML = '';
document.getElementById('listener').appendChild(nacl);
listener.addEventListener('message', function(message) {
if (typeof message.data == 'string' && message.data.startsWith('tty:')) {
console.log('%c ' + message.data.slice(4,message.data.length), 'color:#a32')
}
}, true);
}
document.addEventListener('DOMContentLoaded', domContentLoaded);