Advertisement
jsonbaby92

Untitled

Jun 6th, 2021 (edited)
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const jsnes = require('jsnes')
  2. const { createCanvas } = require('canvas');
  3. const { writeFileSync } = require('fs');
  4. const { requestAnimationFrame, cancelAnimationFrame } = require('request-animation-frame-polyfill')
  5.  
  6. class NESScreenShooter {
  7.     constructor() {
  8.         this.SCREEN_WIDTH = 256;
  9.         this.SCREEN_HEIGHT = 240;
  10.         this.FRAMEBUFFER_SIZE = this.SCREEN_WIDTH * this.SCREEN_HEIGHT;
  11.  
  12.         this.canvas_ctx;
  13.         this.image;
  14.         this.framebuffer_u8;
  15.         this.framebuffer_u32;
  16.  
  17.         this.id = 0;
  18.  
  19.         this.nes = new jsnes.NES({
  20.             onFrame: (framebuffer_24) => {
  21.                 for (var i = 0; i < this.FRAMEBUFFER_SIZE; i++) this.framebuffer_u32[i] = 0xFF000000 | framebuffer_24[i];
  22.             },
  23.         });
  24.     }
  25.  
  26.     requestAnimationFrame(fn) {
  27.         this.id = requestAnimationFrame(fn)
  28.  
  29.         if (this.id > 5000) {
  30.             cancelAnimationFrame(this.id)
  31.         }
  32.     }
  33.  
  34.     onAnimationFrame(time) {
  35.         if(this.id % 60 === 0) this.saveImage(`image_${this.id}`)
  36.         this.requestAnimationFrame(this.onAnimationFrame.bind(this))
  37.  
  38.         this.image.data.set(this.framebuffer_u8);
  39.         this.canvas_ctx.putImageData(this.image, 0, 0);
  40.         this.nes.frame();
  41.  
  42.         if (this.id > 60) {
  43.             this.nes.buttonDown(1, jsnes.Controller.BUTTON_START)
  44.         }
  45.  
  46.         if (this.id > 120) {
  47.             this.nes.buttonDown(1, jsnes.Controller.BUTTON_B)
  48.             this.nes.buttonDown(1, jsnes.Controller.BUTTON_A)            
  49.         }
  50.     }
  51.  
  52.     nes_init() {
  53.         const canvas = createCanvas(this.SCREEN_WIDTH, this.SCREEN_HEIGHT);
  54.         this.canvas_ctx = canvas.getContext("2d");
  55.         this.image = this.canvas_ctx.getImageData(0, 0, this.SCREEN_WIDTH, this.SCREEN_HEIGHT);
  56.  
  57.         this.canvas_ctx.fillStyle = "black";
  58.         this.canvas_ctx.fillRect(0, 0, this.SCREEN_WIDTH, this.SCREEN_HEIGHT);
  59.  
  60.         // Allocate framebuffer array.
  61.         var buffer = new ArrayBuffer(this.image.data.length);
  62.         this.framebuffer_u8 = new Uint8ClampedArray(buffer);
  63.         this.framebuffer_u32 = new Uint32Array(buffer);
  64.     }
  65.  
  66.     nes_boot(rom_data) {
  67.         this.nes.loadROM(rom_data);
  68.  
  69.         this.requestAnimationFrame(this.onAnimationFrame.bind(this));
  70.     }
  71.  
  72.     nes_load_data(rom_data) {
  73.         this.nes_init();
  74.         this.nes_boot(rom_data);
  75.     }
  76.  
  77.     saveImage(name) {
  78.         const buffer = this.canvas_ctx.canvas.toBuffer('image/png')
  79.         writeFileSync(`./images/${name}.png`, buffer);
  80.     }
  81. }
  82. module.exports = NESScreenShooter;
  83.  
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement