problem with class and drawing

Hi there,

I have a data.h file and a data.cpp in my project where I keep all my classes. I want to add a .draw() method to a class so that it draws text when called in testApp.cpp. This is the method

  
void textObjectCollection::draw () {  
	  
	for (int i=data.size(); i==0; i--) {  
		  
		float aNumber = float(i)/float(data.size());  
		float scale = if (aNumber > 0.5) {aNumber} else {0.5} ;  
		  
		ofPushMatrix();  
		ofSetColor(255,255,200);  
		ofScale(scale,scale,1);  
		---->> this is the problematic line -----> someFont.drawString(data[i].text, data[i].x, data[i].y);  
		ofPopMatrix();  
	}   

in the constructor I have

  
textObjectCollection::textObjectCollection () {  
	someFont.loadFont("Microsoft Sans Serif.ttf", 24, true, true);  
}  

If I comment out the problematic line my app works fine - else it gets compiled and then I get an : “EXC_BAD_ACCESS” error and stops

how can I do this ?? It is way more convient to have a .draw() in my class than pass data from it to a draw method in testApp.cpp

thx

try loading the font outside of the constructor, in a setup method for example. depending where you are instantiating the class, since it has to reserve opengl resources, doing it in the constructor can be problematic

I try to load it in the very draw() method but it failed, too. I will make a setup method and call this from the draw() instead to see if this works

thx

then i guess is not related to that, debugging your application will tell you what’s going on, but i will suspect of:

data[i].text

not being initialized or something, what’s data exactly?

  
class textObject {  
	  
public:	  
	  
	string text; // to hold the text  
	int x; // to hold x coordinate  
	int y; // to hold y coordinate  
	string date; // to hold the date  
	unsigned fontSize;  
	  
	textObject (string, int, int, string, unsigned);  
	//~textObject ();  
	  
	void move (int posx, int posy);  
	void appendToFile (string filename);  
	int textSize ();  
	  
};  
  
class textObjectCollection {  
	  
	ofTrueTypeFont someFont;  
	  
public:  
	  
	deque <textObject> data;  
	  
	textObjectCollection ();  
	void add (textObject object);  
	unsigned size ();// add an element to tail  
	void draw ();  
	void setup ();  
	// void remove (); // remove an element from start  
	// void autoRemove (int n); // remove an element from start when size exceeds n  
};  

it seems u r right, the problem is elsewhere.

I made a setup method to load the string and when I print some test string it is ok, I’ ll try to isolate what the problem is

thx for the help

the problem is here:

  
 for (int i=data.size(); i==0; i--) {    

the first element you are trying to access is out of bounds, it should be:

  
 for (int i=data.size()-1; i>=0; i--) {    

or easier

  
 for (int i=0; i<data.size(); i--) {    

unless you really need to go through the elements backwards

thx… :slight_smile: