jsonbaby92

Untitled

Jun 6th, 2021 (edited)
178
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.  
  5. var SCREEN_WIDTH = 256;
  6. var SCREEN_HEIGHT = 240;
  7. var FRAMEBUFFER_SIZE = SCREEN_WIDTH*SCREEN_HEIGHT;
  8.  
  9. var canvas_ctx, image;
  10. var framebuffer_u8, framebuffer_u32;
  11.  
  12. function requestAnimationFrame(f) {
  13.         setImmediate(() => f(Date.now()));
  14. }
  15.  
  16. var nes = new jsnes.NES({
  17.     onFrame: function(framebuffer_24){
  18.         for(var i = 0; i < FRAMEBUFFER_SIZE; i++) framebuffer_u32[i] = 0xFF000000 | framebuffer_24[i];
  19.     },
  20. });
  21.  
  22. function onAnimationFrame(){
  23.     requestAnimationFrame(onAnimationFrame);
  24.    
  25.     image.data.set(framebuffer_u8);
  26.     canvas_ctx.putImageData(image, 0, 0);
  27.     nes.frame();
  28.    
  29.     saveImage(); // Save screenshot.
  30. }
  31.  
  32. function saveImage() {
  33.      const buffer = canvas_ctx.canvas.toBuffer('image/png');
  34.      writeFileSync('./test.png', buffer);
  35. }
  36.  
  37. function nes_init(){
  38.     var canvas = createCanvas(SCREEN_WIDTH, SCREEN_HEIGHT);
  39.     canvas_ctx = canvas.getContext("2d");
  40.     image = canvas_ctx.getImageData(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  41.    
  42.     canvas_ctx.fillStyle = "black";
  43.     canvas_ctx.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  44.    
  45.     // Allocate framebuffer array.
  46.     var buffer = new ArrayBuffer(image.data.length);
  47.     framebuffer_u8 = new Uint8ClampedArray(buffer);
  48.     framebuffer_u32 = new Uint32Array(buffer);
  49. }
  50.  
  51. function nes_boot(rom_data){
  52.     nes.loadROM(rom_data);
  53.     requestAnimationFrame(onAnimationFrame);
  54. }
  55.  
  56. // This is init function.
  57. function nes_load_data(rom_data){
  58.     nes_init();
  59.     nes_boot(rom_data);
  60. }
  61.  
  62. // Start functionality.
  63. nes_load_data('binary rom data');
Add Comment
Please, Sign In to add comment