Skip to content

Instantly share code, notes, and snippets.

@usagi
Last active August 29, 2015 13:59
Show Gist options
  • Select an option

  • Save usagi/10575869 to your computer and use it in GitHub Desktop.

Select an option

Save usagi/10575869 to your computer and use it in GitHub Desktop.
#ifdef EMSCRIPTEN
#include <emscripten/emscripten.h>
#include <SDL/SDL.h>
#else
#include <SDL2/SDL.h>
#endif
#include <iostream>
#include <cstdint>
#include <thread>
#include <chrono>
auto main() -> int
{
SDL_Init
( SDL_INIT_VIDEO
#if SDL_MAJOR_VERSION >= 2
| SDL_INIT_EVENTS
#endif
);
constexpr int window_width_in_px = 320;
constexpr int window_height_in_px = 240;
#if SDL_MAJOR_VERSION >= 2
auto window
= SDL_CreateWindow("sdl", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width_in_px, window_height_in_px, SDL_WINDOW_OPENGL);
#else
auto surface
= SDL_SetVideoMode(window_width_in_px, window_height_in_px, 32, SDL_OPENGL);
#endif
using key_states_t = const std::uint8_t*;
#ifndef EMSCRIPTEN
key_states_t key_states;
do
{
#else
emscripten_set_main_loop([]
{
key_states_t key_states;
#endif
SDL_PumpEvents();
key_states = SDL_GetKeyboardState(nullptr);
std::uint_fast8_t pressed_key_count = 0;
// comment out reason to see: https://github.com/kripken/emscripten/issues/2283
for ( auto code = /*SDL_SCANCODE_A*/0; code < /*SDL_SCANCODE_Z*/255; ++code )
if ( key_states[ code ] )
{
++pressed_key_count;
std::cerr << code << " ";
}
if ( pressed_key_count )
std::cerr << "\n";
#ifndef EMSCRIPTEN
std::this_thread::sleep_for( std::chrono::milliseconds(100) );
}
while( ! key_states[ SDL_SCANCODE_ESCAPE ] );
#else
}, 0, 1);
#endif
#if SDL_MAJOR_VERSION >= 2
SDL_DestroyWindow(window);
#endif
SDL_Quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment