I am currently porting a graphics terminal renderer from Windows to Linux. The Windows version uses the win32 console API to render color, and it runs pretty smoothly. On Linux I'm trying to use ANSI escape codes to set color, but the performance is poor. The below code is a sample from renderer:
for(int row = 0; row < height; row++)
{
for(int col = 0; col < width; col++)
{
struct term_color pix = display[row*width+col];
printf("\x1b[48;2;%d;%d;%dm ", pix.r, pix.g, pix.b);
}
}
Note:
- I have extended its functionality from 16 color to truecolor (which may contribute to the render time increasing)
- From reading StackOverflow, I know other people have attempted to do the same thing, with results like mine. I also read others advising not to do this, so I'm uncertain how best to proceed.
Attempting:
- I know that the
printf
function operates according to a format and in terms of the data types the format specifies. I have changed the way that I convert color to int
and optimized it as best as I can do, but it did not make a noticeable difference.
So, is there a console API on Linux that performs better than ANSI sequences?