>>103256842
i'm making a space invaders clone with SDL, trying to figure out the best way to handle input. if i simply do something like this:
while (true) {
while (SDL_PollEvent(&event.type)) {
switch(event.type) {
case SDL_EVENT_KEY_DOWN:
switch (event.key.key) {
case SDLK_W:
player.y -= speed * delta_time;
break;
...
}
}
}
}
where `player` is an SDL_FRect, there's gonna be a short delay when you hold down W as if you're typing.
i ended up making a struct like this:
typedef struct player {
const float speed;
SDL_FRect sprite;
bool m_right;
bool m_left;
bool m_up;
bool m_down;
} player;
and then the code becomes
player p = { ... };
while (true) {
while (SDL_PollEvent(&event.type)) {
switch(event.type) {
case SDL_EVENT_KEY_DOWN:
switch (event.key.key) {
case SDLK_W:
p.m_up = true;
break;
...
}
case SDL_EVENT_KEY_UP:
switch(event.key.key) {
case SDLK_W:
p.m_up = false;
break;
...
}
}
}
if (p.m_up)
p.sprite.y -= p.speed * delta_time;
...
}
is there a better way to do this? i feel like this really sucks but i don't know how else to write it.