I’m trying to make a text animation for an application made in ncurses.
User presses a key, selects a direction and an object in a text grid should move from one cell of the grid to the next in the given direction, waiting 500ms before it moves. The code I used is
while (!checkcollisions(pos_f, input)) { // Checks if it can move to next grid
pos_f = moveobject(pos_f, input, ".."); // Moves object to next cell
usleep(50000);
}
But when I execute it, instead of moving, waiting and moving again, it waits a long time, and the object suddenly appears at the final cell of the grid, without showing the animation.
Is this because of how ncurses work? I already tried using other solutions like the select() stalling function.
You need to call
refresh()(before theusleep).Update: the new pastebin-ed code segments (in several comments) point to the real problem, which is the same one in ncurses-refresh: mixing
stdscr(implied by the next two calls) andgetchandrefreshwithnewwinandwrefresh.Update 2: using the full code, plus some hacking, I got it to work (for some value of “work”, I’m clearly not calling printmap() correctly, and I made up a bogus “map” file).
Without looking closely, I just changed all occurrences of
getch()towgetch(win.window), allmvprintwcalls tomvwprintw(to use that same window), and removed at least one unneeded getch/wgetch. Then the heart of the problem:The above call to
printmapis definitely wrong, but still you definitely need to do something in the loop to change what’s inwin.window(orstdscror some other window you put up or whatever); and then you need to force it to refresh, and force the output to stdout withfflush(stdout), before sleeping.