I'm making a game in SDL2 that so far is basically just a top down shooter, where the player aims at the mouse. When I run the game, you can move properly and shoot properly, and shoot and move the mouse at the same time properly, but you cannot shoot while moving. For example, if I'm using the gatling gun which is fully automatic (as long as I hold the mouse down it keeps on shooting), and I start shooting, then press the w key to move, it stops shooting right away, and doesn't start again until I click again. Here is my code:
void Main::handleInput2(void)
{
double xcomponent,ycomponent,xVel,yVel,angle;
SDL_GetMouseState(&mouseX,&mouseY);
xcomponent = mouseX - (player.getBarrelX() + xOffset);
ycomponent = mouseY - (player.getBarrelY() + yOffset);
angle = atan2(ycomponent,xcomponent);
xVel = cos(angle);
yVel = sin(angle);
if(screen->getEvent()->type == SDL_KEYDOWN)
{
if(screen->getEvent()->key.keysym.sym == SDLK_a)
{
player.setVelX(-1 * player.getSpeed());
}
if(screen->getEvent()->key.keysym.sym == SDLK_d)
{
player.setVelX(player.getSpeed());
}
if(screen->getEvent()->key.keysym.sym == SDLK_w)
{
player.setVelY(-1 * player.getSpeed());
}
if(screen->getEvent()->key.keysym.sym == SDLK_s)
{
player.setVelY(player.getSpeed());
}
}
if(screen->getEvent()->type == SDL_KEYUP){
//if(screen->getEvent()->key.keysym.sym = SDLK_8) player.setVelocity(-2,-2); TEST PASSED
if(screen->getEvent()->key.keysym.sym == SDLK_a)
{
player.setVelX(0);
}
if(screen->getEvent()->key.keysym.sym == SDLK_d)
{
player.setVelX(0);
}
if(screen->getEvent()->key.keysym.sym == SDLK_w)
{
player.setVelY(0);
}
if(screen->getEvent()->key.keysym.sym == SDLK_s)
{
player.setVelY(0);
}
if(screen->getEvent()->key.keysym.sym == SDLK_1) //Paintball Gun
{
player.setWeapon(0);
}
if(screen->getEvent()->key.keysym.sym == SDLK_0) //Glock
{
player.setWeapon(1);
}
if(screen->getEvent()->key.keysym.sym == SDLK_2) //Skorpion
{
player.setWeapon(2);
}
if(screen->getEvent()->key.keysym.sym == SDLK_3) //Ballistic Knife
{
player.setWeapon(5);
}
if(screen->getEvent()->key.keysym.sym == SDLK_4) //Spas 12
{
player.setWeapon(4);
}
if(screen->getEvent()->key.keysym.sym == SDLK_5)
{
player.setWeapon(3);
}
}
if(screen->getEvent()->type == SDL_MOUSEBUTTONDOWN || screen->getEvent()->type == SDL_MOUSEMOTION&SDL_MOUSEBUTTONDOWN)
{
if(screen->getEvent()->button.button == 1)
{
player.Shoot(1,xVel,yVel,screen);
}
if(screen->getEvent()->button.button == 3)
{
player.Shoot(3,xVel,yVel,screen);
}
}
}
This was originally all in switch statements, but I thought it might work in if statements. I also tried handling SDL_KEYDOWN and SDL_MOUSEDOWN the same way as I did with SDL_MOUSEDOWN and SDL_MOUSEMOTION. I tried stuff like
if(screen->getEvent()->type == SDL_KEYDOWN|SDL_MOUSEDOWN)
{
...
}
if(screen->getEvent()->type == SDL_MOUSEDOWN|SDL_KEYDOWN)
{
...
}
if(screen->getEvent()->type == SDL_MOUSEDOWN&SDL_KEYDOWN)
{
...
}
if(screen->getEvent()->type == SDL_KEYDOWN&SDL_MOUSEDOWN)
{
...
}
But none of those helped, each one in fact made it worse. Seeing as that SDL is meant for making games, I would imagine there must be a way around this. Does anyone know how?