Writing to console without delays

ⅰ亾dé卋堺 提交于 2019-12-11 07:48:26

问题


I'm writing a command line game that should work at 4-40 FPS (will choose later). But, I have a problem. Drawing an "image" consisting of 1920 colored characters using putchar() takes 0.2-0.3 seconds, and I can see my image getting drawn line by line. However, for example, in Firefox, I can draw 64000 RGB pixels on canvas almost in less than tenth of a second.

Is there a way to avoid that delay, and is that delay forced by console or that's really how long it takes to process output?


回答1:


Don't use putchar. Make a buffer full of your characters, representing the screen state, and use write to send your buffer all at once to stdout, then flush it.

For example: write(STDOUT_FILENO, buffer, buffer_size); fflush(stdout);




回答2:


You should assemble your output string in memory and write it in one peace e.g using printf



来源:https://stackoverflow.com/questions/8777210/writing-to-console-without-delays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!