From d74d87764f4fc1275705fe5ae4c26b3f577a0af3 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 7 Jun 2008 21:01:05 +0100 Subject: [PATCH] Get rid of FreeType class --- src/Font.cpp | 232 +++++++++++++++ src/{FreeType.hpp => Font.hpp} | 36 +-- src/FreeType.cpp | 503 --------------------------------- src/Game.cpp | 55 ++-- src/HighScores.cpp | 75 ++--- src/HighScores.hpp | 4 +- src/Lander.hpp | 3 + src/Main.cpp | 4 +- src/Makefile.am | 2 +- src/Menu.cpp | 2 - src/Menu.hpp | 2 +- src/OpenGL.hpp | 1 - 12 files changed, 293 insertions(+), 626 deletions(-) create mode 100644 src/Font.cpp rename src/{FreeType.hpp => Font.hpp} (54%) delete mode 100644 src/FreeType.cpp diff --git a/src/Font.cpp b/src/Font.cpp new file mode 100644 index 0000000..269b284 --- /dev/null +++ b/src/Font.cpp @@ -0,0 +1,232 @@ +/* + * FreeType.cpp -- A wrapper around FreeType. + * Copyright (C) 2006 Nick Gasson + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "Font.hpp" + +int Font::fontRefCount = 0; +FT_Library Font::library; + +Font::Font(string filename, unsigned int h) +{ + if (++fontRefCount == 1) { + if (FT_Init_FreeType(&library)) + throw runtime_error("FT_Init_FreeType failed"); + } + + unsigned char i; + + // Allocate memory for textures + textures = new GLuint[128]; + height = (float)h; + widths = new unsigned short[128]; + + // Create the face + FT_Face face; + if (FT_New_Face(library, filename.c_str(), 0, &face)) + throw runtime_error("FT_New_Face failed, file name: " + filename); + + // FreeType measures font sizes in 1/64ths of a pixel... + FT_Set_Char_Size(face, h<<6, h<<6, 96, 96); + + listBase = glGenLists(128); + glGenTextures(128, textures); + + // Generate the characters + for (i = 0; i < 128; i++) + MakeDisplayList(face, i, listBase, textures, widths); + + // Free face data + FT_Done_Face(face); +} + +Font::~Font() +{ + glDeleteLists(listBase, 128); + glDeleteTextures(128, textures); + delete[] textures; + delete[] widths; + + if (--fontRefCount == 0) { + FT_Done_FreeType(library); + } +} + +int Font::NextPowerOf2(int a) +{ + int rval = 1; + + while (rval < a) + rval <<= 1; + + return rval; +} + +void Font::MakeDisplayList(FT_Face face, char ch, GLuint listBase, + GLuint *texBase, unsigned short *widths) +{ + int i, j; + + // Load the character's glyph + if (FT_Load_Glyph(face, FT_Get_Char_Index(face, ch), FT_LOAD_DEFAULT)) + throw runtime_error("FT_Load_Glyph failed"); + + // Store the face's glyph in a glyph object + FT_Glyph glyph; + if (FT_Get_Glyph(face->glyph, &glyph)) + throw runtime_error("FT_Get_Glyph failed"); + + // Convert the glyph to a bitmap + FT_Glyph_To_Bitmap(&glyph, ft_render_mode_normal, 0, 1); + FT_BitmapGlyph bitmapGlyph = (FT_BitmapGlyph)glyph; + + // Get a reference to the bitmap + FT_Bitmap& bitmap = bitmapGlyph->bitmap; + + // Make the width and height a power of 2 + int width = NextPowerOf2(bitmap.width); + int height = NextPowerOf2(bitmap.rows); + + // Allocate memory for the texture data + GLubyte* expandedData = new GLubyte[2 * width * height]; + + // Fill in the bitmap's extended data + 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]; + } + } + + // Set texture parameters + glBindTexture(GL_TEXTURE_2D, texBase[ch]); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + + // Create the texture + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, + GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expandedData); + + // Free expanded data + delete[] expandedData; + + // Create the display list + glNewList(listBase+ch, GL_COMPILE); + glBindTexture(GL_TEXTURE_2D, texBase[ch]); + + glPushMatrix(); + + // Insert some space between characters + glTranslatef((float)bitmapGlyph->left, 0, 0); + widths[ch] = bitmapGlyph->left; + + // Move down a bit to accomodate characters such as p and q + glTranslatef(0, (float)(-bitmapGlyph->top), 0); + + float x = (float)bitmap.width / (float)width; + float y = (float)bitmap.rows / (float)height; + + // Draw the quad + glBegin(GL_QUADS); + glTexCoord2f(0, y); glVertex2f(0, (float)bitmap.rows); + glTexCoord2f(0, 0); glVertex2f(0, 0); + glTexCoord2f(x, 0); glVertex2f((float)bitmap.width, 0); + glTexCoord2f(x, y); glVertex2f((float)bitmap.width, (float)bitmap.rows); + glEnd(); + + glPopMatrix(); + + // Move along to the next character + glTranslatef((float)(face->glyph->advance.x >> 6), 0, 0); + widths[ch] += (short)face->glyph->advance.x >> 6; + + // Finish display list + glEndList(); +} + +void Font::Print(int x, int y, const char *fmt, ...) +{ + 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++) { + if (*c == '\n') { + string line; + for (const char *n = start_line; n < c; n++) + line.append(1, *n); + lines.push_back(line); + start_line = c+1; + } + } + + if (start_line) { + string line; + for (const char *n = start_line; n < c; n++) + line.append(1, *n); + lines.push_back(line); + } + + // Set attributes + glEnable(GL_TEXTURE_2D); + glDisable(GL_DEPTH_TEST); + glEnable(GL_BLEND); + glListBase(font); + + // Draw the text + for (unsigned int i = 0; i < lines.size(); i++) { + glPushMatrix(); + glLoadIdentity(); + glTranslatef((float)x, (float)y - h*i, 0.0f); + + glCallLists((GLsizei)lines[i].length(), GL_UNSIGNED_BYTE, lines[i].c_str()); + + glPopMatrix(); + } + + // Restore previous matrix + glPopMatrix(); + glPopMatrix(); +} + +int Font::GetStringWidth(const char *s) +{ + size_t i = strlen(s); + int result = 0; + while (i--) + result += widths[s[i]]; + + return result; +} diff --git a/src/FreeType.hpp b/src/Font.hpp similarity index 54% rename from src/FreeType.hpp rename to src/Font.hpp index 5071363..971cda4 100644 --- a/src/FreeType.hpp +++ b/src/Font.hpp @@ -1,5 +1,5 @@ /* - * FreeType.hpp - Definition of FreeType wrapper class. + * FreeType.hpp -- A wrapper around FreeType. * Copyright (C) 2006 Nick Gasson * * This program is free software; you can redistribute it and/or modify @@ -43,39 +43,5 @@ private: static FT_Library library; }; -/* - * Wrapper around the GNU FreeType font library. Code adapted from NeHe. - * tutorial #43. - */ -class FreeType -{ - public: - FreeType(); - ~FreeType(); - - // Public methods - static FreeType &GetInstance(); - void LoadFont(int index, string face, unsigned int h); - void Print(int index, int x, int y, const char *fmt, ...); - int GetStringWidth(int index, const char *s); - private: - // Holds data about a single font - struct FreeTypeFont - { - float height; // The height of the font in pixels - GLuint *textures; // Array of texture IDs - GLuint listBase; // Display list base - unsigned short *widths; // The widths of each character - }; - - // Private methods - int NextPowerOf2(int a); - void MakeDisplayList(FT_Face face, char ch, GLuint listBase, GLuint *texBase, unsigned short *widths); - void CreateFont(FreeTypeFont *font, string filename, unsigned int height); - void DeleteFont(FreeTypeFont *font); - - FT_Library library; - map fonts; -}; #endif diff --git a/src/FreeType.cpp b/src/FreeType.cpp deleted file mode 100644 index cf071d8..0000000 --- a/src/FreeType.cpp +++ /dev/null @@ -1,503 +0,0 @@ -/* - * FreeType.cpp -- Implementation of FreeType wrapper class. - * Copyright (C) 2006 Nick Gasson - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "FreeType.hpp" - -#include "OpenGL.hpp" // TODO: Remove - -int Font::fontRefCount = 0; -FT_Library Font::library; - -Font::Font(string filename, unsigned int h) -{ - if (++fontRefCount == 1) { - if (FT_Init_FreeType(&library)) - throw runtime_error("FT_Init_FreeType failed"); - - cout << "Loaded freetype library" << endl; - } - - unsigned char i; - - // Allocate memory for textures - textures = new GLuint[128]; - height = (float)h; - widths = new unsigned short[128]; - - // Create the face - FT_Face face; - if (FT_New_Face(library, filename.c_str(), 0, &face)) - throw runtime_error("FT_New_Face failed, file name: " + filename); - - // FreeType measures font sizes in 1/64ths of a pixel... - FT_Set_Char_Size(face, h<<6, h<<6, 96, 96); - - listBase = glGenLists(128); - glGenTextures(128, textures); - - // Generate the characters - for (i = 0; i < 128; i++) - MakeDisplayList(face, i, listBase, textures, widths); - - // Free face data - FT_Done_Face(face); -} - -Font::~Font() -{ - glDeleteLists(listBase, 128); - glDeleteTextures(128, textures); - delete[] textures; - delete[] widths; - - if (--fontRefCount == 0) { - FT_Done_FreeType(library); - cout << "Unloaded freetype library" << endl; - } -} - -int Font::NextPowerOf2(int a) -{ - int rval = 1; - - while (rval < a) - rval <<= 1; - - return rval; -} - -void Font::MakeDisplayList(FT_Face face, char ch, GLuint listBase, - GLuint *texBase, unsigned short *widths) -{ - int i, j; - - // Load the character's glyph - if (FT_Load_Glyph(face, FT_Get_Char_Index(face, ch), FT_LOAD_DEFAULT)) - throw runtime_error("FT_Load_Glyph failed"); - - // Store the face's glyph in a glyph object - FT_Glyph glyph; - if (FT_Get_Glyph(face->glyph, &glyph)) - throw runtime_error("FT_Get_Glyph failed"); - - // Convert the glyph to a bitmap - FT_Glyph_To_Bitmap(&glyph, ft_render_mode_normal, 0, 1); - FT_BitmapGlyph bitmapGlyph = (FT_BitmapGlyph)glyph; - - // Get a reference to the bitmap - FT_Bitmap& bitmap = bitmapGlyph->bitmap; - - // Make the width and height a power of 2 - int width = NextPowerOf2(bitmap.width); - int height = NextPowerOf2(bitmap.rows); - - // Allocate memory for the texture data - GLubyte* expandedData = new GLubyte[2 * width * height]; - - // Fill in the bitmap's extended data - 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]; - } - } - - // Set texture parameters - glBindTexture(GL_TEXTURE_2D, texBase[ch]); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - - // Create the texture - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, - GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expandedData); - - // Free expanded data - delete[] expandedData; - - // Create the display list - glNewList(listBase+ch, GL_COMPILE); - glBindTexture(GL_TEXTURE_2D, texBase[ch]); - - glPushMatrix(); - - // Insert some space between characters - glTranslatef((float)bitmapGlyph->left, 0, 0); - widths[ch] = bitmapGlyph->left; - - // Move down a bit to accomodate characters such as p and q - glTranslatef(0, (float)(-bitmapGlyph->top), 0); - - float x = (float)bitmap.width / (float)width; - float y = (float)bitmap.rows / (float)height; - - // Draw the quad - glBegin(GL_QUADS); - glTexCoord2f(0, y); glVertex2f(0, (float)bitmap.rows); - glTexCoord2f(0, 0); glVertex2f(0, 0); - glTexCoord2f(x, 0); glVertex2f((float)bitmap.width, 0); - glTexCoord2f(x, y); glVertex2f((float)bitmap.width, (float)bitmap.rows); - glEnd(); - - glPopMatrix(); - - // Move along to the next character - glTranslatef((float)(face->glyph->advance.x >> 6), 0, 0); - widths[ch] += (short)face->glyph->advance.x >> 6; - - // Finish display list - glEndList(); -} - -void Font::Print(int x, int y, const char *fmt, ...) -{ - 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++) { - if (*c == '\n') { - string line; - for (const char *n = start_line; n < c; n++) - line.append(1, *n); - lines.push_back(line); - start_line = c+1; - } - } - - if (start_line) { - string line; - for (const char *n = start_line; n < c; n++) - line.append(1, *n); - lines.push_back(line); - } - - // Set attributes - glEnable(GL_TEXTURE_2D); - glDisable(GL_DEPTH_TEST); - glEnable(GL_BLEND); - glListBase(font); - - // Draw the text - for (unsigned int i = 0; i < lines.size(); i++) { - glPushMatrix(); - glLoadIdentity(); - glTranslatef((float)x, (float)y - h*i, 0.0f); - - glCallLists((GLsizei)lines[i].length(), GL_UNSIGNED_BYTE, lines[i].c_str()); - - glPopMatrix(); - } - - // Restore previous matrix - glPopMatrix(); - glPopMatrix(); -} - -int Font::GetStringWidth(const char *s) -{ - size_t i = strlen(s); - int result = 0; - while (i--) - result += widths[s[i]]; - - return result; -} - - - - - - - - - - - - -FreeType::FreeType() -{ - // Create FreeType library - if (FT_Init_FreeType(&library)) - throw runtime_error("FT_Init_FreeType failed!"); -} - -FreeType::~FreeType() -{ - // Close the library - FT_Done_FreeType(library); -} - -FreeType &FreeType::GetInstance() -{ - static FreeType ft; - - return ft; -} - - -/* - * Loads a font from a TTF file. The font can be referenced later by the - * index parameter. - * index -> Integer key to associate with font. - * face -> Name of TTF file. - * height -> Desired hight in pixels. - * Throws std::runtime_error on failure. - */ -void FreeType::LoadFont(int index, string face, unsigned int height) -{ - if (fonts.find(index) != fonts.end()) - throw runtime_error("Font index in use"); - - FreeTypeFont *fnt = new FreeTypeFont; - CreateFont(fnt, face, height); - - fonts[index] = fnt; -} - - -/* Creates a new font */ -void FreeType::CreateFont(FreeTypeFont *font, string filename, unsigned int h) -{ - unsigned char i; - - // Allocate memory for textures - font->textures = new GLuint[128]; - font->height = (float)h; - font->widths = new unsigned short[128]; - - // Create the face - FT_Face face; - if (FT_New_Face(library, filename.c_str(), 0, &face)) - throw runtime_error("FT_New_Face failed, file name: " + filename); - - // FreeType measures font sizes in 1/64ths of a pixel... - FT_Set_Char_Size(face, h<<6, h<<6, 96, 96); - - font->listBase = glGenLists(128); - glGenTextures(128, font->textures); - - // Generate the characters - for (i = 0; i < 128; i++) - MakeDisplayList(face, i, font->listBase, font->textures, font->widths); - - // Free face data - FT_Done_Face(face); -} - -/* Unloads a font */ -void FreeType::DeleteFont(FreeTypeFont *font) -{ - glDeleteLists(font->listBase, 128); - glDeleteTextures(128, font->textures); - delete[] font->textures; - delete[] font->widths; -} - -int FreeType::NextPowerOf2(int a) -{ - int rval = 1; - - while (rval < a) - rval <<= 1; - - return rval; -} - -void FreeType::MakeDisplayList(FT_Face face, char ch, GLuint listBase, - GLuint *texBase, unsigned short *widths) -{ - int i, j; - - // Load the character's glyph - if (FT_Load_Glyph(face, FT_Get_Char_Index(face, ch), FT_LOAD_DEFAULT)) - throw runtime_error("FT_Load_Glyph failed"); - - // Store the face's glyph in a glyph object - FT_Glyph glyph; - if (FT_Get_Glyph(face->glyph, &glyph)) - throw runtime_error("FT_Get_Glyph failed"); - - // Convert the glyph to a bitmap - FT_Glyph_To_Bitmap(&glyph, ft_render_mode_normal, 0, 1); - FT_BitmapGlyph bitmapGlyph = (FT_BitmapGlyph)glyph; - - // Get a reference to the bitmap - FT_Bitmap& bitmap = bitmapGlyph->bitmap; - - // Make the width and height a power of 2 - int width = NextPowerOf2(bitmap.width); - int height = NextPowerOf2(bitmap.rows); - - // Allocate memory for the texture data - GLubyte* expandedData = new GLubyte[2 * width * height]; - - // Fill in the bitmap's extended data - 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]; - } - } - - // Set texture parameters - glBindTexture(GL_TEXTURE_2D, texBase[ch]); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - - // Create the texture - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, - GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expandedData); - - // Free expanded data - delete[] expandedData; - - // Create the display list - glNewList(listBase+ch, GL_COMPILE); - glBindTexture(GL_TEXTURE_2D, texBase[ch]); - - glPushMatrix(); - - // Insert some space between characters - glTranslatef((float)bitmapGlyph->left, 0, 0); - widths[ch] = bitmapGlyph->left; - - // Move down a bit to accomodate characters such as p and q - glTranslatef(0, (float)(-bitmapGlyph->top), 0); - - float x = (float)bitmap.width / (float)width; - float y = (float)bitmap.rows / (float)height; - - // Draw the quad - glBegin(GL_QUADS); - glTexCoord2f(0, y); glVertex2f(0, (float)bitmap.rows); - glTexCoord2f(0, 0); glVertex2f(0, 0); - glTexCoord2f(x, 0); glVertex2f((float)bitmap.width, 0); - glTexCoord2f(x, y); glVertex2f((float)bitmap.width, (float)bitmap.rows); - glEnd(); - - glPopMatrix(); - - // Move along to the next character - glTranslatef((float)(face->glyph->advance.x >> 6), 0, 0); - widths[ch] += (short)face->glyph->advance.x >> 6; - - // Finish display list - glEndList(); -} - -void FreeType::Print(int index, int x, int y, const char *fmt, ...) -{ - vector lines; - - assert(fonts.find(index) != fonts.end()); - - FreeTypeFont *ft_font = fonts[index]; - - // Store the current matrix - glPushMatrix(); - - GLuint font = ft_font->listBase; - float h = ft_font->height / 0.63f; // Add some space between lines - const int MAX_TEXT_LEN = 256; - char text[MAX_TEXT_LEN]; - va_list ap; - - OpenGL &opengl = OpenGL::GetInstance(); - - 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++) { - if (*c == '\n') { - string line; - for (const char *n = start_line; n < c; n++) - line.append(1, *n); - lines.push_back(line); - start_line = c+1; - } - } - - if (start_line) { - string line; - for (const char *n = start_line; n < c; n++) - line.append(1, *n); - lines.push_back(line); - } - - // Set attributes - opengl.EnableTexture(); - opengl.DisableDepthBuffer(); - opengl.EnableBlending(); - //opengl.DisableLighting(); - - glListBase(font); - - // Draw the text - for (unsigned int i = 0; i < lines.size(); i++) { - glPushMatrix(); - glLoadIdentity(); - glTranslatef((float)x, (float)y - h*i, 0.0f); - - glCallLists((GLsizei)lines[i].length(), GL_UNSIGNED_BYTE, lines[i].c_str()); - - glPopMatrix(); - } - - // Restore previous matrix - glPopMatrix(); - glPopMatrix(); -} - -int FreeType::GetStringWidth(int index, const char *s) -{ - assert(fonts.find(index) != fonts.end()); - - FreeTypeFont *font = fonts[index]; - - size_t i = strlen(s); - int result = 0; - while (i--) - result += font->widths[s[i]]; - - return result; -} diff --git a/src/Game.cpp b/src/Game.cpp index 80c4166..d3a0853 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -60,10 +60,13 @@ Game::Game() speedmeter(&ship), state(gsNone), fadeTexture("images/fade.png"), - starImage("images/star.png"), levelComp("images/levelcomp.png"), smallShip("images/shipsmall.png"), - gameOver("images/gameover.png") + starImage("images/star.png"), + gameOver("images/gameover.png"), + normalFont(LocateResource("Default_Font.ttf"), 11), + scoreFont(LocateResource("Default_Font.ttf"), 16), + bigFont(LocateResource("Default_Font.ttf"), 20) { } @@ -535,7 +538,6 @@ void Game::ExplodeShip() void Game::Display() { - FreeType &ft = FreeType::GetInstance(); OpenGL &opengl = OpenGL::GetInstance(); // Draw the stars @@ -612,11 +614,9 @@ void Game::Display() ship.DrawExplosion(true); opengl.Colour(0.0f, 1.0f, 0.0f); const char *sdeath = i18n("Press SPACE to continue"); - ft.Print - (ftNormal, - (opengl.GetWidth() - ft.GetStringWidth(ftNormal, sdeath)) / 2, - opengl.GetHeight() - 40, - sdeath); + int x = (opengl.GetWidth() - normalFont.GetStringWidth(sdeath)) / 2; + int y = opengl.GetHeight() - 40; + normalFont.Print(x, y, sdeath); } else if (state == gsDeathWait || state == gsGameOver || state == gsFadeToDeath || state == gsFadeToRestart) { @@ -628,8 +628,8 @@ void Game::Display() (*it).DrawArrow(&viewport); // Draw HUD - opengl.Colour(0.0f, 0.9f, 0.0f); - ft.Print(ftScore, 10, SCORE_Y, "%.7d", score); + glColor3f(0.0f, 0.9f, 0.0f); + scoreFont.Print(10, SCORE_Y, "%.7d", score); fuelmeter.Display(); speedmeter.Display(); @@ -672,10 +672,8 @@ void Game::Display() } opengl.Colour(0.0f, 1.0f, 0.0f); const char *sland = i18n("Land now"); - ft.Print - (ftNormal, - (opengl.GetWidth() - ft.GetStringWidth(ftNormal, sland))/2, - 30, sland); + int x = (opengl.GetWidth() - normalFont.GetStringWidth(sland)) / 2; + normalFont.Print(x, 30, sland); } // Draw level complete messages @@ -685,24 +683,19 @@ void Game::Display() int lc_y = (opengl.GetHeight() - levelComp.GetHeight()) / 2 - 50; levelComp.Draw(lc_x, lc_y); - opengl.Colour(0.0f, 0.5f, 0.9f); - ft.Print - (ftBig, - (opengl.GetWidth() - ft.GetStringWidth(ftBig, scoretxt) - 40)/2, - (opengl.GetHeight() - 30)/2 + 50, - scoretxt, - newscore > 0 ? newscore : 0); + glColor3f(0.0f, 0.5f, 0.9f); + int x = (opengl.GetWidth() - bigFont.GetStringWidth(scoretxt) - 40) / 2; + int y = (opengl.GetHeight() - 30)/2 + 50; + bigFont.Print(x, y, scoretxt, newscore > 0 ? newscore : 0); } // Draw level number text if (leveltext_timeout) { opengl.Colour(0.9f, 0.9f, 0.0f); const char *lvltxt = i18n("Level %d"); - ft.Print - (ftBig, - (opengl.GetWidth() - ft.GetStringWidth(ftBig, lvltxt) - 20)/2, - (opengl.GetHeight() - 30)/2, - lvltxt, level); + int x = (opengl.GetWidth() - bigFont.GetStringWidth(lvltxt) - 20) / 2; + int y = (opengl.GetHeight() - 30) / 2; + bigFont.Print(x, y, lvltxt, level); } // Draw the fade @@ -719,12 +712,10 @@ void Game::Display() // Draw paused message if (state == gsPaused) { const char *txtpaused = i18n("Paused"); - opengl.Colour(0.0f, 0.5f, 1.0f); - ft.Print - (ftBig, - (opengl.GetWidth() - ft.GetStringWidth(ftBig, txtpaused) - 20)/2, - (opengl.GetHeight() - 150)/2, - txtpaused); + glColor3f(0.0f, 0.5f, 1.0f); + int x = (opengl.GetWidth() - bigFont.GetStringWidth(txtpaused) - 20) / 2; + int y = (opengl.GetHeight() - 150) / 2; + bigFont.Print(x, y, txtpaused); } } diff --git a/src/HighScores.cpp b/src/HighScores.cpp index 07b4bae..1331c55 100644 --- a/src/HighScores.cpp +++ b/src/HighScores.cpp @@ -19,13 +19,13 @@ #include "HighScores.hpp" #include "Input.hpp" -#include "FreeType.hpp" #include "File.hpp" -#include "Lander.hpp" // TODO: Remove HighScores::HighScores() - : hscoreImage("images/hscore.png") + : hscoreImage("images/hscore.png"), + largeFont(LocateResource("Default_Font.ttf"), 15), + scoreNameFont(LocateResource("Default_Font.ttf"), 14) { } @@ -154,7 +154,6 @@ void HighScores::Process() void HighScores::Display() { OpenGL &opengl = OpenGL::GetInstance(); - FreeType &ft = FreeType::GetInstance(); // Draw the fireworks for (int i = 0; i < MAX_FIREWORKS; i++) { @@ -169,64 +168,46 @@ void HighScores::Display() int x = (opengl.GetWidth() - 280) / 2; int y = (opengl.GetHeight() - 250) / 2; - opengl.Colour(0.0f, 1.0f, 0.0f, flAlpha); + glColor4f(0.0f, 1.0f, 0.0f, flAlpha); for (int i = 0; i < 10; i++) { - ft.Print(ftScoreName, x, y + 22*i, "%s", scoreFile[i].GetName()); - ft.Print(ftScoreName, x + 230, y + 22*i, "%d", scoreFile[i].GetScore()); + scoreNameFont.Print(x, y + 22*i, scoreFile[i].GetName()); + scoreNameFont.Print(x + 230, y + 22*i, "%d", scoreFile[i].GetScore()); } } // Draw other stuff - const char *hsnext = i18n("Press SPACE or FIRE to return"); + const char *hsnext = i18n("Press SPACE or FIRE to return"); if (state == hssDisplay) { int title_x = (opengl.GetWidth() - hscoreImage.GetWidth()) / 2; int title_y = 50; hscoreImage.Draw(title_x, title_y, 0.0, 1.0, flAlpha); + + int x = (opengl.GetWidth() - largeFont.GetStringWidth(hsnext)) / 2; + int y = opengl.GetHeight() - 50; - opengl.Colour(0.0f, 0.5f, 1.0f, flAlpha); - ft.Print - ( - ftLarge, - (opengl.GetWidth() - ft.GetStringWidth(ftLarge, hsnext)) / 2, - opengl.GetHeight() - 50, - hsnext - ); + glColor4f(0.0f, 0.5f, 1.0f, flAlpha); + largeFont.Print(x, y, hsnext); } else if (state == hssEnterName) { Input &input = Input::GetInstance(); + + const char *hsscore = i18n("Well done - You got a high score"); + int x = (opengl.GetWidth() - largeFont.GetStringWidth(hsscore)) / 2; + glColor4f(0.0f, 1.0f, 0.0f, flAlpha); + largeFont.Print(x, 100, hsscore); - const char *hsscore = i18n("Well done - You got a high score"); - opengl.Colour(0.0f, 1.0f, 0.0f, flAlpha); - ft.Print - ( - ftLarge, - (opengl.GetWidth() - ft.GetStringWidth(ftLarge, hsscore)) / 2, - 100, - hsscore - ); - - const char *hscont = i18n("Press ENTER or FIRE to continue"); - ft.Print - ( - ftLarge, - (opengl.GetWidth() - ft.GetStringWidth(ftLarge, hscont)) / 2, - opengl.GetHeight() - 60, - hscont - ); + const char *hscont = i18n("Press ENTER or FIRE to continue"); + x = (opengl.GetWidth() - largeFont.GetStringWidth(hscont)) / 2; + int y = opengl.GetHeight() - 60; + largeFont.Print(x, y, hscont); const char *hsname = i18n("Name? %s"); - opengl.Colour(0.8f, 0.0f, 1.0f, flAlpha); - ft.Print - ( - ftLarge, - (opengl.GetWidth() - - ft.GetStringWidth(ftLarge, input.GetInput()) - - ft.GetStringWidth(ftLarge, hsname) - ) / 2, - (opengl.GetHeight() - 50) / 2, - hsname, - input.GetInput() - ); + x = (opengl.GetWidth() + - largeFont.GetStringWidth(input.GetInput()) + - largeFont.GetStringWidth(hsname)) / 2; + y = (opengl.GetHeight() - 50) / 2; + glColor4f(0.8f, 0.0f, 1.0f, flAlpha); + largeFont.Print(x, y, hsname, input.GetInput()); } } @@ -246,7 +227,6 @@ void HighScores::WriteHighScores() scoreFile.Save(); } - /* * Displays the highest scores screen to the user. */ @@ -266,7 +246,6 @@ void HighScores::DisplayScores() fade = HS_FADE_IN_SPEED; } - /* * Check to see if the player has a high score. */ diff --git a/src/HighScores.hpp b/src/HighScores.hpp index bb92d66..111c1a4 100644 --- a/src/HighScores.hpp +++ b/src/HighScores.hpp @@ -24,6 +24,7 @@ #include "OpenGL.hpp" #include "Emitter.hpp" #include "Image.hpp" +#include "Font.hpp" #define HS_FADE_IN_SPEED 0.2f #define HS_FADE_OUT_SPEED -0.02f @@ -66,7 +67,7 @@ private: bool needsWrite; typedef vector ScoreEntryVec; typedef ScoreEntryVec::iterator ScoreEntryVecIt; - ScoreEntryVec scores; + ScoreEntryVec scores;; }; class HighScores : public Screen { @@ -91,6 +92,7 @@ private: float flAlpha, fade; HighScoreState state; ScoreFile scoreFile; + Font largeFont, scoreNameFont; // Fireworks static const int MAX_FIREWORKS = 7; diff --git a/src/Lander.hpp b/src/Lander.hpp index 868ecd2..003e88f 100644 --- a/src/Lander.hpp +++ b/src/Lander.hpp @@ -25,6 +25,7 @@ #include "Emitter.hpp" #include "ScreenManager.hpp" #include "Input.hpp" +#include "Font.hpp" #include "Menu.hpp" #include "HighScores.hpp" @@ -125,6 +126,8 @@ private: Image levelComp, smallShip; Image starImage, gameOver; + + Font normalFont, scoreFont, bigFont; // Stars static const int MAX_GAME_STARS = 2048; diff --git a/src/Main.cpp b/src/Main.cpp index ff177c0..f55b149 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -77,12 +77,12 @@ int main(int argc, char **argv) sm.AddScreen("HIGH SCORES", hs); // Load fonts - FreeType &ft = FreeType::GetInstance(); + /*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); + ft.LoadFont(ftLarge, File::LocateResource("Default_Font", "ttf"), 15);*/ // Run the game sm.SelectScreen("MAIN MENU"); diff --git a/src/Makefile.am b/src/Makefile.am index 07861f4..39600b9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,7 +3,7 @@ bin_PROGRAMS = lander AM_CXXFLAGS=$(LANDER_CFLAGS) $(SDL_CFLAGS) -Wall AM_LDFLAGS=$(LANDER_LIBS) $(SDL_LIBS) -lGLU $(LTLIBINTL) lander_SOURCES = Main.cpp Lander.hpp Game.cpp \ - Bitmap.cpp Bitmap.hpp FreeType.cpp FreeType.hpp \ + Bitmap.cpp Bitmap.hpp Font.cpp Font.hpp \ HighScores.cpp HighScores.hpp Input.cpp Input.hpp \ File.cpp File.hpp Platform.hpp LoadOnce.hpp \ OpenGL.cpp OpenGL.hpp Menu.cpp Menu.hpp Emitter.cpp \ diff --git a/src/Menu.cpp b/src/Menu.cpp index 53f3d92..22b47ee 100644 --- a/src/Menu.cpp +++ b/src/Menu.cpp @@ -224,8 +224,6 @@ void MainMenu::Display() optionsOpt.Display(selOption == optOptions, bigness, fade); exitOpt.Display(selOption == optExit, bigness, fade); - FreeType &ft = FreeType::GetInstance(); - int title_x = (opengl.GetWidth() - titleImage.GetWidth()) / 2; int title_y = 100; titleImage.Draw(title_x, title_y, 0.0, 1.0, fade); diff --git a/src/Menu.hpp b/src/Menu.hpp index 298cd0d..1c1c7c9 100644 --- a/src/Menu.hpp +++ b/src/Menu.hpp @@ -23,7 +23,7 @@ #include "ScreenManager.hpp" #include "Mechanics.hpp" #include "Image.hpp" -#include "FreeType.hpp" +#include "Font.hpp" class MenuStar { diff --git a/src/OpenGL.hpp b/src/OpenGL.hpp index c3e79be..03f3155 100644 --- a/src/OpenGL.hpp +++ b/src/OpenGL.hpp @@ -21,7 +21,6 @@ #define INC_OPENGL_HPP #include "Platform.hpp" -#include "FreeType.hpp" #include "Geometry.hpp" #define WINDOW_TITLE "Lunar Lander" -- 2.39.2