繁体   English   中英

通过JS中的Emscripten获取C ++ SDL代码有麻烦

[英]Trouble getting C++ SDL Code working through Emscripten in JS

总体目标是在10x10的棋盘格上获得象棋一样的棋盘游戏,因此,如果兼容Emscripten的SDL中已经存在类似的示例,请发布链接。 无论如何,这是我的代码:

//Using SDL and standard IO
#include <iostream>
#include <SDL.h>
#include <stdio.h>
#include <math.h>

//Screen dimension constants
const int SCREEN_WIDTH = 480;
const int SCREEN_HEIGHT = 480;

bool init();
void close();
SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gHelloWorld = NULL;

bool init()
{
    //Initialization flag
    bool success = true;
    //Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        success = false;
    }
    else
    {
        //Create window
        gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (gWindow == NULL)
        {
            printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
            success = false;
        }
        else
        {
            //Get window surface
            gScreenSurface = SDL_GetWindowSurface(gWindow);
        }
    }

    return success;
}


void close()
{
    //Deallocate surface
    SDL_FreeSurface(gHelloWorld);
    gHelloWorld = NULL;

    //Destroy window
    SDL_DestroyWindow(gWindow);
    gWindow = NULL;

    //Quit SDL subsystems
    SDL_Quit();
}

int main(int argc, char* args[])
{
    //Start up SDL and create window
    if (!init())
    {
        printf("Failed to initialize!\n");
    }
    else
    {
        SDL_Event event;
        int i = 0;
        std::cout << "Working\n";
        while (i<10) {
            if (SDL_PollEvent(&event))
            {
                //If a key was pressed
                if (event.type == SDL_MOUSEBUTTONDOWN)
                {
                    i++;
                    int x, y;
                    SDL_GetMouseState(&x, &y);
                    printf("Mouse Location: %i %i\n", x/48, y/48);
                }

            }
        }
    }

    //Free resources and close SDL
    close();

    return 0;
}

我知道代码格式错误,这只是测试功能的一种技巧。 无论如何,它可以在Visual Studio上完美运行,并可以使用emscripten成功编译,但是在启动JS应用程序时崩溃。 我确定问题出在主事件轮询循环中,但不确定为什么

代替主循环,我们必须使用emscripten_set_main_loop()和回调。

https://kripken.github.io/emscripten-site/docs/porting/emscripten-runtime-environment.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM