/* text.cpp - extremely quick and dirty "font engine". Copyright (C) 2006 Mark boyd 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 fun to play, 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. */ //Our "Font" is actually a png, containing the 96 ASCII characters from 32 to 127 #include "text.h" #include "loadimag.h" namespace text { const int begin_char=32; const int end_char=128; //this is 1 past the last character! SDL_Surface *the_font=0; bool initialised = false; int w; int h; }; void text::init() { if (initialised) return; the_font=load_image("font.png"); initialised = true; h=the_font->h; w=the_font->w/(end_char-begin_char); } void text::exit() { if (!initialised) return; SDL_FreeSurface(the_font); initialised = false; } void text::draw(const std::string &str, SDL_Surface *bmp, int x, int y, Uint32) { init(); //TODO: don't ignore colour! for(int i=0; i=end_char) c='?'; SDL_Rect from = {(c-begin_char)*w,0,w,h}; SDL_Rect to = {x+i*w, y, 0, 0}; SDL_BlitSurface(the_font, &from, bmp, &to); } SDL_UpdateRect(bmp, x,y,str.size()*w,h); } void text::draw_centre(const std::string &str, SDL_Surface *bmp, int x, int y, Uint32 colour) { text::draw(str, bmp, x-w*str.size()/2, y-h/2, colour); } int text::width(const std::string &s) { return s.size()*w; }