Skip to content

Instantly share code, notes, and snippets.

@ybootin ybootin/README.md
Last active Sep 15, 2016

Embed
What would you like to do?
A simple loader for Mamejs emulator - run shinobi
<!doctype html>
<html>
<head></head>
<body>
<h1>Proof Of Concept : a simple MAME loader</h1>
<div id="loading">Loading game, please wait !</div>
<div class="container">
<canvas id="display" style="width:320px;height:224px" width="320" height="224"></canvas>
</div>
<div id="console"></div>
<script>
// legacy, must be define
var JSMESS = JSMESS || {};
JSMESS.ready = function (f) { f(); };
var game = {
files : {
'shinobi.zip': 'https://cors.archive.org/cors/arcade_shinobi/shinobi.zip',
//'snowbros.zip': '../emulators/roms/snowbros.zip',
//'twincobr.zip': '../emulators/roms/twincobr.zip',
//'bublbobl.zip': '../emulators/bublbobl.zip',
//'shinobi.zip': '../emulators/roms/shinobi.zip',
//'sf2.zip' : '../emulators/roms/sf2.zip',
//'ghouls.zip' : '../emulators/cps1/ghouls.zip',
//'outrun.zip' : '../emulators/outrun/outrun.zip',
//'outrun.cfg' : '../emulators/outrun/outrun.cfg',
//'outrun.zip': 'https://cors.archive.org/cors/arcade_outrun/outrun.zip',
},
driver: 'shinobi',
emulator: 'https://rawgit.com/mamejs/mamejs-emulators/master/mamejs-serie1.js',
resolution: {
width: 320,
height: 224
}
}
var canvas = document.getElementById('display')
function fetchFile(url) {
return new Promise(function (resolve, reject) {
let xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
let errorMsg = 'error loading ' + url
xhr.onload = function (e) {
if (xhr.status === 200) {
resolve(xhr.response);
} else {
reject(errorMsg + ' : status code ' + xhr.status);
}
}
xhr.onerror = function (e) {
reject(errorMsg + ' : ' + e.toString());
};
xhr.send();
});
}
// Fetch all game files
Promise.all(Object.keys(game.files).map(function(romName) {
return fetchFile(game.files[romName]).then(function(arrayBuffer) {
return {
rom: romName,
data: arrayBuffer
}
})
})).then(function(data) { // Promise.all will return an array of {rom: string, data: ArrayBuffer}
// MAME Module for emscripten
window.Module = {
arguments: [//'-listfull'
game.driver,
'-verbose',
'-rompath',
'/roms',
'-resolution',
game.resolution.width + 'x' + game.resolution.height,
'-samplerate',
'48000',
// '-sound',
// 'none',
],
screenIsReadOnly: true,
print: function (text) {
if (!window.mameConsole) {
window.mameConsole = []
}
window.mameConsole.push(text)
// var console = document.getElementById('console')
// var output = console.innerHTML
// output += text.replace(/</g, "&lt;").replace(/>/g, "&gt;")
// console.innerHTML = output
},
canvas: display,
noInitialRun: false,
preInit: function () {
// create and mount FS into /roms folder
FS.mkdir('/roms');
FS.mount(MEMFS, {root: '/'}, '/roms');
// write all game files into the /roms folder
data.forEach(function(file) {
FS.writeFile('/roms/' + file.rom, new Uint8Array(file.data), {
encoding: 'binary'
})
})
},
onRuntimeInitialized: function() {
Module.setCanvasSize(game.resolution.width, game.resolution.height)
}
}
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = game.emulator;
document.getElementsByTagName('head')[0].appendChild(script);
}).catch(function(error) {
document.getElementById('loading').innerHTML = 'error : ' + error
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.