使用SDL2 + Emscripten时,浏览器不会加载本地图像

时间:2019-01-09 12:47:29

标签: javascript c++ sdl-2 emscripten

我正在尝试使用emscripten将SDL2 c ++代码移植到JS。我当前的文件系统如下所示(文件夹为大写,文件为小写):

C
|-VC
   |-SDL
       |-test.cpp
       |-RESOURCES
              |-hello.bmp

'hello.bmp'是任何640x480 px位图,而'test.cpp'包含下一个源代码:

#include <SDL.h>
#include <iostream>

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

struct context
{
    SDL_Renderer *ren;
    SDL_Texture *tex;
};

void mainloop(void* arg)
{
    context *con = static_cast<context*>(arg);
    SDL_RenderClear(con->ren);
    SDL_RenderCopy(con->ren, con->tex, NULL, NULL);
    SDL_RenderPresent(con->ren);
}

int main(int, char**)
{
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
        return 1;
    }

    SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
    if (win == nullptr)
    {
        std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (ren == nullptr)
    {
        SDL_DestroyWindow(win);
        std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    std::string imagePath = "Resources/hello.bmp";
    SDL_Surface *sur = SDL_LoadBMP(imagePath.c_str());
    if (sur == nullptr)
    {
        SDL_DestroyRenderer(ren);
        SDL_DestroyWindow(win);
        std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, sur);
    SDL_FreeSurface(sur);
    if (tex == nullptr)
    {
        SDL_DestroyRenderer(ren);
        SDL_DestroyWindow(win);
        std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

#ifdef __EMSCRIPTEN__
    context con = { ren, tex };
    emscripten_set_main_loop_arg(mainloop, &con, -1, 1);
#else
    for (int i = 0; i < 5; ++i)
    {
        SDL_RenderClear(ren);
        SDL_RenderCopy(ren, tex, NULL, NULL);
        SDL_RenderPresent(ren);
        SDL_Delay(1000);
    }
#endif

    SDL_DestroyTexture(tex);
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();

    return 0;
}

我正在使用Windows 10和emscripten 1.38.21。我正在控制台中目录“ C:\ emsdk-master \ emscripten \ 1.38.21”中的下一个命令行进行反编译:

emcc c:/vc/sdl/test.cpp -O2 -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s --preload-file c:/vc/sdl/Resources -o prueba.html

问题在于,一旦生成hello.html,我便在Firefox 64.0浏览器中将其打开,并显示下一个错误文本:

SDL_LoadBMP Error: Couldn't open Resources/hello.bmp

在Chrome 71.0中,将启动异常并暂停程序。

请问这对Firefox / Chrome浏览器有帮助吗?

1 个答案:

答案 0 :(得分:1)

我不确定,但也许此args组合会为您提供帮助。  最好放置-lSDL,emcc将处理链接库的其他问题。

您需要先转到当前目录。

 cd c:/vc/sdl/

通过这种方式,我们将仅使用相对路径。

 emcc test.cpp -s USE_SDL=2 -lSDL --preload-file Resources -s USE_SDL_IMAGE=2 -s ALLOW_MEMORY_GROWTH=1 --use-preload-plugins -s SDL2_IMAGE_FORMATS='["bmp","png"]' -s GL_UNSAFE_OPTS=0  -o prueba.html