/** * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** * * 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. The Blender * Foundation also sells licenses for use in proprietary software under * the Blender License. See http://www.blender.org/BL/ for information * about this. * * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. * All rights reserved. * * The Original Code is: all of this file. * * Contributor(s): none yet. * * ***** END GPL/BL DUAL LICENSE BLOCK ***** */ /** * Copyright (C) 2001 NaN Technologies B.V. */ #include #if defined(WIN32) || defined(__APPLE__) #ifdef WIN32 #if !defined(__CYGWIN32__) #pragma warning(disable:4244) #endif /* __CYGWIN32__ */ #include #include #else // WIN32 // __APPLE__ is defined #include #endif // WIN32 #else // defined(WIN32) || defined(__APPLE__) #include #endif // defined(WIN32) || defined(__APPLE__) #include "BMF_BitmapFont.h" BMF_BitmapFont::BMF_BitmapFont(BMF_FontData* fontData) : m_fontData(fontData) { } BMF_BitmapFont::~BMF_BitmapFont(void) { } void BMF_BitmapFont::DrawString(const char* str) { if (!str) return; GLint alignment; unsigned char c; glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); while (c = (unsigned char) *str++) { BMF_CharData & cd = m_fontData->chars[c]; if (cd.data_offset==-1) { GLubyte nullBitmap = 0; glBitmap(1, 1, 0, 0, cd.advance, 0, &nullBitmap); } else { GLubyte *bitmap = &m_fontData->bitmap_data[cd.data_offset]; glBitmap(cd.width, cd.height, cd.xorig, cd.yorig, cd.advance, 0, bitmap); } } glPixelStorei(GL_UNPACK_ALIGNMENT, alignment); } int BMF_BitmapFont::GetStringWidth(char* str) { unsigned char c; int length = 0; while (c = (unsigned char) *str++) { length += m_fontData->chars[c].advance; } return length; } void BMF_BitmapFont::GetBoundingBox(int & xMin, int & yMin, int & xMax, int & yMax) { xMin = m_fontData->xmin; yMin = m_fontData->ymin; xMax = m_fontData->xmax; yMax = m_fontData->ymax; } int BMF_BitmapFont::GetTexture() { int fWidth = m_fontData->xmax - m_fontData->xmin; int fHeight = m_fontData->ymax - m_fontData->ymin; if (fWidth>=16 || fHeight>=16) { return -1; } int cRows = 16, cCols = 16; int cWidth = 16, cHeight = 16; int iWidth = cCols*cWidth; int iHeight = cRows*cHeight; GLubyte *img = new GLubyte [iHeight*iWidth]; GLuint texId; int baseLine = -(m_fontData->ymin); memset(img, 0, iHeight*iWidth); for (int i = 0; i<256; i++) { BMF_CharData & cd = m_fontData->chars[i]; if (cd.data_offset != -1) { int cellX = i%16; int cellY = i/16; for (int y = 0; ybitmap_data[cd.data_offset + ((cd.width+7)/8)*y]; for (int x = 0; xymin); glBegin(GL_QUADS); while (c = (unsigned char) *str++) { BMF_CharData & cd = m_fontData->chars[c]; if (cd.data_offset != -1) { float cellX = (c%16)/16.0; float cellY = (c/16)/16.0; glTexCoord2f(cellX + 1.0/16.0, cellY); glVertex3f(x + pos + 16.0, -baseLine + y + 0.0, z); glTexCoord2f(cellX + 1.0/16.0, cellY + 1.0/16.0); glVertex3f(x + pos + 16.0, -baseLine + y + 16.0, z); glTexCoord2f(cellX, cellY + 1.0/16.0); glVertex3f(x + pos + 0.0, -baseLine + y + 16.0, z); glTexCoord2f(cellX, cellY); glVertex3f(x + pos + 0.0, -baseLine + y + 0.0, z); } pos += cd.advance; } glEnd(); }