Javascript 获取C++;SDL代码通过JS中的Emscripten工作

Javascript 获取C++;SDL代码通过JS中的Emscripten工作,javascript,c++,interop,emscripten,Javascript,C++,Interop,Emscripten,总体目标是在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_WID

总体目标是在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;
}
//使用SDL和标准IO
#包括
#包括
#包括
#包括
//屏幕尺寸常数
屏幕宽度=480;
屏幕上的常数=480;
bool init();
无效关闭();
SDL_窗口*gWindow=NULL;
SDL_表面*gScreenSurface=NULL;
SDL_表面*gHelloWorld=NULL;
boolinit()
{
//初始化标志
布尔成功=真;
//初始化SDL
if(SDL_Init(SDL_Init_视频)<0)
{
printf(“SDL无法初始化!SDL_错误:%s\n”,SDL_GetError());
成功=错误;
}
其他的
{
//创建窗口
gWindow=SDL_CreateWindow(“SDL教程”,SDL_WINDOWPOS_未定义,SDL_WINDOWPOS_未定义,屏幕宽度,屏幕高度,显示SDL_窗口);
if(gWindow==NULL)
{
printf(“无法创建窗口!SDL_错误:%s\n”,SDL_GetError());
成功=错误;
}
其他的
{
//获取窗口表面
gScreenSurface=SDL_GetWindowSurface(gWindow);
}
}
回归成功;
}
无效关闭()
{
//释放面
SDL_自由曲面(gHelloWorld);
gHelloWorld=NULL;
//破坏窗口
SDL_(格温多);
gWindow=NULL;
//退出SDL子系统
SDL_退出();
}
int main(int argc,char*args[]
{
//启动SDL并创建窗口
如果(!init())
{
printf(“初始化失败!\n”);
}
其他的
{
SDL_事件;
int i=0;

std::cout代替主循环,我们必须使用
emscripten\u set\u main\u loop()
和回调


我们必须使用
emscripten\u set\u main\u loop()
和回调来代替主循环