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 the usleep
).
stdscr
(implied by the next two calls) and getch
and refresh
with newwin
and wrefresh
.Without looking closely, I just changed all occurrences of getch()
to wgetch(win.window)
, all mvprintw
calls to mvwprintw
(to use that same window), and removed at least one unneeded getch/wgetch. Then the heart of the problem:
while (!checkcollisions(pos_f, input)) {
- pos_f = moveobject(pos_f, input, "..");
- // sleep + wrefresh(win.window) doesn't work, neither does refresh()
+ struct position new_pos = moveobject(pos_f, input, "..");
+ printmap(pos_f, new_pos);
+ pos_f = new_pos;
+ wrefresh(win.window);
+ fflush(stdout);
+ usleep(50000);
}
The above call to printmap
is definitely wrong, but still you definitely need to do something in the loop to change what's in win.window
(or stdscr
or some other window you put up or whatever); and then you need to force it to refresh, and force the output to stdout with fflush(stdout)
, before sleeping.
Try something like
while (!checkcollisions(pos_f, input)) { // Checks if it can move to next grid
pos_f = moveobject(pos_f, input, ".."); // Moves object to next cell
refresh();
napms(200);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With