From 4abe0d02fa68cf488128e687b0031efb897a03b3 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 7 Jun 2008 21:19:11 +0100 Subject: [PATCH] Fix hint text --- src/Font.cpp | 76 ++++++++++++++++++++++++++++++++-------------------- src/Font.hpp | 8 ++++-- src/Main.cpp | 8 ------ src/Menu.cpp | 34 ++++++++--------------- 4 files changed, 64 insertions(+), 62 deletions(-) diff --git a/src/Font.cpp b/src/Font.cpp index 269b284..563abe4 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -31,6 +31,8 @@ Font::Font(string filename, unsigned int h) unsigned char i; + buf = new char[MAX_TXT_BUF]; + // Allocate memory for textures textures = new GLuint[128]; height = (float)h; @@ -61,6 +63,7 @@ Font::~Font() glDeleteTextures(128, textures); delete[] textures; delete[] widths; + delete[] buf; if (--fontRefCount == 0) { FT_Done_FreeType(library); @@ -109,7 +112,9 @@ void Font::MakeDisplayList(FT_Face face, char ch, GLuint listBase, for (j = 0; j < height; j++) { for (i = 0; i < width; i++) { expandedData[2*(i+j*width)] = expandedData[2*(i+j*width)+1] = - (i >= bitmap.width || j >= bitmap.rows) ? 0 : bitmap.buffer[i + bitmap.width*j]; + (i >= bitmap.width || j >= bitmap.rows) + ? 0 + : bitmap.buffer[i + bitmap.width*j]; } } @@ -159,30 +164,15 @@ void Font::MakeDisplayList(FT_Face face, char ch, GLuint listBase, glEndList(); } -void Font::Print(int x, int y, const char *fmt, ...) +void Font::SplitIntoLines(vector &lines, const char *fmt, va_list ap) { - vector lines; - - // Store the current matrix - glPushMatrix(); - - GLuint font = listBase; - float h = height / 0.63f; // Add some space between lines - const int MAX_TEXT_LEN = 256; - char text[MAX_TEXT_LEN]; - va_list ap; - if (fmt == NULL) - *text=0; - else { - va_start(ap, fmt); - vsnprintf(text, MAX_TEXT_LEN, fmt, ap); - va_end(ap); - } - - // Split text into lines - based on NeHe code - const char *start_line = text, *c; - for (c = text; *c; c++) { + *buf = 0; + else + vsnprintf(buf, MAX_TXT_BUF, fmt, ap); + + const char *start_line = buf, *c; + for (c = buf; *c; c++) { if (*c == '\n') { string line; for (const char *n = start_line; n < c; n++) @@ -198,6 +188,21 @@ void Font::Print(int x, int y, const char *fmt, ...) line.append(1, *n); lines.push_back(line); } +} + +void Font::Print(int x, int y, const char *fmt, ...) +{ + // Store the current matrix + glPushMatrix(); + + GLuint font = listBase; + float h = height / 0.63f; // Add some space between lines + + vector lines; + va_list ap; + va_start(ap, fmt); + SplitIntoLines(lines, fmt, ap); + va_end(ap); // Set attributes glEnable(GL_TEXTURE_2D); @@ -221,12 +226,25 @@ void Font::Print(int x, int y, const char *fmt, ...) glPopMatrix(); } -int Font::GetStringWidth(const char *s) +int Font::GetStringWidth(const char *fmt, ...) { - size_t i = strlen(s); - int result = 0; - while (i--) - result += widths[s[i]]; + va_list ap; + vector lines; + + va_start(ap, fmt); + SplitIntoLines(lines, fmt, ap); + va_end(ap); + + int maxlen = 0; + vector::iterator it; + for (it = lines.begin(); it != lines.end(); ++it) { + int len = 0; + for (string::iterator ch = (*it).begin(); ch != (*it).end(); ++ch) + len += widths[*ch]; + + if (len > maxlen) + maxlen = len; + } - return result; + return maxlen; } diff --git a/src/Font.hpp b/src/Font.hpp index 971cda4..6cdd12a 100644 --- a/src/Font.hpp +++ b/src/Font.hpp @@ -27,17 +27,21 @@ public: Font(string filename, unsigned int h); ~Font(); - void Print(int x, int y, const char *fmt, ...); - int GetStringWidth(const char *s); + void Print(int x, int y, const char *fmt, ...); + int GetStringWidth(const char *fmt, ...); private: int NextPowerOf2(int a); void MakeDisplayList(FT_Face face, char ch, GLuint listBase, GLuint *texBase, unsigned short *widths); + void SplitIntoLines(vector &lines, const char *fmt, va_list ap); GLuint *textures; GLuint listBase; float height; unsigned short *widths; + char *buf; + + static const int MAX_TXT_BUF = 1024; static int fontRefCount; static FT_Library library; diff --git a/src/Main.cpp b/src/Main.cpp index f55b149..5afe678 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -76,14 +76,6 @@ int main(int argc, char **argv) sm.AddScreen("GAME", g); sm.AddScreen("HIGH SCORES", hs); - // Load fonts - /*FreeType &ft = FreeType::GetInstance(); - ft.LoadFont(ftNormal, File::LocateResource("Default_Font", "ttf"), 11); - ft.LoadFont(ftBig, File::LocateResource("Default_Font", "ttf"), 20); - ft.LoadFont(ftScore, File::LocateResource("Default_Font", "ttf"), 16); - ft.LoadFont(ftScoreName, File::LocateResource("Default_Font", "ttf"), 14); - ft.LoadFont(ftLarge, File::LocateResource("Default_Font", "ttf"), 15);*/ - // Run the game sm.SelectScreen("MAIN MENU"); opengl.Run(); diff --git a/src/Menu.cpp b/src/Menu.cpp index 22b47ee..97d8b7e 100644 --- a/src/Menu.cpp +++ b/src/Menu.cpp @@ -230,17 +230,14 @@ void MainMenu::Display() // Draw some hint texts const int numhints = 7; - const char *hints[][2] = { - { i18n("Use the arrow keys to rotate the ship"), "" }, - { i18n("Press the up arrow to fire the thruster"), "" }, - { i18n("Smaller landing pads give you more points"), "" }, - { i18n("Press P to pause the game"), "" }, - { i18n("Press escape to self destruct"), "" }, - // TODO: automatically split text into multiple lines - { i18n("You can only land safely when the"), - i18n("speed bar is green") }, - { i18n("Collect the spinning rings to"), - i18n("unlock the landing pads") } + const char *hints[] = { + i18n("Use the arrow keys to rotate the ship"), + i18n("Press the up arrow to fire the thruster"), + i18n("Smaller landing pads give you more points"), + i18n("Press P to pause the game"), + i18n("Press escape to self destruct"), + i18n("You can only land safely when then speed bar is green"), + i18n("Collect the spinning rings to unlock the landing pads") }; if (hint_timeout == 0) { @@ -251,18 +248,9 @@ void MainMenu::Display() hint_timeout--; glColor4d(0.0, 1.0, 0.0, fade); - hintFont.Print(0, opengl.GetHeight() - 120, hints[hintidx][0]); - - /* - opengl.Colour(0.0f, 1.0f, 0.0f, fade); - ft.Print(ftNormal, - (opengl.GetWidth() - ft.GetStringWidth(ftNormal, hints[hintidx][0])) / 2, - opengl.GetHeight() - 120, - hints[hintidx][0]); - ft.Print(ftNormal, - (opengl.GetWidth() - ft.GetStringWidth(ftNormal, hints[hintidx][1])) / 2, - opengl.GetHeight() - 100, - hints[hintidx][1]);*/ + int x = (opengl.GetWidth() - hintFont.GetStringWidth(hints[hintidx])) / 2; + int y = opengl.GetHeight() - 120; + hintFont.Print(x, y, hints[hintidx]); } double MenuStar::starRotate = 0.0; -- 2.39.2