Triang3l
Triang3l

Reputation: 1238

Writing to console without delays

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?

Upvotes: 1

Views: 496

Answers (2)

Thomas Maierhofer
Thomas Maierhofer

Reputation: 2681

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

Upvotes: 2

user703016
user703016

Reputation: 37975

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

Upvotes: 7

Related Questions