-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathplay.html
103 lines (97 loc) · 3.77 KB
/
play.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
<body>
<div id='game'>
<div class='heading'><h1 id='title'></h1><span id='by'></span></div>
<div id='play'><canvas id='target' width=512 height=256></canvas></div>
<div id='meta'>
<div>Released: <span id='release'></span></div>
<div>Platform: <span id='platform'></span></div>
<div><a id='download' href='#'>Download Rom</a></div>
<div><a id='source' href='#'>Source and Details</a></div>
</div>
</div>
</body>
<style>
body{font-family:Helvetica;margin-left:10%;margin-right:10%;margin-top:5%;}
h1,#meta>div{font-family:Futura;display:inline-block;margin:20px;}
.heading,#meta{background:gray;}
canvas{border:1px solid black;}
#game{background:gray;border-radius:14px;background:lightgray;overflow:hidden;}
#play{display:flex;justify-content:center;padding:20px;}
#meta{display:flex;justify-content:space-between;}
</style>
<script src='https://johnearnest.github.io/Octo/js/emulator.js'></script>
<script src='https://johnearnest.github.io/Octo/js/shared.js'></script>
<script src='https://johnearnest.github.io/Octo/js/input.js'></script>
<script>
id=x=>document.querySelector('#'+x)
send=(u,t)=>{let r=new XMLHttpRequest();return r.open('GET',u),r.responseType=t,r.send(),r}
ajax=(u,t,f)=>{let r=send(u,t);r.onreadystatechange=_=>r.readyState==4&&f(r.response)}
const emulator = new Emulator()
function run(options,rom) {
unpackOptions(emulator, options)
setRenderTarget(4, 'target')
emulator.init(rom)
emulator.importFlags = _=> JSON.parse(localStorage.getItem('octoFlagRegisters'))
emulator.exportFlags = x=> localStorage.setItem('octoFlagRegisters', JSON.stringify(x))
try { localStorage.getItem('octoFlagRegisters') }
catch(e) {
console.warn("Persistent flag register storage is unavailable! Flag registers will not be saved across sessions.")
emulator.importFlags = _ => emulator.flags
emulator.exportFlags = x => emulator.flags = x
}
emulator.buzzTrigger = (ticks,rest)=> playPattern(ticks, emulator.pattern, rest)
const kd = e=>{
if (!audio) audioSetup(emulator)
if (!(e.key in emulator.keys)) emulator.keys[e.key]=true
e.preventDefault()
}
const ku = e=>{
if (e.key in emulator.keys) delete emulator.keys[e.key]
if (!emulator.waiting) return
const kindex = keymapInverse[e.key]
if (kindex != undefined) {
emulator.waiting = false
emulator.v[emulator.waitReg] = kindex
}
e.preventDefault()
}
window.addEventListener('keydown',kd,false)
window.addEventListener('keyup',ku,false)
const frameTime=1000/60
let last=Date.now(), origin=last+frameTime/2
intervalHandle=setInterval(_=>{
last+=(Date.now()-last)
if (emulator.halted) return
for(var k=0; origin<last-frameTime&&k<2; origin+=frameTime,k++){
for(var z=0;(z<emulator.tickrate) && (!emulator.waiting); z++){
if (emulator.vBlankQuirks &&((emulator.m[emulator.pc] & 0xF0)==0xD0)) z=emulator.tickrate
emulator.tick()
}
if (emulator.dt > 0) emulator.dt--
if (emulator.st > 0) emulator.st--
XOAudio && XOAudio.refresh(frameTime/1000)
}
renderDisplay(emulator)
id('game').style.backgroundColor = emulator.st?emulator.buzzColor:emulator.quietColor
}, frameTime)
injectAdaptiveControls(emulator.touchInputMode,document.getElementById('target'),ku,kd)
}
ajax('programs.json','',x=>{
const slug=new URLSearchParams(document.location.search).get('p')
const g=JSON.parse(x)[slug]
id('title').innerHTML=g.title
id('release').innerHTML=g.release||'Unknown'
id('platform').innerHTML=g.platform||'Unknown'
id('download').href='roms/'+slug+'.ch8'
id('source').href='https://github.com/JohnEarnest/chip8Archive/tree/master/src/'+slug
ajax('authors.json','',x=>{
const a=JSON.parse(x)
id('by').innerHTML='by '+g.authors.map(
x=>a[x].url?`<a href='${a[x].url}'>${x}</a>`:x
).join(', ')
})
ajax('roms/'+slug+'.ch8','arraybuffer',x=>{
run(g.options||{},{rom:new Uint8Array(x)})
})
})
</script>