Skip to content

Instantly share code, notes, and snippets.

@DanielGibson DanielGibson/sdl2test.c
Last active Sep 30, 2019

Embed
What would you like to do?
test SDL2 input (mouse and keyboard events)
/*
* SDL2 mousebutton test
*
* build with:
* $ gcc $(sdl2-config --cflags) -o sdl2test sdl2test.c $(sdl2-config --libs)
*/
#include <stdio.h>
#include <SDL.h>
#include <errno.h>
void eventloop(FILE* outfile) {
while(1) {
SDL_Event ev;
if(SDL_PollEvent(&ev) == 0) {
//printf(".\n");
SDL_Delay(1);
continue;
}
switch(ev.type) {
case SDL_QUIT:
fprintf(outfile, "Received SDL_QUIT - bye!\n");
return;
case SDL_MOUSEBUTTONUP:
fprintf(outfile, "SDL_MOUSEBUTTONUP, button %d clicks %d\n", ev.button.button, (int)ev.button.clicks);
break;
case SDL_MOUSEBUTTONDOWN:
fprintf(outfile, "SDL_MOUSEBUTTONDOWN, button %d clicks %d\n", ev.button.button, (int)ev.button.clicks);
break;
case SDL_KEYUP:
case SDL_KEYDOWN:
if(ev.type == SDL_KEYUP)
fprintf(outfile, "SDL_KEYUP: ");
else
fprintf(outfile, "SDL_KEYDOWN: ");
fprintf(outfile, "Keycode: %s (%d) Scancode: %s (%d)\n",
SDL_GetKeyName(ev.key.keysym.sym), ev.key.keysym.sym,
SDL_GetScancodeName(ev.key.keysym.scancode),
ev.key.keysym.scancode);
if(ev.key.keysym.sym == SDLK_q) {
fprintf(outfile, "You pressed Q - bye!\n");
return;
}
break;
case SDL_MOUSEWHEEL:
fprintf(outfile, "MouseWheel: x: %d, y: %d\n", ev.wheel.x, ev.wheel.y);
break;
case SDL_TEXTINPUT:
fprintf(outfile, "SDL_TEXTINPUT: %s\n", ev.text.text ? ev.text.text : "<NULL>");
break;
case SDL_JOYBUTTONDOWN:
fprintf(outfile, "SDL_JOYBUTTONDOWN dev %d button %d state %d\n",
(int)ev.jbutton.which, (int)ev.jbutton.button, (int)ev.jbutton.state);
break;
case SDL_JOYBUTTONUP:
fprintf(outfile, "SDL_JOYBUTTONUP dev %d button %d state %d\n",
(int)ev.jbutton.which, (int)ev.jbutton.button, (int)ev.jbutton.state);
break;
case SDL_MOUSEMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
case SDL_FINGERMOTION:
break; // ignore
case SDL_WINDOWEVENT:
break;
default:
// fprintf(outfile, "SDL_Event of type: 0x%x received\n", ev.type);
break;
}
}
}
int main(int argc, char** argv) {
//SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
SDL_Init(SDL_INIT_EVERYTHING);
//SDL_StartTextInput();
SDL_Joystick* gGameController = NULL;
SDL_Window* win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
//SDL_SetRelativeMouseMode(1);
//SDL_SetWindowGrab(win, 1);
SDL_SetWindowTitle(win, "asdf");
if( SDL_NumJoysticks() < 1 )
{
printf( "Warning: No joysticks connected!\n" );
}
else
{
//Load joystick
gGameController = SDL_JoystickOpen( 0 );
if( gGameController == NULL )
{
printf( "Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError() );
}
}
#ifdef _WIN32
// output debug prints to file on windows
const char* filename = "sdl2_test_out.txt";
#else
// output debug prints to stdout on other OS
const char* filename = "-";
#endif
FILE* output = NULL;
if(argc > 1) {
filename = argv[1];
}
if(strcmp(filename, "-") == 0)
output = stdout;
else
output = fopen(filename, "w");
if(!output) {
fprintf(stderr, "Couldn't create debug output file!\n");
exit(1);
}
eventloop(output);
if(strcmp(filename, "-") != 0)
fclose(output);
SDL_JoystickClose( gGameController );
gGameController = NULL;
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
@mirabilos

This comment has been minimized.

Copy link

mirabilos commented Sep 30, 2019

Fixed compile command (yours doesn’t work):

gcc -O2 $(sdl2-config --cflags) -o sdl2test sdl2test.c $(sdl2-config --libs)

Order matters.

Other than that, it should be noted that the window looks like it crashed, but things work well. Thanks, this helped me!

@DanielGibson

This comment has been minimized.

Copy link
Owner Author

DanielGibson commented Sep 30, 2019

Yeah, I'm not drawing anything so the window can contain garbage.
I changed it according to your suggestion.

Danke, mira!
- caedes

@mirabilos

This comment has been minimized.

Copy link

mirabilos commented Sep 30, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.