What is a good way to put background color for codes?

#1

I’m generating a shader code and displaying it.
But sometimes it’s not readable, so I would like to put black as a background color for shader code.

code for displaying shader code
shader.begin();
...
ofDrawRectangle(0,0, ofGetWidth(), ofGetHeight());
shader.end();

ofPushMatrix();
ofSetColor(255);
codeFont.drawString(fsCode, ofGetWidth()*.1, ofGetHeight()*.1);
ofPopMatrix();

Could anyone tell me a good way to do that?

Thank you in advance,

#2

Hi you can use ofDrawBitmapStringHighlight(string , float x, float y) which will use the default bitmap string (there is no need to load the font or anything) and it will put a black background and white letters. If you pass a 4th and 5th arguments you can specify its colors.
If you want to use another font you can call ofTrueTypeFont::getStringBoundingBox(...) which will return the rectangle that contains the text, which then you can draw.

1 Like
#3

Hi @roymacdonald,

Thank you for your answer.
It works!