jsonbaby92

Untitled

Jun 6th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** ---------------------------------------------- */
  2. let timer = null;
  3. let time = 0;
  4. function requestAnimationFrame(f) {
  5.   timer = setImmediate(() => {
  6.     time++;
  7.     f(Date.now());
  8.   });
  9. }
  10.  
  11. const fs = require('fs');
  12. const jsnes = require('../../jsnes-web/node_modules/jsnes');
  13. const { createCanvas } = require('canvas');
  14.  
  15. var SCREEN_WIDTH = 256;
  16. var SCREEN_HEIGHT = 240;
  17. var FRAMEBUFFER_SIZE = SCREEN_WIDTH * SCREEN_HEIGHT;
  18.  
  19. var canvas_ctx, image;
  20. var framebuffer_u8, framebuffer_u32;
  21.  
  22. var nes = new jsnes.NES({
  23.   onFrame: function (framebuffer_24) {
  24.     for (var i = 0; i < FRAMEBUFFER_SIZE; i++) framebuffer_u32[i] = 0xff000000 | framebuffer_24[i];
  25.   },
  26. });
  27.  
  28. function onAnimationFrame() {
  29.   requestAnimationFrame(onAnimationFrame);
  30.   image.data.set(framebuffer_u8);
  31.   canvas_ctx.putImageData(image, 0, 0);
  32.  
  33.   let img = canvas_ctx.canvas.toDataURL();
  34.   var data = img.replace(/^data:image\/\w+;base64,/, '');
  35.   var buf = new Buffer(data, 'base64');
  36.   fs.writeFileSync('image.png', buf);
  37.  
  38.   if (time >= 60 && timer !== null) {
  39.     clearImmediate(timer);
  40.     time = 0;
  41.     process.exit();
  42.   }
  43. }
  44.  
  45. function nes_init() {
  46.   const canvas = createCanvas(SCREEN_WIDTH, SCREEN_HEIGHT);
  47.   canvas_ctx = canvas.getContext('2d');
  48.  
  49.   image = canvas_ctx.getImageData(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  50.  
  51.   canvas_ctx.fillStyle = 'black';
  52.   canvas_ctx.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  53.  
  54.   // Allocate framebuffer array.
  55.   var buffer = new ArrayBuffer(image.data.length);
  56.   framebuffer_u8 = new Uint8ClampedArray(buffer);
  57.   framebuffer_u32 = new Uint32Array(buffer);
  58. }
  59.  
  60. function nes_boot(rom_data) {
  61.   nes.loadROM(rom_data);
  62.  
  63.   setInterval(() => {
  64.     nes.frame();
  65.   });
  66.  
  67.   requestAnimationFrame(onAnimationFrame);
  68. }
  69.  
  70. function nes_load_url(romData) {
  71.   nes_init();
  72.   nes_boot(romData);
  73. }
  74.  
  75. module.exports = nes_load_url;
  76.  
  77. /** ---------------------------------------------- */
  78.  
Add Comment
Please, Sign In to add comment