0

problem with movement

HI, so I have just a simple game loop in SDL when I press W, nothing happens, the character is not moving uKeyDown; is created outside the loop everything was working fine before I added the switch key handling, so there is probably some stupid mistake in that switch statement. // main game loop while (isRunning) { while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: // close on X isRunning = 0; break; case SDL_KEYDOWN: // KEYDOWN switch (event.key.keysym.scancode) { case SDL_SCANCODE_W: // W press upKeyDown = 1; break; } case SDL_KEYUP: // KEYUP switch (event.key.keysym.scancode) { case SDL_SCANCODE_W: upKeyDown = 0; break; } } } printf("y = %d\n", dest.y); printf("x = %d\n", dest.x); // clear the window SDL_RenderClear(renderer); // set the y position in the struct dest.y = (int)y_pos; // draw the image to the window SDL_RenderCopy(renderer, tex, NULL, &dest); SDL_RenderPresent(renderer); if (upKeyDown == 1) { y_pos -= (float)2; } printf("dest.w = %d\n", &dest.w); // wait 1/60th of a second SDL_Delay(1000 / 60); }

1st Dec 2020, 7:36 PM
Kry$tof
6 Answers
+ 11
Can you please post the entire code and share the link.. cause it's difficult to get things done by this.
2nd Dec 2020, 3:49 AM
Aditya
Aditya - avatar
+ 9
Sorry but I'm unable to just out to conclusion for this question.
3rd Dec 2020, 4:11 PM
Aditya
Aditya - avatar
+ 8
Yes on a close observation I see : To some degree, it depends on the language. In the C (or C++) language, an if statement can have any expression as the condition. The conditions of a switch statement may be only a constant integer or enumerated type. That’s a big disadvantage of switch statements.
2nd Dec 2020, 5:29 PM
Aditya
Aditya - avatar
+ 1
@EnCoDeR I can't post the code here, because it has 220 lines of code, and it's too long. I still believe there is no need to post it here, the problem is in this main loop in the event handling. Thank you for the reply and if you really need the whole code I can create a link for it but like I said I believe there is no need for it.
2nd Dec 2020, 3:38 PM
Kry$tof
+ 1
I decided to replace switch statement with IF statement: if (event.type == SDL_KEYDOWN) { if (event.key.keysym.scancode == SDL_SCANCODE_W) { upKeyDown = 1; } } else if (event.type == SDL_KEYUP) { if (event.key.keysym.scancode == SDL_SCANCODE_W) { upKeyDown = 0; } } and its working just fine.... i don't know what is wrong with the switch statement.
2nd Dec 2020, 3:45 PM
Kry$tof
+ 1
EnCoDeR thanks do you know why is switch statement not working here?
2nd Dec 2020, 5:36 PM
Kry$tof