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.
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.
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;
};
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