V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
lib360
V2EX  ›  问与答

寻找 JavaScript 大神, jsnes 这个模拟器为什么在小程序里面不行呢?代码无报错,但是是黑屏的一个状态

  •  
  •   lib360 · 2019-05-28 11:52:57 +08:00 · 1704 次点击
    这是一个创建于 2011 天前的主题,其中的信息可能已经有所发展或是发生改变。

    JSNES 官网 https://jsnes.org/

    
    
    var jsnes = require("./nes/index.js");
    
    
    var sysinfo = wx.getSystemInfoSync();
    
    var SCREEN_WIDTH = 360;
    var SCREEN_HEIGHT = 640;
    
    var FRAMEBUFFER_SIZE = SCREEN_WIDTH * SCREEN_HEIGHT;
    
    var canvas_ctx, image;
    var framebuffer_u8, framebuffer_u32;
    
    
    var AUDIO_BUFFERING = 512;
    var SAMPLE_COUNT = 4 * 1024;
    var SAMPLE_MASK = SAMPLE_COUNT - 1;
    var audio_samples_L = new Float32Array(SAMPLE_COUNT);
    var audio_samples_R = new Float32Array(SAMPLE_COUNT);
    var audio_write_cursor = 0,
      audio_read_cursor = 0;
    
    var nes = null;
    
    
    /**
     * 游戏主函数
     */
    export default class Main {
      constructor() {
    
        // 维护当前 requestAnimationFrame 的 id
        this.aniId = 0
        this.restart()
      }
    
      restart() {
        let ctx = canvas.getContext('2d')
    
        canvas_ctx = ctx;
    
    
        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);
        
        nes = new jsnes.NES({
          onFrame: function (framebuffer_24) {
            for (var i = 0; i < FRAMEBUFFER_SIZE; i++) framebuffer_u32[i] = 0xFF000000 | framebuffer_24[i];
          }
    
        });
    
    
    
        this.bindLoop = this.loop.bind(this)
    
        window.cancelAnimationFrame(this.aniId);
    
        let that = this;
    
        this.nes_load_url(() => {
          console.log('load_succ');
    
          that.aniId = window.requestAnimationFrame(
            that.bindLoop,
            canvas_ctx
          )
    
          /*
          setTimeout(() => {
    
            that.keyboard(nes.buttonDown, 13);
            that.keyboard(nes.buttonUp, 13);
            console.log('start');
    
          }, 1000);
    */
    
    
        });
      }
    
      keyboard(callback, keyCode) {
        var player = 1;
        switch (keyCode) {
          case 38: // UP
            callback(player, jsnes.Controller.BUTTON_UP); break;
          case 40: // Down
            callback(player, jsnes.Controller.BUTTON_DOWN); break;
          case 37: // Left
            callback(player, jsnes.Controller.BUTTON_LEFT); break;
          case 39: // Right
            callback(player, jsnes.Controller.BUTTON_RIGHT); break;
          case 65: // 'a' - qwerty, dvorak
          case 81: // 'q' - azerty
            callback(player, jsnes.Controller.BUTTON_A); break;
          case 83: // 's' - qwerty, azerty
          case 79: // 'o' - dvorak
            callback(player, jsnes.Controller.BUTTON_B); break;
          case 9: // Tab
            callback(player, jsnes.Controller.BUTTON_SELECT); break;
          case 13: // Return
            callback(player, jsnes.Controller.BUTTON_START); break;
          default: break;
        }
      }
    
    
      // 实现游戏帧循环
      loop() {
        
        this.aniId = window.requestAnimationFrame(
          this.bindLoop,
          canvas_ctx
        )
     
        //ctx.clearRect(0, 0, canvas.width, canvas.height)
    
        image.data.set(framebuffer_u8);
        console.log(image);
        canvas_ctx.putImageData(image, 0, 0);
        nes.frame();
    
    
    
    
      }
    
    
    
    
    
      nes_load_data(rom_data, callback) {
    
        nes.loadROM(rom_data);
    
        callback && callback();
      }
    
      nes_load_url(callback) {
        let that = this;
    
        var url = 'https://localhost/test/InterglacticTransmissing.nes';
    
        wx.request({
          url: url,
          data: 'ArrayBuffer',
          dataType:"text",
          header: {
            //'content-type': 'text/plain; charset=x-user-defined' // 默认值
          },
          success: function (res) {
            that.nes_load_data(res.data, callback);
          }
        })
    
    
    
      }
    
    
    
    
    }
    
    
    
    1 条回复    2022-05-31 02:10:17 +08:00
    azh7138m
        1
    azh7138m  
       2022-05-31 02:10:17 +08:00
    编码问题,默认情况下返回值的 text 是按照 utf8 来做 decode 的。jsnes 目前的设计,在小程序里面需要拿 arrayBuffer ,用 Uint8Array 转换成 string ,就可以了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3711 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 10:37 · PVG 18:37 · LAX 02:37 · JFK 05:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.