[2826] | 1 | //*******************************************************************
|
---|
| 2 | //glfont2.cpp -- glFont Version 2.0 implementation
|
---|
| 3 | //Copyright (c) 1998-2002 Brad Fish
|
---|
| 4 | //See glfont.html for terms of use
|
---|
| 5 | //May 14, 2002
|
---|
| 6 | //*******************************************************************
|
---|
| 7 |
|
---|
| 8 | //STL headers
|
---|
| 9 | #include <string>
|
---|
| 10 | #include <utility>
|
---|
| 11 | #include <iostream>
|
---|
| 12 | #include <fstream>
|
---|
| 13 | using namespace std;
|
---|
| 14 |
|
---|
| 15 | //OpenGL headers
|
---|
| 16 | #include "glInterface.h"
|
---|
| 17 |
|
---|
| 18 | //glFont header
|
---|
| 19 | #include "glfont2.h"
|
---|
| 20 | using namespace glfont;
|
---|
| 21 |
|
---|
| 22 | //*******************************************************************
|
---|
| 23 | //GLFont Class Implementation
|
---|
| 24 | //*******************************************************************
|
---|
| 25 | GLFont::GLFont ()
|
---|
| 26 | {
|
---|
| 27 | //Initialize header to safe state
|
---|
| 28 | header.tex = -1;
|
---|
| 29 | header.tex_width = 0;
|
---|
| 30 | header.tex_height = 0;
|
---|
| 31 | header.start_char = 0;
|
---|
| 32 | header.end_char = 0;
|
---|
| 33 | header.chars = NULL;
|
---|
| 34 | }
|
---|
| 35 | //*******************************************************************
|
---|
| 36 | GLFont::~GLFont ()
|
---|
| 37 | {
|
---|
| 38 | //Destroy the font
|
---|
| 39 | Destroy();
|
---|
| 40 | }
|
---|
| 41 | //*******************************************************************
|
---|
| 42 | bool GLFont::Create (const char *file_name, int tex)
|
---|
| 43 | {
|
---|
| 44 | ifstream input;
|
---|
| 45 | int num_chars, num_tex_bytes;
|
---|
| 46 | char *tex_bytes;
|
---|
| 47 |
|
---|
| 48 | //Destroy the old font if there was one, just to be safe
|
---|
| 49 | Destroy();
|
---|
| 50 |
|
---|
| 51 | //Open input file
|
---|
| 52 | input.open(file_name, ios::in | ios::binary);
|
---|
| 53 | if (!input)
|
---|
| 54 | return false;
|
---|
| 55 |
|
---|
| 56 | //Read the header from file
|
---|
| 57 | input.read((char *)&header, sizeof(header));
|
---|
| 58 | header.tex = tex;
|
---|
| 59 |
|
---|
| 60 | //Allocate space for character array
|
---|
| 61 | num_chars = header.end_char - header.start_char + 1;
|
---|
| 62 | if ((header.chars = new GLFontChar[num_chars]) == NULL)
|
---|
| 63 | return false;
|
---|
| 64 |
|
---|
| 65 | //Read character array
|
---|
| 66 | input.read((char *)header.chars, sizeof(GLFontChar) *
|
---|
| 67 | num_chars);
|
---|
| 68 |
|
---|
| 69 | //Read texture pixel data
|
---|
| 70 | num_tex_bytes = header.tex_width * header.tex_height * 2;
|
---|
| 71 | tex_bytes = new char[num_tex_bytes];
|
---|
| 72 | input.read(tex_bytes, num_tex_bytes);
|
---|
| 73 |
|
---|
| 74 | //Create OpenGL texture
|
---|
| 75 | glBindTexture(GL_TEXTURE_2D, tex);
|
---|
| 76 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
---|
| 77 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
---|
| 78 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 79 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
| 80 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
---|
| 81 | glTexImage2D(GL_TEXTURE_2D, 0, 2, header.tex_width,
|
---|
| 82 | header.tex_height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE,
|
---|
| 83 | (void *)tex_bytes);
|
---|
| 84 |
|
---|
| 85 | //Free texture pixels memory
|
---|
| 86 | delete[] tex_bytes;
|
---|
| 87 |
|
---|
| 88 | //Close input file
|
---|
| 89 | input.close();
|
---|
| 90 |
|
---|
| 91 | //Return successfully
|
---|
| 92 | return true;
|
---|
| 93 | }
|
---|
| 94 | //*******************************************************************
|
---|
| 95 | bool GLFont::Create (const std::string &file_name, int tex)
|
---|
| 96 | {
|
---|
| 97 | return Create(file_name.c_str(), tex);
|
---|
| 98 | }
|
---|
| 99 | //*******************************************************************
|
---|
| 100 | void GLFont::Destroy (void)
|
---|
| 101 | {
|
---|
| 102 | //Delete the character array if necessary
|
---|
| 103 | if (header.chars)
|
---|
| 104 | {
|
---|
| 105 | delete[] header.chars;
|
---|
| 106 | header.chars = NULL;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | //*******************************************************************
|
---|
| 110 | void GLFont::GetTexSize (std::pair<int, int> *size)
|
---|
| 111 | {
|
---|
| 112 | //Retrieve texture size
|
---|
| 113 | size->first = header.tex_width;
|
---|
| 114 | size->second = header.tex_height;
|
---|
| 115 | }
|
---|
| 116 | //*******************************************************************
|
---|
| 117 | int GLFont::GetTexWidth (void)
|
---|
| 118 | {
|
---|
| 119 | //Return texture width
|
---|
| 120 | return header.tex_width;
|
---|
| 121 | }
|
---|
| 122 | //*******************************************************************
|
---|
| 123 | int GLFont::GetTexHeight (void)
|
---|
| 124 | {
|
---|
| 125 | //Return texture height
|
---|
| 126 | return header.tex_height;
|
---|
| 127 | }
|
---|
| 128 | //*******************************************************************
|
---|
| 129 | void GLFont::GetCharInterval (std::pair<int, int> *interval)
|
---|
| 130 | {
|
---|
| 131 | //Retrieve character interval
|
---|
| 132 | interval->first = header.start_char;
|
---|
| 133 | interval->second = header.end_char;
|
---|
| 134 | }
|
---|
| 135 | //*******************************************************************
|
---|
| 136 | int GLFont::GetStartChar (void)
|
---|
| 137 | {
|
---|
| 138 | //Return start character
|
---|
| 139 | return header.start_char;
|
---|
| 140 | }
|
---|
| 141 | //*******************************************************************
|
---|
| 142 | int GLFont::GetEndChar (void)
|
---|
| 143 | {
|
---|
| 144 | //Return end character
|
---|
| 145 | return header.end_char;
|
---|
| 146 | }
|
---|
| 147 | //*******************************************************************
|
---|
| 148 | void GLFont::GetCharSize (int c, std::pair<int, int> *size)
|
---|
| 149 | {
|
---|
| 150 | //Make sure character is in range
|
---|
| 151 | if (c < header.start_char || c > header.end_char)
|
---|
| 152 | {
|
---|
| 153 | //Not a valid character, so it obviously has no size
|
---|
| 154 | size->first = 0;
|
---|
| 155 | size->second = 0;
|
---|
| 156 | }
|
---|
| 157 | else
|
---|
| 158 | {
|
---|
| 159 | GLFontChar *glfont_char;
|
---|
| 160 |
|
---|
| 161 | //Retrieve character size
|
---|
| 162 | glfont_char = &header.chars[c - header.start_char];
|
---|
| 163 | size->first = (int)(glfont_char->dx * header.tex_width);
|
---|
| 164 | size->second = (int)(glfont_char->dy *
|
---|
| 165 | header.tex_height);
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | //*******************************************************************
|
---|
| 169 | int GLFont::GetCharWidth (int c)
|
---|
| 170 | {
|
---|
| 171 | //Make sure in range
|
---|
| 172 | if (c < header.start_char || c > header.end_char)
|
---|
| 173 | return 0;
|
---|
| 174 | else
|
---|
| 175 | {
|
---|
| 176 | GLFontChar *glfont_char;
|
---|
| 177 |
|
---|
| 178 | //Retrieve character width
|
---|
| 179 | glfont_char = &header.chars[c - header.start_char];
|
---|
| 180 | return (int)(glfont_char->dx * header.tex_width);
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | //*******************************************************************
|
---|
| 184 | int GLFont::GetCharHeight (int c)
|
---|
| 185 | {
|
---|
| 186 | //Make sure in range
|
---|
| 187 | if (c < header.start_char || c > header.end_char)
|
---|
| 188 | return 0;
|
---|
| 189 | else
|
---|
| 190 | {
|
---|
| 191 | GLFontChar *glfont_char;
|
---|
| 192 |
|
---|
| 193 | //Retrieve character height
|
---|
| 194 | glfont_char = &header.chars[c - header.start_char];
|
---|
| 195 | return (int)(glfont_char->dy * header.tex_height);
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | //*******************************************************************
|
---|
| 199 | void GLFont::Begin (void)
|
---|
| 200 | {
|
---|
| 201 | //Bind to font texture
|
---|
| 202 | glBindTexture(GL_TEXTURE_2D, header.tex);
|
---|
| 203 | }
|
---|
| 204 | //*******************************************************************
|
---|
| 205 |
|
---|
| 206 | //End of file
|
---|
| 207 |
|
---|