I'm making a SDL and C++ game engine and I don't know where I'm failing. Here iss my code:
Game.cpp
int mapKeyToInt(SDL_Keycode key) {
switch (key) {
case SDLK_w:
return 1; // Up
break;
case SDLK_UP:
return 1; // Up
break;
case SDLK_s:
return 2; // Down
break;
case SDLK_DOWN:
return 2; // Down
break;
case SDLK_a:
return 3; // Left
break;
case SDLK_LEFT:
return 3; // Left
break;
case SDLK_d:
return 4; // Right
break;
case SDLK_RIGHT:
return 4; // Right
break;
default:
return 0; // Default
break;
}
}
void Game::HandleEvents()
{
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
_closed = true;
break;
case SDL_KEYDOWN:
entity->getComponent<SpriteComponent>()->xspeed = 5;
entity->getComponent<SpriteComponent>()->yspeed = 5;
switch (mapKeyToInt(event.key.keysym.sym)) {
case 4: // Right
// Move player to the right
entity->getComponent<SpriteComponent>()->changeAnimationFrames(Walk);
entity->getComponent<SpriteComponent>()->moveRight();
break;
case 3: // Left
// Move player to the left
entity->getComponent<SpriteComponent>()->changeAnimationFrames(Walk);
entity->getComponent<SpriteComponent>()->moveLeft();
break;
case 1: // Up
entity->getComponent<SpriteComponent>()->changeAnimationFrames(Walk);
entity->getComponent<SpriteComponent>()->moveUp();
break;
case 2: // Down
entity->getComponent<SpriteComponent>()->changeAnimationFrames(Walk);
entity->getComponent<SpriteComponent>()->moveDown();
break;
case 0: // Default
// Handle other key inputs if needed
break;
case SDLK_k:
// Trigger attack animation
entity->getComponent<SpriteComponent>()->changeAnimationFrames(Attack);
break;
case SDLK_SPACE:
entity->getComponent<SpriteComponent>()->changeAnimationFrames(Crouch);
default:
break;
}
break;
case SDL_KEYUP:
switch (mapKeyToInt(event.key.keysym.sym)) {
case 1: // Up
case 2: // Down
case 3: // Left
case 4: // Right
case SDLK_k:
case SDLK_SPACE:
// Stop player movement if no movement input
entity->getComponent<SpriteComponent>()->xspeed = 0;
entity->getComponent<SpriteComponent>()->yspeed = 0;
entity->getComponent<SpriteComponent>()->changeAnimationFrames(Idle);
break;
case SDLK_ESCAPE:
_closed = true;
default:
break;
}
break;
default:
break;
}
}
...
// Method to change the animation frames dynamically
void changeAnimationFrames(const std::vector<std::string>& frames) {
_Rframes = frames;
// Reset frame index and last frame time
frameIndex = 0;
lastFrameTime = SDL_GetTicks(); // Reset last frame time to current time
// Load the new animation frames
TextureManager m;
texs = m.LoadAnimation(frames);
}
void moveUp() {
if (transform != nullptr) { // Check if transform is not null
dest.y -= yspeed;
transform->position.y = static_cast<int>(dest.y);
}
//_flip = SDL_FLIP_NONE;
}
void moveDown() {
if (transform != nullptr) { // Check if transform is not null
dest.y += yspeed;
transform->position.y = static_cast<int>(dest.y);
}
//_flip = SDL_FLIP_NONE;
}
void moveLeft() {
if (transform != nullptr) { // Check if transform is not null
dest.x -= xspeed;
transform->position.x = static_cast<int>(dest.x);
}
_flip = SDL_FLIP_HORIZONTAL;
}
void moveRight(){
if (transform != nullptr) { // Check if transform is not null
dest.x += xspeed;
transform->position.x = static_cast<int>(dest.x);
}
_flip = SDL_FLIP_NONE;
}
...
In theory if I press key up, it should go at the same speed as if I pressed W, but the real scenario it's that it goes faster with the key arrows, while WASD goes slower. I've tried several methods and none worked. PLEASE HELP!