Skip to content

Instantly share code, notes, and snippets.

@DanielGibson
Created May 6, 2018 00:38
Show Gist options
  • Save DanielGibson/40fbade753c78088e235ebf22d1f2555 to your computer and use it in GitHub Desktop.
Save DanielGibson/40fbade753c78088e235ebf22d1f2555 to your computer and use it in GitHub Desktop.
testcase for SDL2 bug #4152 (and the old #3931)
// gcc -Wall -o warptest `sdl2-config --cflags` warptest.c `sdl2-config --libs`
#include <stdio.h>
#include <SDL.h>
static FILE* outfile = NULL;
static void UpdateWinTitle(int accumX, int accumY, SDL_Window* win)
{
char buf[128];
snprintf(buf, 128, "Accumulated: X: %d Y: %d (Press Q to quit)\n", accumX, accumY);
SDL_SetWindowTitle(win, buf);
}
int main(int argc, char** argv)
{
outfile = stdout;
//outfile = fopen("warptest.log", "wt");
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* win = SDL_CreateWindow("Move your mouse! (Press Q to quit)", 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
SDL_Surface* winSurf = SDL_GetWindowSurface(win);
Uint32 grey = SDL_MapRGB(winSurf->format, 127, 127, 127);
if(argc > 1 && strcmp(argv[1], "-raw") == 0) {
fprintf(outfile, "Using default raw mouse input for relative mouse mode (unless RegisterRawInputDevices() failed)\n");
} else {
fprintf(outfile, "Setting SDL_HINT_MOUSE_RELATIVE_MODE_WARP 1 => using mouse warp for relative mouse mode\n");
SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, "1");
}
fflush(outfile);
SDL_SetRelativeMouseMode(1);
// accum* holds the sum of all relative movements and is shown in window title
int accumX = 0, accumY = 0;
int keepRunning = 1;
while(keepRunning) {
SDL_Delay(7); // real games would do something useful (game logic) here..
winSurf = SDL_GetWindowSurface(win);
SDL_FillRect(winSurf, NULL, grey);
SDL_UpdateWindowSurface(win);
SDL_Event ev;
while(SDL_PollEvent(&ev)) {
switch(ev.type) {
case SDL_KEYDOWN:
if(ev.key.keysym.sym == SDLK_q || ev.key.keysym.sym == SDLK_ESCAPE) {
fprintf(outfile, "You pressed Q or Esc - bye!\n");
keepRunning = 0;
}
break;
case SDL_MOUSEMOTION:
if(ev.motion.xrel > 0)
fprintf(outfile, "> Mouse motion: rel: %d %d pos: %d %d\n", (int)ev.motion.xrel, (int)ev.motion.yrel, (int)ev.motion.x, (int)ev.motion.y);
else
fprintf(outfile, "<<< Mouse motion: rel: %d %d pos: %d %d\n", (int)ev.motion.xrel, (int)ev.motion.yrel, (int)ev.motion.x, (int)ev.motion.y);
fflush(outfile);
accumX += ev.motion.xrel;
accumY += ev.motion.yrel;
UpdateWinTitle(accumX, accumY, win);
break;
}
}
//fprintf(outfile, "### end of frame ###\n");
//fflush(outfile);
}
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment