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?
Don’t use
putchar. Make a buffer full of your characters, representing the screen state, and usewriteto send your buffer all at once to stdout, then flush it.For example:
write(STDOUT_FILENO, buffer, buffer_size); fflush(stdout);