Created
June 27, 2014 17:53
-
-
Save gsathya/1f55d0bf87c81bbde516 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <stdio.h> | |
#include <SDL2/SDL.h> | |
#include <assert.h> | |
#ifdef __EMSCRIPTEN__ | |
#include <emscripten.h> | |
#endif | |
int result = 1; | |
int done = 0; | |
void one() { | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) { | |
switch(event.type) { | |
case SDL_MOUSEMOTION: { | |
SDL_MouseMotionEvent *m = (SDL_MouseMotionEvent*)&event; | |
assert(m->state == 0); | |
int x, y; | |
SDL_GetMouseState(&x, &y); | |
printf("motion: %d = %d, %d = %d, %d,%d\n", m->x, x, m->y, y, m->xrel, m->yrel); | |
//assert(x == m->x && y == m->y); | |
if(x != m->x && y != m->y) {printf("fail\n");} | |
result += 2 * (m->x + m->y + m->xrel + m->yrel); | |
break; | |
} | |
case SDL_MOUSEBUTTONDOWN: { | |
SDL_MouseButtonEvent *m = (SDL_MouseButtonEvent*)&event; | |
printf("button down: %d,%d %d,%d\n", m->button, m->state, m->x, m->y); | |
result += 3 * (m->button + m->state + m->x + m->y); | |
break; | |
} | |
case SDL_MOUSEBUTTONUP: { | |
SDL_MouseButtonEvent *m = (SDL_MouseButtonEvent*)&event; | |
printf("button up: %d,%d %d,%d\n", m->button, m->state, m->x, m->y); | |
result += 5 * (m->button + m->state + m->x + m->y); | |
// Remove another click we want to ignore | |
assert(SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONDOWN) == 1); | |
assert(SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEBUTTONUP, SDL_MOUSEBUTTONUP) == 1); | |
break; | |
case SDL_QUIT: | |
done = 1; | |
break; | |
} | |
} | |
} | |
} | |
int main() { | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_Window *window; | |
SDL_Renderer *renderer; | |
SDL_CreateWindowAndRenderer(600, 450, 0, &window, &renderer); | |
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF ); | |
SDL_Rect rect = { 0, 0, 600, 450 }; | |
SDL_RenderFillRect(renderer, &rect); | |
#ifdef __EMSCRIPTEN__ | |
emscripten_set_main_loop(one, 1000, 1); | |
#else | |
while(!done){ | |
one(); | |
} | |
#endif | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment