More incomplete gl text code

Originally committed to SVN as r1063.
This commit is contained in:
Rodrigo Braz Monteiro 2007-04-12 20:37:56 +00:00
parent b91f8f29ae
commit 50c14d9067
2 changed files with 62 additions and 20 deletions

View File

@ -54,6 +54,16 @@ OpenGLText::OpenGLText() {
//////////////
// Destructor
OpenGLText::~OpenGLText() {
Reset();
}
/////////
// Reset
void OpenGLText::Reset() {
for (unsigned int i=0;i<textures.size();i++) delete textures[i];
textures.clear();
glyphs.clear();
}
@ -108,27 +118,23 @@ OpenGLTextGlyph OpenGLText::CreateGlyph(int n) {
OpenGLTextGlyph glyph;
glyph.x = 0;
glyph.y = 0;
glyph.w = 0;
glyph.h = 0;
glyph.w = 16;
glyph.h = 16;
glyph.tex = 0;
// Render glyph;
int w = 16;
int h = 16;
// Insert into some texture
bool ok = false;
for (unsigned int i=0;i<textures.size();i++) {
if (textures[i]->CanFit(w,h)) {
textures[i]->Insert(glyph);
if (textures[i]->TryToInsert(glyph)) {
ok = true;
break;
}
}
// No texture could fit it, create a new one
if (!ok) {
textures.push_back(new OpenGLTextTexture());
textures.back()->Insert(glyph);
textures.push_back(new OpenGLTextTexture(256,256));
textures.back()->TryToInsert(glyph);
}
// Set glyph and return it
@ -139,25 +145,54 @@ OpenGLTextGlyph OpenGLText::CreateGlyph(int n) {
///////////////////////
// Texture constructor
OpenGLTextTexture::OpenGLTextTexture() {
OpenGLTextTexture::OpenGLTextTexture(int w,int h) {
width = w;
height = h;
tex = 0;
}
//////////////////////
// Texture destructor
OpenGLTextTexture::~OpenGLTextTexture() {
if (tex) {
tex = 0;
}
}
//////////////////////////
// Can fit a glyph in it?
bool OpenGLTextTexture::CanFit(int w,int h) {
bool OpenGLTextTexture::TryToInsert(OpenGLTextGlyph glyph) {
// Get size
int w = glyph.w;
int h = glyph.h;
// Can fit in this row?
if (x + w < width) {
x += w;
if (y+h > nextY) nextY = y+h;
Insert(glyph);
return true;
}
////////////////////////
// Insert a glyph in it
void OpenGLTextTexture::Insert(OpenGLTextGlyph glyph) {
// Can fit the next row?
else {
if (w < width) {
y = nextY;
nextY = y+h;
x = 0;
Insert(glyph);
return true;
}
}
// Can't fit
return false;
}
//////////
// Insert
void OpenGLTextTexture::Insert(OpenGLTextGlyph glyph) {
}

View File

@ -48,7 +48,8 @@
class OpenGLTextGlyph {
public:
int tex;
float x,y,w,h;
float x,y;
int w,h;
};
typedef std::map<int,OpenGLTextGlyph> glyphMap;
@ -57,13 +58,18 @@ typedef std::map<int,OpenGLTextGlyph> glyphMap;
///////////////
// Texture map
class OpenGLTextTexture {
private:
int x,y,nextY;
int width,height;
void Insert(OpenGLTextGlyph glyph);
public:
GLuint tex;
bool CanFit(int w,int h);
void Insert(OpenGLTextGlyph glyph);
bool TryToInsert(OpenGLTextGlyph glyph);
OpenGLTextTexture();
OpenGLTextTexture(int w,int h);
~OpenGLTextTexture();
};
@ -83,6 +89,7 @@ private:
OpenGLTextGlyph GetGlyph(int i);
OpenGLTextGlyph CreateGlyph(int i);
void Reset();
static OpenGLText* GetInstance();
void DoSetFont(wxString face,int size);