2016-02-25 81 views
0

我正在使用SDL2和C++。使用SDL2返回错误枚举值的函数

我做了一个Player类。它包含Input类中的一个对象。

我制作了两个Player对象。

Player构造Player(),我请setControls()上构件Input对象m_Controls。然后我在同一个对象上调用keyPressed()。这两个功能都属于Input

我的“错误”是在行89,我呼吁m_Controls.keyPressed(SDL_SCANCODE_W)

函数循环到Input成员数组m_Keys - 玩家可以按的键。如果迭代的元素匹配SDL_Scancode传递给keyPressed(),它应该从Controls枚举中返回相应的值。

#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; 
} 

鉴于上面的代码,keyPressed()返回以下后,我做`player``:

Making player 1 
userInput: 26 
0 41 
1 26 
2 7 
3 22 
4 4 
5 44 
m_PressedKey: 1 

到目前为止,这是很好的。 SDL_SCANCODE_Wplayer1的控件之一,所以m_PressedKey正确设置为1。但这里的时候创建player2输出:

Making player 2 
userInput: 26 
0 41 
1 82 
2 79 
3 81 
4 80 
5 44 
m_PressedKey: 0 

由于SDL_SCANCODE_W不是player2的控制的一部分,我想m_PressedKey设置为-1。它被设置为0

keyPressed()得到一个无效SDL_Scancode时,我必须更改哪些代码才能使此代码集m_PressedKey变为-1

回答

1

在ctor或keyPressed方法中初始化它; m_PressedKey = CONTROLS_INVALID;默认ctors是邪恶:)

+0

恐怕我不明白你的意思。我是C++新手。你能告诉我你的代码示例吗? – Username

+1

public: Input(){m_PressedKey = CONTROLS_INVALID; } 〜Input(){} – user5976242

+1

或在该方法中:控制keyPressed(SDL_Scancode userInput){0} {0} {mPressedKey = CONTROLS_INVALID; std :: cout <<“userInput:”<< userInput << std :: endl; – user5976242