Emscripten - surfData.colors32未定义,SDL_FillRect

时间:2014-12-18 03:17:32

标签: javascript c++ sdl emscripten

我正在使用Emscripten试图让一个开源游戏在浏览器中运行。它编译得很好,加载所有文件的任何东西,但是当我运行它时会得到以下异常:

exception thrown: TypeError: surfData.colors32 is undefined,_SDL_FillRect@file:///home/misson20000/dev/js/game.js:9702:9
__ZN9Surface5ClearEhhh@file:///home/misson20000/dev/js/game.js:112026:3
...
_main@file:///home/misson20000/dev/js/game.js:10525:11
asm._main@file:///home/misson20000/dev/js/game.js:170793:10
callMain@file:///home/misson20000/dev/js/game.js:173065:15
doRun@file:///home/misson20000/dev/js/game.js:173122:7
run/<@file:///home/misson20000/dev/js/game.js:173134:7

调用SDL_FillRect(一个简单的清除函数)的代码如下:

SDL_FillRect(fSurface, NULL, MapColor(r, g, b));

MapColor定义为

return SDL_MapRGB(fSurface->format, r, g, b);

在源代码中挖掘一下,可以发现所讨论的表面是一个屏幕表面。

如何使surfData.colors32 未定义?

1 个答案:

答案 0 :(得分:0)

使用SDL_HWPALETTE标志创建SDL曲面时使用colors32。要正确使用此类型的表面,您应该在SDL_FillRect之前调用SDL_SetColors。看看src / library_sdl.js:

SDL_SetColors: function(surf, colors, firstColor, nColors) {
  var surfData = SDL.surfaces[surf];

  // we should create colors array
  // only once cause client code
  // often wants to change portion
  // of palette not all palette.
  if (!surfData.colors) {
   var buffer = new ArrayBuffer(256 * 4); // RGBA, A is unused, but faster this way
   surfData.colors = new Uint8Array(buffer);
   surfData.colors32 = new Uint32Array(buffer);
  }
  //...
}