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
|
//......SDL init.........
if (TTF_Init () == -1)
{
printf ("Can't initialize TTF_FONT: %s", TTF_GetError ());
}
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
Uint32 rmask = 0xff000000;
Uint32 gmask = 0x00ff0000;
Uint32 bmask = 0x0000ff00;
Uint32 amask = 0x000000ff;
#else
Uint32 rmask = 0x000000ff;
Uint32 gmask = 0x0000ff00;
Uint32 bmask = 0x00ff0000;
Uint32 amask = 0xff000000;
#endif
SDL_Surafe *bitmap;
bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, 160,32, 32,rmask, gmask, bmask, amask);
#ifdef __unix__
#define F_PFAD "/usr/share/fonts/"
#define F_NAME "truetype/freefont"
#elif linux
#define F_PFAD "/usr/share/fonts/"
#define F_NAME "truetype/freefont"
#elif __MSDOS__ || __WIN32__ || _MSC_VER
#define F_PFAD "C:\\WINDOWS\\Fonts\\"
#define F_NAME "ARIAL.TTF"
#endif
std::string tmp = F_PFAD+F_NAME;
TTF_Font* font;
font = TTF_OpenFont(tmp.c_str(), 22);
if(!font)
printf("Font create:cant open font, error: %s\n", TTF_GetError());
void draw_text(Sint16 x,Sint16 y,Uint16 width,Uint16 height,std::string str,Uint8 align = 0){
if ( SDL_LockSurface(bitmap) == -1 ){
printf("draw_text:cant lock bitmap,error: %s\n",SDL_GetError());
return;
}
if (align == 2)
x += width/2; //zentriert (hoffe rs ist richtig ^^)
else if (align == 1)
x += width-str.size(); //rechtsbündig (das auch)
SDL_Surface *message;
int w = width;
int h = height;
SDL_Color color = {0,0,0}
if (TTF_SizeText(font,str.c_str(), &w, &h) == -1)
printf("draw text:cant set width and height for text,error: %s\n", TTF_GetError());
if ( !(message = TTF_RenderText_Solid( font, str.c_str(), color)) )
printf("draw text:cant create text,error: %s\n", TTF_GetError());
SDL_Rect rect = {x,y,width,height};
if (SDL_BlitSurface(message, NULL, bitmap, &rect) == -1)
printf("draw_text: cant draw text on bitmap ,error: %s\n",SDL_GetError());
SDL_UnlockSurface(bitmap);
}
int main{
draw_text(0,0,160,32,"Hello Word");
SDL_Rect rect = {160,160,bimtap->w,bitmap->h};
SDL_BlitSurface(bitmap, NULL, screen, &rect)
if (SDL_Flip(screen) == -1)
printf ("cant update the Screen,error : %s\n",SDL_GetError ());
SDL_Delay(5000);
TTF_Quit();
SDL_Quit();
return 0;
}
|