emulator/gba2/user_scripts/IodineGBACoreGlueCode.js
2019-03-05 20:53:03 +08:00

259 lines
8.1 KiB
JavaScript
Executable File

// "use strict";
/*
* This file is part of IodineGBA
*
* Copyright (C) 2012-2013 Grant Galitz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
* The full license is available at http://www.gnu.org/licenses/gpl.html
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
var KEY_MAP = {
ST: 13,
SE: 16,
A: [88, 74],
B: [90, 81, 89],
Y: 50,
X: 49,
AB: [88, 74,90, 81, 89],
UP: 38,
DOWN: 40,
LEFT: 37,
RIGHT: 39,
L_D: [37, 40],
L_U: [37, 38],
R_D: [39, 40],
R_U: [39, 38]
};
window.vm = {
oldDire: null,
keyEvent: function(t, e, i) {
var r = KEY_MAP[t];
Array.isArray(r) ? (vm.simulateKeyPress(r[0], e), vm.simulateKeyPress(r[1], e)) : vm.simulateKeyPress(r, e)
},
initCtrl: function() {
var s;
vm.initGameDireCtrl(function(t, e) {
var i = -1 == t ? s : ["DOWN", "L_D", "LEFT", "L_U", "UP", "R_U", "RIGHT", "R_D"][t],
r = -1 == t;
s && (s != i || r) && vm.keyEvent(s, "keyup", e), s = i, !r && vm.keyEvent(i, "keydown", e)
}), vm.initGameKeyCtrl(), $(".ctrl-wrap").on("click contextmenu touchstart touchmove touchend touchcancel tap", function(t) {
return t.stopPropagation(), t.preventDefault(), !1
})
},
initGameDireCtrl: function(u) {
var d = this,
t = $(".dire-wrap"),
p = $(".dc-centre"),
e = $(".dire-ctrl"),
l = $(".dc-dire"),
i = e.offset(),
n = i.top,
a = i.left,
f = p.width() / 2,
m = e.width() / 2,
r = function() {
p.css({
transform: "translate3d(83px,83px,0)"
}), p.css("opacity", "1"), l.css("opacity", "0")
},
y = function(t, e) {
var i = t[e];
if (!i) return {
x: 160,
y: 500
};
var r = i.pageX - a,
s = i.pageY - n;
return 600 < s ? y(t, e + 1) : {
x: r,
y: s
}
};
r(), t.on("touchstart touchmove", function(t) {
var e, i, r = y(t.touches, 0),
s = r.x,
n = r.y,
a = Math.atan2(n - m, s - m) / (Math.PI / 180);
if (l.css("transform", "rotate(" + a + "deg)"), d.onCtrlDire(a, u), (e = m - s, i = m - n, Math.sqrt(e * e + i * i)) <= 86) {
var o = s - f,
h = n - f;
p.css("transform", "translate3d(" + o + "px," + h + "px,0)")
} else {
var c = a * Math.PI / 180;
o = m + 86 * Math.cos(c) - f, h = m + 86 * Math.sin(c) - f;
p.css("transform", "translate3d(" + o + "px," + h + "px,0)")
}
"touchstart" === t.type && (p.css("opacity", "0.8"), l.css("opacity", "0.6"))
}).on("touchend", function() {
r(), u(-1, 0), d.oldDire = -1
})
},
initGameKeyCtrl: function() {
var s = this;
$(".key-btn").bind("touchstart touchend", function(t) {
var e = $(t.target),
i = e.attr("data-key"),
r = "touchstart" === t.type;
s.keyEvent(i, r ? "keydown" : "keyup", 0), e[r ? "addClass" : "removeClass"]("btn-on")
})
},
onCtrlDire: function(t, e) {
this.onCtrlDire;
var i = Math.round((t + 180) / 45) % 8;
this.oldDire != i && (e(i, t), this.oldDire = i)
},
simulateKeyPress: function(t, e) {
if(e=="keydown"){
keyDown({keyCode:t});
}
else{
keyUpPreprocess({keyCode:t});
}
// var i = $.Event(e);
// i.keyCode = t, i.which = i.keyCode, $(window).trigger(i)
}
};
var Iodine = null;
var Blitter = null;
var Mixer = null;
var MixerInput = null;
var timerID = null;
window.onload = function () {
// if (!games[location.hash.substr(1)]) {
// alert("Invalid game request!");
// return;
// }
//Initialize Iodine:
Iodine = new GameBoyAdvanceEmulator();
//Initialize the graphics:
registerBlitterHandler();
//Initialize the audio:
registerAudioHandler();
//Register the save handler callbacks:
registerSaveHandlers();
//Hook the GUI controls.
registerGUIEvents();
//Enable Sound:
Iodine.enableAudio();
//Download the BIOS:
downloadBIOS();
vm.initCtrl()
}
function getParameter (t) {
var e = window.location.search,
i = new RegExp(t + "=([^&?]*)", "ig");
return e.match(i) ? e.match(i)[0].substr(t.length + 1) : null
}
function downloadBIOS() {
downloadFile("Binaries/gba_bios.bin", registerBIOS);
}
function registerBIOS() {
processDownload(this, attachBIOS);
downloadROM();
}
function downloadROM() {
Iodine.pause();
showTempString("Downloading");
var gameId = getParameter("id")
var path = "http://h5games-al.kingsome.cn/emulator-static/roms/" + gameId + ".zip"
downloadFileRemote(path,registerROM2);
// downloadFile("Binaries/" + gamename + ".gba", registerROM);
}
function attachROM2(ROM) {
JSZip.loadAsync(ROM).then((zip)=>{
console.log(zip)
for (var x in zip.files) {
zip.file(x).async("uint8array").then((gamedt)=>{
Iodine.attachROM(gamedt);
Iodine.play();
});
break;
}
})
}
function registerROM2() {
clearTempString();
processDownload(this, attachROM2);
// if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)) {
// Iodine.disableAudio();
// }
// Iodine.disableAudio();
// Iodine.play();
}
// function registerROM() {
// clearTempString();
// processDownload(this, attachROM);
// if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)) {
// Iodine.disableAudio();
// }
// // Iodine.disableAudio();
// Iodine.play();
// }
function registerBlitterHandler() {
Blitter = new GlueCodeGfx();
Blitter.attachCanvas(document.getElementById("screen"));
Blitter.setSmoothScaling(false);
Iodine.attachGraphicsFrameHandler(function (buffer) {Blitter.copyBuffer(buffer);});
}
function registerAudioHandler() {
Mixer = new GlueCodeMixer();
MixerInput = new GlueCodeMixerInput(Mixer);
Iodine.attachAudioHandler(MixerInput);
}
function registerGUIEvents() {
addEvent("keydown", document, keyDown);
addEvent("keyup", document, keyUpPreprocess);
addEvent("unload", window, ExportSave);
Iodine.attachSpeedHandler(function (speed) {
document.title = ""
});
}
function lowerVolume() {
Iodine.incrementVolume(-0.04);
}
function raiseVolume() {
Iodine.incrementVolume(0.04);
}
function writeRedTemporaryText(textString) {
if (timerID) {
clearTimeout(timerID);
}
showTempString(textString);
timerID = setTimeout(clearTempString, 5000);
}
function showTempString(textString) {
document.getElementById("tempMessage").style.display = "block";
document.getElementById("tempMessage").textContent = textString;
}
function clearTempString() {
document.getElementById("tempMessage").style.display = "none";
}
//Some wrappers and extensions for non-DOM3 browsers:
function addEvent(sEvent, oElement, fListener) {
try {
oElement.addEventListener(sEvent, fListener, false);
}
catch (error) {
oElement.attachEvent("on" + sEvent, fListener); //Pity for IE.
}
}
function removeEvent(sEvent, oElement, fListener) {
try {
oElement.removeEventListener(sEvent, fListener, false);
}
catch (error) {
oElement.detachEvent("on" + sEvent, fListener); //Pity for IE.
}
}