javascript - emscripten + sdl = 抛出异常 : TypeError: cannot set property 'widthNative' of undefined

标签 javascript c++ html emscripten

我今天刚启动并运行 emscripten,但无论我使用什么示例,我都会不断收到以下错误:

exception thrown: TypeError: Cannot set property 'widthNative' of undefined,TypeError: Cannot set property 'widthNative' of undefined
    at Object.Browser.updateCanvasDimensions (file:///Users/rspice/LocalDev/UAPDevTests/bld/app.out.js:6964:30)
    at Object.Browser.setCanvasSize (file:///Users/rspice/LocalDev/UAPDevTests/bld/app.out.js:6944:17)
    at _emscripten_set_canvas_size (file:///Users/rspice/LocalDev/UAPDevTests/bld/app.out.js:9485:15)
    at Array._Emscripten_CreateWindow (file:///Users/rspice/LocalDev/UAPDevTests/bld/app.out.js:150624:2)
    at _SDL_CreateWindow (file:///Users/rspice/LocalDev/UAPDevTests/bld/app.out.js:58841:41)
    at _SDL_CreateWindowAndRenderer (file:///Users/rspice/LocalDev/UAPDevTests/bld/app.out.js:34710:8)
    at _main (file:///Users/rspice/LocalDev/UAPDevTests/bld/app.out.js:10433:4)
    at Object.asm._main (file:///Users/rspice/LocalDev/UAPDevTests/bld/app.out.js:179274:19)
    at Object.callMain (file:///Users/rspice/LocalDev/UAPDevTests/bld/app.out.js:179457:30)
    at doRun (file:///Users/rspice/LocalDev/UAPDevTests/bld/app.out.js:179515:60)
2016-04-28 16:30:39.060 app.out.js:6964 Uncaught TypeError: Cannot set property 'widthNative' of undefinedBrowser.updateCanvasDimensions @ app.out.js:6964Browser.setCanvasSize @ app.out.js:6944_emscripten_set_canvas_size @ app.out.js:9485_Emscripten_CreateWindow @ app.out.js:150624_SDL_CreateWindow @ app.out.js:58841_SDL_CreateWindowAndRenderer @ app.out.js:34710_main @ app.out.js:10433asm._main @ app.out.js:179274callMain @ app.out.js:179457doRun @ app.out.js:179515run @ app.out.js:179529(anonymous function) @ app.out.js:179618

我正在使用 chrome 和 osx elcrapitan。

使用它来编译:

/Users/rspice/LocalDev/Emscripten/emscripten/1.35.0/em++  -s USE_SDL=2 -s GL_DEBUG=1 /Users/rspice/LocalDev/UAPDevTests/src/main.cpp  -o /Users/rspice/LocalDev/UAPDevTests/bld/app.out.js --use-preload-plugins

然后我将教程中的 .cpp 复制到我自己的文件中 http://kripken.github.io/emscripten-site/docs/getting_started/Tutorial.html#generating-html

谢谢。

编辑:

如果我从网络上的其他地方运行这个 .cpp,我会得到红框和一条线,但有一个错误:

app.out.js:6269 emscripten_set_main_loop_timing: Cannot set timing mode for main loop since a main loop does not exist! Call emscripten_set_main_loop first to set one up.

我不明白,因为它存在...

主要.cpp

#include <stdio.h>
#include <SDL2/SDL.h>
#include <assert.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

SDL_Window * window;
SDL_Renderer * renderer;

void render_func(void) {
    SDL_SetRenderDrawColor(renderer, 200, 0, 0, 255);
    SDL_RenderClear(renderer);

    SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
    SDL_RenderDrawLine(renderer, 0, 0, 640, 320);
    SDL_RenderPresent(renderer);

#ifdef EMSCRIPTEN
    emscripten_cancel_main_loop();
#endif
}

int main(int argc, char* argv[])
{
    assert(SDL_Init(SDL_INIT_VIDEO) == 0);
    SDL_CreateWindowAndRenderer(640, 320, 0, &window, &renderer);

#ifdef EMSCRIPTEN
    emscripten_set_main_loop(render_func, 0, 1);
#else
    render_func();
#endif

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

如果我运行这个 .cpp,我会得到

(index):40 you should see a smoothly-colored square - no sharp lines but the square borders!
2016-04-29 09:36:44.049 (index):40 and here is some text that should be HTML-friendly: amp: |&| double-quote: |"| quote: |'| less-than, greater-than, html-like tags: |<cheez></cheez>|
2016-04-29 09:36:44.050 (index):40 another line.

但没有颜色或正方形。

hello_world_sdl.cpp

#include <stdio.h>
#include <SDL/SDL.h>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

extern "C" int main(int argc, char** argv) {
  printf("hello, world!\n");

  SDL_Init(SDL_INIT_VIDEO);
  SDL_Surface *screen = SDL_SetVideoMode(256, 256, 32, SDL_SWSURFACE);

#ifdef TEST_SDL_LOCK_OPTS
  EM_ASM("SDL.defaults.copyOnLock = false; SDL.defaults.discardOnLock = true; SDL.defaults.opaqueFrontBuffer = false;");
#endif

  if (SDL_MUSTLOCK(screen)) SDL_LockSurface(screen);
  for (int i = 0; i < 256; i++) {
    for (int j = 0; j < 256; j++) {
#ifdef TEST_SDL_LOCK_OPTS
      // Alpha behaves like in the browser, so write proper opaque pixels.
      int alpha = 255;
#else
      // To emulate native behavior with blitting to screen, alpha component is ignored. Test that it is so by outputting
      // data (and testing that it does get discarded)
      int alpha = (i+j) % 255;
#endif
      *((Uint32*)screen->pixels + i * 256 + j) = SDL_MapRGBA(screen->format, i, j, 255-i, alpha);
    }
  }
  if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
  SDL_Flip(screen);

  printf("you should see a smoothly-colored square - no sharp lines but the square borders!\n");
  printf("and here is some text that should be HTML-friendly: amp: |&| double-quote: |\"| quote: |'| less-than, greater-than, html-like tags: |<cheez></cheez>|\nanother line.\n");

  SDL_Quit();

  return 0;
}

当我尝试加载图像的示例时,我遇到了 IMG_Error:
或类似的东西,但我会在之后进行调查。

现在我很确定这对我来说并不清楚,我不理解生成的 JS 以及我需要做什么来运行该模块......因为我没有写任何东西,而且一个有效的包括 JS 代码。你们知道有什么好的例子吗?

最佳答案

答案是我在JS中需要这个模块。

  var Module = {
    print: (function() {
      var element = document.getElementById('output');
      return function(text) {
        element.innerHTML += text.replace('\n', '<br>', 'g') + '<br>';
      };
    })(),
    canvas: document.getElementById('canvas')
  };

“基本上就是这样,是的。定义 Module.print 和 printErr,控制台应用程序可以工作,添加 Module.canvas 以进行渲染。请参阅 src/shell.html 了解默认内容。”

关于javascript - emscripten + sdl = 抛出异常 : TypeError: cannot set property 'widthNative' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36925029/

相关文章:

c++ - 返回序列中子序列的最大可能总和的算法

C++ 代码迁移。方法实现一直说缺少类型

C++ 分割字符串的函数

JavaScript: "SyntaxError: missing ) after argument list"文件最后一行

javascript - HTML 和 JavaScript 编程的最佳引用站点有哪些

javascript - 仅使用 API key 访问 Google Analytics 数据

javascript - 对同一个对象属性进行多次解构

javascript - 在 HTML 文件中包含另一个 HTML 文件

javascript - mobx 可观察日期字段不会自动重新渲染

javascript - 使用javascript将div的内容更改为许多文本字段