-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsbp.min.js
14 lines (14 loc) · 7.11 KB
/
jsbp.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
JSBP={MEMSIZE:16777224,running:!1,tickId:0,speed:1E3/60,mem:null,init:function(){console.time("Allocating 16MiB");JSBP.mem=new Uint8Array(JSBP.MEMSIZE);console.timeEnd("Allocating 16MiB");JSBP.screen.init();JSBP.audio.init();JSBP.input.init();JSBP.gui.init();JSBP.demos.init()},tick:function(){JSBP.core.tick();JSBP.screen.tick();JSBP.audio.tick()},start:function(){JSBP.running||(JSBP.tickId=setInterval(JSBP.tick,JSBP.speed),JSBP.audio.node.connect(JSBP.audio.context.destination),JSBP.running=!0)},
stop:function(){JSBP.running&&(clearInterval(JSBP.tickId),JSBP.audio.node.disconnect(),JSBP.running=!1)},toggle:function(){JSBP.running?JSBP.stop():JSBP.start()},setSpeed:function(a){JSBP.speed=a;JSBP.running&&(clearInterval(JSBP.tickId),JSBP.tickId=setInterval(JSBP.tick,a))}};$(JSBP.init);JSBP.core={};JSBP.core.first=!0;JSBP.core.tick=function(){for(var a=JSBP.mem[2]<<16|JSBP.mem[3]<<8|JSBP.mem[4],b,c,d=655360;--d;)b=JSBP.mem[a]<<16|JSBP.mem[a+1]<<8|JSBP.mem[a+2],c=JSBP.mem[a+3]<<16|JSBP.mem[a+4]<<8|JSBP.mem[a+5],a=JSBP.mem[a+6]<<16|JSBP.mem[a+7]<<8|JSBP.mem[a+8],JSBP.mem[c]=JSBP.mem[b]};
JSBP.core.loadArrayBuffer=function(a){console.time("Loading from ArrayBuffer");for(var b=new DataView(a),c=0;c<a.byteLength;c++)JSBP.mem[c]=b.getUint8(c);for(c=a.byteLength;c<JSBP.MEMSIZE;c++)JSBP.mem[c]=0;console.timeEnd("Loading from ArrayBuffer");JSBP.core.first&&(JSBP.core.first=!1,JSBP.gui.hideOverlay(),JSBP.start())};
JSBP.core.loadFile=function(a){console.time("Reading file "+a.name+" to ArrayBuffer");var b=new FileReader;b.onload=function(c){c=c.target.result;console.timeEnd("Reading file "+a.name+" to ArrayBuffer");JSBP.core.loadArrayBuffer(c)};b.readAsArrayBuffer(a)};JSBP.screen={};JSBP.screen.palette=new Uint32Array(256);
JSBP.screen.init=function(){JSBP.screen.context=$("#canvas")[0].getContext("2d");console.time("Initiating fast per-pixel drawing");JSBP.screen.image=JSBP.screen.context.getImageData(0,0,256,256);JSBP.screen.buffer=new ArrayBuffer(JSBP.screen.image.data.length);JSBP.screen.buffer8=new Uint8ClampedArray(JSBP.screen.buffer);JSBP.screen.array=new Uint32Array(JSBP.screen.buffer);console.timeEnd("Initiating fast per-pixel drawing");for(var a=0,b=0;255>=b;b+=51)for(var c=0;255>=c;c+=51)for(var d=0;255>=
d;d+=51)JSBP.screen.palette[a++]=4278190080|d<<16|c<<8|b;for(;255>=a;)JSBP.screen.palette[a++]=4278190080};JSBP.screen.clear=function(){JSBP.screen.context.clearRect(0,0,256,256)};JSBP.screen.tick=function(){for(var a=JSBP.mem[5]<<16,b=65536;b--;)JSBP.screen.array[b]=JSBP.screen.palette[JSBP.mem[a|b]];JSBP.screen.image.data.set(JSBP.screen.buffer8);JSBP.screen.context.putImageData(JSBP.screen.image,0,0)};JSBP.audio={};JSBP.audio.init=function(){console.time("Initiating audio device");JSBP.audio.context=new webkitAudioContext;JSBP.audio.buffer=JSBP.audio.context.createBuffer(1,512,30720);JSBP.audio.data=JSBP.audio.buffer.getChannelData(0);JSBP.audio.node=JSBP.audio.context.createBufferSource(0);JSBP.audio.node.loop=!0;JSBP.audio.node.buffer=JSBP.audio.buffer;JSBP.audio.node.start();console.timeEnd("Initiating audio device")};
JSBP.audio.tick=function(){for(var a=JSBP.mem[6]<<16|JSBP.mem[7]<<8,b,c,d=0;256>=d;d++)b=(128<=JSBP.mem[a|d]?JSBP.mem[a|d]-256:JSBP.mem[a|d])/127,c=255<=d?b:(128<=JSBP.mem[a|d+1]?JSBP.mem[a|d+1]-256:JSBP.mem[a|d+1])/127,JSBP.audio.data[2*d]=b,JSBP.audio.data[2*d+1]=(b+c)/2};JSBP.input={};JSBP.input.keyMap=[86,70,82,52,67,89,68,83,65,69,87,81,51,50,49,88];JSBP.input.keyStates=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];JSBP.input.init=function(){$("body").bind("keydown",JSBP.input.keyDown);$("body").bind("keyup",JSBP.input.keyUp)};JSBP.input.refresh=function(){for(var a=0,b=0,c=0;16>c;c++)8<=c?a=a<<1|JSBP.input.keyStates[c]:b=b<<1|JSBP.input.keyStates[c];JSBP.mem[1]=a;JSBP.mem[0]=b};
JSBP.input.keyDown=function(a){for(var b=0;b<JSBP.input.keyMap.length;b++)if(a.keyCode==JSBP.input.keyMap[b]){JSBP.input.keyStates[b]=1;JSBP.input.refresh();break}};JSBP.input.keyUp=function(a){for(var b=0;b<JSBP.input.keyMap.length;b++)if(a.keyCode==JSBP.input.keyMap[b]){JSBP.input.keyStates[b]=0;JSBP.input.refresh();break}};JSBP.gui={};JSBP.gui.init=function(){JSBP.gui.bindOverlay();JSBP.gui.bindDocument();JSBP.gui.bindCanvas();JSBP.gui.showOverlay('This is a <a href="http://esolangs.org/wiki/BytePusher">BytePusher</a> VM.<br/> Drop a binary file here or select a demo.',"file-binary")};
JSBP.gui.showOverlay=function(a,b,c,d){b=b||"info";c=c||"rgba(40, 40, 40, 0.8)";var e=$("#overlay"),f=function(){$("#overlay-icon").attr("class","mega-octicon octicon-"+b);$("#overlay-text").html(a);e.css("background-color",c).fadeIn("fast");d&&e.delay(d).fadeOut("fast")};"none"==e.attr("display")?f():e.fadeOut("fast",f)};JSBP.gui.hideOverlay=function(){var a=$("#overlay");"none"!=a.attr("display")&&a.stop().fadeOut("fast")};
JSBP.gui.bindOverlay=function(){$("#overlay").on("dragenter",function(a){a.stopPropagation();a.preventDefault();a.originalEvent.dataTransfer.dropEffect="copy"}).on("drop",function(a){a.preventDefault();a.stopPropagation();0!=a.originalEvent.dataTransfer.files.length&&(a=a.originalEvent.dataTransfer.files[0],a.size<JSBP.MEMSIZE?(JSBP.screen.clear(),JSBP.core.loadFile(a)):$(this).fadeOut("fast",function(){JSBP.gui.showOverlay("That file is too large<br/>(16MiB max.)","alert","rgba(90, 40, 40, 0.8)",
1E3)}))})};JSBP.gui.bindDocument=function(){var a,b;$(document).on("dragover",function(c){c.preventDefault();clearInterval(a);a=setInterval(function(){b=!1;clearInterval(a);JSBP.gui.hideOverlay()},100);b||(b=!0,c.originalEvent.dataTransfer.dropEffect="none",JSBP.gui.showOverlay("Drop the file here<br/>(16 MiB max.)","file-binary"))}).on("drop",function(a){a.preventDefault();a.stopPropagation()})};
JSBP.gui.bindCanvas=function(){$("#canvas").on("mouseenter",function(a){JSBP.core.first||(JSBP.running?$("#pause").attr("class","mega-octicon octicon-playback-pause"):$("#pause").attr("class","mega-octicon octicon-playback-play"),$("#pause").fadeIn("fast"))}).on("mouseleave",function(a){JSBP.core.first||$("#pause").fadeOut("fast")}).on("click",function(a){JSBP.core.first||(JSBP.toggle(),JSBP.running?$("#pause").attr("class","mega-octicon octicon-playback-pause"):$("#pause").attr("class","mega-octicon octicon-playback-play"))})};JSBP.demos={};JSBP.demos.paths={"Audio Test":"demos/audiotest.bp","Inverted Sine":"demos/invertloopsine.bp","Keyboard Test":"demos/keyboard.bp",Logo:"demos/logo.bp","Munching Squares":"demos/munching.bp","Nyan Cat":"demos/nyan.bp","Palette Test":"demos/palette.bp","Sine Scroller":"demos/scroller.bp","Sprite Test":"demos/sprites.bp"};JSBP.demos.init=function(){$("#right").children("a").each(function(a,b){$(b).on("click",function(){var a=$(b).text();JSBP.demos.paths.hasOwnProperty(a)&&JSBP.demos.load(JSBP.demos.paths[a])})})};
JSBP.demos.load=function(a){var b=JSBP.running;JSBP.stop();JSBP.screen.clear();JSBP.gui.showOverlay("Downloading demo","cloud-download");console.time("Downloading file from "+a);var c=new XMLHttpRequest;c.open("GET",a,!0);c.responseType="arraybuffer";c.onload=function(d){console.timeEnd("Downloading file from "+a);d=c.response;200==c.status?(JSBP.core.loadArrayBuffer(d),JSBP.gui.hideOverlay(),b&&JSBP.start()):JSBP.gui.showOverlay("Could not download the demo","alert","rgba(90, 40, 40, 0.8)",1E3)};
c.send(null)};