I have a question regarding the handling of events in SDL. I still do not know exactly how the SDL event pool works and I am having problems with the code that I will show next. Basically I'm trying to make the character move and can shoot at the same time. But if I move the character can not shoot and vice versa. I understand that you can not have two inputs at the same time or something, again I do not have it completely clear.
Here the code:
void Player::HandleEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: //Changes sense of vector when pressed if (event.key.keysym.sym == SDLK_w) { speedSenseY = -1; } if (event.key.keysym.sym == SDLK_s) { speedSenseY = 1; } if (event.key.keysym.sym == SDLK_a) { speedSenseX = -1; } if (event.key.keysym.sym == SDLK_d) { speedSenseX = 1; } //Shooting if (event.key.keysym.sym == SDLK_UP ) { if (!shooting) { shooting = true; //speed = 0; Bullet* bullet = new Bullet(); bullet->speedSenseY = -1; bullet->bulletRect.x = playerRect.x + (frameW / 2); bullet->bulletRect.y = playerRect.y + (frameH / 2); spawnedBullets.push_back(bullet); } } if (event.key.keysym.sym == SDLK_DOWN) { if (!shooting) { shooting = true; //speed = 0; Bullet* bullet = new Bullet(); bullet->speedSenseY = 1; bullet->bulletRect.x = playerRect.x + (frameW / 2); bullet->bulletRect.y = playerRect.y + (frameH / 2); spawnedBullets.push_back(bullet); } } if (event.key.keysym.sym == SDLK_LEFT) { if (!shooting) { shooting = true; //speed = 0; Bullet* bullet = new Bullet(); bullet->speedSenseX = -1; bullet->bulletRect.x = playerRect.x + (frameW / 2); bullet->bulletRect.y = playerRect.y + (frameH / 2); spawnedBullets.push_back(bullet); } } if (event.key.keysym.sym == SDLK_RIGHT) { if (!shooting) { shooting = true; //speed = 0; Bullet* bullet = new Bullet(); bullet->speedSenseX = 1; bullet->bulletRect.x = playerRect.x + (frameW / 2); bullet->bulletRect.y = playerRect.y + (frameH / 2); spawnedBullets.push_back(bullet); } } break; case SDL_KEYUP: //Sets speed to 0 when not pressed if (event.key.keysym.sym == SDLK_w && speedSenseY 0) { speedSenseY = 0; } if (event.key.keysym.sym == SDLK_a && speedSenseX 0) { speedSenseX = 0; } if (event.key.keysym.sym == SDLK_UP) { shooting = false; } if (event.key.keysym.sym == SDLK_DOWN) { shooting = false; } if (event.key.keysym.sym == SDLK_LEFT) { shooting = false; } if (event.key.keysym.sym == SDLK_RIGHT) { shooting = false; } break; } } }