Fonts in multiple classes?


I made a sketch in Processing, but since Processing is to slow for that sketch I decided to use openFrameworks instead. (Just to illustrate on what c++ level I am)

But I have a problem involving fonts. (and probably all other variables)

I have a font that I want to use in multiple classes. It does work when I reload it for each object of a class. But there must be a way to use a single font in multiple objects and classes.

i don’t know if this is what you are after, and if the solution is the most correct one.
i had the same problem the other day and the solution i found was the following:

in testApp.h

ofTrueTypeFont fonte;  

then in testApp.cpp

void testApp::setup(){  
fonte.loadFont("letra.ttf", 12, true,false);  

so you have a font called “fonte”

now in the other classes

you should have in your .h file the following:

class Thing {  
  void draw(ofTrueTypeFont * fonteref);  

now in the .cpp file

void Thing::draw(ofTrueTypeFont * fonteref){  
   fonteref->drawString("hahaha this is my string", someXposition, someYposition);  

there should be a workaround for not having to call the draw() with the “ofTrueTypeFont * fonteref” inside it, probably the classes should have a ofTrueTypeFont variable which is a reference or pointer (don’t really know the correct term for this) to the main one. i don’t know how to do that, so i just send a pointer to the font every time i draw.
it’s not correct i guess but it get things done.
also i’m not really sure about this but in the Thing.h you should probably have this:

#include “ofTrueTypeFont.h”

again, this probably is not the most correct approach, but i was really needing to get a font working in other classes and the solution i found was this.

Thanks for your reply! :slight_smile:

But I’m not really sure how to use pointers.

void testApp::draw(){  
	Thing::draw(ofTrueTypeFont * fonte);  

Do I use it in this way? Because if I do I get an “Illegal” error.

Thing::draw(ofTrueTypeFont * fonte); should be written inside the Thing.h file were Thing is your custom Class, not inside the testApp draw() method.

like this:


#ifndef _THING_H  
#define _THING_H  
#include "ofTrueTypeFont.h"  
class Thing {  
  void draw(ofTrueTypeFont * fonteref);  
  float someXposition;  
  float someYposition;  
//other class functions and variables here...  


#include "Thing.h"  
//define the initial values for the position  
   someXposition = 100;  
   someYposition = 20;  
void Thing::draw(ofTrueTypeFont * fonteref){  
//this will write the text "hahaha this is my string" at x: 100 and y: 20;  
   fonteref->drawString("hahaha this is my string", someXposition, someYposition);  

Just found i forgot to mention something…

you have a ofTrueTypeFont called fonte and let’s assume you have a Thing class object called coisa.

in testApp draw()

void testApp::draw(){  
coisa.draw(&fonte);  //the &fonte tells the coisa to use the fonte declared in testApp  

sorry but i haven’t slept well these last days and sometimes can’t think straight…

Another thing you could maybe do is to create a “singleton” class which just contains the font and render it from there.

A singleton is basically just an object which contains one and only one instance of itself; along with its font and some function to render the font with. Then you can just call it whenever.

could you post an example of creating and using a singleton?

Thanks :slight_smile:

I needed the code for the testApp.cpp draw() :slight_smile:

And half of the “cant think straight” part is mine, I haven’t had much sleep either.

For the Singleton part:
after some googling I found this, I haven’t read it yet, but it looks like a good article.

I found the solution!

in testApp.cpp:

void testApp::setup(){  
	verdana.loadFont("verdana.ttf", 32, true, false);   
void testApp::draw(){  

and then in your_class.h

class wordCircle : public ofBaseApp{  
		void draw(ofTrueTypeFont *);  

and then in testApp.h (I don’t know if this is neccesary to write that up, but why not :stuck_out_tongue: )

 class testApp : public ofBaseApp{  
		wordCircle circle1;  
		ofTrueTypeFont verdana;  

and then in your_class.cpp

void wordCircle::draw(ofTrueTypeFont * font){  
	font->drawString("hahaha this is my string", 200, 200);  

Where you went wrong, is when you called
circle1.draw(ofTrueTypeFont * verdana);

because you redeclared verdana, to not be a font but to be a pointer.

What you should do is:

In that case you give the memory adress of the verdana font to the function, and then in the wordCircle.draw() funtion you describe the received variable to be a pointer to the adress it has been given:

wordCircle.draw(ofTrueTypeFont * font)

now font is the verdana, so you can use it:


1 Like


I had the same problem.