1 /*-----------------------------------------------------------------------------
2 Njamfont.cpp
3
4 Simple font class that loads font resource (fixed width/height bitmap)
5 and provides functions to render it to screen
6
7
8 Copyright 2003 Milan Babuskov
9
10 This file is part of Njam (http://njam.sourceforge.net).
11
12 Njam is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 Njam is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with Njam in file COPYING; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 -----------------------------------------------------------------------------*/
26 #include <stdio.h>
27 #include "SDL.h"
28 #include "njamutils.h"
29 #include "njamfont.h"
30 //---------------------------------------------------------------------------
NjamFont(const char * Filename,int CharWidth,int CharHeight)31 NjamFont::NjamFont(const char *Filename, int CharWidth, int CharHeight)
32 {
33 m_Surface = NULL;
34
35 LogFile("Loading font...");
36 SDL_Surface *temp = SDL_LoadBMP(Filename);
37 if (!temp)
38 {
39 LogFile("FAILED.\n");
40 LogFile((const char *)SDL_GetError());
41 return;
42 }
43 LogFile("OK.\n");
44
45 // setting color key for font: black is transparent
46 Uint32 black = SDL_MapRGB(temp->format, 0, 0, 0);
47 SDL_SetColorKey(temp, SDL_SRCCOLORKEY, black);
48
49 /* Convert image to video format */
50 m_Surface = SDL_DisplayFormat(temp);
51 SDL_FreeSurface(temp);
52 if ( m_Surface == NULL )
53 fprintf(stderr, "Couldn't convert font image: %s\n", SDL_GetError());
54
55 m_CharWidth = CharWidth;
56 m_CharHeight = CharHeight;
57 }
58 //---------------------------------------------------------------------------
~NjamFont()59 NjamFont::~NjamFont()
60 {
61 if (m_Surface)
62 SDL_FreeSurface(m_Surface);
63 }
64 //---------------------------------------------------------------------------
GetCharWidth()65 int NjamFont::GetCharWidth()
66 {
67 return m_CharWidth;
68 }
69 //---------------------------------------------------------------------------
GetCharHeight()70 int NjamFont::GetCharHeight()
71 {
72 return m_CharHeight;
73 }
74 //---------------------------------------------------------------------------
75 // Writes text centered at screen
WriteTextCentered(SDL_Surface * Destination,int y,const char * Text)76 bool NjamFont::WriteTextCentered(SDL_Surface *Destination, int y, const char *Text)
77 {
78 int len=0;
79 while (Text[len++]); // get string length
80 int xpos = (Destination->w - len * m_CharWidth) / 2;
81 return WriteTextXY(Destination, xpos, y, Text);
82 }
83 //---------------------------------------------------------------------------
84 // Uses CharWidth and Height to split screen into Columns and Rows
WriteTextColRow(SDL_Surface * Destination,int Col,int Row,const char * Text)85 bool NjamFont::WriteTextColRow(SDL_Surface *Destination, int Col, int Row, const char *Text)
86 {
87 return WriteTextXY(Destination, Col * m_CharWidth, Row * m_CharHeight, Text);
88 }
89 //---------------------------------------------------------------------------
90 // Outputs Text into Destionation surface at coordinate (x, y)
WriteTextXY(SDL_Surface * Destination,int x,int y,const char * Text)91 bool NjamFont::WriteTextXY(SDL_Surface *Destination, int x, int y, const char *Text)
92 {
93 const char FontMap[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,;:!@*()%/";
94 if (!m_Surface || !Destination)
95 return false;
96
97 SDL_Rect src, dest;
98 src.y = 0;
99 src.w = m_CharWidth;
100 src.h = m_CharHeight;
101
102 if (y < -m_CharHeight || y > Destination->h) // out of screen
103 return true;
104
105 for (int i=0; Text[i]; i++)
106 {
107 dest.y = y;
108 dest.x = x + i * m_CharWidth;
109
110 if (dest.x < -m_CharWidth) // not yet in screen
111 continue;
112
113 if (dest.x > Destination->w) // outside
114 break;
115
116 if (Text[i] == ' ' || Text[i] == 10 || Text[i] == 13) // SPACE, NL, CR
117 continue;
118
119 bool Found = false;
120 for (int j=0; j < (sizeof(FontMap)/sizeof(char)); j++)
121 {
122 if (Text[i] == FontMap[j])
123 {
124 Found = true;
125 src.x = j * m_CharWidth;
126
127 if (0 != SDL_BlitSurface(m_Surface, &src, Destination, &dest))
128 {
129 LogFile("Failed to blit font character image.\n");
130 LogFile((const char *)SDL_GetError());
131 return false;
132 }
133 break;
134 }
135 }
136
137 if (!Found)
138 printf("TEXT OUTPUT WARNING: Character: %c (code: %d) was not found.\n", Text[i], Text[i]);
139 }
140 return true;
141 }
142 //---------------------------------------------------------------------------
143
144