font.drawString() does not show up on screen

Hi there, I have created a couple of classes in my project, both of which use the same font (stored in the data folder). I am declaring the fonts in the header files of the classes and then calling the header.load(“header.ttf”, 23) in the constructors (header is the name of my font). Finally, I am calling header.drawString(“hello”, 500, 500) in the classes draw function.

My problem is that in one of my classes the font draws to the screen as expected, but in the other, the font does not draw at all. I have also tried using ofDrawBitmapString in this second function which worked, so I know the draw function is being called properly. Does anyone know why this might be?

Many thanks.

Here is the problematic classes header file:

#pragma once

#include "ofMain.h"

class Tweet
{
	//integer variables for tweet data
	//height variable will be calculated based on tweet lengthand any media included, yVal is the sum height of all tweets above current one.
	int likes, reTweets, replies, height, yVal;
	//string variables for names and tweet content (text)
	string userName, displayName, content;
	ofTrueTypeFont header, main;
public:
	//default class constructor:
	Tweet();
	//class constructor sets all tweet variables:
	Tweet(int likes, int reTweets, int replies, string userName, string displayName, string content);

	int calcHeight();	//method to calculate tweet height:
	//setters:		(the rest are set in class constructor)

	void draw(int y);	//renders tweets with y param as height.
};

Here is its .cpp file:

#include "Tweet.h"

//default class constructor
Tweet::Tweet() {
	std::cout << "No tweet constructors supplied" << endl;
}

//class constructor sets all tweet variables:
Tweet::Tweet(int likes, int reTweets, int replies, string userName, string displayName, string content) {
	this->likes = likes;
	this->reTweets = reTweets;
	this->replies = replies;
	this->userName = userName;
	this->displayName = displayName;
	this->content = content;

	header.load("header.ttf", 23);	//load font

	//cout << likes << endl << reTweets << endl << replies << endl << userName << endl << displayName << endl << content << endl << endl << endl;
}	//may need to change this function to a normal getter function to be able to set vector using a for loop

int Tweet::calcHeight() {		//method to calculate tweet height:

	return 0;
}

void Tweet::draw(int y) {			//renders tweets with y param as height.
	ofSetColor(0);
	
	header.drawString("Font String", 500, 500);
	
	ofDrawBitmapString("Bitmap String", 500, 500);

	cout << "tweet.draw() called" << endl;
}

I have now solved this problem by calling header.load("header.ttf", 23); in a setup function, not the constructor.

1 Like

Hi,
I usually do the same think for all the initializations that depend on OF components. This is the best method I’ve found to be sure that OF is properly initialized before I initialize my objects. And of course I call these setup() methods in ofApp::setup(), or later.