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