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;
c53e6443 sago007 2012-04-17 11:04 76
		ac/*
c53e6443 sago007 2012-04-17 11:04 77
Block Attack - Rise of the Blocks, SDL game, besed on Nintendo's Tetris Attack
c53e6443 sago007 2012-04-17 11:04 78
Copyright (C) 2008 Poul Sander
c53e6443 sago007 2012-04-17 11:04 79
c53e6443 sago007 2012-04-17 11:04 80
    This program is free software; you can redistribute it and/or modify
c53e6443 sago007 2012-04-17 11:04 81
    it under the terms of the GNU General Public License as published by
c53e6443 sago007 2012-04-17 11:04 82
    the Free Software Foundation; either version 2 of the License, or
c53e6443 sago007 2012-04-17 11:04 83
    (at your option) any later version.
c53e6443 sago007 2012-04-17 11:04 84
c53e6443 sago007 2012-04-17 11:04 85
    This program is distributed in the hope that it will be useful,
c53e6443 sago007 2012-04-17 11:04 86
    but WITHOUT ANY WARRANTY; without even the implied warranty of
c53e6443 sago007 2012-04-17 11:04 87
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c53e6443 sago007 2012-04-17 11:04 88
    GNU General Public License for more details.
c53e6443 sago007 2012-04-17 11:04 89
c53e6443 sago007 2012-04-17 11:04 90
    You should have received a copy of the GNU General Public License
c53e6443 sago007 2012-04-17 11:04 91
    along with this program; if not, write to the Free Software
c53e6443 sago007 2012-04-17 11:04 92
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
c53e6443 sago007 2012-04-17 11:04 93
c53e6443 sago007 2012-04-17 11:04 94
    Poul Sander
c53e6443 sago007 2012-04-17 11:04 95
    R�vehjvej 36, V. 1111
c53e6443 sago007 2012-04-17 11:04 96
    2800 Kgs. Lyngby
c53e6443 sago007 2012-04-17 11:04 97
    DENMARK
c53e6443 sago007 2012-04-17 11:04 98
    blockattack@poulsander.com
c53e6443 sago007 2012-04-17 11:04 99
    http://blockattack.sf.net
c53e6443 sago007 2012-04-17 11:04 100
*/tualInstance = false;
c53e6443 sago007 2012-04-17 11:04 101
	}
c53e6443 sago007 2012-04-17 11:04 102
	else
c53e6443 sago007 2012-04-17 11:04 103
	{
c53e6443 sago007 2012-04-17 11:04 104
		font = t.font;
c53e6443 sago007 2012-04-17 11:04 105
		actualInstance = true;
c53e6443 sago007 2012-04-17 11:04 106
		count++;
c53e6443 sago007 2012-04-17 11:04 107
	}
89c4a3a6 sago007 2008-08-29 14:32 108
}
89c4a3a6 sago007 2008-08-29 14:32 109
89c4a3a6 sago007 2008-08-29 14:32 110
int TTFont::getTextHeight()
89c4a3a6 sago007 2008-08-29 14:32 111
{
c53e6443 sago007 2012-04-17 11:04 112
	return TTF_FontHeight(font);
89c4a3a6 sago007 2008-08-29 14:32 113
}
89c4a3a6 sago007 2008-08-29 14:32 114
89c4a3a6 sago007 2008-08-29 14:32 115
int TTFont::getTextWidth(string text)
89c4a3a6 sago007 2008-08-29 14:32 116
{
c53e6443 sago007 2012-04-17 11:04 117
	int width = 0;
c53e6443 sago007 2012-04-17 11:04 118
	if(TTF_SizeText(font,text.c_str(),&width,NULL)!=0)
c53e6443 sago007 2012-04-17 11:04 119
		cout << "Failed to get text width!" << endl;
c53e6443 sago007 2012-04-17 11:04 120
	return width;
89c4a3a6 sago007 2008-08-29 14:32 121
}
89c4a3a6 sago007 2008-08-29 14:32 122
89c4a3a6 sago007 2008-08-29 14:32 123
void TTFont::writeText(string text, SDL_Surface *target, int x, int y)
89c4a3a6 sago007 2008-08-29 14:32 124
{
c53e6443 sago007 2012-04-17 11:04 125
	SDL_Surface *text_surface;
c53e6443 sago007 2012-04-17 11:04 126
	SDL_Color color= {255,255,255};
c53e6443 sago007 2012-04-17 11:04 127
	if(!(text_surface=TTF_RenderText_Solid(font,text.c_str(), color)))
c53e6443 sago007 2012-04-17 11:04 128
	{
c53e6443 sago007 2012-04-17 11:04 129
		cout << "Error writing text: " << TTF_GetError() << endl;
c53e6443 sago007 2012-04-17 11:04 130
	}
c53e6443 sago007 2012-04-17 11:04 131
	else
c53e6443 sago007 2012-04-17 11:04 132
	{
c53e6443 sago007 2012-04-17 11:04 133
		SDL_Rect dest;
c53e6443 sago007 2012-04-17 11:04 134
		dest.x = x;
c53e6443 sago007 2012-04-17 11:04 135
		dest.y = y;
c53e6443 sago007 2012-04-17 11:04 136
		SDL_BlitSurface(text_surface,NULL,target,&dest);
c53e6443 sago007 2012-04-17 11:04 137
		SDL_FreeSurface(text_surface);
c53e6443 sago007 2012-04-17 11:04 138
	}
89c4a3a6 sago007 2008-08-29 14:32 139
}
89c4a3a6 sago007 2008-08-29 14:32 140
1970-01-01 00:00 141