3

I'm writing a Tetris game with ncurses getting keyboard input with getch(). For the finished game nodelay() will be active but for testing purposes I have it commented out. For some reason the input function called by my game loop is ignoring every second character. For instance an input of 'q' closes my program. If I enter a sequence of aqaqaqaqaq... the program will never close because the q's do not register. Below is the function that init's my ncurses code and below that is my input function.

Edit: In case it matters I'm testing the code in Ubuntu running in virtualbox on Windows.

****INIT FUNCTION****
void init_ui()
{
initscr();
cbreak();
raw();
keypad(stdscr, TRUE);
noecho();
//nodelay(stdscr, TRUE);

start_color();
init_pair(9, COLOR_BLACK, COLOR_RED);
init_pair(2, COLOR_BLACK, COLOR_WHITE);
init_pair(3, COLOR_BLACK, COLOR_CYAN);
init_pair(4, COLOR_BLACK, COLOR_YELLOW);
init_pair(5, COLOR_BLACK, COLOR_MAGENTA);
init_pair(6, COLOR_BLACK, COLOR_BLUE);
init_pair(7, COLOR_BLACK, COLOR_GREEN);
init_pair(8, COLOR_BLACK, COLOR_BLACK);
attron(COLOR_PAIR(6));
for(int i=0; i<HEIGHT; i++)
{
    mvprintw(i, 0, "#%*c", WIDTH*2+1, '#');
}
for(int i=0; i<=WIDTH; i++)
{
    mvprintw(HEIGHT, i*2, "##");
}
attroff(COLOR_PAIR(6));
}

****INPUT FUNCTION****
int get_input()
{
int ch = getch();
int result;
switch(ch)
{
    case KEY_LEFT:
        result = LEFT;
        break;
    case KEY_RIGHT:
        result = RIGHT;
        break;
    case KEY_DOWN:
        result = DOWN;
        break;
    case 'q':
        result = 'q';
        break;
    default:
        result = DOWN;
        break;
}
return result;
}
Reid A
  • 101
  • 1
  • 1
  • 7
  • 2
    Is there a chance you are accidentally calling get_input() twice in your main loop? – iagreen Dec 23 '12 at 02:13
  • Ha I feel silly. I combed through my code and I found a stray getch() I placed to stop the code for testing purposes. Was certain I had removed it. – Reid A Dec 23 '12 at 02:35

1 Answers1

2

Turns out I left a stray getch() in another part of my program and I forgot to remove it. Just a dumb mistake on my part.

Reid A
  • 101
  • 1
  • 1
  • 7