File: fonts.cpp

package info (click to toggle)
qonk 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,076 kB
  • ctags: 2,026
  • sloc: cpp: 14,978; sh: 3,464; makefile: 200
file content (88 lines) | stat: -rw-r--r-- 2,360 bytes parent folder | download | duplicates (3)
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
// Copyright 2005 by Anthony Liekens anthony@liekens.net

#include "fonts.h"

#include "canvas.h"

using namespace std;

Font::Font( char* fileName, int size ) {
  std::string s = PKG_DATA_DIR;
  s += fileName;

  if (font = TTF_OpenFont (s.c_str(), size)) {
    TTF_SetFontStyle( font, TTF_STYLE_NORMAL );
    return;
  }

  s = "data";
#ifdef WINDOWS
  s += "\\";
#else
  s += "/";
#endif
  s += fileName;

  if (font = TTF_OpenFont (s.c_str(), size)) {
    TTF_SetFontStyle( font, TTF_STYLE_NORMAL );
    return;
  }
  
  cerr << "Could not load " << size << "pt font " << fileName << " from the following directories: " << endl
       << PKG_DATA_DIR << endl
       << "data" << endl;
  exit( 1 );
}

void
Font::render( SDL_Surface* screen, int x, int y, const char* text, Uint8 r, Uint8 g, Uint8 b, Uint8 a ) {
  SDL_Color color = { r, g, b, a };
  SDL_Surface* textSurface = TTF_RenderText_Blended( font, text, color );
	if ( textSurface != NULL ) {
		SDL_Rect dstrect;
		dstrect.x = x;
		dstrect.y = y;
		dstrect.w = textSurface->w;
		dstrect.h = textSurface->h;
		Canvas::alphaBlend( textSurface, a );
		SDL_BlitSurface( textSurface, NULL, screen, &dstrect );
		SDL_FreeSurface( textSurface );
	}
}

void
Font::renderCenterJustified( SDL_Surface* screen, int x, int y, const char* text, Uint8 r, Uint8 g, Uint8 b, Uint8 a ) {
  SDL_Color color = { r, g, b, 0 };
  SDL_Surface* textSurface = TTF_RenderText_Blended( font, text, color );
	if ( textSurface != NULL ) {
		SDL_Rect dstrect;
		dstrect.x = x - textSurface->w / 2;
		dstrect.y = y - textSurface->h / 2;
		dstrect.w = textSurface->w;
		dstrect.h = textSurface->h;
		Canvas::alphaBlend( textSurface, a );
		SDL_BlitSurface( textSurface, NULL, screen, &dstrect );
		SDL_FreeSurface( textSurface );
	}
}

void
Font::renderRightJustified( SDL_Surface* screen, int x, int y, const char* text, Uint8 r, Uint8 g, Uint8 b, Uint8 a ) {
  SDL_Color color = { r, g, b, 0 };
  SDL_Surface* textSurface = TTF_RenderText_Blended( font, text, color );
	if ( textSurface != NULL ) {
		SDL_Rect dstrect;
		dstrect.x = x - textSurface->w;
		dstrect.y = y;
		dstrect.w = textSurface->w;
		dstrect.h = textSurface->h;
		Canvas::alphaBlend( textSurface, a );
		SDL_BlitSurface( textSurface, NULL, screen, &dstrect );
		SDL_FreeSurface( textSurface );
	}
}

int
Font::getHeight() {
	return TTF_FontHeight( font );
}