The overall goal is to get a chess-like board game on a 10x10 checkered board, so if there is already a similar example in Emscripten-compatible SDL, do post a link. Anyway, here is the code I have:
//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;
}
I know that the code is badly formatted, it's just a hack to test the capability. Anyway, it works perfectly on Visual Studio, and successfully compiles with emscripten, but crashes when the JS application is launched. I'm sure the problem is the event-polling loop in the main but am not sure why