Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I created a simple Keyboard manager to detect whether buttons are currently pressed, or if where pressed in that frame. It seems like it should work, yet it seems to not read anything. Here is my class:

KeyboardManager.h file

class KeyboardManager
{
public:
    KeyboardManager(CL_DisplayWindow*);
    ~KeyboardManager(void);

    void Update();

    bool isPressed(int code);
    bool isReleased(int code);
    bool wasJustPressed(int code);

private:
    bool currState[0xffff];
    bool prevState[0xffff];
    CL_InputDevice *input;
};

KeyboardManager.cpp file

KeyboardManager::KeyboardManager(CL_DisplayWindow* window)
{
    input = &window->get_ic().get_keyboard();
    for(int i = 0; i < 0xFFFF; i++)
    {
        currState[i] = false;
        prevState[i] = false;
    }
}


KeyboardManager::~KeyboardManager(void)
{

}

void KeyboardManager::Update()
{
    for(int i = 0; i < 0xFFFF; i++)
    {
        prevState[i] = currState[i];
        currState[i] = input->get_keycode(i);
    }
}

bool KeyboardManager::isPressed(int code) { return currState[code]; }
bool KeyboardManager::isReleased(int code) { return !currState[code]; }
bool KeyboardManager::wasJustPressed(int code) { return currState[code] && !prevState[code]; }

As you can see, I ommited the includes. My question is, why does it not work! BTW. I based the keys array size on this page http://www.cl.cam.ac.uk/~mgk25/ucs/keysymdef.h . It seems like the Clanlib input is based on it. Also, it does not cover all the posible chars, but I only put enough for the ones I use, the basic keyboard letters, numbers, control, shift, arrow keys, etc.

This souldn't be important, but I'm using VS 2010

EDIT: I updated th keyboard manager and added the game logic as a whole here:

Game.h

class Game
{
public:
    Game();
    ~Game();

    void setName(string);

    static int main(const vector<CL_String>&);
    void run();
    void OnWindowClose();
    void quitGame();

    void setWidth(int);
    void setHeight(int);

private:
    bool quit;
    string name;

    int width;
    int height;
};

Game.cpp

Game::Game()
{

}

Game::~Game()
{

}

void Game::setWidth(int newWidth)
{
    width = newWidth;
}

void Game::setHeight(int newHeight)
{
    height = newHeight;
}

void Game::OnWindowClose()
{
    quit = true;
}

void Game::quitGame()
{
    quit = true;
}

void Game::setName(string newName) { name = newName; }

void Game::run()
{
    int x = 0;

    setWidth(800);
    setHeight(600);
    setName("Game");
    quit = false;

    CL_DisplayWindow window(name, width, height);

    CL_Slot slot_quit = window.sig_window_close().connect(this, &Game::OnWindowClose);

    CL_GraphicContext gc = window.get_gc();
    KeyboardManager keyboard(&window);
    CL_InputDevice mouse = window.get_ic().get_mouse();

    CL_ResourceManager resources("resources.xml");
    CL_Sprite* sprite = new CL_Sprite(gc, "WalkingBlankPonyRight", &resources);

    while (!quit)
    {
        if(keyboard.isPressed(CL_KEY_ESCAPE))
            quit = true;

        sprite->update();

        if(keyboard.isPressed(CL_KEY_D))
            x++;
        else if(keyboard.isPressed(CL_KEY_A))
            x--;

        CL_Draw::fill(gc, CL_Rectf(0, 0, 1024, 720), CL_Colorf::cornflowerblue);
        sprite->draw(gc, x, 200);

        window.flip();
        CL_KeepAlive::process();

        CL_System::sleep(10);
    }
}

int Game::main(const std::vector<CL_String> &args)
{
    CL_SetupCore setup_core;
    CL_SetupDisplay setup_display;
    CL_SetupGL setup_gl;

    try
    {
        Game* game = new Game;
        game->run();
    }
    catch(CL_Exception &exception)
    {
        // Create a console window for text-output if not available
        CL_ConsoleWindow console("Console", 80, 160);
        CL_Console::write_line("Error: " + exception.get_message_and_stack_trace());

        console.display_close_message();

        return -1;
    }

    return 0;
}

CL_ClanApplication app(&Game::main);
share|improve this question

closed as too localized by Byte56, Sean Middleditch, bummzack, Trevor Powell, Tetrad Mar 12 '13 at 20:11

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
The "fix my code" questions are too localized for the site, I'm voting to close. –  Byte56 Mar 7 '13 at 16:28
1  
Missing keyboard.Update() in game loop –  Lufi Mar 7 '13 at 17:17
    
@Lufi Oh, problem solved... It seems it is a mix between that and the fact it was not a reference –  rickyman20 Mar 7 '13 at 18:45

1 Answer 1

up vote 1 down vote accepted

Your code:

KeyboardManager::KeyboardManager(CL_DisplayWindow window)
{
    input = &window.get_ic().get_keyboard();
    for(int i = 0; i < 0xFFFF; i++)
    {
        currState[i] = false;
        prevState[i] = false;
    }
 }

input = &localVariable

You pass "window" by value. When "window" goes out of scope, using "input" will cause undefined bahaviour, it points at something that "might" not be in there anymore. Pass a pointer to "window" instead.

share|improve this answer
    
Or use references instead of pointers altogether. –  rootlocus Mar 7 '13 at 8:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.