Created
May 8, 2022 23:38
-
-
Save ggorlen/0e59a8c7fc26cd286e479c6dcd26d943 to your computer and use it in GitHub Desktop.
sdl2 + emscripten
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// emcc -s USE_SDL=2 index.c -o index.html | |
// For images, see https://lyceum-allotments.github.io/2016/06/emscripten-and-sdl2-tutorial-part-4-look-owl/ | |
// See also https://github.com/paked/sdl2-cmake-emscripten/blob/master/src/main.cpp | |
#include <emscripten.h> | |
#include <SDL2/SDL.h> | |
SDL_Surface *screen; | |
SDL_Window *window; | |
SDL_Renderer *renderer; | |
const int WINDOW_W = 640; | |
const int WINDOW_H = 480; | |
int mouseX = 0; | |
int mouseY = 0; | |
void render_loop() { | |
SDL_SetRenderDrawColor(renderer, 100, 150, 100, 200); | |
for (SDL_Event event; SDL_PollEvent(&event);) { | |
if (event.type == SDL_MOUSEBUTTONDOWN) { | |
//|| event.type == SDL_MOUSEBUTTONUP) { | |
printf("%d %d\n", mouseX, mouseY); | |
SDL_FillRect(screen, NULL, 0); | |
} | |
else if (event.type == SDL_MOUSEMOTION) { | |
SDL_GetMouseState(&mouseX, &mouseY); | |
SDL_RenderDrawLine(renderer, 0, 0, mouseX, mouseY); | |
} | |
} | |
SDL_RenderPresent(renderer); | |
} | |
int main(int argc, char **argv) { | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError()); | |
return 1; | |
} | |
SDL_CreateWindowAndRenderer(WINDOW_W, WINDOW_H, 0, &window, &renderer); | |
emscripten_set_main_loop(render_loop, 0, 0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment