When I run this code, the two for()
loops that draws the box isn't drawn. I've tested with gdb and it shows that the program does execute that two loops but somehow it isn't appearing. If the while(true)
loop is removed, the box gets drawn. So, why?
#include <iostream>
#include <curses.h>
int main(){
std::pair<int,int> csr_pos{1,1};
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
curs_set(0);
WINDOW *win = newwin(50,80,0,0);
if(has_colors()){
start_color();
init_pair(1,COLOR_CYAN,COLOR_BLACK);
init_pair(2,COLOR_RED,COLOR_BLACK);
init_pair(3,COLOR_WHITE,COLOR_RED);
init_pair(4,COLOR_YELLOW,COLOR_BLACK);
}
for(int i = 0; i<80; i++){
mvwaddch(win,0,i,'#');
mvwaddch(win,49,i,'#');
wrefresh(win);
}
for(int i = 1; i<49; i++){
mvwaddch(win,i,0,'#');
mvwaddch(win,i,79,'#');
wrefresh(win);
}
mvwaddch(win,csr_pos.first,csr_pos.second,'@');
while(true){
int ch = getch();
if(ch==KEY_LEFT){
mvwaddch(win,csr_pos.first,csr_pos.second,' ');
csr_pos.second--;
mvwaddch(win,csr_pos.first,csr_pos.second,'@');
}
if(ch==KEY_RIGHT){
mvwaddch(win,csr_pos.first,csr_pos.second,' ');
csr_pos.second++;
mvwaddch(win,csr_pos.first,csr_pos.second,'@');
}
wrefresh(win);
}
}
The issue is you have used getch()
instead of wgetch(win)
. Another thing to be mindful of is the size of the window you declare, which can cause a similar error.