/* * $Id: font.cc,v 1.1.1.1 2002/08/18 17:28:53 aeneas Exp $ * Copyright (c) 2002, Dominik Schnitzer * * JFK - JFK Fucking Killerz, a massive multiplayer 2d shoot'em-up game * http://relax.ath.cx/jfk/ * * 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 Library 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. */ #include #include "font.h" #include "output.h" #include "exception.h" using namespace std; using namespace JFK::client; font::font(output* outp, string file) { out = outp; fontset = out->load_image("font", file); if (fontset == NULL) throw exception(string("Error loading font: " + file)); char_width = out->get_image_width(fontset) / MAX_CHARS; char_height = out->get_image_height(fontset); } font::~font() { out->free_image(fontset); } void font::draw_text(int x, int y, string text) { /* Dont draw if the text is outside or invisible anyways */ if ((x > out->get_width()) || (y > out->get_height()) || ((x + get_textwidth(text)) < 0) || ((y + char_height) < 0)) { return; } int characters = text.length(); int srcx = 0; int dstx = x; /* Now draw the text */ for (int i = 0; i < characters; i++) { /* Don't draw, if it's an unknown (unmapped) ascii character */ if ((text[i] < 32) || (text[i] > 126)) continue; char n = text[i]; if ((text[i] >= 'a') && (text[i] <= 'z')) n = text[i] - 32; srcx = (n - 32) * char_width; out->draw_image_ext(fontset, srcx, 0, char_width, char_height, dstx, y, char_width, char_height); dstx += char_width; } } int font::get_textwidth(string text) { return text.length() * char_width; }