textures don't like <vector>?

hi list,

I’ve been trying to add instances of a class that contains an ofTrueTypeFont to a vector. However,it seems the class’ constructor cannot allocate textures when the instances are created for a vector…
I then tried it with a simple texture and the result is only a white square (the result of not allocating the array of pixels)

class:

  
  
class fontTest{  
 public:  
  
 test(int _x, int _y, int _s) {  
     x=_x;  
     y=_y;  
     s=_s;  
       
     font.loadFont("fonts/Vera.ttf",s,true,true);  
  
 }  
  
 void draw(){  
     font.drawString("hahaha",x,y);  
      
 }  
  
    int x,y,s;  
    ofTrueTypeFont font;  
  
  
  
};  
  

testApp.cpp:

  
  
// declare vector  
  
	std::vector<fontTest> testVector;  
  
// in testApp::setup:  
  
	testVector.push_back(fontTest(100,100,20));  
  
// in testApp::draw()  
  
	testVector[0].draw();  

the second class with a texture:

  
  
class textureTest{  
 public:  
  
 test(int _x, int _y, int _s) {  
     x=_x;  
     y=_y;  
     s=_s;  
  
    texture.allocate(s,s,GL_RGBA);  
    grayPixels			= new unsigned char [s*s];  
    for (int i = 0; i < s; i++){  
		for (int j = 0; j < s; j++){  
			grayPixels[j*s+i] = (unsigned char)(ofRandomuf() * 125);  
		}  
	}  
	texture.loadData(grayPixels, s,s, GL_LUMINANCE);  
 }  
  
 void draw(){  
  
    texture.draw(x,y);  
  
 }  
  
    int x,y,s;  
  
    ofTexture texture;  
    unsigned char 	* grayPixels;  
  
  
};  

Am I doing something wrong? I’m starting to think that vectors and textures somehow may not work together.

Thanks for your attention!

Regards,
Rodrigo

Hi
i’ve used vectors and textures (with ofImage, ofVideoGrabber, etc) and never had problems.
so its probably something else…
i dont know why your text is not working but looking at the texture code that you have there is a place that could be problematic, you’re creating a texture with 4 channels (GL_RGBA) and you’re passing pixel information with only one channel (GL_LUMINANCE), that might explain why you can only see a white rectangle.

yes, you’re right, there was a mistake there.

however, after replacing GL_RGBA with GL_LUMINANCE there was no improvement.

I also tried vector with ofImage and there were no problems, but I’m not sure if ofImage is actually uploading textures to the graphics card or not…

So at the moment I’m a bit unclear what to try next, since the syntax seems to be correct (it is working for ofImage, after all)

thanks!!

Rodrigo

I guess you have to work with pointers (at least I would do it that way). And as far as I can see your class constructor is named wrong (only checked the font version). Maybe you should try this:
your class:

  
  
#include "ofMain.h"  
  
class fontTest{  
public:  
	fontTest(int _x, int _y, int _s) {  
		x=_x;  
		y=_y;  
		s=_s;  
		//font.loadFont("fonts/Vera.ttf",s,true,true);  
		font.loadFont("Verdana.ttf",s,true,true);  
	}  
	void draw(){  
		font.drawString("hahaha",x,y);  
	}  
    int x,y,s;  
    ofTrueTypeFont font;  
};  
  

testApp.h:

  
std::vector<fontTest*> testVector;  

testApp.cpp

  
  
void testApp::setup(){  
	testVector.push_back(new fontTest(100,100,20));  
}  
void testApp::draw(){  
	testVector[0]->draw();  
}  
  

If anyone could tell why we have to work with pointers here, well, I would be interested, too!

Regards,
Gestalt

IMHO you’ll get nasty errors if the copy-constructor of any class is not defined well.

  
  
 testVector.push_back(fontTest(100,100,20));  
  

this line does: create a fontTest-object on the stack, testVector gets a reference to this object. vector copies a copy of this object into the array and returns, the temporary fontTest-object onto the stack get destructed.

So if any class inside fontTest has an incomplete copy-constructor the copy will fail, A simple test would be

  
  
fontTest test1(100,100,20);  
fontTest test2 = test1;  
  
test2.draw();  
  

If this fails then there’s a problem with a copy constructor (most probably in the ofTrueTypeFont-class)

There are even more pitfalls. Consider this scenario:

  
  
class Data {  
};  
  
class Test {  
public:  
	Test()   
	{   
		_data = new Data();   
	}  
	~Test()   
	{   
		std::cout << "deleting " << _data << std::endl;  
		if (_data)   
			delete _data;   
	}  
private:  
	Data* _data;  
};  
  
  
int main (int argc, char * const argv[]) {  
      
	{  
		Test t1;  
		Test t2 = t1;  
	}  
	  
	return 0;  
}  
  

If you run this code, you’ll get a double free, because two objects have the ownership over the _data-member.

These are the nasty parts of c++ and this is one reason why I am using reference-counted objects in c++, use them and forget about memory management.

cheers,
Stephan