ncurses应用程序中的sleep()(sleep() in ncurses application)

编程入门 行业动态 更新时间:2024-10-26 07:37:05
ncurses应用程序中的sleep()(sleep() in ncurses application)

我正在尝试为ncurses中的应用程序制作文本动画

用户按下一个键,选择一个方向,文本网格中的一个对象应该从网格的一个单元格移动到给定方向的下一个单元格,等待它移动前500ms。 我使用的代码是

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); }

但是当我执行它时,它不是移动,等待和再次移动,而是等待很长时间,并且对象突然出现在网格的最后一个单元格中,而不显示动画

这是因为ncurses如何工作? 我已经尝试过使用其他解决方案,比如select()停止功能。

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.

最满意答案

你需要调用refresh() (在usleep之前)。


更新:新的pastebin-ed代码段(在几个注释中)指向真正的问题,这在 ncurses-refresh中是相同的:混合 stdscr (由接下来的两个调用隐含)和 getch并用 newwin和 wrefresh refresh 。
更新2:使用完整的代码,加上一些黑客,我得到它的工作(对于一些“工作”的价值,我显然没有正确调用printmap(),我编造了一个虚假的“地图”文件)。

没有仔细观察,我只是将所有出现的getch()更改为wgetch(win.window) ,所有mvprintw调用mvwprintw (使用相同的窗口),并删除至少一个不需要的getch / wgetch。 然后问题的核心:

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); }

上面对printmap调用肯定是错误的,但你仍然需要在循环中做一些事情来改变win.window (或stdscr或你提出的其他窗口或其他什么)中的内容; 然后你需要强制它刷新,并在睡觉前用fflush(stdout)强制输出到stdout。

You need to call refresh() (before the usleep).


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) and getch and refresh with newwin and wrefresh.
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() 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.

更多推荐

本文发布于:2023-07-15 18:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   ncurses   application   sleep

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!