Skip to content

Instantly share code, notes, and snippets.

@Jegp
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jegp/8907982 to your computer and use it in GitHub Desktop.
Save Jegp/8907982 to your computer and use it in GitHub Desktop.
Simple emscripten test
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <emscripten.h>
int x = 0;
int y = 0;
SDL_Surface *screen;
void main_loop() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_RIGHT: x++; break;
case SDLK_LEFT: x--; break;
case SDLK_UP: y--; break;
case SDLK_DOWN: y++; break;
default: printf("Other key"); break;
}
default: printf("Event"); break;
}
}
// Clears the screen
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
// fill stuff
SDL_Rect rect = { x, y, 175, 125 };
SDL_FillRect(screen, &rect, SDL_MapRGBA(screen->format, 0x22, 0x22, 0xff, 0xff));
}
int main(int argc, char **argv) {
#if EMSCRIPTEN
// include GL stuff, to check that we can compile hybrid 2d/GL apps
extern void glBegin(int mode);
extern void glBindBuffer(int target, int buffer);
if (argc == 9876) {
glBegin(0);
glBindBuffer(0, 0);
}
// init main loop
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
printf("Init: %d\n", TTF_Init());
emscripten_set_main_loop(main_loop, 30, 1);
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment