ALSO POSTED ON STACKOVERFLOW: http://stackoverflow.com/questions/35639815/function-using-sdl2-returning-wrong-enum-value
I’m using SDL2 with C++.
I’ve made a Player
class. It contains an object from the Input
class.
I’ve made two Player
objects.
In the Player
constructor Player()
, I call setControls()
on member Input
object m_Controls
. Then I call keyPressed()
on the same object. Both those functions belong to class Input
.
My “error” is on line 89, where I call m_Controls.keyPressed(SDL_SCANCODE_W)
.
The function loops thru Input
member array m_Keys
– keys the player can press. If the element it’s iterating matches the SDL_Scancode
passed to keyPressed()
, it’s supposed to return the corresponding value from the Controls
enum.
Code:
#include <SDL2/SDL.h>
#include <iostream>
enum Controls {
CONTROLS_INVALID= -1,
CONTROLS_QUIT_GAME,
CONTROLS_UP,
CONTROLS_RIGHT,
CONTROLS_DOWN,
CONTROLS_LEFT,
CONTROLS_CONFIRM
};
class Input {
private:
enum {m_NumberOfKeys= 6};
SDL_Scancode m_Keys[m_NumberOfKeys];
Controls m_PressedKey;
public:
Input(){}
~Input(){}
void setControls(SDL_Scancode up, SDL_Scancode right, SDL_Scancode down, SDL_Scancode left, SDL_Scancode confirm){
m_Keys[0]= SDL_SCANCODE_ESCAPE;
m_Keys[1]= up;
m_Keys[2]= right;
m_Keys[3]= down;
m_Keys[4]= left;
m_Keys[5]= confirm;
}
Controls keyPressed(SDL_Scancode userInput){
std::cout << "userInput: " << userInput << std::endl;
for (int i = 0; i < m_NumberOfKeys; ++i){
std::cout << i << ' ' << m_Keys[i] << std::endl;
if (m_Keys[i] == userInput){
switch (i) {
case CONTROLS_QUIT_GAME:
m_PressedKey= CONTROLS_QUIT_GAME;
break;
case CONTROLS_UP:
m_PressedKey= CONTROLS_UP;
break;
case CONTROLS_RIGHT:
m_PressedKey= CONTROLS_RIGHT;
break;
case CONTROLS_DOWN:
m_PressedKey= CONTROLS_DOWN;
break;
case CONTROLS_LEFT:
m_PressedKey= CONTROLS_LEFT;
break;
case CONTROLS_CONFIRM:
m_PressedKey= CONTROLS_CONFIRM;
break;
default:
m_PressedKey= CONTROLS_INVALID;
break;
}
}
}
std::cout << "m_PressedKey: " << m_PressedKey << std::endl;
return m_PressedKey;
}
};
class Player {
private:
static int s_IdGenerator;
int m_Id;
Input m_Controls;
public:
Player() {
m_Id= s_IdGenerator++;
std::cout << "Making player " << m_Id << std::endl;
switch (m_Id) {
case 1:
m_Controls.setControls(SDL_SCANCODE_W, SDL_SCANCODE_D, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_SPACE);
break;
case 2:
m_Controls.setControls(SDL_SCANCODE_UP, SDL_SCANCODE_RIGHT, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_SPACE);
break;
default:
break;
}
m_Controls.keyPressed(SDL_SCANCODE_W);
std::cout << "==\n";
}
~Player(){}
Input& getControls(){
return m_Controls;
}
};
int Player::s_IdGenerator= 1;
int main(int argc, char **argv) {
SDL_Init(SDL_INIT_EVERYTHING);
Player player1;
Player player2;
return 0;
}
Given the code above, keyPressed()
returns the following after I make `player``:
Code:
Making player 1
userInput: 26
0 41
1 26
2 7
3 22
4 4
5 44
m_PressedKey: 1
So far it’s good. SDL_SCANCODE_W
is one of player1
's controls, so m_PressedKey
is correctly set at 1
. But here’s the output when player2
is created:
Code:
Making player 2
userInput: 26
0 41
1 82
2 79
3 81
4 80
5 44
m_PressedKey: 0
Since SDL_SCANCODE_W
is NOT part of player2
's controls, I want m_PressedKey
to be set to -1
. It’s set to 0
instead.
What must I change to make this code set m_PressedKey
to -1
when keyPressed()
gets an invalid SDL_Scancode
?[/code][/url][/b]