英文:
Why mvprintw works only after getch?
问题
mvprintw/mvaddch (pdcurses) 只在 getch() 之后打印文本(即使它与 getch 没有任何连接,而 getch 在分离线程中)。
从不同角度提出的问题:如何在分离线程中使用 `mvprintw()` 而不需要 `getch()` 进行循环打印?(类似于小型渲染引擎或类似的东西)//编辑
// 主循环(仅用于调试的强烈简化)
do
{
//printf(" [%d] ", exit_flag);
char player_action = getch();
//render_player_info();
//printf("%d ", player_x);
mvprintw(1, player_x, "%d ", player_y);
mvaddch(1, player_y, 'C');
//Sleep(1);
}
while (exit_flag == 0);
// 启动函数中的代码块(主循环在线程分离之后开始)
int res;
pthread_t thread_render_engine;
void *thread_func_render_engine(void * arg)
{
int count = 0;
do
{
count++;
printf(" F%d ", count);
mvprintw(count/10 + 1, count, "P");
mvaddch(count/10 + 2, count, 'A');
render_player_info();
Sleep(1000);
} while (!exit_flag);
pthread_exit(NULL);
}
res = pthread_create (&thread_render_engine, NULL, thread_func_render_engine, NULL);
if (res != 0) {
mvprintw(29, 0, "主错误:无法创建线程,状态 = %d\n", res);
exit(-10);
}
res = pthread_detach(thread_render_engine);
if (res != 0) {
mvprintw(29, 0, "主错误:无法分离线程,状态 = %d\n", res);
exit(-11);
}
我尝试删除主 do-while 循环中的 getch,但默认的 printf 可以正常工作,但 mvprintw/mvaddch 不行。
英文:
mvprintw/mvaddch (pdcurses) print text only after getch() (even if it is not connected in any way and the getch is in detach thread).
A question from a different angle: How can i use mvprintw()
in a detach thread without getch()
for loop print? (A small rendering engine or something like that) //EDIT
// MAIN WHILE() (Strong simplification for debug)
do
{
//printf(" [%d] ", exit_flag);
char player_action = getch();
//render_player_info();
//printf("%d ", player_x);
mvprintw(1, player_x, "%d ", player_y);
mvaddch(1, player_y, 'C');
//Sleep(1);
}
while(exit_flag == 0);
// CODE BLOCK FROM LAUNCH FUNC (Main while() start after thread detach)
int res;
pthread_t thread_render_engine;
void *thread_func_render_engine(void * arg)
{
int count = 0;
do
{
count++;
printf(" F%d ", count);
mvprintw(count/10 + 1, count, "P");
mvaddch(count/10 + 2, count, 'A');
render_player_info();
Sleep(1000);
} while (!exit_flag);
pthread_exit(NULL);
}
res = pthread_create (&thread_render_engine, NULL, thread_func_render_engine, NULL);
if (res != 0) {
mvprintw(29, 0, "main error: can't create thread, status = %d\n", res);
exit(-10);
}
res = pthread_detach(thread_render_engine);
if (res != 0) {
mvprintw(29, 0, "main error: can't detach thread, status = %d\n", res);
exit(-11);
}
I tried to delete getch form amin dowhile and default printf working without getch, but not mvprintw/mvaddch
答案1
得分: 0
在你调用mvprintw()
和mvaddch()
等函数来修改stdscr之后,然后调用refresh()
来更新显示(getch()
会在副作用中执行这个操作)。
curses不会立即更新窗口,因为这样可以最大程度地减少在(可能很慢的)终端连接上传送的光标移动操作的数量。
英文:
After you've called functions like mvprintw()
and mvaddch()
to modify stdscr, you then call refresh()
to update the display (getch()
does this as a side-effect).
curses doesn't update the window immediately because this way the number of cursor movement operations sent over a (possibly slow) terminal connection is minimised.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论