Skip to content

Instantly share code, notes, and snippets.

@igormorgado
Created July 5, 2020 04:06
Show Gist options
  • Save igormorgado/0eed5470fddfb079123704e7f281a124 to your computer and use it in GitHub Desktop.
Save igormorgado/0eed5470fddfb079123704e7f281a124 to your computer and use it in GitHub Desktop.
#include <SDL2/SDL.h>
int main(void) {
SDL_bool quit = SDL_FALSE;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("Mouse events", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 127, 127, 127, 255);
SDL_RenderClear(renderer);
SDL_Event event;
int x = -1 , y = -1, rx = -1, ry = -1, wx = -1, wy = -1 , b[5] = {0};
while(quit == SDL_FALSE) {
while(SDL_PollEvent(&event) > 0 ) {
SDL_PumpEvents();
switch(event.type) {
case SDL_QUIT: quit = SDL_TRUE; break;
case SDL_MOUSEWHEEL:
wx += event.wheel.x;
wy += event.wheel.y;
break;
case SDL_MOUSEBUTTONUP:
printf("\nA mouse button was released\n");
if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) b[0] = 0;
if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT)) b[1] = 0;
if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_MIDDLE)) b[2] = 0;
if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_X1)) b[3] = 0;
if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_X2)) b[4] = 0;
break;
case SDL_MOUSEBUTTONDOWN:
printf("\nA mouse button was pressed\n");
if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) b[0] = 1;
if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT)) b[1] = 1;
if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_MIDDLE)) b[2] = 1;
if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_X1)) b[3] = 1;
if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_X2)) b[4] = 1;
break;
case SDL_MOUSEMOTION:
SDL_GetMouseState(&x, &y);
SDL_GetRelativeMouseState(&rx, &ry);
break;
}
}
fprintf(stdout,
"(x,y): (%4d,%4d): (rx,ry): (%2d,%2d) (wx, wy): (%4d,%4d) btn: %d%d%d%d%d \r",
x, y, rx, ry, wx, wy, b[0], b[1], b[2], b[3], b[4]);
fflush(stdout);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(20);
}
fprintf(stdout, "\n");
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment