/
sdl2_test.c
95 lines (77 loc) · 2.72 KB
/
sdl2_test.c
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
89
90
91
92
93
94
95
#include <stdlib.h>
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
int main(int argc, const char *argv[])
{
if(argc != 2) {
printf("%s takes .ttf file as argument.\n", argv[0]);
return 1;
}
SDL_version compile_version;
const SDL_version *link_version=TTF_Linked_Version();
SDL_TTF_VERSION(&compile_version);
printf("compiled with SDL_ttf version: %d.%d.%d\n",
compile_version.major,
compile_version.minor,
compile_version.patch);
printf("running with SDL_ttf version: %d.%d.%d\n",
link_version->major,
link_version->minor,
link_version->patch);
if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("Error with import!\n");
return 1;
}
SDL_Window *win = SDL_CreateWindow( "TTF Test",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800,
600,
SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(win,
-1,
SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);
if(TTF_Init() == -1) {
printf("Error loading TTF_Init()!\n%s\n", TTF_GetError());
SDL_Quit();
exit(2);
}
TTF_Font *font;
/* MS Himalaya (himalaya.ttf): http://fontzone.net/font-details/microsoft-himalaya */
font = TTF_OpenFont(argv[1], 600);
if(!font) {
printf("%s\n", TTF_GetError());
return 1;
}
const char tibstring[] = { 0xe0, 0xbd, 0x96,
0xe0, 0xbd, 0xa6,
0xe0, 0xbe, 0x90,
0xe0, 0xbe, 0xb1,
0xe0, 0xbd, 0xbc,
0xe0, 0xbd, 0x84,
0xe0, 0xbd, 0xa6 };
SDL_Color colour = { 255, 255, 255, 255 };
SDL_Surface *surface = TTF_RenderUTF8_Solid(font, tibstring, colour);
if(surface == NULL) {
TTF_CloseFont(font);
printf("Surface error!\n");
return 0;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Event event;
int quit = 0;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
quit = 1;
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
TTF_CloseFont(font);
SDL_Quit();
return 0;
}