Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages

cfont.cpp

00001 #include "globals.h"
00002 
00003 #include "cfont.h"
00004 #include "cvideo.h"
00005 #include "cgame.h"
00006 
00007 #include <stdlib.h>
00008 #include <assert.h>
00009 #include <string.h>
00010 
00011 /* Constructs a new font object.  Loads a tiled font file from the given bitmap image
00012    file, using the given character width and height (the font is monospaced).  The
00013    characters-per-line are used when extracting a character from the tiled bitmap file
00014    and corresponds to the number of characters per "line of characters" there are
00015    in the bitmap file. */
00016 CFont::CFont(const char *bmp_filename, int char_width, int char_height, int bitmap_chars_per_line)
00017 {
00018         this->bitmap_chars_per_line = bitmap_chars_per_line;
00019         this->char_height = char_height;
00020         this->char_width = char_width;
00021 
00022   CSurface bmp_image(bmp_filename);
00023   bitmap = bmp_image;
00024   bitmap.set_transparent_color(CColor(1));
00025 }
00026 
00027 /* Draws the given string in the given font, centered at the specified (x,y) coordinates. */
00028 void CFont::draw_string_centered(CSurface *surface, CPoint point, const char *string)
00029 {
00030         point.x -= ( ((int) strlen(string) * char_width) / 2);
00031         point.y -= (char_height / 2);
00032 
00033         draw_string(surface, point, string);
00034 }
00035 
00036 /* Draws the given string with a black rectangle behind it and then updates the
00037    game's rectangle list for redrawing. */
00038 void CFont::draw_string_opaque(CSurface *surface, CPoint point, const char *string)
00039 {
00040   CRect r;
00041 
00042   /* Draw a black rectangle behind where the string is going to be drawn */
00043   r.pos = point;
00044   r.size.x = (char_width*(int)strlen(string));
00045   r.size.y = char_height;
00046 
00047   surface->fill_rect(r, 0);
00048 
00049   /* Draw the string */
00050   draw_string(surface, point, string);
00051 }
00052 
00053 /* Draws the given string in the given font, so that the upper-left corner of the string
00054    is at the given (x,y) pixel coordinates. */
00055 void CFont::draw_string(CSurface *surface, CPoint point, const char *string)
00056 {
00057         int i;
00058         int string_length;
00059 
00060   CRect r_src;
00061 
00062         r_src.size.x = char_width;
00063         r_src.size.y = char_height;
00064 
00065         string_length = (int) strlen(string);
00066 
00067         for (i = 0; i < string_length; i++) {
00068                 int char_code, char_x, char_y;
00069 
00070                 char_code = string[i] - CHAR_CODE_OFFSET;
00071 
00072     char_x = char_code % bitmap_chars_per_line;
00073                 char_y = char_code / bitmap_chars_per_line;
00074 
00075                 r_src.pos.x = char_x * char_width;
00076                 r_src.pos.y = char_y * char_height;
00077 
00078     surface->blit(bitmap, point, r_src);
00079                 point.x += char_width;
00080   }
00081 }

Generated on Wed Aug 27 11:58:41 2003 for GFW by doxygen 1.3.3