git repos / blockattack-game

blame: source/code/Libs/NFont.cpp

normal view · raw

925b7e58 sago007 2011-04-25 16:35 1
/*
925b7e58 sago007 2011-04-25 16:35 2
NFont v2.0.0: A bitmap font class for SDL
925b7e58 sago007 2011-04-25 16:35 3
by Jonathan Dearborn 2-4-10
925b7e58 sago007 2011-04-25 16:35 4
(class adapted from Florian Hufsky)
925b7e58 sago007 2011-04-25 16:35 5
925b7e58 sago007 2011-04-25 16:35 6
License:
925b7e58 sago007 2011-04-25 16:35 7
    The short:
c53e6443 sago007 2012-04-17 11:04 8
    Use it however you'd like, but keep the copyright and license notice
925b7e58 sago007 2011-04-25 16:35 9
    whenever these files or parts of them are distributed in uncompiled form.
c53e6443 sago007 2012-04-17 11:04 10
925b7e58 sago007 2011-04-25 16:35 11
    The long:
925b7e58 sago007 2011-04-25 16:35 12
Copyright (c) 2010 Jonathan Dearborn
925b7e58 sago007 2011-04-25 16:35 13
925b7e58 sago007 2011-04-25 16:35 14
Permission is hereby granted, free of charge, to any person obtaining a copy
925b7e58 sago007 2011-04-25 16:35 15
of this software and associated documentation files (the "Software"), to deal
925b7e58 sago007 2011-04-25 16:35 16
in the Software without restriction, including without limitation the rights
925b7e58 sago007 2011-04-25 16:35 17
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
925b7e58 sago007 2011-04-25 16:35 18
copies of the Software, and to permit persons to whom the Software is
925b7e58 sago007 2011-04-25 16:35 19
furnished to do so, subject to the following conditions:
925b7e58 sago007 2011-04-25 16:35 20
925b7e58 sago007 2011-04-25 16:35 21
The above copyright notice and this permission notice shall be included in
925b7e58 sago007 2011-04-25 16:35 22
all copies or substantial portions of the Software.
925b7e58 sago007 2011-04-25 16:35 23
925b7e58 sago007 2011-04-25 16:35 24
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
925b7e58 sago007 2011-04-25 16:35 25
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
925b7e58 sago007 2011-04-25 16:35 26
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
925b7e58 sago007 2011-04-25 16:35 27
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
925b7e58 sago007 2011-04-25 16:35 28
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
925b7e58 sago007 2011-04-25 16:35 29
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
925b7e58 sago007 2011-04-25 16:35 30
THE SOFTWARE.
925b7e58 sago007 2011-04-25 16:35 31
*/
925b7e58 sago007 2011-04-25 16:35 32
#include "NFont.h"
925b7e58 sago007 2011-04-25 16:35 33
925b7e58 sago007 2011-04-25 16:35 34
char* NFont::buffer = NULL;
925b7e58 sago007 2011-04-25 16:35 35
NFont::AnimData NFont::data;
925b7e58 sago007 2011-04-25 16:35 36
925b7e58 sago007 2011-04-25 16:35 37
#define MIN(a,b) (a < b? a : b)
925b7e58 sago007 2011-04-25 16:35 38
#define MAX(a,b) (a > b? a : b)
925b7e58 sago007 2011-04-25 16:35 39
925b7e58 sago007 2011-04-25 16:35 40
// Static setters
925b7e58 sago007 2011-04-25 16:35 41
void NFont::setAnimData(void* data)
925b7e58 sago007 2011-04-25 16:35 42
{
c53e6443 sago007 2012-04-17 11:04 43
	NFont::data.userVar = data;
925b7e58 sago007 2011-04-25 16:35 44
}
925b7e58 sago007 2011-04-25 16:35 45
925b7e58 sago007 2011-04-25 16:35 46
void NFont::setBuffer(unsigned int size)
925b7e58 sago007 2011-04-25 16:35 47
{
c53e6443 sago007 2012-04-17 11:04 48
	delete[] buffer;
c53e6443 sago007 2012-04-17 11:04 49
	if(size > 0)
c53e6443 sago007 2012-04-17 11:04 50
		buffer = new char[size];
c53e6443 sago007 2012-04-17 11:04 51
	else
c53e6443 sago007 2012-04-17 11:04 52
		buffer = new char[1024];
925b7e58 sago007 2011-04-25 16:35 53
}
925b7e58 sago007 2011-04-25 16:35 54
925b7e58 sago007 2011-04-25 16:35 55
925b7e58 sago007 2011-04-25 16:35 56
// Static functions
925b7e58 sago007 2011-04-25 16:35 57
char* NFont::copyString(const char* c)
925b7e58 sago007 2011-04-25 16:35 58
{
c53e6443 sago007 2012-04-17 11:04 59
	if(c == NULL) return NULL;
925b7e58 sago007 2011-04-25 16:35 60
c53e6443 sago007 2012-04-17 11:04 61
	int count = 0;
c53e6443 sago007 2012-04-17 11:04 62
	for(; c[count] != '\0'; count++);
925b7e58 sago007 2011-04-25 16:35 63
c53e6443 sago007 2012-04-17 11:04 64
	char* result = new char[count+1];
925b7e58 sago007 2011-04-25 16:35 65
c53e6443 sago007 2012-04-17 11:04 66
	for(int i = 0; i < count; i++)
c53e6443 sago007 2012-04-17 11:04 67
	{
c53e6443 sago007 2012-04-17 11:04 68
		result[i] = c[i];
c53e6443 sago007 2012-04-17 11:04 69
	}
925b7e58 sago007 2011-04-25 16:35 70
c53e6443 sago007 2012-04-17 11:04 71
	result[count] = '\0';
c53e6443 sago007 2012-04-17 11:04 72
	return result;
925b7e58 sago007 2011-04-25 16:35 73
}
925b7e58 sago007 2011-04-25 16:35 74
925b7e58 sago007 2011-04-25 16:35 75
Uint32 NFont::getPixel(SDL_Surface *Surface, int x, int y)  // No Alpha?
925b7e58 sago007 2011-04-25 16:35 76
{
c53e6443 sago007 2012-04-17 11:04 77
	Uint8* bits;
c53e6443 sago007 2012-04-17 11:04 78
	Uint32 bpp;
c53e6443 sago007 2012-04-17 11:04 79
c53e6443 sago007 2012-04-17 11:04 80
	if(x < 0 || x >= Surface->w)
c53e6443 sago007 2012-04-17 11:04 81
		return 0;  // Best I could do for errors
c53e6443 sago007 2012-04-17 11:04 82
c53e6443 sago007 2012-04-17 11:04 83
	bpp = Surface->format->BytesPerPixel;
c53e6443 sago007 2012-04-17 11:04 84
	bits = ((Uint8*)Surface->pixels) + y*Surface->pitch + x*bpp;
c53e6443 sago007 2012-04-17 11:04 85
c53e6443 sago007 2012-04-17 11:04 86
	switch (bpp)
c53e6443 sago007 2012-04-17 11:04 87
	{
c53e6443 sago007 2012-04-17 11:04 88
	case 1:
c53e6443 sago007 2012-04-17 11:04 89
		return *((Uint8*)Surface->pixels + y * Surface->pitch + x);
c53e6443 sago007 2012-04-17 11:04 90
		break;
c53e6443 sago007 2012-04-17 11:04 91
	case 2:
c53e6443 sago007 2012-04-17 11:04 92
		return *((Uint16*)Surface->pixels + y * Surface->pitch/2 + x);
c53e6443 sago007 2012-04-17 11:04 93
		break;
c53e6443 sago007 2012-04-17 11:04 94
	case 3:
c53e6443 sago007 2012-04-17 11:04 95
		// Endian-correct, but slower
c53e6443 sago007 2012-04-17 11:04 96
		Uint8 r, g, b;
c53e6443 sago007 2012-04-17 11:04 97
		r = *((bits)+Surface->format->Rshift/8);
c53e6443 sago007 2012-04-17 11:04 98
		g = *((bits)+Surface->format->Gshift/8);
c53e6443 sago007 2012-04-17 11:04 99
		b = *((bits)+Surface->format->Bshift/8);
c53e6443 sago007 2012-04-17 11:04 100
		return SDL_MapRGB(Surface->format, r, g, b);
c53e6443 sago007 2012-04-17 11:04 101
		break;
c53e6443 sago007 2012-04-17 11:04 102
	case 4:
c53e6443 sago007 2012-04-17 11:04 103
		return *((Uint32*)Surface->pixels + y * Surface->pitch/4 + x);
c53e6443 sago007 2012-04-17 11:04 104
		break;
c53e6443 sago007 2012-04-17 11:04 105
	}
c53e6443 sago007 2012-04-17 11:04 106
c53e6443 sago007 2012-04-17 11:04 107
	return 0;  // Best I could do for errors
925b7e58 sago007 2011-04-25 16:35 108
}
925b7e58 sago007 2011-04-25 16:35 109
925b7e58 sago007 2011-04-25 16:35 110
SDL_Rect NFont::rectUnion(const SDL_Rect& A, const SDL_Rect& B)
925b7e58 sago007 2011-04-25 16:35 111
{
c53e6443 sago007 2012-04-17 11:04 112
	Sint16 x,x2,y,y2;
c53e6443 sago007 2012-04-17 11:04 113
	x = MIN(A.x, B.x);
c53e6443 sago007 2012-04-17 11:04 114
	y = MIN(A.y, B.y);
c53e6443 sago007 2012-04-17 11:04 115
	x2 = MAX(A.x+A.w, B.x+B.w);
c53e6443 sago007 2012-04-17 11:04 116
	y2 = MAX(A.y+A.h, B.y+B.h);
c53e6443 sago007 2012-04-17 11:04 117
	SDL_Rect result = {x, y, x2 - x, y2 - y};
c53e6443 sago007 2012-04-17 11:04 118
	return result;
925b7e58 sago007 2011-04-25 16:35 119
}
925b7e58 sago007 2011-04-25 16:35 120
925b7e58 sago007 2011-04-25 16:35 121
SDL_Surface* NFont::copySurface(SDL_Surface *Surface)
925b7e58 sago007 2011-04-25 16:35 122
{
c53e6443 sago007 2012-04-17 11:04 123
	return SDL_ConvertSurface(Surface, Surface->format, Surface->flags);
925b7e58 sago007 2011-04-25 16:35 124
}
925b7e58 sago007 2011-04-25 16:35 125
925b7e58 sago007 2011-04-25 16:35 126
SDL_Surface* NFont::verticalGradient(SDL_Surface* targetSurface, Uint32 top, Uint32 bottom, int heightAdjust)
925b7e58 sago007 2011-04-25 16:35 127
{
c53e6443 sago007 2012-04-17 11:04 128
	SDL_Surface* surface = targetSurface;
c53e6443 sago007 2012-04-17 11:04 129
	if(surface == NULL)
c53e6443 sago007 2012-04-17 11:04 130
		return NULL;
c53e6443 sago007 2012-04-17 11:04 131
c53e6443 sago007 2012-04-17 11:04 132
	Uint8 tr, tg, tb;
c53e6443 sago007 2012-04-17 11:04 133
c53e6443 sago007 2012-04-17 11:04 134
	SDL_GetRGB(top, surface->format, &tr, &tg, &tb);
c53e6443 sago007 2012-04-17 11:04 135
c53e6443 sago007 2012-04-17 11:04 136
	Uint8 br, bg, bb;
c53e6443 sago007 2012-04-17 11:04 137
c53e6443 sago007 2012-04-17 11:04 138
	SDL_GetRGB(bottom, surface->format, &br, &bg, &bb);
c53e6443 sago007 2012-04-17 11:04 139
c53e6443 sago007 2012-04-17 11:04 140
	bool useCK = (surface->flags & SDL_SRCALPHA) != SDL_SRCALPHA;  // colorkey if no alpha
c53e6443 sago007 2012-04-17 11:04 141
	Uint32 colorkey = surface->format->colorkey;
c53e6443 sago007 2012-04-17 11:04 142
c53e6443 sago007 2012-04-17 11:04 143
	Uint8 r, g, b, a;
c53e6443 sago007 2012-04-17 11:04 144
	float ratio;
c53e6443 sago007 2012-04-17 11:04 145
	Uint32 color;
c53e6443 sago007 2012-04-17 11:04 146
	int temp;
c53e6443 sago007 2012-04-17 11:04 147
c53e6443 sago007 2012-04-17 11:04 148
	for (int x = 0, y = 0; y < surface->h; x++)
c53e6443 sago007 2012-04-17 11:04 149
	{
c53e6443 sago007 2012-04-17 11:04 150
		if (x >= surface->w)
c53e6443 sago007 2012-04-17 11:04 151
		{
c53e6443 sago007 2012-04-17 11:04 152
			x = 0;
c53e6443 sago007 2012-04-17 11:04 153
			y++;
c53e6443 sago007 2012-04-17 11:04 154
c53e6443 sago007 2012-04-17 11:04 155
			if (y >= surface->h)
c53e6443 sago007 2012-04-17 11:04 156
				break;
c53e6443 sago007 2012-04-17 11:04 157
		}
c53e6443 sago007 2012-04-17 11:04 158
c53e6443 sago007 2012-04-17 11:04 159
		ratio = (y - 2)/float(surface->h - heightAdjust);  // the neg 3s are for full color at top and bottom
c53e6443 sago007 2012-04-17 11:04 160
c53e6443 sago007 2012-04-17 11:04 161
		if(!useCK)
c53e6443 sago007 2012-04-17 11:04 162
		{
c53e6443 sago007 2012-04-17 11:04 163
			color = getPixel(surface, x, y);
c53e6443 sago007 2012-04-17 11:04 164
			SDL_GetRGBA(color, surface->format, &r, &g, &b, &a);  // just getting alpha
c53e6443 sago007 2012-04-17 11:04 165
		}
c53e6443 sago007 2012-04-17 11:04 166
		else
c53e6443 sago007 2012-04-17 11:04 167
			a = SDL_ALPHA_OPAQUE;
c53e6443 sago007 2012-04-17 11:04 168
c53e6443 sago007 2012-04-17 11:04 169
		// Get and clamp the new values
c53e6443 sago007 2012-04-17 11:04 170
		temp = int(tr*(1-ratio) + br*ratio);
c53e6443 sago007 2012-04-17 11:04 171
		r = temp < 0? 0 : temp > 255? 255 : temp;
c53e6443 sago007 2012-04-17 11:04 172
c53e6443 sago007 2012-04-17 11:04 173
		temp = int(tg*(1-ratio) + bg*ratio);
c53e6443 sago007 2012-04-17 11:04 174
		g = temp < 0? 0 : temp > 255? 255 : temp;
c53e6443 sago007 2012-04-17 11:04 175
c53e6443 sago007 2012-04-17 11:04 176
		temp = int(tb*(1-ratio) + bb*ratio);
c53e6443 sago007 2012-04-17 11:04 177
		b = temp < 0? 0 : temp > 255? 255 : temp;
c53e6443 sago007 2012-04-17 11:04 178
c53e6443 sago007 2012-04-17 11:04 179
c53e6443 sago007 2012-04-17 11:04 180
		color = SDL_MapRGBA(surface->format, r, g, b, a);
c53e6443 sago007 2012-04-17 11:04 181
c53e6443 sago007 2012-04-17 11:04 182
c53e6443 sago007 2012-04-17 11:04 183
		if(useCK)
c53e6443 sago007 2012-04-17 11:04 184
		{
c53e6443 sago007 2012-04-17 11:04 185
			if(getPixel(surface, x, y) == colorkey)
c53e6443 sago007 2012-04-17 11:04 186
				continue;
c53e6443 sago007 2012-04-17 11:04 187
			if(color == colorkey)
c53e6443 sago007 2012-04-17 11:04 188
				color == 0? color++ : color--;
c53e6443 sago007 2012-04-17 11:04 189
		}
c53e6443 sago007 2012-04-17 11:04 190
c53e6443 sago007 2012-04-17 11:04 191
		// make sure it isn't pink
c53e6443 sago007 2012-04-17 11:04 192
		if(color == SDL_MapRGBA(surface->format, 0xFF, 0, 0xFF, a))
c53e6443 sago007 2012-04-17 11:04 193
			color--;
c53e6443 sago007 2012-04-17 11:04 194
		if(getPixel(surface, x, y) == SDL_MapRGBA(surface->format, 0xFF, 0, 0xFF, SDL_ALPHA_OPAQUE))
c53e6443 sago007 2012-04-17 11:04 195
			continue;
c53e6443 sago007 2012-04-17 11:04 196
c53e6443 sago007 2012-04-17 11:04 197
		int bpp = surface->format->BytesPerPixel;
c53e6443 sago007 2012-04-17 11:04 198
		Uint8* bits = ((Uint8 *)surface->pixels) + y*surface->pitch + x*bpp;
c53e6443 sago007 2012-04-17 11:04 199
c53e6443 sago007 2012-04-17 11:04 200
		/* Set the pixel */
c53e6443 sago007 2012-04-17 11:04 201
		switch(bpp)
c53e6443 sago007 2012-04-17 11:04 202
		{
c53e6443 sago007 2012-04-17 11:04 203
		case 1:
c53e6443 sago007 2012-04-17 11:04 204
			*((Uint8 *)(bits)) = (Uint8)color;
c53e6443 sago007 2012-04-17 11:04 205
			break;
c53e6443 sago007 2012-04-17 11:04 206
		case 2:
c53e6443 sago007 2012-04-17 11:04 207
			*((Uint16 *)(bits)) = (Uint16)color;
c53e6443 sago007 2012-04-17 11:04 208
			break;
c53e6443 sago007 2012-04-17 11:04 209
		case 3:   /* Format/endian independent */
c53e6443 sago007 2012-04-17 11:04 210
		{
c53e6443 sago007 2012-04-17 11:04 211
			r = (color >> surface->format->Rshift) & 0xFF;
c53e6443 sago007 2012-04-17 11:04 212
			g = (color >> surface->format->Gshift) & 0xFF;
c53e6443 sago007 2012-04-17 11:04 213
			b = (color >> surface->format->Bshift) & 0xFF;
c53e6443 sago007 2012-04-17 11:04 214
			*((bits)+surface->format->Rshift/8) = r;
c53e6443 sago007 2012-04-17 11:04 215
			*((bits)+surface->format->Gshift/8) = g;
c53e6443 sago007 2012-04-17 11:04 216
			*((bits)+surface->format->Bshift/8) = b;
c53e6443 sago007 2012-04-17 11:04 217
		}
c53e6443 sago007 2012-04-17 11:04 218
		break;
c53e6443 sago007 2012-04-17 11:04 219
		case 4:
c53e6443 sago007 2012-04-17 11:04 220
			*((Uint32 *)(bits)) = (Uint32)color;
c53e6443 sago007 2012-04-17 11:04 221
			break;
c53e6443 sago007 2012-04-17 11:04 222
		}
c53e6443 sago007 2012-04-17 11:04 223
c53e6443 sago007 2012-04-17 11:04 224
	}
c53e6443 sago007 2012-04-17 11:04 225
	return surface;
925b7e58 sago007 2011-04-25 16:35 226
}
925b7e58 sago007 2011-04-25 16:35 227
925b7e58 sago007 2011-04-25 16:35 228
925b7e58 sago007 2011-04-25 16:35 229
// Constructors
925b7e58 sago007 2011-04-25 16:35 230
NFont::NFont()
925b7e58 sago007 2011-04-25 16:35 231
{
c53e6443 sago007 2012-04-17 11:04 232
	init();
925b7e58 sago007 2011-04-25 16:35 233
}
925b7e58 sago007 2011-04-25 16:35 234
925b7e58 sago007 2011-04-25 16:35 235
NFont::NFont(SDL_Surface* src)
925b7e58 sago007 2011-04-25 16:35 236
{
c53e6443 sago007 2012-04-17 11:04 237
	init();
c53e6443 sago007 2012-04-17 11:04 238
	load(src);
925b7e58 sago007 2011-04-25 16:35 239
}
925b7e58 sago007 2011-04-25 16:35 240
925b7e58 sago007 2011-04-25 16:35 241
NFont::NFont(SDL_Surface* dest, SDL_Surface* src)
925b7e58 sago007 2011-04-25 16:35 242
{
c53e6443 sago007 2012-04-17 11:04 243
	init();
c53e6443 sago007 2012-04-17 11:04 244
	load(src);
c53e6443 sago007 2012-04-17 11:04 245
	setDest(dest);
925b7e58 sago007 2011-04-25 16:35 246
}
925b7e58 sago007 2011-04-25 16:35 247
925b7e58 sago007 2011-04-25 16:35 248
#ifdef NFONT_USE_TTF
925b7e58 sago007 2011-04-25 16:35 249
NFont::NFont(TTF_Font* ttf, SDL_Color fg)
925b7e58 sago007 2011-04-25 16:35 250
{
c53e6443 sago007 2012-04-17 11:04 251
	init();
c53e6443 sago007 2012-04-17 11:04 252
	load(ttf, fg);
925b7e58 sago007 2011-04-25 16:35 253
}
925b7e58 sago007 2011-04-25 16:35 254
NFont::NFont(TTF_Font* ttf, SDL_Color fg, SDL_Color bg)
925b7e58 sago007 2011-04-25 16:35 255
{
c53e6443 sago007 2012-04-17 11:04 256
	init();
c53e6443 sago007 2012-04-17 11:04 257
	load(ttf, fg, bg);
925b7e58 sago007 2011-04-25 16:35 258
}
925b7e58 sago007 2011-04-25 16:35 259
NFont::NFont(const char* filename_ttf, Uint32 pointSize, SDL_Color fg, int style)
925b7e58 sago007 2011-04-25 16:35 260
{
c53e6443 sago007 2012-04-17 11:04 261
	init();
c53e6443 sago007 2012-04-17 11:04 262
	load(filename_ttf, pointSize, fg, style);
925b7e58 sago007 2011-04-25 16:35 263
}
925b7e58 sago007 2011-04-25 16:35 264
NFont::NFont(const char* filename_ttf, Uint32 pointSize, SDL_Color fg, SDL_Color bg, int style)
925b7e58 sago007 2011-04-25 16:35 265
{
c53e6443 sago007 2012-04-17 11:04 266
	init();
c53e6443 sago007 2012-04-17 11:04 267
	load(filename_ttf, pointSize, fg, bg, style);
925b7e58 sago007 2011-04-25 16:35 268
}
925b7e58 sago007 2011-04-25 16:35 269
925b7e58 sago007 2011-04-25 16:35 270
NFont::NFont(SDL_Surface* dest, TTF_Font* ttf, SDL_Color fg)
925b7e58 sago007 2011-04-25 16:35 271
{
c53e6443 sago007 2012-04-17 11:04 272
	init();
c53e6443 sago007 2012-04-17 11:04 273
	load(ttf, fg);
c53e6443 sago007 2012-04-17 11:04 274
	setDest(dest);
925b7e58 sago007 2011-04-25 16:35 275
}
925b7e58 sago007 2011-04-25 16:35 276
NFont::NFont(SDL_Surface* dest, TTF_Font* ttf, SDL_Color fg, SDL_Color bg)
925b7e58 sago007 2011-04-25 16:35 277
{
c53e6443 sago007 2012-04-17 11:04 278
	init();
c53e6443 sago007 2012-04-17 11:04 279
	load(ttf, fg, bg);
c53e6443 sago007 2012-04-17 11:04 280
	setDest(dest);
925b7e58 sago007 2011-04-25 16:35 281
}
925b7e58 sago007 2011-04-25 16:35 282
NFont::NFont(SDL_Surface* dest, const char* filename_ttf, Uint32 pointSize, SDL_Color fg, int style)
925b7e58 sago007 2011-04-25 16:35 283
{
c53e6443 sago007 2012-04-17 11:04 284
	init();
c53e6443 sago007 2012-04-17 11:04 285
	load(filename_ttf, pointSize, fg, style);
c53e6443 sago007 2012-04-17 11:04 286
	setDest(dest);
925b7e58 sago007 2011-04-25 16:35 287
}
925b7e58 sago007 2011-04-25 16:35 288
NFont::NFont(SDL_Surface* dest, const char* filename_ttf, Uint32 pointSize, SDL_Color fg, SDL_Color bg, int style)
925b7e58 sago007 2011-04-25 16:35 289
{
c53e6443 sago007 2012-04-17 11:04 290
	init();
c53e6443 sago007 2012-04-17 11:04 291
	load(filename_ttf, pointSize, fg, bg, style);
c53e6443 sago007 2012-04-17 11:04 292
	setDest(dest);
925b7e58 sago007 2011-04-25 16:35 293
}
925b7e58 sago007 2011-04-25 16:35 294
#endif
925b7e58 sago007 2011-04-25 16:35 295
925b7e58 sago007 2011-04-25 16:35 296
void NFont::init()
925b7e58 sago007 2011-04-25 16:35 297
{
c53e6443 sago007 2012-04-17 11:04 298
	src = NULL;
c53e6443 sago007 2012-04-17 11:04 299
	dest = NULL;
c53e6443 sago007 2012-04-17 11:04 300
c53e6443 sago007 2012-04-17 11:04 301
	maxPos = 0;
925b7e58 sago007 2011-04-25 16:35 302
c53e6443 sago007 2012-04-17 11:04 303
	height = 0; // ascent+descent
925b7e58 sago007 2011-04-25 16:35 304
c53e6443 sago007 2012-04-17 11:04 305
	maxWidth = 0;
c53e6443 sago007 2012-04-17 11:04 306
	baseline = 0;
c53e6443 sago007 2012-04-17 11:04 307
	ascent = 0;
c53e6443 sago007 2012-04-17 11:04 308
	descent = 0;
925b7e58 sago007 2011-04-25 16:35 309
c53e6443 sago007 2012-04-17 11:04 310
	lineSpacing = 0;
c53e6443 sago007 2012-04-17 11:04 311
	letterSpacing = 0;
925b7e58 sago007 2011-04-25 16:35 312
c53e6443 sago007 2012-04-17 11:04 313
	if(buffer == NULL)
c53e6443 sago007 2012-04-17 11:04 314
		buffer = new char[1024];
925b7e58 sago007 2011-04-25 16:35 315
}
925b7e58 sago007 2011-04-25 16:35 316
925b7e58 sago007 2011-04-25 16:35 317
NFont::~NFont()
925b7e58 sago007 2011-04-25 16:35 318
{}
925b7e58 sago007 2011-04-25 16:35 319
925b7e58 sago007 2011-04-25 16:35 320
925b7e58 sago007 2011-04-25 16:35 321
// Loading
925b7e58 sago007 2011-04-25 16:35 322
bool NFont::load(SDL_Surface* FontSurface)
925b7e58 sago007 2011-04-25 16:35 323
{
c53e6443 sago007 2012-04-17 11:04 324
	src = FontSurface;
c53e6443 sago007 2012-04-17 11:04 325
	if (src == NULL)
c53e6443 sago007 2012-04-17 11:04 326
	{
c53e6443 sago007 2012-04-17 11:04 327
		printf("\n ERROR: NFont given a NULL surface\n");
c53e6443 sago007 2012-04-17 11:04 328
		return false;
c53e6443 sago007 2012-04-17 11:04 329
	}
c53e6443 sago007 2012-04-17 11:04 330
c53e6443 sago007 2012-04-17 11:04 331
	int x = 1, i = 0;
c53e6443 sago007 2012-04-17 11:04 332
c53e6443 sago007 2012-04-17 11:04 333
	// memset would be faster
c53e6443 sago007 2012-04-17 11:04 334
	for(int j = 0; j < 256; j++)
c53e6443 sago007 2012-04-17 11:04 335
	{
c53e6443 sago007 2012-04-17 11:04 336
		charWidth[j] = 0;
c53e6443 sago007 2012-04-17 11:04 337
		charPos[j] = 0;
c53e6443 sago007 2012-04-17 11:04 338
	}
c53e6443 sago007 2012-04-17 11:04 339
c53e6443 sago007 2012-04-17 11:04 340
	SDL_LockSurface(src);
c53e6443 sago007 2012-04-17 11:04 341
c53e6443 sago007 2012-04-17 11:04 342
	Uint32 pixel = SDL_MapRGB(src->format, 255, 0, 255); // pink pixel
c53e6443 sago007 2012-04-17 11:04 343
c53e6443 sago007 2012-04-17 11:04 344
	maxWidth = 0;
c53e6443 sago007 2012-04-17 11:04 345
c53e6443 sago007 2012-04-17 11:04 346
	// Get the character positions and widths
c53e6443 sago007 2012-04-17 11:04 347
	while (x < src->w)
c53e6443 sago007 2012-04-17 11:04 348
	{
c53e6443 sago007 2012-04-17 11:04 349
		if(getPixel(src, x, 0) != pixel)
c53e6443 sago007 2012-04-17 11:04 350
		{
c53e6443 sago007 2012-04-17 11:04 351
			charPos[i] = x;
c53e6443 sago007 2012-04-17 11:04 352
			charWidth[i] = x;
c53e6443 sago007 2012-04-17 11:04 353
			while(x < src->w && getPixel(src, x, 0) != pixel)
c53e6443 sago007 2012-04-17 11:04 354
				x++;
c53e6443 sago007 2012-04-17 11:04 355
			charWidth[i] = x - charWidth[i];
c53e6443 sago007 2012-04-17 11:04 356
			if(charWidth[i] > maxWidth)
c53e6443 sago007 2012-04-17 11:04 357
				maxWidth = charWidth[i];
c53e6443 sago007 2012-04-17 11:04 358
			i++;
c53e6443 sago007 2012-04-17 11:04 359
		}
c53e6443 sago007 2012-04-17 11:04 360
c53e6443 sago007 2012-04-17 11:04 361
		x++;
c53e6443 sago007 2012-04-17 11:04 362
	}
c53e6443 sago007 2012-04-17 11:04 363
c53e6443 sago007 2012-04-17 11:04 364
	maxPos = x - 1;
c53e6443 sago007 2012-04-17 11:04 365
c53e6443 sago007 2012-04-17 11:04 366
c53e6443 sago007 2012-04-17 11:04 367
	pixel = getPixel(src, 0, src->h - 1);
c53e6443 sago007 2012-04-17 11:04 368
	int j;
c53e6443 sago007 2012-04-17 11:04 369
	setBaseline();
c53e6443 sago007 2012-04-17 11:04 370
c53e6443 sago007 2012-04-17 11:04 371
	// Get the max ascent
c53e6443 sago007 2012-04-17 11:04 372
	j = 1;
c53e6443 sago007 2012-04-17 11:04 373
	while(j < baseline && j < src->h)
c53e6443 sago007 2012-04-17 11:04 374
	{
c53e6443 sago007 2012-04-17 11:04 375
		x = 0;
c53e6443 sago007 2012-04-17 11:04 376
		while(x < src->w)
c53e6443 sago007 2012-04-17 11:04 377
		{
c53e6443 sago007 2012-04-17 11:04 378
			if(getPixel(src, x, j) != pixel)
c53e6443 sago007 2012-04-17 11:04 379
			{
c53e6443 sago007 2012-04-17 11:04 380
				ascent = baseline - j;
c53e6443 sago007 2012-04-17 11:04 381
				j = src->h;
c53e6443 sago007 2012-04-17 11:04 382
				break;
c53e6443 sago007 2012-04-17 11:04 383
			}
c53e6443 sago007 2012-04-17 11:04 384
			x++;
c53e6443 sago007 2012-04-17 11:04 385
		}
c53e6443 sago007 2012-04-17 11:04 386
		j++;
c53e6443 sago007 2012-04-17 11:04 387
	}
c53e6443 sago007 2012-04-17 11:04 388
c53e6443 sago007 2012-04-17 11:04 389
	// Get the max descent
c53e6443 sago007 2012-04-17 11:04 390
	j = src->h - 1;
c53e6443 sago007 2012-04-17 11:04 391
	while(j > 0 && j > baseline)
c53e6443 sago007 2012-04-17 11:04 392
	{
c53e6443 sago007 2012-04-17 11:04 393
		x = 0;
c53e6443 sago007 2012-04-17 11:04 394
		while(x < src->w)
c53e6443 sago007 2012-04-17 11:04 395
		{
c53e6443 sago007 2012-04-17 11:04 396
			if(getPixel(src, x, j) != pixel)
c53e6443 sago007 2012-04-17 11:04 397
			{
c53e6443 sago007 2012-04-17 11:04 398
				descent = j - baseline+1;
c53e6443 sago007 2012-04-17 11:04 399
				j = 0;
c53e6443 sago007 2012-04-17 11:04 400
				break;
c53e6443 sago007 2012-04-17 11:04 401
			}
c53e6443 sago007 2012-04-17 11:04 402
			x++;
c53e6443 sago007 2012-04-17 11:04 403
		}
c53e6443 sago007 2012-04-17 11:04 404
		j--;
c53e6443 sago007 2012-04-17 11:04 405
	}
c53e6443 sago007 2012-04-17 11:04 406
c53e6443 sago007 2012-04-17 11:04 407
c53e6443 sago007 2012-04-17 11:04 408
	height = ascent + descent;
c53e6443 sago007 2012-04-17 11:04 409
c53e6443 sago007 2012-04-17 11:04 410
c53e6443 sago007 2012-04-17 11:04 411
	if((src->flags & SDL_SRCALPHA) != SDL_SRCALPHA)
c53e6443 sago007 2012-04-17 11:04 412
	{
c53e6443 sago007 2012-04-17 11:04 413
		pixel = getPixel(src, 0, src->h - 1);
c53e6443 sago007 2012-04-17 11:04 414
		SDL_UnlockSurface(src);
c53e6443 sago007 2012-04-17 11:04 415
		SDL_SetColorKey(src, SDL_SRCCOLORKEY, pixel);
c53e6443 sago007 2012-04-17 11:04 416
	}
c53e6443 sago007 2012-04-17 11:04 417
	else
c53e6443 sago007 2012-04-17 11:04 418
		SDL_UnlockSurface(src);
c53e6443 sago007 2012-04-17 11:04 419
c53e6443 sago007 2012-04-17 11:04 420
	return true;
925b7e58 sago007 2011-04-25 16:35 421
}
925b7e58 sago007 2011-04-25 16:35 422
925b7e58 sago007 2011-04-25 16:35 423
bool NFont::load(SDL_Surface* destSurface, SDL_Surface* FontSurface)
925b7e58 sago007 2011-04-25 16:35 424
{
c53e6443 sago007 2012-04-17 11:04 425
	setDest(destSurface);
c53e6443 sago007 2012-04-17 11:04 426
	return load(FontSurface);
925b7e58 sago007 2011-04-25 16:35 427
}
925b7e58 sago007 2011-04-25 16:35 428
925b7e58 sago007 2011-04-25 16:35 429
#ifdef NFONT_USE_TTF
925b7e58 sago007 2011-04-25 16:35 430
bool NFont::load(TTF_Font* ttf, SDL_Color fg, SDL_Color bg)
925b7e58 sago007 2011-04-25 16:35 431
{
c53e6443 sago007 2012-04-17 11:04 432
	if(ttf == NULL)
c53e6443 sago007 2012-04-17 11:04 433
		return false;
c53e6443 sago007 2012-04-17 11:04 434
	SDL_Surface* surfs[127 - 33];
c53e6443 sago007 2012-04-17 11:04 435
	int width = 0;
c53e6443 sago007 2012-04-17 11:04 436
	int height = 0;
c53e6443 sago007 2012-04-17 11:04 437
c53e6443 sago007 2012-04-17 11:04 438
	char buff[2];
c53e6443 sago007 2012-04-17 11:04 439
	buff[1] = '\0';
c53e6443 sago007 2012-04-17 11:04 440
	for(int i = 0; i < 127 - 33; i++)
c53e6443 sago007 2012-04-17 11:04 441
	{
c53e6443 sago007 2012-04-17 11:04 442
		buff[0] = i + 33;
c53e6443 sago007 2012-04-17 11:04 443
		surfs[i] = TTF_RenderText_Shaded(ttf, buff, fg, bg);
c53e6443 sago007 2012-04-17 11:04 444
		width += surfs[i]->w;
c53e6443 sago007 2012-04-17 11:04 445
		height = (height < surfs[i]->h)? surfs[i]->h : height;
c53e6443 sago007 2012-04-17 11:04 446
	}
c53e6443 sago007 2012-04-17 11:04 447
c53e6443 sago007 2012-04-17 11:04 448
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
c53e6443 sago007 2012-04-17 11:04 449
	SDL_Surface* result = SDL_CreateRGBSurface(SDL_SWSURFACE,width + 127 - 33 + 1,height,24, 0xFF0000, 0x00FF00, 0x0000FF, 0);
c53e6443 sago007 2012-04-17 11:04 450
#else
c53e6443 sago007 2012-04-17 11:04 451
	SDL_Surface* result = SDL_CreateRGBSurface(SDL_SWSURFACE,width + 127 - 33 + 1,height,24, 0x0000FF, 0x00FF00, 0xFF0000, 0);
c53e6443 sago007 2012-04-17 11:04 452
#endif
c53e6443 sago007 2012-04-17 11:04 453
	Uint32 pink = SDL_MapRGB(result->format, 255, 0, 255);
c53e6443 sago007 2012-04-17 11:04 454
	Uint32 bgcolor = SDL_MapRGB(result->format, bg.r, bg.g, bg.b);
c53e6443 sago007 2012-04-17 11:04 455
c53e6443 sago007 2012-04-17 11:04 456
	SDL_Rect pixel = {1, 0, 1, 1};
c53e6443 sago007 2012-04-17 11:04 457
	SDL_Rect line = {1, 0, 1, result->h};
c53e6443 sago007 2012-04-17 11:04 458
c53e6443 sago007 2012-04-17 11:04 459
	int x = 1;
c53e6443 sago007 2012-04-17 11:04 460
	SDL_Rect dest = {x, 0, 0, 0};
c53e6443 sago007 2012-04-17 11:04 461
	for(int i = 0; i < 127 - 33; i++)
c53e6443 sago007 2012-04-17 11:04 462
	{
c53e6443 sago007 2012-04-17 11:04 463
		pixel.x = line.x = x-1;
c53e6443 sago007 2012-04-17 11:04 464
		SDL_FillRect(result, &line, bgcolor);
c53e6443 sago007 2012-04-17 11:04 465
		SDL_FillRect(result, &pixel, pink);
c53e6443 sago007 2012-04-17 11:04 466
c53e6443 sago007 2012-04-17 11:04 467
		SDL_BlitSurface(surfs[i], NULL, result, &dest);
c53e6443 sago007 2012-04-17 11:04 468
c53e6443 sago007 2012-04-17 11:04 469
		x += surfs[i]->w + 1;
c53e6443 sago007 2012-04-17 11:04 470
		dest.x = x;
c53e6443 sago007 2012-04-17 11:04 471
c53e6443 sago007 2012-04-17 11:04 472
		SDL_FreeSurface(surfs[i]);
c53e6443 sago007 2012-04-17 11:04 473
	}
c53e6443 sago007 2012-04-17 11:04 474
	pixel.x = line.x = x-1;
c53e6443 sago007 2012-04-17 11:04 475
	SDL_FillRect(result, &line, bgcolor);
c53e6443 sago007 2012-04-17 11:04 476
	SDL_FillRect(result, &pixel, pink);
c53e6443 sago007 2012-04-17 11:04 477
c53e6443 sago007 2012-04-17 11:04 478
	return load(result);
925b7e58 sago007 2011-04-25 16:35 479
}
925b7e58 sago007 2011-04-25 16:35 480
925b7e58 sago007 2011-04-25 16:35 481
925b7e58 sago007 2011-04-25 16:35 482
bool NFont::load(TTF_Font* ttf, SDL_Color fg)
925b7e58 sago007 2011-04-25 16:35 483
{
c53e6443 sago007 2012-04-17 11:04 484
	if(ttf == NULL)
c53e6443 sago007 2012-04-17 11:04 485
		return false;
c53e6443 sago007 2012-04-17 11:04 486
	SDL_Surface* surfs[127 - 33];
c53e6443 sago007 2012-04-17 11:04 487
	int width = 0;
c53e6443 sago007 2012-04-17 11:04 488
	int height = 0;
c53e6443 sago007 2012-04-17 11:04 489
c53e6443 sago007 2012-04-17 11:04 490
	char buff[2];
c53e6443 sago007 2012-04-17 11:04 491
	buff[1] = '\0';
c53e6443 sago007 2012-04-17 11:04 492
	for(int i = 0; i < 127 - 33; i++)
c53e6443 sago007 2012-04-17 11:04 493
	{
c53e6443 sago007 2012-04-17 11:04 494
		buff[0] = i + 33;
c53e6443 sago007 2012-04-17 11:04 495
		surfs[i] = TTF_RenderText_Blended(ttf, buff, fg);
c53e6443 sago007 2012-04-17 11:04 496
		width += surfs[i]->w;
c53e6443 sago007 2012-04-17 11:04 497
		height = (height < surfs[i]->h)? surfs[i]->h : height;
c53e6443 sago007 2012-04-17 11:04 498
	}
c53e6443 sago007 2012-04-17 11:04 499
c53e6443 sago007 2012-04-17 11:04 500
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
c53e6443 sago007 2012-04-17 11:04 501
	SDL_Surface* result = SDL_CreateRGBSurface(SDL_SWSURFACE,width + 127 - 33 + 1,height,32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
c53e6443 sago007 2012-04-17 11:04 502
#else
c53e6443 sago007 2012-04-17 11:04 503
	SDL_Surface* result = SDL_CreateRGBSurface(SDL_SWSURFACE,width + 127 - 33 + 1,height,32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
c53e6443 sago007 2012-04-17 11:04 504
#endif
c53e6443 sago007 2012-04-17 11:04 505
	Uint32 pink = SDL_MapRGBA(result->format, 255, 0, 255, SDL_ALPHA_OPAQUE);
c53e6443 sago007 2012-04-17 11:04 506
c53e6443 sago007 2012-04-17 11:04 507
	SDL_SetAlpha(result, 0, SDL_ALPHA_OPAQUE);
c53e6443 sago007 2012-04-17 11:04 508
c53e6443 sago007 2012-04-17 11:04 509
	SDL_Rect pixel = {1, 0, 1, 1};
c53e6443 sago007 2012-04-17 11:04 510
c53e6443 sago007 2012-04-17 11:04 511
	int x = 1;
c53e6443 sago007 2012-04-17 11:04 512
	SDL_Rect dest = {x, 0, 0, 0};
c53e6443 sago007 2012-04-17 11:04 513
	for(int i = 0; i < 127 - 33; i++)
c53e6443 sago007 2012-04-17 11:04 514
	{
c53e6443 sago007 2012-04-17 11:04 515
		pixel.x = x-1;
c53e6443 sago007 2012-04-17 11:04 516
		SDL_FillRect(result, &pixel, pink);
c53e6443 sago007 2012-04-17 11:04 517
c53e6443 sago007 2012-04-17 11:04 518
		SDL_SetAlpha(surfs[i], 0, SDL_ALPHA_OPAQUE);
c53e6443 sago007 2012-04-17 11:04 519
		SDL_BlitSurface(surfs[i], NULL, result, &dest);
c53e6443 sago007 2012-04-17 11:04 520
c53e6443 sago007 2012-04-17 11:04 521
		x += surfs[i]->w + 1;
c53e6443 sago007 2012-04-17 11:04 522
		dest.x = x;
c53e6443 sago007 2012-04-17 11:04 523
c53e6443 sago007 2012-04-17 11:04 524
		SDL_FreeSurface(surfs[i]);
c53e6443 sago007 2012-04-17 11:04 525
	}
c53e6443 sago007 2012-04-17 11:04 526
	pixel.x = x-1;
c53e6443 sago007 2012-04-17 11:04 527
	SDL_FillRect(result, &pixel, pink);
c53e6443 sago007 2012-04-17 11:04 528
c53e6443 sago007 2012-04-17 11:04 529
	SDL_SetAlpha(result, SDL_SRCALPHA, SDL_ALPHA_OPAQUE);
c53e6443 sago007 2012-04-17 11:04 530
c53e6443 sago007 2012-04-17 11:04 531
	return load(result);
925b7e58 sago007 2011-04-25 16:35 532
}
925b7e58 sago007 2011-04-25 16:35 533
925b7e58 sago007 2011-04-25 16:35 534
925b7e58 sago007 2011-04-25 16:35 535
bool NFont::load(const char* filename_ttf, Uint32 pointSize, SDL_Color fg, int style)
925b7e58 sago007 2011-04-25 16:35 536
{
c53e6443 sago007 2012-04-17 11:04 537
	if(!TTF_WasInit() && TTF_Init() < 0)
c53e6443 sago007 2012-04-17 11:04 538
	{
c53e6443 sago007 2012-04-17 11:04 539
		printf("Unable to initialize SDL_ttf: %s \n", TTF_GetError());
c53e6443 sago007 2012-04-17 11:04 540
		return false;
c53e6443 sago007 2012-04-17 11:04 541
	}
c53e6443 sago007 2012-04-17 11:04 542
c53e6443 sago007 2012-04-17 11:04 543
	TTF_Font* ttf = TTF_OpenFont(filename_ttf, pointSize);
c53e6443 sago007 2012-04-17 11:04 544
c53e6443 sago007 2012-04-17 11:04 545
	if(ttf == NULL)
c53e6443 sago007 2012-04-17 11:04 546
	{
c53e6443 sago007 2012-04-17 11:04 547
		printf("Unable to load TrueType font: %s \n", TTF_GetError());
c53e6443 sago007 2012-04-17 11:04 548
		return false;
c53e6443 sago007 2012-04-17 11:04 549
	}
c53e6443 sago007 2012-04-17 11:04 550
	TTF_SetFontStyle(ttf, style);
c53e6443 sago007 2012-04-17 11:04 551
	bool result = load(ttf, fg);
c53e6443 sago007 2012-04-17 11:04 552
	TTF_CloseFont(ttf);
c53e6443 sago007 2012-04-17 11:04 553
	return result;
925b7e58 sago007 2011-04-25 16:35 554
}
925b7e58 sago007 2011-04-25 16:35 555
925b7e58 sago007 2011-04-25 16:35 556
bool NFont::load(const char* filename_ttf, Uint32 pointSize, SDL_Color fg, SDL_Color bg, int style)
925b7e58 sago007 2011-04-25 16:35 557
{
c53e6443 sago007 2012-04-17 11:04 558
	if(!TTF_WasInit() && TTF_Init() < 0)
c53e6443 sago007 2012-04-17 11:04 559
	{
c53e6443 sago007 2012-04-17 11:04 560
		printf("Unable to initialize SDL_ttf: %s \n", TTF_GetError());
c53e6443 sago007 2012-04-17 11:04 561
		return false;
c53e6443 sago007 2012-04-17 11:04 562
	}
c53e6443 sago007 2012-04-17 11:04 563
c53e6443 sago007 2012-04-17 11:04 564
	TTF_Font* ttf = TTF_OpenFont(filename_ttf, pointSize);
c53e6443 sago007 2012-04-17 11:04 565
c53e6443 sago007 2012-04-17 11:04 566
	if(ttf == NULL)
c53e6443 sago007 2012-04-17 11:04 567
	{
c53e6443 sago007 2012-04-17 11:04 568
		printf("Unable to load TrueType font: %s \n", TTF_GetError());
c53e6443 sago007 2012-04-17 11:04 569
		return false;
c53e6443 sago007 2012-04-17 11:04 570
	}
c53e6443 sago007 2012-04-17 11:04 571
	TTF_SetFontStyle(ttf, style);
c53e6443 sago007 2012-04-17 11:04 572
	bool result = load(ttf, fg, bg);
c53e6443 sago007 2012-04-17 11:04 573
	TTF_CloseFont(ttf);
c53e6443 sago007 2012-04-17 11:04 574
	return result;
925b7e58 sago007 2011-04-25 16:35 575
}
925b7e58 sago007 2011-04-25 16:35 576
925b7e58 sago007 2011-04-25 16:35 577
#endif
925b7e58 sago007 2011-04-25 16:35 578
925b7e58 sago007 2011-04-25 16:35 579
925b7e58 sago007 2011-04-25 16:35 580
925b7e58 sago007 2011-04-25 16:35 581
void NFont::freeSurface()
925b7e58 sago007 2011-04-25 16:35 582
{
c53e6443 sago007 2012-04-17 11:04 583
	SDL_FreeSurface(src);
925b7e58 sago007 2011-04-25 16:35 584
}
925b7e58 sago007 2011-04-25 16:35 585
925b7e58 sago007 2011-04-25 16:35 586
925b7e58 sago007 2011-04-25 16:35 587
925b7e58 sago007 2011-04-25 16:35 588
// Drawing
925b7e58 sago007 2011-04-25 16:35 589
SDL_Rect NFont::drawToSurface(int x, int y, const char* text) const
925b7e58 sago007 2011-04-25 16:35 590
{
c53e6443 sago007 2012-04-17 11:04 591
	const char* c = text;
c53e6443 sago007 2012-04-17 11:04 592
	unsigned char num;
c53e6443 sago007 2012-04-17 11:04 593
	SDL_Rect srcRect, dstRect, copyS, copyD;
c53e6443 sago007 2012-04-17 11:04 594
	data.dirtyRect = makeRect(x, y, 0, 0);
c53e6443 sago007 2012-04-17 11:04 595
c53e6443 sago007 2012-04-17 11:04 596
	if(c == NULL || src == NULL || dest == NULL)
c53e6443 sago007 2012-04-17 11:04 597
		return data.dirtyRect;
c53e6443 sago007 2012-04-17 11:04 598
c53e6443 sago007 2012-04-17 11:04 599
	srcRect.y = baseline - ascent;
c53e6443 sago007 2012-04-17 11:04 600
	srcRect.h = dstRect.h = height;
c53e6443 sago007 2012-04-17 11:04 601
	dstRect.x = x;
c53e6443 sago007 2012-04-17 11:04 602
	dstRect.y = y;
c53e6443 sago007 2012-04-17 11:04 603
c53e6443 sago007 2012-04-17 11:04 604
	int newlineX = x;
c53e6443 sago007 2012-04-17 11:04 605
c53e6443 sago007 2012-04-17 11:04 606
	for(; *c != '\0'; c++)
c53e6443 sago007 2012-04-17 11:04 607
	{
c53e6443 sago007 2012-04-17 11:04 608
		if(*c == '\n')
c53e6443 sago007 2012-04-17 11:04 609
		{
c53e6443 sago007 2012-04-17 11:04 610
			dstRect.x = newlineX;
c53e6443 sago007 2012-04-17 11:04 611
			dstRect.y += height + lineSpacing;
c53e6443 sago007 2012-04-17 11:04 612
			continue;
c53e6443 sago007 2012-04-17 11:04 613
		}
c53e6443 sago007 2012-04-17 11:04 614
c53e6443 sago007 2012-04-17 11:04 615
		if (*c == ' ')
c53e6443 sago007 2012-04-17 11:04 616
		{
c53e6443 sago007 2012-04-17 11:04 617
			dstRect.x += charWidth[0] + letterSpacing;
c53e6443 sago007 2012-04-17 11:04 618
			continue;
c53e6443 sago007 2012-04-17 11:04 619
		}
c53e6443 sago007 2012-04-17 11:04 620
		unsigned char ctest = (unsigned char)(*c);
c53e6443 sago007 2012-04-17 11:04 621
		// Skip bad characters
c53e6443 sago007 2012-04-17 11:04 622
		if(ctest < 33 || (ctest > 126 && ctest < 161))
c53e6443 sago007 2012-04-17 11:04 623
			continue;
c53e6443 sago007 2012-04-17 11:04 624
		if(dstRect.x >= dest->w)
c53e6443 sago007 2012-04-17 11:04 625
			continue;
c53e6443 sago007 2012-04-17 11:04 626
		if(dstRect.y >= dest->h)
c53e6443 sago007 2012-04-17 11:04 627
			continue;
c53e6443 sago007 2012-04-17 11:04 628
c53e6443 sago007 2012-04-17 11:04 629
		num = ctest - 33;  // Get array index
c53e6443 sago007 2012-04-17 11:04 630
		if(num > 126) // shift the extended characters down to the correct index
c53e6443 sago007 2012-04-17 11:04 631
			num -= 34;
c53e6443 sago007 2012-04-17 11:04 632
		srcRect.x = charPos[num];
c53e6443 sago007 2012-04-17 11:04 633
		srcRect.w = dstRect.w = charWidth[num];
c53e6443 sago007 2012-04-17 11:04 634
		copyS = srcRect;
c53e6443 sago007 2012-04-17 11:04 635
		copyD = dstRect;
c53e6443 sago007 2012-04-17 11:04 636
		SDL_BlitSurface(src, &srcRect, dest, &dstRect);
c53e6443 sago007 2012-04-17 11:04 637
		if(data.dirtyRect.w == 0 || data.dirtyRect.h == 0)
c53e6443 sago007 2012-04-17 11:04 638
			data.dirtyRect = dstRect;
c53e6443 sago007 2012-04-17 11:04 639
		else
c53e6443 sago007 2012-04-17 11:04 640
			data.dirtyRect = rectUnion(data.dirtyRect, dstRect);
c53e6443 sago007 2012-04-17 11:04 641
		srcRect = copyS;
c53e6443 sago007 2012-04-17 11:04 642
		dstRect = copyD;
c53e6443 sago007 2012-04-17 11:04 643
c53e6443 sago007 2012-04-17 11:04 644
		dstRect.x += dstRect.w + letterSpacing;
c53e6443 sago007 2012-04-17 11:04 645
	}
c53e6443 sago007 2012-04-17 11:04 646
c53e6443 sago007 2012-04-17 11:04 647
	return data.dirtyRect;
925b7e58 sago007 2011-04-25 16:35 648
}
925b7e58 sago007 2011-04-25 16:35 649
925b7e58 sago007 2011-04-25 16:35 650
SDL_Rect NFont::drawToSurfacePos(int x, int y, NFont::AnimFn posFn) const
925b7e58 sago007 2011-04-25 16:35 651
{
c53e6443 sago007 2012-04-17 11:04 652
	data.font = this;
c53e6443 sago007 2012-04-17 11:04 653
	data.dest = dest;
c53e6443 sago007 2012-04-17 11:04 654
	data.src = src;
c53e6443 sago007 2012-04-17 11:04 655
	data.text = buffer;  // Buffer for efficient drawing
c53e6443 sago007 2012-04-17 11:04 656
	data.height = height;
c53e6443 sago007 2012-04-17 11:04 657
	data.charPos = charPos;
c53e6443 sago007 2012-04-17 11:04 658
	data.charWidth = charWidth;
c53e6443 sago007 2012-04-17 11:04 659
	data.maxX = maxPos;
c53e6443 sago007 2012-04-17 11:04 660
	data.dirtyRect = makeRect(x,y,0,0);
c53e6443 sago007 2012-04-17 11:04 661
c53e6443 sago007 2012-04-17 11:04 662
	data.index = -1;
c53e6443 sago007 2012-04-17 11:04 663
	data.letterNum = 0;
c53e6443 sago007 2012-04-17 11:04 664
	data.wordNum = 1;
c53e6443 sago007 2012-04-17 11:04 665
	data.lineNum = 1;
c53e6443 sago007 2012-04-17 11:04 666
	data.startX = x;  // used as reset value for line feed
c53e6443 sago007 2012-04-17 11:04 667
	data.startY = y;
c53e6443 sago007 2012-04-17 11:04 668
c53e6443 sago007 2012-04-17 11:04 669
	int preFnX = x;
c53e6443 sago007 2012-04-17 11:04 670
	int preFnY = y;
c53e6443 sago007 2012-04-17 11:04 671
c53e6443 sago007 2012-04-17 11:04 672
	const char* c = buffer;
c53e6443 sago007 2012-04-17 11:04 673
	unsigned char num;
c53e6443 sago007 2012-04-17 11:04 674
	SDL_Rect srcRect, dstRect, copyS, copyD;
c53e6443 sago007 2012-04-17 11:04 675
c53e6443 sago007 2012-04-17 11:04 676
	if(c == NULL || src == NULL || dest == NULL)
c53e6443 sago007 2012-04-17 11:04 677
		return makeRect(x,y,0,0);
c53e6443 sago007 2012-04-17 11:04 678
c53e6443 sago007 2012-04-17 11:04 679
	srcRect.y = baseline - ascent;
c53e6443 sago007 2012-04-17 11:04 680
	srcRect.h = dstRect.h = height;
c53e6443 sago007 2012-04-17 11:04 681
	dstRect.x = x;
c53e6443 sago007 2012-04-17 11:04 682
	dstRect.y = y;
c53e6443 sago007 2012-04-17 11:04 683
c53e6443 sago007 2012-04-17 11:04 684
	for(; *c != '\0'; c++)
c53e6443 sago007 2012-04-17 11:04 685
	{
c53e6443 sago007 2012-04-17 11:04 686
		data.index++;
c53e6443 sago007 2012-04-17 11:04 687
		data.letterNum++;
c53e6443 sago007 2012-04-17 11:04 688
c53e6443 sago007 2012-04-17 11:04 689
		if(*c == '\n')
c53e6443 sago007 2012-04-17 11:04 690
		{
c53e6443 sago007 2012-04-17 11:04 691
			data.letterNum = 1;
c53e6443 sago007 2012-04-17 11:04 692
			data.wordNum = 1;
c53e6443 sago007 2012-04-17 11:04 693
			data.lineNum++;
c53e6443 sago007 2012-04-17 11:04 694
c53e6443 sago007 2012-04-17 11:04 695
			x = data.startX;  // carriage return
c53e6443 sago007 2012-04-17 11:04 696
			y += height + lineSpacing;
c53e6443 sago007 2012-04-17 11:04 697
			continue;
c53e6443 sago007 2012-04-17 11:04 698
		}
c53e6443 sago007 2012-04-17 11:04 699
		if (*c == ' ')
c53e6443 sago007 2012-04-17 11:04 700
		{
c53e6443 sago007 2012-04-17 11:04 701
			data.letterNum = 1;
c53e6443 sago007 2012-04-17 11:04 702
			data.wordNum++;
c53e6443 sago007 2012-04-17 11:04 703
c53e6443 sago007 2012-04-17 11:04 704
			x += charWidth[0] + letterSpacing;
c53e6443 sago007 2012-04-17 11:04 705
			continue;
c53e6443 sago007 2012-04-17 11:04 706
		}
c53e6443 sago007 2012-04-17 11:04 707
		unsigned char ctest = (unsigned char)(*c);
c53e6443 sago007 2012-04-17 11:04 708
		// Skip bad characters
c53e6443 sago007 2012-04-17 11:04 709
		if(ctest < 33 || (ctest > 126 && ctest < 161))
c53e6443 sago007 2012-04-17 11:04 710
			continue;
c53e6443 sago007 2012-04-17 11:04 711
		//if(x >= dest->w) // This shouldn't be used with position control
c53e6443 sago007 2012-04-17 11:04 712
		//    continue;
c53e6443 sago007 2012-04-17 11:04 713
		num = ctest - 33;
c53e6443 sago007 2012-04-17 11:04 714
		if(num > 126) // shift the extended characters down to the array index
c53e6443 sago007 2012-04-17 11:04 715
			num -= 34;
c53e6443 sago007 2012-04-17 11:04 716
		srcRect.x = charPos[num];
c53e6443 sago007 2012-04-17 11:04 717
		srcRect.w = dstRect.w = charWidth[num];
c53e6443 sago007 2012-04-17 11:04 718
c53e6443 sago007 2012-04-17 11:04 719
		preFnX = x;  // Save real position
c53e6443 sago007 2012-04-17 11:04 720
		preFnY = y;
c53e6443 sago007 2012-04-17 11:04 721
c53e6443 sago007 2012-04-17 11:04 722
		// Use function pointer to get final x, y values
c53e6443 sago007 2012-04-17 11:04 723
		posFn(x, y, data);
c53e6443 sago007 2012-04-17 11:04 724
c53e6443 sago007 2012-04-17 11:04 725
		dstRect.x = x;
c53e6443 sago007 2012-04-17 11:04 726
		dstRect.y = y;
c53e6443 sago007 2012-04-17 11:04 727
c53e6443 sago007 2012-04-17 11:04 728
		copyS = srcRect;
c53e6443 sago007 2012-04-17 11:04 729
		copyD = dstRect;
c53e6443 sago007 2012-04-17 11:04 730
		SDL_BlitSurface(src, &srcRect, dest, &dstRect);
c53e6443 sago007 2012-04-17 11:04 731
		if(data.dirtyRect.w == 0 || data.dirtyRect.h == 0)
c53e6443 sago007 2012-04-17 11:04 732
			data.dirtyRect = dstRect;
c53e6443 sago007 2012-04-17 11:04 733
		else
c53e6443 sago007 2012-04-17 11:04 734
			data.dirtyRect = rectUnion(data.dirtyRect, dstRect);
c53e6443 sago007 2012-04-17 11:04 735
		srcRect = copyS;
c53e6443 sago007 2012-04-17 11:04 736
		dstRect = copyD;
c53e6443 sago007 2012-04-17 11:04 737
c53e6443 sago007 2012-04-17 11:04 738
		x = preFnX;  // Restore real position
c53e6443 sago007 2012-04-17 11:04 739
		y = preFnY;
c53e6443 sago007 2012-04-17 11:04 740
c53e6443 sago007 2012-04-17 11:04 741
		x += dstRect.w + letterSpacing;
c53e6443 sago007 2012-04-17 11:04 742
	}
c53e6443 sago007 2012-04-17 11:04 743
c53e6443 sago007 2012-04-17 11:04 744
	return data.dirtyRect;
925b7e58 sago007 2011-04-25 16:35 745
}
925b7e58 sago007 2011-04-25 16:35 746
925b7e58 sago007 2011-04-25 16:35 747
SDL_Rect NFont::draw(int x, int y, const char* formatted_text, ...) const
925b7e58 sago007 2011-04-25 16:35 748
{
c53e6443 sago007 2012-04-17 11:04 749
	if(formatted_text == NULL)
c53e6443 sago007 2012-04-17 11:04 750
		return makeRect(x, y, 0, 0);
925b7e58 sago007 2011-04-25 16:35 751
c53e6443 sago007 2012-04-17 11:04 752
	va_list lst;
c53e6443 sago007 2012-04-17 11:04 753
	va_start(lst, formatted_text);
c53e6443 sago007 2012-04-17 11:04 754
	vsprintf(buffer, formatted_text, lst);
c53e6443 sago007 2012-04-17 11:04 755
	va_end(lst);
925b7e58 sago007 2011-04-25 16:35 756
c53e6443 sago007 2012-04-17 11:04 757
	return drawToSurface(x, y, buffer);
925b7e58 sago007 2011-04-25 16:35 758
}
925b7e58 sago007 2011-04-25 16:35 759
925b7e58 sago007 2011-04-25 16:35 760
SDL_Rect NFont::drawCenter(int x, int y, const char* formatted_text, ...) const
925b7e58 sago007 2011-04-25 16:35 761
{
c53e6443 sago007 2012-04-17 11:04 762
	if(formatted_text == NULL)
c53e6443 sago007 2012-04-17 11:04 763
		return makeRect(x, y, 0, 0);
c53e6443 sago007 2012-04-17 11:04 764
c53e6443 sago007 2012-04-17 11:04 765
	va_list lst;
c53e6443 sago007 2012-04-17 11:04 766
	va_start(lst, formatted_text);
c53e6443 sago007 2012-04-17 11:04 767
	vsprintf(buffer, formatted_text, lst);
c53e6443 sago007 2012-04-17 11:04 768
	va_end(lst);
c53e6443 sago007 2012-04-17 11:04 769
c53e6443 sago007 2012-04-17 11:04 770
	char* str = copyString(buffer);
c53e6443 sago007 2012-04-17 11:04 771
	char* del = str;
c53e6443 sago007 2012-04-17 11:04 772
c53e6443 sago007 2012-04-17 11:04 773
	// Go through str, when you find a \n, replace it with \0 and print it
c53e6443 sago007 2012-04-17 11:04 774
	// then move down, back, and continue.
c53e6443 sago007 2012-04-17 11:04 775
	for(char* c = str; *c != '\0';)
c53e6443 sago007 2012-04-17 11:04 776
	{
c53e6443 sago007 2012-04-17 11:04 777
		if(*c == '\n')
c53e6443 sago007 2012-04-17 11:04 778
		{
c53e6443 sago007 2012-04-17 11:04 779
			*c = '\0';
c53e6443 sago007 2012-04-17 11:04 780
			drawToSurface(x - getWidth("%s", str)/2, y, str);
c53e6443 sago007 2012-04-17 11:04 781
			*c = '\n';
c53e6443 sago007 2012-04-17 11:04 782
			c++;
c53e6443 sago007 2012-04-17 11:04 783
			str = c;
c53e6443 sago007 2012-04-17 11:04 784
			y += height;
c53e6443 sago007 2012-04-17 11:04 785
		}
c53e6443 sago007 2012-04-17 11:04 786
		else
c53e6443 sago007 2012-04-17 11:04 787
			c++;
c53e6443 sago007 2012-04-17 11:04 788
	}
c53e6443 sago007 2012-04-17 11:04 789
	char s[strlen(str)+1];
c53e6443 sago007 2012-04-17 11:04 790
	strcpy(s, str);
c53e6443 sago007 2012-04-17 11:04 791
	delete[] del;
c53e6443 sago007 2012-04-17 11:04 792
c53e6443 sago007 2012-04-17 11:04 793
	return drawToSurface(x - getWidth("%s", s)/2, y, s);
925b7e58 sago007 2011-04-25 16:35 794
}
925b7e58 sago007 2011-04-25 16:35 795
925b7e58 sago007 2011-04-25 16:35 796
SDL_Rect NFont::drawRight(int x, int y, const char* formatted_text, ...) const
925b7e58 sago007 2011-04-25 16:35 797
{
c53e6443 sago007 2012-04-17 11:04 798
	if(formatted_text == NULL)
c53e6443 sago007 2012-04-17 11:04 799
		return makeRect(x, y, 0, 0);
c53e6443 sago007 2012-04-17 11:04 800
c53e6443 sago007 2012-04-17 11:04 801
	va_list lst;
c53e6443 sago007 2012-04-17 11:04 802
	va_start(lst, formatted_text);
c53e6443 sago007 2012-04-17 11:04 803
	vsprintf(buffer, formatted_text, lst);
c53e6443 sago007 2012-04-17 11:04 804
	va_end(lst);
c53e6443 sago007 2012-04-17 11:04 805
c53e6443 sago007 2012-04-17 11:04 806
	char* str = copyString(buffer);
c53e6443 sago007 2012-04-17 11:04 807
	char* del = str;
c53e6443 sago007 2012-04-17 11:04 808
c53e6443 sago007 2012-04-17 11:04 809
	for(char* c = str; *c != '\0';)
c53e6443 sago007 2012-04-17 11:04 810
	{
c53e6443 sago007 2012-04-17 11:04 811
		if(*c == '\n')
c53e6443 sago007 2012-04-17 11:04 812
		{
c53e6443 sago007 2012-04-17 11:04 813
			*c = '\0';
c53e6443 sago007 2012-04-17 11:04 814
			drawToSurface(x - getWidth("%s", str), y, str);
c53e6443 sago007 2012-04-17 11:04 815
			*c = '\n';
c53e6443 sago007 2012-04-17 11:04 816
			c++;
c53e6443 sago007 2012-04-17 11:04 817
			str = c;
c53e6443 sago007 2012-04-17 11:04 818
			y += height;
c53e6443 sago007 2012-04-17 11:04 819
		}
c53e6443 sago007 2012-04-17 11:04 820
		else
c53e6443 sago007 2012-04-17 11:04 821
			c++;
c53e6443 sago007 2012-04-17 11:04 822
	}
c53e6443 sago007 2012-04-17 11:04 823
	char s[strlen(str)+1];
c53e6443 sago007 2012-04-17 11:04 824
	strcpy(s, str);
c53e6443 sago007 2012-04-17 11:04 825
	delete[] del;
c53e6443 sago007 2012-04-17 11:04 826
c53e6443 sago007 2012-04-17 11:04 827
	return drawToSurface(x - getWidth("%s", s), y, s);
925b7e58 sago007 2011-04-25 16:35 828
}
925b7e58 sago007 2011-04-25 16:35 829
925b7e58 sago007 2011-04-25 16:35 830
SDL_Rect NFont::drawPos(int x, int y, NFont::AnimFn posFn, const char* text, ...) const
925b7e58 sago007 2011-04-25 16:35 831
{
c53e6443 sago007 2012-04-17 11:04 832
	va_list lst;
c53e6443 sago007 2012-04-17 11:04 833
	va_start(lst, text);
c53e6443 sago007 2012-04-17 11:04 834
	vsprintf(buffer, text, lst);
c53e6443 sago007 2012-04-17 11:04 835
	va_end(lst);
925b7e58 sago007 2011-04-25 16:35 836
c53e6443 sago007 2012-04-17 11:04 837
	return drawToSurfacePos(x, y, posFn);
925b7e58 sago007 2011-04-25 16:35 838
}
925b7e58 sago007 2011-04-25 16:35 839
925b7e58 sago007 2011-04-25 16:35 840
SDL_Rect NFont::drawAll(int x, int y, NFont::AnimFn allFn, const char* text, ...) const
925b7e58 sago007 2011-04-25 16:35 841
{
c53e6443 sago007 2012-04-17 11:04 842
	va_list lst;
c53e6443 sago007 2012-04-17 11:04 843
	va_start(lst, text);
c53e6443 sago007 2012-04-17 11:04 844
	vsprintf(buffer, text, lst);
c53e6443 sago007 2012-04-17 11:04 845
	va_end(lst);
925b7e58 sago007 2011-04-25 16:35 846
c53e6443 sago007 2012-04-17 11:04 847
	allFn(x, y, data);
c53e6443 sago007 2012-04-17 11:04 848
	return data.dirtyRect;
925b7e58 sago007 2011-04-25 16:35 849
}
925b7e58 sago007 2011-04-25 16:35 850
925b7e58 sago007 2011-04-25 16:35 851
925b7e58 sago007 2011-04-25 16:35 852
925b7e58 sago007 2011-04-25 16:35 853
925b7e58 sago007 2011-04-25 16:35 854
// Getters
925b7e58 sago007 2011-04-25 16:35 855
SDL_Surface* NFont::getDest() const
925b7e58 sago007 2011-04-25 16:35 856
{
c53e6443 sago007 2012-04-17 11:04 857
	return dest;
925b7e58 sago007 2011-04-25 16:35 858
}
925b7e58 sago007 2011-04-25 16:35 859
925b7e58 sago007 2011-04-25 16:35 860
SDL_Surface* NFont::getSurface() const
925b7e58 sago007 2011-04-25 16:35 861
{
c53e6443 sago007 2012-04-17 11:04 862
	return src;
925b7e58 sago007 2011-04-25 16:35 863
}
925b7e58 sago007 2011-04-25 16:35 864
925b7e58 sago007 2011-04-25 16:35 865
int NFont::getHeight(const char* formatted_text, ...) const
925b7e58 sago007 2011-04-25 16:35 866
{
c53e6443 sago007 2012-04-17 11:04 867
	if(formatted_text == NULL)
c53e6443 sago007 2012-04-17 11:04 868
		return height;
925b7e58 sago007 2011-04-25 16:35 869
c53e6443 sago007 2012-04-17 11:04 870
	va_list lst;
c53e6443 sago007 2012-04-17 11:04 871
	va_start(lst, formatted_text);
c53e6443 sago007 2012-04-17 11:04 872
	vsprintf(buffer, formatted_text, lst);
c53e6443 sago007 2012-04-17 11:04 873
	va_end(lst);
925b7e58 sago007 2011-04-25 16:35 874
c53e6443 sago007 2012-04-17 11:04 875
	int numLines = 1;
c53e6443 sago007 2012-04-17 11:04 876
	const char* c;
925b7e58 sago007 2011-04-25 16:35 877
c53e6443 sago007 2012-04-17 11:04 878
	for (c = buffer; *c != '\0'; c++)
c53e6443 sago007 2012-04-17 11:04 879
	{
c53e6443 sago007 2012-04-17 11:04 880
		if(*c == '\n')
c53e6443 sago007 2012-04-17 11:04 881
			numLines++;
c53e6443 sago007 2012-04-17 11:04 882
	}
925b7e58 sago007 2011-04-25 16:35 883
c53e6443 sago007 2012-04-17 11:04 884
	//   Actual height of letter region + line spacing
c53e6443 sago007 2012-04-17 11:04 885
	return height*numLines + lineSpacing*(numLines - 1);  //height*numLines;
925b7e58 sago007 2011-04-25 16:35 886
}
925b7e58 sago007 2011-04-25 16:35 887
925b7e58 sago007 2011-04-25 16:35 888
int NFont::getWidth(const char* formatted_text, ...) const
925b7e58 sago007 2011-04-25 16:35 889
{
c53e6443 sago007 2012-04-17 11:04 890
	if (formatted_text == NULL)
c53e6443 sago007 2012-04-17 11:04 891
		return 0;
c53e6443 sago007 2012-04-17 11:04 892
c53e6443 sago007 2012-04-17 11:04 893
	va_list lst;
c53e6443 sago007 2012-04-17 11:04 894
	va_start(lst, formatted_text);
c53e6443 sago007 2012-04-17 11:04 895
	vsprintf(buffer, formatted_text, lst);
c53e6443 sago007 2012-04-17 11:04 896
	va_end(lst);
c53e6443 sago007 2012-04-17 11:04 897
c53e6443 sago007 2012-04-17 11:04 898
	const char* c;
c53e6443 sago007 2012-04-17 11:04 899
	int charnum = 0;
c53e6443 sago007 2012-04-17 11:04 900
	int width = 0;
c53e6443 sago007 2012-04-17 11:04 901
	int bigWidth = 0;  // Allows for multi-line strings
c53e6443 sago007 2012-04-17 11:04 902
c53e6443 sago007 2012-04-17 11:04 903
	for (c = buffer; *c != '\0'; c++)
c53e6443 sago007 2012-04-17 11:04 904
	{
c53e6443 sago007 2012-04-17 11:04 905
		charnum = (unsigned char)(*c) - 33;
c53e6443 sago007 2012-04-17 11:04 906
c53e6443 sago007 2012-04-17 11:04 907
		// skip spaces and nonprintable characters
c53e6443 sago007 2012-04-17 11:04 908
		if(*c == '\n')
c53e6443 sago007 2012-04-17 11:04 909
		{
c53e6443 sago007 2012-04-17 11:04 910
			bigWidth = bigWidth >= width? bigWidth : width;
c53e6443 sago007 2012-04-17 11:04 911
			width = 0;
c53e6443 sago007 2012-04-17 11:04 912
		}
c53e6443 sago007 2012-04-17 11:04 913
		else if (*c == ' ' || charnum > 222)
c53e6443 sago007 2012-04-17 11:04 914
		{
c53e6443 sago007 2012-04-17 11:04 915
			width += charWidth[0];
c53e6443 sago007 2012-04-17 11:04 916
			continue;
c53e6443 sago007 2012-04-17 11:04 917
		}
c53e6443 sago007 2012-04-17 11:04 918
c53e6443 sago007 2012-04-17 11:04 919
		width += charWidth[charnum];
c53e6443 sago007 2012-04-17 11:04 920
	}
c53e6443 sago007 2012-04-17 11:04 921
	bigWidth = bigWidth >= width? bigWidth : width;
c53e6443 sago007 2012-04-17 11:04 922
c53e6443 sago007 2012-04-17 11:04 923
	return bigWidth;
925b7e58 sago007 2011-04-25 16:35 924
}
925b7e58 sago007 2011-04-25 16:35 925
925b7e58 sago007 2011-04-25 16:35 926
int NFont::getAscent(const char character) const
925b7e58 sago007 2011-04-25 16:35 927
{
c53e6443 sago007 2012-04-17 11:04 928
	unsigned char test = (unsigned char)character;
c53e6443 sago007 2012-04-17 11:04 929
	if(test < 33 || test > 222 || (test > 126 && test < 161))
c53e6443 sago007 2012-04-17 11:04 930
		return 0;
c53e6443 sago007 2012-04-17 11:04 931
	unsigned char num = (unsigned char)character - 33;
c53e6443 sago007 2012-04-17 11:04 932
	// Get the max ascent
c53e6443 sago007 2012-04-17 11:04 933
	int x = charPos[num];
c53e6443 sago007 2012-04-17 11:04 934
	int i, j = 1, result = 0;
c53e6443 sago007 2012-04-17 11:04 935
	Uint32 pixel = getPixel(src, 0, src->h - 1); // bg pixel
c53e6443 sago007 2012-04-17 11:04 936
	while(j < baseline && j < src->h)
c53e6443 sago007 2012-04-17 11:04 937
	{
c53e6443 sago007 2012-04-17 11:04 938
		i = charPos[num];
c53e6443 sago007 2012-04-17 11:04 939
		while(i < x + charWidth[num])
c53e6443 sago007 2012-04-17 11:04 940
		{
c53e6443 sago007 2012-04-17 11:04 941
			if(getPixel(src, i, j) != pixel)
c53e6443 sago007 2012-04-17 11:04 942
			{
c53e6443 sago007 2012-04-17 11:04 943
				result = baseline - j;
c53e6443 sago007 2012-04-17 11:04 944
				j = src->h;
c53e6443 sago007 2012-04-17 11:04 945
				break;
c53e6443 sago007 2012-04-17 11:04 946
			}
c53e6443 sago007 2012-04-17 11:04 947
			i++;
c53e6443 sago007 2012-04-17 11:04 948
		}
c53e6443 sago007 2012-04-17 11:04 949
		j++;
c53e6443 sago007 2012-04-17 11:04 950
	}
c53e6443 sago007 2012-04-17 11:04 951
	return result;
925b7e58 sago007 2011-04-25 16:35 952
}
925b7e58 sago007 2011-04-25 16:35 953
925b7e58 sago007 2011-04-25 16:35 954
int NFont::getAscent(const char* formatted_text, ...) const
925b7e58 sago007 2011-04-25 16:35 955
{
c53e6443 sago007 2012-04-17 11:04 956
	if(formatted_text == NULL)
c53e6443 sago007 2012-04-17 11:04 957
		return ascent;
c53e6443 sago007 2012-04-17 11:04 958
c53e6443 sago007 2012-04-17 11:04 959
	va_list lst;
c53e6443 sago007 2012-04-17 11:04 960
	va_start(lst, formatted_text);
c53e6443 sago007 2012-04-17 11:04 961
	vsprintf(buffer, formatted_text, lst);
c53e6443 sago007 2012-04-17 11:04 962
	va_end(lst);
c53e6443 sago007 2012-04-17 11:04 963
c53e6443 sago007 2012-04-17 11:04 964
	int max = 0;
c53e6443 sago007 2012-04-17 11:04 965
	const char* c = buffer;
c53e6443 sago007 2012-04-17 11:04 966
c53e6443 sago007 2012-04-17 11:04 967
	for (; *c != '\0'; c++)
c53e6443 sago007 2012-04-17 11:04 968
	{
c53e6443 sago007 2012-04-17 11:04 969
		int asc = getAscent(*c);
c53e6443 sago007 2012-04-17 11:04 970
		if(asc > max)
c53e6443 sago007 2012-04-17 11:04 971
			max = asc;
c53e6443 sago007 2012-04-17 11:04 972
	}
c53e6443 sago007 2012-04-17 11:04 973
	return max;
925b7e58 sago007 2011-04-25 16:35 974
}
925b7e58 sago007 2011-04-25 16:35 975
925b7e58 sago007 2011-04-25 16:35 976
int NFont::getDescent(const char character) const
925b7e58 sago007 2011-04-25 16:35 977
{
c53e6443 sago007 2012-04-17 11:04 978
	unsigned char test = (unsigned char)character;
c53e6443 sago007 2012-04-17 11:04 979
	if(test < 33 || test > 222 || (test > 126 && test < 161))
c53e6443 sago007 2012-04-17 11:04 980
		return 0;
c53e6443 sago007 2012-04-17 11:04 981
	unsigned char num = (unsigned char)character - 33;
c53e6443 sago007 2012-04-17 11:04 982
	// Get the max descent
c53e6443 sago007 2012-04-17 11:04 983
	int x = charPos[num];
c53e6443 sago007 2012-04-17 11:04 984
	int i, j = src->h - 1, result = 0;
c53e6443 sago007 2012-04-17 11:04 985
	Uint32 pixel = getPixel(src, 0, src->h - 1); // bg pixel
c53e6443 sago007 2012-04-17 11:04 986
	while(j > 0 && j > baseline)
c53e6443 sago007 2012-04-17 11:04 987
	{
c53e6443 sago007 2012-04-17 11:04 988
		i = charPos[num];
c53e6443 sago007 2012-04-17 11:04 989
		while(i < x + charWidth[num])
c53e6443 sago007 2012-04-17 11:04 990
		{
c53e6443 sago007 2012-04-17 11:04 991
			if(getPixel(src, i, j) != pixel)
c53e6443 sago007 2012-04-17 11:04 992
			{
c53e6443 sago007 2012-04-17 11:04 993
				result = j - baseline;
c53e6443 sago007 2012-04-17 11:04 994
				j = 0;
c53e6443 sago007 2012-04-17 11:04 995
				break;
c53e6443 sago007 2012-04-17 11:04 996
			}
c53e6443 sago007 2012-04-17 11:04 997
			i++;
c53e6443 sago007 2012-04-17 11:04 998
		}
c53e6443 sago007 2012-04-17 11:04 999
		j--;
c53e6443 sago007 2012-04-17 11:04 1000
	}
c53e6443 sago007 2012-04-17 11:04 1001
	return result;
925b7e58 sago007 2011-04-25 16:35 1002
}
925b7e58 sago007 2011-04-25 16:35 1003
925b7e58 sago007 2011-04-25 16:35 1004
int NFont::getDescent(const char* formatted_text, ...) const
925b7e58 sago007 2011-04-25 16:35 1005
{
c53e6443 sago007 2012-04-17 11:04 1006
	if(formatted_text == NULL)
c53e6443 sago007 2012-04-17 11:04 1007
		return descent;
c53e6443 sago007 2012-04-17 11:04 1008
c53e6443 sago007 2012-04-17 11:04 1009
	va_list lst;
c53e6443 sago007 2012-04-17 11:04 1010
	va_start(lst, formatted_text);
c53e6443 sago007 2012-04-17 11:04 1011
	vsprintf(buffer, formatted_text, lst);
c53e6443 sago007 2012-04-17 11:04 1012
	va_end(lst);
c53e6443 sago007 2012-04-17 11:04 1013
c53e6443 sago007 2012-04-17 11:04 1014
	int max = 0;
c53e6443 sago007 2012-04-17 11:04 1015
	const char* c = buffer;
c53e6443 sago007 2012-04-17 11:04 1016
c53e6443 sago007 2012-04-17 11:04 1017
	for (; *c != '\0'; c++)
c53e6443 sago007 2012-04-17 11:04 1018
	{
c53e6443 sago007 2012-04-17 11:04 1019
		int des = getDescent(*c);
c53e6443 sago007 2012-04-17 11:04 1020
		if(des > max)
c53e6443 sago007 2012-04-17 11:04 1021
			max = des;
c53e6443 sago007 2012-04-17 11:04 1022
	}
c53e6443 sago007 2012-04-17 11:04 1023
	return max;
925b7e58 sago007 2011-04-25 16:35 1024
}
925b7e58 sago007 2011-04-25 16:35 1025
925b7e58 sago007 2011-04-25 16:35 1026
int NFont::getSpacing() const
925b7e58 sago007 2011-04-25 16:35 1027
{
c53e6443 sago007 2012-04-17 11:04 1028
	return letterSpacing;
925b7e58 sago007 2011-04-25 16:35 1029
}
925b7e58 sago007 2011-04-25 16:35 1030
925b7e58 sago007 2011-04-25 16:35 1031
int NFont::getLineSpacing() const
925b7e58 sago007 2011-04-25 16:35 1032
{
c53e6443 sago007 2012-04-17 11:04 1033
	return lineSpacing;
925b7e58 sago007 2011-04-25 16:35 1034
}
925b7e58 sago007 2011-04-25 16:35 1035
925b7e58 sago007 2011-04-25 16:35 1036
int NFont::getBaseline() const
925b7e58 sago007 2011-04-25 16:35 1037
{
c53e6443 sago007 2012-04-17 11:04 1038
	return baseline;
925b7e58 sago007 2011-04-25 16:35 1039
}
925b7e58 sago007 2011-04-25 16:35 1040
925b7e58 sago007 2011-04-25 16:35 1041
int NFont::getMaxWidth() const
925b7e58 sago007 2011-04-25 16:35 1042
{
c53e6443 sago007 2012-04-17 11:04 1043
	return maxWidth;
925b7e58 sago007 2011-04-25 16:35 1044
}
925b7e58 sago007 2011-04-25 16:35 1045
925b7e58 sago007 2011-04-25 16:35 1046
925b7e58 sago007 2011-04-25 16:35 1047
925b7e58 sago007 2011-04-25 16:35 1048
925b7e58 sago007 2011-04-25 16:35 1049
925b7e58 sago007 2011-04-25 16:35 1050
// Setters
925b7e58 sago007 2011-04-25 16:35 1051
void NFont::setSpacing(int LetterSpacing)
925b7e58 sago007 2011-04-25 16:35 1052
{
c53e6443 sago007 2012-04-17 11:04 1053
	letterSpacing = LetterSpacing;
925b7e58 sago007 2011-04-25 16:35 1054
}
925b7e58 sago007 2011-04-25 16:35 1055
925b7e58 sago007 2011-04-25 16:35 1056
void NFont::setLineSpacing(int LineSpacing)
925b7e58 sago007 2011-04-25 16:35 1057
{
c53e6443 sago007 2012-04-17 11:04 1058
	lineSpacing = LineSpacing;
925b7e58 sago007 2011-04-25 16:35 1059
}
925b7e58 sago007 2011-04-25 16:35 1060
925b7e58 sago007 2011-04-25 16:35 1061
void NFont::setDest(SDL_Surface* Dest)
925b7e58 sago007 2011-04-25 16:35 1062
{
c53e6443 sago007 2012-04-17 11:04 1063
	dest = Dest;
925b7e58 sago007 2011-04-25 16:35 1064
}
925b7e58 sago007 2011-04-25 16:35 1065
925b7e58 sago007 2011-04-25 16:35 1066
int NFont::setBaseline(int Baseline)
925b7e58 sago007 2011-04-25 16:35 1067
{
c53e6443 sago007 2012-04-17 11:04 1068
	if(Baseline >= 0)
c53e6443 sago007 2012-04-17 11:04 1069
		baseline = Baseline;
c53e6443 sago007 2012-04-17 11:04 1070
	else
c53e6443 sago007 2012-04-17 11:04 1071
	{
c53e6443 sago007 2012-04-17 11:04 1072
		// Get the baseline by checking a, b, and c and averaging their lowest y-value.
c53e6443 sago007 2012-04-17 11:04 1073
		// Is there a better way?
c53e6443 sago007 2012-04-17 11:04 1074
		Uint32 pixel = getPixel(src, 0, src->h - 1);
c53e6443 sago007 2012-04-17 11:04 1075
		int heightSum = 0;
c53e6443 sago007 2012-04-17 11:04 1076
		int x, i, j;
c53e6443 sago007 2012-04-17 11:04 1077
		for(unsigned char avgChar = 64; avgChar < 67; avgChar++)
c53e6443 sago007 2012-04-17 11:04 1078
		{
c53e6443 sago007 2012-04-17 11:04 1079
			x = charPos[avgChar];
c53e6443 sago007 2012-04-17 11:04 1080
c53e6443 sago007 2012-04-17 11:04 1081
			j = src->h - 1;
c53e6443 sago007 2012-04-17 11:04 1082
			while(j > 0)
c53e6443 sago007 2012-04-17 11:04 1083
			{
c53e6443 sago007 2012-04-17 11:04 1084
				i = x;
c53e6443 sago007 2012-04-17 11:04 1085
				while(i - x < charWidth[64])
c53e6443 sago007 2012-04-17 11:04 1086
				{
c53e6443 sago007 2012-04-17 11:04 1087
					if(getPixel(src, i, j) != pixel)
c53e6443 sago007 2012-04-17 11:04 1088
					{
c53e6443 sago007 2012-04-17 11:04 1089
						heightSum += j;
c53e6443 sago007 2012-04-17 11:04 1090
						j = 0;
c53e6443 sago007 2012-04-17 11:04 1091
						break;
c53e6443 sago007 2012-04-17 11:04 1092
					}
c53e6443 sago007 2012-04-17 11:04 1093
					i++;
c53e6443 sago007 2012-04-17 11:04 1094
				}
c53e6443 sago007 2012-04-17 11:04 1095
				j--;
c53e6443 sago007 2012-04-17 11:04 1096
			}
c53e6443 sago007 2012-04-17 11:04 1097
		}
c53e6443 sago007 2012-04-17 11:04 1098
		baseline = int(heightSum/3.0f + 0.5f);  // Round up and cast
c53e6443 sago007 2012-04-17 11:04 1099
	}
c53e6443 sago007 2012-04-17 11:04 1100
	return baseline;
925b7e58 sago007 2011-04-25 16:35 1101
}
925b7e58 sago007 2011-04-25 16:35 1102
925b7e58 sago007 2011-04-25 16:35 1103
925b7e58 sago007 2011-04-25 16:35 1104
925b7e58 sago007 2011-04-25 16:35 1105
925b7e58 sago007 2011-04-25 16:35 1106
925b7e58 sago007 2011-04-25 16:35 1107
925b7e58 sago007 2011-04-25 16:35 1108
1970-01-01 00:00 1109