windows95/src/renderer/renderer.js

100 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-08-23 07:03:28 +02:00
const BUTTONS = document.querySelector('#buttons')
2018-08-23 07:25:17 +02:00
let cursorCaptured = false
2018-08-23 04:51:31 +02:00
const OPTIONS = {
win95: {
hda: {
url: './images/windows95.img',
async: true,
2018-08-23 07:25:17 +02:00
size: 242049024
2018-08-23 04:51:31 +02:00
}
}
}
2018-08-23 07:25:17 +02:00
async function main (id) {
2018-08-23 04:51:31 +02:00
const opts = Object.assign({
memory_size: 64 * 1024 * 1024,
screen_container: document.getElementById('emulator'),
bios: {
2018-08-23 07:25:17 +02:00
url: './bios/seabios.bin'
2018-08-23 04:51:31 +02:00
},
vga_bios: {
2018-08-23 07:25:17 +02:00
url: './bios/vgabios.bin'
2018-08-23 07:03:28 +02:00
}
2018-08-23 04:51:31 +02:00
}, OPTIONS[id])
2018-08-23 07:03:28 +02:00
// New v86 instance
2018-08-23 04:51:31 +02:00
window.emulator = new V86Starter(opts)
2018-08-23 07:03:28 +02:00
// Restore state. We can't do this right away.
setTimeout(async () => {
await windows95.restoreState()
cursorCaptured = true
window.emulator.lock_mouse()
window.emulator.run()
}, 500)
2018-08-23 04:51:31 +02:00
}
2018-08-23 07:25:17 +02:00
function setupButtons () {
2018-08-23 07:14:50 +02:00
document.querySelectorAll('.btn-start').forEach((btn) => {
2018-08-23 04:51:31 +02:00
btn.addEventListener('click', () => {
2018-08-23 07:03:28 +02:00
BUTTONS.remove()
2018-08-23 07:25:17 +02:00
document.body.className = ''
2018-08-23 04:51:31 +02:00
main(btn.id)
})
})
2018-08-23 07:03:28 +02:00
document.querySelector('#reset').addEventListener('click', () => {
2018-08-23 07:14:50 +02:00
if (window.emulator.stop) {
2018-08-23 07:03:28 +02:00
window.emulator.stop()
}
windows95.resetState()
2018-08-23 07:14:50 +02:00
if (window.emulator.run) {
2018-08-23 07:03:28 +02:00
window.emulator.run()
}
2018-08-23 07:25:17 +02:00
document.querySelector('#reset').disabled = true
2018-08-23 07:03:28 +02:00
})
}
2018-08-23 07:25:17 +02:00
function setupEscListener () {
document.onkeydown = function (evt) {
evt = evt || window.event
if (evt.keyCode === 27) {
2018-08-23 07:03:28 +02:00
if (cursorCaptured) {
cursorCaptured = false
document.exitPointerLock()
} else {
cursorCaptured = true
window.emulator.lock_mouse()
}
}
}
}
2018-08-23 07:25:17 +02:00
function setupCloseListener () {
2018-08-23 07:03:28 +02:00
let isQuitting = false
const handleClose = async () => {
await windows95.saveState()
isQuitting = true
windows95.quit()
}
window.onbeforeunload = (event) => {
if (isQuitting) return
handleClose()
event.preventDefault()
event.returnValue = false
}
2018-08-23 04:51:31 +02:00
}
2018-08-23 07:03:28 +02:00
setupEscListener()
setupCloseListener()
2018-08-23 04:51:31 +02:00
setupButtons()