Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const jsnes = require('jsnes');
- const { createCanvas } = require('canvas');
- const { writeFileSync } = require('fs');
- var SCREEN_WIDTH = 256;
- var SCREEN_HEIGHT = 240;
- var FRAMEBUFFER_SIZE = SCREEN_WIDTH*SCREEN_HEIGHT;
- var canvas_ctx, image;
- var framebuffer_u8, framebuffer_u32;
- function requestAnimationFrame(f) {
- setImmediate(() => f(Date.now()));
- }
- var nes = new jsnes.NES({
- onFrame: function(framebuffer_24){
- for(var i = 0; i < FRAMEBUFFER_SIZE; i++) framebuffer_u32[i] = 0xFF000000 | framebuffer_24[i];
- },
- });
- function onAnimationFrame(){
- requestAnimationFrame(onAnimationFrame);
- image.data.set(framebuffer_u8);
- canvas_ctx.putImageData(image, 0, 0);
- nes.frame();
- saveImage(); // Save screenshot.
- }
- function saveImage() {
- const buffer = canvas_ctx.canvas.toBuffer('image/png');
- writeFileSync('./test.png', buffer);
- }
- function nes_init(){
- var canvas = createCanvas(SCREEN_WIDTH, SCREEN_HEIGHT);
- canvas_ctx = canvas.getContext("2d");
- image = canvas_ctx.getImageData(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
- canvas_ctx.fillStyle = "black";
- canvas_ctx.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
- // Allocate framebuffer array.
- var buffer = new ArrayBuffer(image.data.length);
- framebuffer_u8 = new Uint8ClampedArray(buffer);
- framebuffer_u32 = new Uint32Array(buffer);
- }
- function nes_boot(rom_data){
- nes.loadROM(rom_data);
- requestAnimationFrame(onAnimationFrame);
- }
- // This is init function.
- function nes_load_data(rom_data){
- nes_init();
- nes_boot(rom_data);
- }
- // Start functionality.
- nes_load_data('binary rom data');
Add Comment
Please, Sign In to add comment