Initial merge of Emscripten port!

With this commit, you can compile SDL2 with Emscripten
( http://emscripten.org/ ), and make your SDL-based C/C++ program
into a web app.

This port was due to the efforts of several people, including: Charlie Birks,
Sathyanarayanan Gunasekaran, Jukka Jyl?nki, Alon Zakai, Edward Rudd,
Bruce Mitchener, and Martin Gerhardy. (Thanks, everyone!)
This commit is contained in:
Ryan C. Gordon 2014-12-18 00:19:52 -05:00
parent a228b67d88
commit fe40a17224
61 changed files with 4047 additions and 600 deletions

View file

@ -20,6 +20,10 @@
#include <stdio.h>
#include <string.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
#include "SDL.h"
#define MOOSEPIC_W 64
@ -135,6 +139,18 @@ SDL_Color MooseColors[84] = {
, {239, 206, 173}
};
Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2];
SDL_Texture *MooseTexture;
SDL_Rect displayrect;
int window_w;
int window_h;
SDL_Window *window;
SDL_Renderer *renderer;
int paused = 0;
int i;
SDL_bool done = SDL_FALSE;
Uint32 pixel_format = SDL_PIXELFORMAT_YV12;
int fpsdelay;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
@ -246,21 +262,65 @@ PrintUsage(char *argv0)
SDL_Log("\n");
}
void
loop()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
SDL_RenderSetViewport(renderer, NULL);
displayrect.w = window_w = event.window.data1;
displayrect.h = window_h = event.window.data2;
}
break;
case SDL_MOUSEBUTTONDOWN:
displayrect.x = event.button.x - window_w / 2;
displayrect.y = event.button.y - window_h / 2;
break;
case SDL_MOUSEMOTION:
if (event.motion.state) {
displayrect.x = event.motion.x - window_w / 2;
displayrect.y = event.motion.y - window_h / 2;
}
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_SPACE) {
paused = !paused;
break;
}
if (event.key.keysym.sym != SDLK_ESCAPE) {
break;
}
case SDL_QUIT:
done = SDL_TRUE;
break;
}
}
#ifndef __EMSCRIPTEN__
SDL_Delay(fpsdelay);
#endif
if (!paused) {
i = (i + 1) % MOOSEFRAMES_COUNT;
SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W*SDL_BYTESPERPIXEL(pixel_format));
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
SDL_RenderPresent(renderer);
}
int
main(int argc, char **argv)
{
Uint8 *RawMooseData;
SDL_RWops *handle;
int window_w;
int window_h;
SDL_Window *window;
SDL_Renderer *renderer;
Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2];
SDL_Texture *MooseTexture;
SDL_Rect displayrect;
SDL_Event event;
int paused = 0;
int i, j;
int j;
int fps = 12;
int fpsdelay;
int nodelay = 0;
@ -270,7 +330,6 @@ main(int argc, char **argv)
Uint32 pixel_format = SDL_PIXELFORMAT_YV12;
#endif
int scale = 5;
SDL_bool done = SDL_FALSE;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
@ -430,50 +489,14 @@ main(int argc, char **argv)
SDL_EventState(SDL_KEYUP, SDL_IGNORE);
/* Loop, waiting for QUIT or RESIZE */
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, nodelay ? 0 : fps, 1);
#else
while (!done) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
SDL_RenderSetViewport(renderer, NULL);
displayrect.w = window_w = event.window.data1;
displayrect.h = window_h = event.window.data2;
}
break;
case SDL_MOUSEBUTTONDOWN:
displayrect.x = event.button.x - window_w / 2;
displayrect.y = event.button.y - window_h / 2;
break;
case SDL_MOUSEMOTION:
if (event.motion.state) {
displayrect.x = event.motion.x - window_w / 2;
displayrect.y = event.motion.y - window_h / 2;
}
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_SPACE) {
paused = !paused;
break;
}
if (event.key.keysym.sym != SDLK_ESCAPE) {
break;
}
case SDL_QUIT:
done = SDL_TRUE;
break;
loop();
}
}
SDL_Delay(fpsdelay);
#endif
if (!paused) {
i = (i + 1) % MOOSEFRAMES_COUNT;
SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W*SDL_BYTESPERPIXEL(pixel_format));
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
quit(0);
return 0;