/* render/text.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include "auxiliary/functions.h" #include "render/text.h" #include "render/textures.h" #include "sys/sys.h" namespace render { float Text::text_fontwidth = 16.0f; float Text::text_fontheight = 24.0f; math::Color * Text::base_color[10]; math::Color * Text::core_color[26]; void Text::init() { // base colors // Black=0, Red=1, Green=2, Yellow=3, Blue=4, Cyan=5, Magenta=6, White=7 base_color[0] = new math::Color(0, 0, 0); base_color[1] = new math::Color(1, 0, 0); base_color[2] = new math::Color(0, 1, 0); base_color[3] = new math::Color(1, 1, 0); base_color[4] = new math::Color(0, 0, 1); base_color[5] = new math::Color(0, 1, 1); base_color[6] = new math::Color(1, 0, 1); base_color[7] = new math::Color(1, 1, 1); base_color[8] = new math::Color(1, 1, 1); base_color[9] = new math::Color(1, 1, 1); for (size_t i = 0; i < 26; i++) { core_color[i] = new math::Color(.75, .75, .75); } // N - normal color core_color[(size_t)('N'-'A')]->assign(.75, .75, .75); // D - Debug color core_color[(size_t)('D'-'A')]->assign(.5, .5, .5); // B - bold color core_color[(size_t)('B'-'A')]->assign(1, 1, 1); // W - warning color core_color[(size_t)('W'-'A')]->assign(1, 1, 0); // R - Error color core_color[(size_t)('R'-'A')]->assign(1, 0, 0); // F - Fancy color core_color[(size_t)('F'-'A')]->assign(0, 1, 0); } void Text::shutdown() { for (size_t i = 0; i < 10; i++) { delete base_color[i]; base_color[i] = 0; } for (size_t i = 0; i < 26; i++) { delete core_color[i]; core_color[i] = 0; } } void Text::assign_color(const char c, const math::Color &color) { if (('A' <= c) && (c <= 'Z')) { core_color[(size_t)(c - 'A')]->assign(color); } else if (('0' <= c) && (c <= '9')) { base_color[(size_t)(c - '0')]->assign(color); } } void Text::setcolor(const char color) { if (('A' <= color) && (color <= 'Z')) { gl::color(*core_color[(size_t)(color - 'A')]); } else if (('0' <= color) && (color <= '9')) { gl::color(*base_color[(size_t)(color - '0')]); } } void Text::setfont(const char *texture, const float width, const float height) { std::string texture_filename("bitmaps/fonts/"); texture_filename.append(texture); Textures::bind(texture_filename, false); text_fontwidth = width; text_fontheight = height; } // draw a single character void Text::draw(const float x, const float y, const char ascii) { if (ascii != ' ') { int row = (int) ascii >> 4; int col = (int) ascii & 15; float frow = (float) row * 0.0625f; float fcol = (float) col * 0.0625f; gl::begin(gl::Quads); gl::texcoord(fcol, frow); gl::vertex(x, y, 0); gl::texcoord(fcol + 0.0625f, frow); gl::vertex(x + text_fontwidth, y, 0); gl::texcoord(fcol + 0.0625f, frow + 0.0625f); gl::vertex(x + text_fontwidth, y + text_fontheight, 0); gl::texcoord(fcol, frow + 0.0625f); gl::vertex(x, y + text_fontheight, 0); gl::end(); } } // draw one or more lines of text void Text::draw(const float x, const float y, const char *text) { const char *c = text; float x1 = x; float y1 = y; while (*c) { if (aux::is_base_color_code(c)) { c++; gl::color(*base_color[(size_t)(*c - '0')]); } else if (aux::is_core_color_code(c)) { c++; gl::color(*core_color[(size_t)(*c - 'A')]); } else if (*c == '\n' ) { y1 += text_fontheight; x1 = x; } else { draw(x1, y1, *c); x1 += text_fontwidth; } c++; } } /* void Text::draw(float x, float y, std::stringstream & textstream) { char line[MAXCMDSIZE]; while (textstream.getline(line, MAXCMDSIZE - 1)) { draw(x, y, line); y += text_fontheight; } textstream.clear(); } */ } // namespace render