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
ecef5838 sago007 2015-11-24 20:01 26
TTFont::TTFont() {
acd79baf sago007 2015-11-22 19:37 27
	font = nullptr;
c53e6443 sago007 2012-04-17 11:04 28
	actualInstance = false;
89c4a3a6 sago007 2008-08-29 14:32 29
}
89c4a3a6 sago007 2008-08-29 14:32 30
89c4a3a6 sago007 2008-08-29 14:32 31
int TTFont::count = 0;
89c4a3a6 sago007 2008-08-29 14:32 32
ecef5838 sago007 2015-11-24 20:01 33
TTFont::TTFont(TTF_Font *f) {
ecef5838 sago007 2015-11-24 20:01 34
	if (!(TTF_WasInit())) {
c53e6443 sago007 2012-04-17 11:04 35
		//Init TTF for the first time
c53e6443 sago007 2012-04-17 11:04 36
		TTF_Init();
c53e6443 sago007 2012-04-17 11:04 37
	}
c53e6443 sago007 2012-04-17 11:04 38
c53e6443 sago007 2012-04-17 11:04 39
ecef5838 sago007 2015-11-24 20:01 40
	if (f == nullptr) {
c53e6443 sago007 2012-04-17 11:04 41
		cout << "Font was null!" << endl;
ecef5838 sago007 2015-11-24 20:01 42
	}
c53e6443 sago007 2012-04-17 11:04 43
c53e6443 sago007 2012-04-17 11:04 44
	actualInstance = false; //We have not yet copied to final location
c53e6443 sago007 2012-04-17 11:04 45
	font = f;
c53e6443 sago007 2012-04-17 11:04 46
89c4a3a6 sago007 2008-08-29 14:32 47
}
89c4a3a6 sago007 2008-08-29 14:32 48
ecef5838 sago007 2015-11-24 20:01 49
TTFont::~TTFont() {
ecef5838 sago007 2015-11-24 20:01 50
	if (!actualInstance) {
c53e6443 sago007 2012-04-17 11:04 51
		return;
ecef5838 sago007 2015-11-24 20:01 52
	}
c53e6443 sago007 2012-04-17 11:04 53
c53e6443 sago007 2012-04-17 11:04 54
	cout << "Closing a font" << endl;
c53e6443 sago007 2012-04-17 11:04 55
c53e6443 sago007 2012-04-17 11:04 56
	TTF_CloseFont(font);
acd79baf sago007 2015-11-22 19:37 57
	font = nullptr;
c53e6443 sago007 2012-04-17 11:04 58
c53e6443 sago007 2012-04-17 11:04 59
	count--;
ecef5838 sago007 2015-11-24 20:01 60
	if (count==0) {
c53e6443 sago007 2012-04-17 11:04 61
		TTF_Quit();
ecef5838 sago007 2015-11-24 20:01 62
	}
89c4a3a6 sago007 2008-08-29 14:32 63
}
89c4a3a6 sago007 2008-08-29 14:32 64
89c4a3a6 sago007 2008-08-29 14:32 65
//Copy constructor, you cannot copy an actual instance
ecef5838 sago007 2015-11-24 20:01 66
TTFont::TTFont(const TTFont &t) {
ecef5838 sago007 2015-11-24 20:01 67
	if (t.font == nullptr || t.actualInstance) {
acd79baf sago007 2015-11-22 19:37 68
		font = nullptr;
8f632c05 sago007 2015-08-22 16:21 69
		actualInstance = false;
c53e6443 sago007 2012-04-17 11:04 70
	}
ecef5838 sago007 2015-11-24 20:01 71
	else {
c53e6443 sago007 2012-04-17 11:04 72
		font = t.font;
c53e6443 sago007 2012-04-17 11:04 73
		actualInstance = true;
c53e6443 sago007 2012-04-17 11:04 74
		count++;
c53e6443 sago007 2012-04-17 11:04 75
	}
89c4a3a6 sago007 2008-08-29 14:32 76
}
89c4a3a6 sago007 2008-08-29 14:32 77
ecef5838 sago007 2015-11-24 20:01 78
int TTFont::getTextHeight() {
c53e6443 sago007 2012-04-17 11:04 79
	return TTF_FontHeight(font);
89c4a3a6 sago007 2008-08-29 14:32 80
}
89c4a3a6 sago007 2008-08-29 14:32 81
ecef5838 sago007 2015-11-24 20:01 82
int TTFont::getTextWidth(string text) {
c53e6443 sago007 2012-04-17 11:04 83
	int width = 0;
ecef5838 sago007 2015-11-24 20:01 84
	if (TTF_SizeText(font,text.c_str(),&width,nullptr)!=0) {
c53e6443 sago007 2012-04-17 11:04 85
		cout << "Failed to get text width!" << endl;
ecef5838 sago007 2015-11-24 20:01 86
	}
c53e6443 sago007 2012-04-17 11:04 87
	return width;
89c4a3a6 sago007 2008-08-29 14:32 88
}
89c4a3a6 sago007 2008-08-29 14:32 89
ecef5838 sago007 2015-11-24 20:01 90
void TTFont::writeText(string text, SDL_Surface *target, int x, int y) {
c53e6443 sago007 2012-04-17 11:04 91
	SDL_Surface *text_surface;
c53e6443 sago007 2012-04-17 11:04 92
	SDL_Color color= {255,255,255};
ecef5838 sago007 2015-11-24 20:01 93
	if (!(text_surface=TTF_RenderText_Solid(font,text.c_str(), color))) {
c53e6443 sago007 2012-04-17 11:04 94
		cout << "Error writing text: " << TTF_GetError() << endl;
c53e6443 sago007 2012-04-17 11:04 95
	}
ecef5838 sago007 2015-11-24 20:01 96
	else {
c53e6443 sago007 2012-04-17 11:04 97
		SDL_Rect dest;
c53e6443 sago007 2012-04-17 11:04 98
		dest.x = x;
c53e6443 sago007 2012-04-17 11:04 99
		dest.y = y;
acd79baf sago007 2015-11-22 19:37 100
		SDL_BlitSurface(text_surface,nullptr,target,&dest);
c53e6443 sago007 2012-04-17 11:04 101
		SDL_FreeSurface(text_surface);
c53e6443 sago007 2012-04-17 11:04 102
	}
89c4a3a6 sago007 2008-08-29 14:32 103
}
89c4a3a6 sago007 2008-08-29 14:32 104
1970-01-01 00:00 105