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 // Destructor
OpenGLText::~OpenGLText() { 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; OpenGLTextGlyph glyph;
glyph.x = 0; glyph.x = 0;
glyph.y = 0; glyph.y = 0;
glyph.w = 0; glyph.w = 16;
glyph.h = 0; glyph.h = 16;
glyph.tex = 0; glyph.tex = 0;
// Render glyph;
int w = 16;
int h = 16;
// Insert into some texture // Insert into some texture
bool ok = false; bool ok = false;
for (unsigned int i=0;i<textures.size();i++) { for (unsigned int i=0;i<textures.size();i++) {
if (textures[i]->CanFit(w,h)) { if (textures[i]->TryToInsert(glyph)) {
textures[i]->Insert(glyph);
ok = true; ok = true;
break;
} }
} }
// No texture could fit it, create a new one // No texture could fit it, create a new one
if (!ok) { if (!ok) {
textures.push_back(new OpenGLTextTexture()); textures.push_back(new OpenGLTextTexture(256,256));
textures.back()->Insert(glyph); textures.back()->TryToInsert(glyph);
} }
// Set glyph and return it // Set glyph and return it
@ -139,25 +145,54 @@ OpenGLTextGlyph OpenGLText::CreateGlyph(int n) {
/////////////////////// ///////////////////////
// Texture constructor // Texture constructor
OpenGLTextTexture::OpenGLTextTexture() { OpenGLTextTexture::OpenGLTextTexture(int w,int h) {
width = w;
height = h;
tex = 0;
} }
////////////////////// //////////////////////
// Texture destructor // Texture destructor
OpenGLTextTexture::~OpenGLTextTexture() { OpenGLTextTexture::~OpenGLTextTexture() {
if (tex) {
tex = 0;
}
} }
////////////////////////// //////////////////////////
// Can fit a glyph in it? // Can fit a glyph in it?
bool OpenGLTextTexture::CanFit(int w,int h) { bool OpenGLTextTexture::TryToInsert(OpenGLTextGlyph glyph) {
return true; // 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;
}
// 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 a glyph in it // Insert
void OpenGLTextTexture::Insert(OpenGLTextGlyph glyph) { void OpenGLTextTexture::Insert(OpenGLTextGlyph glyph) {
} }

View File

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