git repos / blockattack-game

blame: source/code/ttfont.cpp

normal view · raw

89c4a3a6 sago007 2008-08-29 14:32 1
/*
c53e6443 sago007 2012-04-17 11:04 2
===========================================================================
c53e6443 sago007 2012-04-17 11:04 3
blockattack - Block Attack - Rise of the Blocks
c53e6443 sago007 2012-04-17 11:04 4
Copyright (C) 2005-2012 Poul Sander
c53e6443 sago007 2012-04-17 11:04 5
c53e6443 sago007 2012-04-17 11:04 6
This program is free software: you can redistribute it and/or modify
c53e6443 sago007 2012-04-17 11:04 7
it under the terms of the GNU General Public License as published by
c53e6443 sago007 2012-04-17 11:04 8
the Free Software Foundation, either version 2 of the License, or
c53e6443 sago007 2012-04-17 11:04 9
(at your option) any later version.
c53e6443 sago007 2012-04-17 11:04 10
c53e6443 sago007 2012-04-17 11:04 11
This program is distributed in the hope that it will be useful,
c53e6443 sago007 2012-04-17 11:04 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
c53e6443 sago007 2012-04-17 11:04 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c53e6443 sago007 2012-04-17 11:04 14
GNU General Public License for more details.
c53e6443 sago007 2012-04-17 11:04 15
c53e6443 sago007 2012-04-17 11:04 16
You should have received a copy of the GNU General Public License
c53e6443 sago007 2012-04-17 11:04 17
along with this program.  If not, see http://www.gnu.org/licenses/
c53e6443 sago007 2012-04-17 11:04 18
c53e6443 sago007 2012-04-17 11:04 19
Source information and contacts persons can be found at
c53e6443 sago007 2012-04-17 11:04 20
http://blockattack.sf.net
c53e6443 sago007 2012-04-17 11:04 21
===========================================================================
89c4a3a6 sago007 2008-08-29 14:32 22
*/
89c4a3a6 sago007 2008-08-29 14:32 23
89c4a3a6 sago007 2008-08-29 14:32 24
#include "ttfont.h"
89c4a3a6 sago007 2008-08-29 14:32 25
78d03b38 sago007 2008-11-13 19:56 26
//extern SDL_Surface *tmp;
78d03b38 sago007 2008-11-13 19:56 27
78d03b38 sago007 2008-11-13 19:56 28
//#define CONVERTA(n) tmp = SDL_DisplayFormatAlpha(n); SDL_FreeSurface(n); n = tmp
78d03b38 sago007 2008-11-13 19:56 29
89c4a3a6 sago007 2008-08-29 14:32 30
TTFont::TTFont()
89c4a3a6 sago007 2008-08-29 14:32 31
{
c53e6443 sago007 2012-04-17 11:04 32
	font = NULL;
c53e6443 sago007 2012-04-17 11:04 33
	actualInstance = false;
89c4a3a6 sago007 2008-08-29 14:32 34
}
89c4a3a6 sago007 2008-08-29 14:32 35
89c4a3a6 sago007 2008-08-29 14:32 36
int TTFont::count = 0;
89c4a3a6 sago007 2008-08-29 14:32 37
89c4a3a6 sago007 2008-08-29 14:32 38
TTFont::TTFont(TTF_Font *f)
89c4a3a6 sago007 2008-08-29 14:32 39
{
c53e6443 sago007 2012-04-17 11:04 40
	if(!(TTF_WasInit()))
c53e6443 sago007 2012-04-17 11:04 41
	{
c53e6443 sago007 2012-04-17 11:04 42
		//Init TTF for the first time
c53e6443 sago007 2012-04-17 11:04 43
		TTF_Init();
c53e6443 sago007 2012-04-17 11:04 44
	}
c53e6443 sago007 2012-04-17 11:04 45
c53e6443 sago007 2012-04-17 11:04 46
c53e6443 sago007 2012-04-17 11:04 47
	if(f == NULL)
c53e6443 sago007 2012-04-17 11:04 48
		cout << "Font was null!" << endl;
c53e6443 sago007 2012-04-17 11:04 49
c53e6443 sago007 2012-04-17 11:04 50
	actualInstance = false; //We have not yet copied to final location
c53e6443 sago007 2012-04-17 11:04 51
	font = f;
c53e6443 sago007 2012-04-17 11:04 52
89c4a3a6 sago007 2008-08-29 14:32 53
}
89c4a3a6 sago007 2008-08-29 14:32 54
89c4a3a6 sago007 2008-08-29 14:32 55
TTFont::~TTFont()
89c4a3a6 sago007 2008-08-29 14:32 56
{
c53e6443 sago007 2012-04-17 11:04 57
	if(!actualInstance)
c53e6443 sago007 2012-04-17 11:04 58
		return;
c53e6443 sago007 2012-04-17 11:04 59
c53e6443 sago007 2012-04-17 11:04 60
	cout << "Closing a font" << endl;
c53e6443 sago007 2012-04-17 11:04 61
c53e6443 sago007 2012-04-17 11:04 62
	TTF_CloseFont(font);
c53e6443 sago007 2012-04-17 11:04 63
	font = NULL;
c53e6443 sago007 2012-04-17 11:04 64
c53e6443 sago007 2012-04-17 11:04 65
	count--;
c53e6443 sago007 2012-04-17 11:04 66
	if(count==0)
c53e6443 sago007 2012-04-17 11:04 67
		TTF_Quit();
89c4a3a6 sago007 2008-08-29 14:32 68
}
89c4a3a6 sago007 2008-08-29 14:32 69
89c4a3a6 sago007 2008-08-29 14:32 70
//Copy constructor, you cannot copy an actual instance
89c4a3a6 sago007 2008-08-29 14:32 71
TTFont::TTFont(const TTFont &t)
89c4a3a6 sago007 2008-08-29 14:32 72
{
c53e6443 sago007 2012-04-17 11:04 73
	if(t.font == NULL || t.actualInstance)
c53e6443 sago007 2012-04-17 11:04 74
	{
c53e6443 sago007 2012-04-17 11:04 75
		font = NULL;
8f632c05 sago007 2015-08-22 16:21 76
		actualInstance = false;
c53e6443 sago007 2012-04-17 11:04 77
	}
c53e6443 sago007 2012-04-17 11:04 78
	else
c53e6443 sago007 2012-04-17 11:04 79
	{
c53e6443 sago007 2012-04-17 11:04 80
		font = t.font;
c53e6443 sago007 2012-04-17 11:04 81
		actualInstance = true;
c53e6443 sago007 2012-04-17 11:04 82
		count++;
c53e6443 sago007 2012-04-17 11:04 83
	}
89c4a3a6 sago007 2008-08-29 14:32 84
}
89c4a3a6 sago007 2008-08-29 14:32 85
89c4a3a6 sago007 2008-08-29 14:32 86
int TTFont::getTextHeight()
89c4a3a6 sago007 2008-08-29 14:32 87
{
c53e6443 sago007 2012-04-17 11:04 88
	return TTF_FontHeight(font);
89c4a3a6 sago007 2008-08-29 14:32 89
}
89c4a3a6 sago007 2008-08-29 14:32 90
89c4a3a6 sago007 2008-08-29 14:32 91
int TTFont::getTextWidth(string text)
89c4a3a6 sago007 2008-08-29 14:32 92
{
c53e6443 sago007 2012-04-17 11:04 93
	int width = 0;
c53e6443 sago007 2012-04-17 11:04 94
	if(TTF_SizeText(font,text.c_str(),&width,NULL)!=0)
c53e6443 sago007 2012-04-17 11:04 95
		cout << "Failed to get text width!" << endl;
c53e6443 sago007 2012-04-17 11:04 96
	return width;
89c4a3a6 sago007 2008-08-29 14:32 97
}
89c4a3a6 sago007 2008-08-29 14:32 98
89c4a3a6 sago007 2008-08-29 14:32 99
void TTFont::writeText(string text, SDL_Surface *target, int x, int y)
89c4a3a6 sago007 2008-08-29 14:32 100
{
c53e6443 sago007 2012-04-17 11:04 101
	SDL_Surface *text_surface;
c53e6443 sago007 2012-04-17 11:04 102
	SDL_Color color= {255,255,255};
c53e6443 sago007 2012-04-17 11:04 103
	if(!(text_surface=TTF_RenderText_Solid(font,text.c_str(), color)))
c53e6443 sago007 2012-04-17 11:04 104
	{
c53e6443 sago007 2012-04-17 11:04 105
		cout << "Error writing text: " << TTF_GetError() << endl;
c53e6443 sago007 2012-04-17 11:04 106
	}
c53e6443 sago007 2012-04-17 11:04 107
	else
c53e6443 sago007 2012-04-17 11:04 108
	{
c53e6443 sago007 2012-04-17 11:04 109
		SDL_Rect dest;
c53e6443 sago007 2012-04-17 11:04 110
		dest.x = x;
c53e6443 sago007 2012-04-17 11:04 111
		dest.y = y;
c53e6443 sago007 2012-04-17 11:04 112
		SDL_BlitSurface(text_surface,NULL,target,&dest);
c53e6443 sago007 2012-04-17 11:04 113
		SDL_FreeSurface(text_surface);
c53e6443 sago007 2012-04-17 11:04 114
	}
89c4a3a6 sago007 2008-08-29 14:32 115
}
89c4a3a6 sago007 2008-08-29 14:32 116
1970-01-01 00:00 117