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:
class Thing {
public:
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.
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:
Thing.h
#ifndef _THING_H
#define _THING_H
#include "ofTrueTypeFont.h"
class Thing {
public:
Thing();
void draw(ofTrueTypeFont * fonteref);
float someXposition;
float someYposition;
//other class functions and variables here...
};
#endif
Thing.cpp
#include "Thing.h"
Thing::Thing(){
//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);
}
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.
class wordCircle : public ofBaseApp{
public:
void draw(ofTrueTypeFont *);
};
and then in testApp.h (I don’t know if this is neccesary to write that up, but why not )
class testApp : public ofBaseApp{
public:
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:
circle1.draw(&verdana);
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: