Skip to content

Instantly share code, notes, and snippets.

@ToadKing
Created July 20, 2013 00:52
Show Gist options
  • Select an option

  • Save ToadKing/6043350 to your computer and use it in GitHub Desktop.

Select an option

Save ToadKing/6043350 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#ifdef EMSCRIPTEN
#include <emscripten/emscripten.h>
// bug - SDL_GetKeyboardState doesn't return scancodes, it returns keycodes, so acts exactly like
// SDL_GetKeyState instead
#define SDL_GetKeyState SDL_GetKeyboardState
#endif
void loop()
{
unsigned i;
#ifdef EMSCRIPTEN
SDL_Event e;
while (SDL_PollEvent(&e));
#else
// emscripten should also have PumpEvents actually run all the events, but that's another issue
SDL_PumpEvents();
#endif
int num;
const Uint8 *keys = SDL_GetKeyState(&num);
if (keys[SDLK_ESCAPE])
exit(0);
for (i = 0; i < 256; i++)
if (keys[i] != 0)
printf("key %u\n", i);
}
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(600, 400, 32, SDL_SWSURFACE);
#ifdef EMSCRIPTEN
emscripten_set_main_loop(loop, 0, 0);
#else
while (1)
loop();
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment