#include <stdio.h>
#ifdef WIN32
#include "windows.h"
#include "SDL.h"
#else
#include <unistd.h>
#include "SDL2/SDL.h"
#include "SDL2/SDL_joystick.h"
#endif
#define EVENT_POLL_MAX 100 // only process maximum of 100 events per loop
int main(void) {
SDL_Event e;
int num_events;
// wastes 64K of memory but keeps track of pretty much any Unicode character you might find on a keyboard
char key_states[65535];
SDL_Window *w;
// should check errors here
SDL_InitSubSystem (SDL_INIT_JOYSTICK | SDL_INIT_VIDEO);
SDL_JoystickEventState(SDL_ENABLE);
// wipe the key_states array
memset(key_states, 0, 65535);
w = SDL_CreateWindow("Bismarck biss Mark, bis Mark Bismarck biss.",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
300, 300, SDL_WINDOW_RESIZABLE);
SDL_RaiseWindow(w);
SDL_ShowWindow(w);
while (1) {
num_events = 0;
while (SDL_PollEvent(&e) && (num_events<EVENT_POLL_MAX)) {
int my_key;
switch (e.type) {
case SDL_QUIT:
printf ("\nSDL_QUIT\n");
exit(0);
case SDL_KEYDOWN:
num_events++;
my_key = e.key.keysym.sym & 0xffff; // make sure key value can't exceed 65535
key_states[my_key] = 1; // game logic can check key_states array to see what is currently pressed
break;
case SDL_KEYUP:
num_events++;
my_key = e.key.keysym.sym & 0xffff; // make sure key value can't exceed 65535
key_states[my_key] = 0; // game logic can check key_states array to see what is currently pressed
break;
}
}
// constantly print out the state of the 'a' key:
printf("key_states['a'] = %i\n", key_states['a']);
#ifdef WIN32
Sleep(100);
#else
usleep(100000);
#endif
}
SDL_Quit();
return 0;
}
this is kind of what I'm doing -- I've tested it on Linux, it ought to work on Windows but I haven't tried it.
it uses a big array key_states to keep track of which keys are pressed, then the game logic can just check key_states to find out what's currently pressed and what isn't
#define EVENT_POLL_MAX 100 // only process maximum of 100 events per loop
heh, that's clever. did you throw that in as a wild guess, do rigorous testing or even learn how exactly the evnt loop works? first time I got suspicious was when I realized putting processing into that loop would (or should) mean holding a button down halts the whole game loop. I did a test doing exactly that, with a call to SDL_delay() outside the event loop. the delay actually happened every 40 runs, so it seems there's a limit coded in there already.
Code:
SDL_JoystickEventState(SDL_ENABLE);
was the gamcontroller feature a thing in the version of SDL you used? supposedly that does wonders to pad compatibility (assuming that isn't part of your full code since you mention good compatibility in that)
Code:
Bismarck biss Mark, bis Mark Bismarck biss.
rotfl
Your switch is interesting. Here's my test thing that doesn't work:
Code:
while(
SDL_PollEvent(&sdl_event) != 0) {
if (sdl_event.type == SDL_KEYDOWN) {
if (sdl_event.key.keysym.sym == SDLK_UP) {
up++;
} else {
up = 0;
}
if (sdl_event.key.keysym.sym == SDLK_RIGHT) {
right++;
} else {
right = 0;
}
if (sdl_event.key.keysym.sym == SDLK_ESCAPE) {
quit = 1;
}
SDL_WaitEvent(&sdl_event);
}
}
if (sdl_event.type == SDL_QUIT) {
quit = 1;
}
Comparing it to yours, the problem with my code must be that switching the keyboard button prevents the loop from checking the other cases. problem is I'm not even using a switch here trying to solve that lol
here's the other thing I tried, which probably does the same as your code. i'm not sure about future remappability (should be possible though).
Code:
SDL_PollEvent(&sdl_event);
if (sdl_event.type == SDL_QUIT) {
quit = 1;
}
if (key_state[SDL_SCANCODE_RIGHT] && key_state[SDL_SCANCODE_UP]) {
printf("pressing RIGHT-UP\n");
} else if (key_state[SDL_SCANCODE_LEFT]) {
printf("pressing-left\n");
} else {
printf("not pressing right-up...\n");
}
if (key_state[SDL_SCANCODE_ESCAPE]) {
quit = 1;
}
if (pad) {
printf("pressing up? %i\n", SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_UP));
printf("pressing A? %i\n", SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_A));
} else {
printf("no pad found :(\n");
}
works too. no reason to use the event loop unless I'm missing something. next is putting it all in an array like you did, let's see how that goes with my method...
guess it makes sense if there's something you order over and over. used to be refill cartridges for my ink pen for me until I switched to a converter and ink bottle