-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
144 lines (121 loc) · 4.14 KB
/
Copy pathtest.c
File metadata and controls
144 lines (121 loc) · 4.14 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "yatref.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdlib.h>
#define CHECK(f) \
if ((f) != 0) { \
printf("ERROR in %s: %s", __STRING(f), SDL_GetError()); \
SDL_Quit(); \
exit(EXIT_FAILURE); \
}
yatref_Font *f[3];
SDL_Texture *userchar;
void init(SDL_Renderer *rend) {
SDL_Surface *surf = SDL_LoadBMP("char.bmp");
CHECK(surf == NULL);
userchar = SDL_CreateTextureFromSurface(rend, surf);
CHECK(userchar == NULL);
CHECK(TTF_Init());
TTF_Font *ttf_font = TTF_OpenFont("test.ttf", 16);
if (ttf_font == NULL) {
printf("ERROR in TTF_OpenFont: %s", SDL_GetError());
SDL_Quit();
exit(EXIT_FAILURE);
}
TTF_Font *ttf_font2 = TTF_OpenFont("test.ttf", 30);
if (ttf_font2 == NULL) {
printf("ERROR in TTF_OpenFont: %s", SDL_GetError());
SDL_Quit();
exit(EXIT_FAILURE);
}
TTF_Font *ttf_font3 = TTF_OpenFont("test.ttf", 10);
if (ttf_font3 == NULL) {
printf("ERROR in TTF_OpenFont: %s", SDL_GetError());
SDL_Quit();
exit(EXIT_FAILURE);
}
char def[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
",.-;:_!\"$%&/()=?+*#'ÄÖÜßäöü\\";
SDL_Color col;
col.r = 0;
col.g = 0;
col.b = 0;
col.a = 0;
f[0] = yatref_CreateFont(ttf_font, rend, def, sizeof(def), &col);
if (f[0] == NULL) {
printf("ERROR in yatref_CreateFont: %s", SDL_GetError());
SDL_Quit();
exit(EXIT_FAILURE);
}
col.r = 255;
f[1] = yatref_CreateFont(ttf_font2, rend, def, sizeof(def), &col);
if (f[1] == NULL) {
printf("ERROR in yatref_CreateFont: %s", SDL_GetError());
SDL_Quit();
exit(EXIT_FAILURE);
}
col.r = 0;
col.g = 255;
f[2] = yatref_CreateFont(ttf_font3, rend, def, sizeof(def), &col);
if (f[2] == NULL) {
printf("ERROR in yatref_CreateFont: %s", SDL_GetError());
SDL_Quit();
exit(EXIT_FAILURE);
}
TTF_CloseFont(ttf_font);
TTF_CloseFont(ttf_font2);
TTF_CloseFont(ttf_font3);
TTF_Quit();
}
void render(SDL_Renderer *rend) {
SDL_Rect a;
a.w = 50;
a.h = 30;
const char *text = "Hello, world.\nHello, world.\nHällö Unicode \x11u\x01\n"
"That was a control character.";
CHECK(yatref_Print(rend, text, f, 3, NULL, NULL, 0, &a, 1, NULL, 10, 20, 790,
SDL_FALSE, NULL, NULL));
CHECK(SDL_SetRenderDrawColor(rend, 255, 0, 0, 0));
CHECK(SDL_RenderDrawRect(rend, &a));
const char *text2 =
"This is a test. Lorem ipsum, dolor sit amet. Lorem ipsum, dolor sit "
"amet. Here is a very long word: "
"Donaudampfschifffahrtskapitänsmützenhaltershgehilfentextili"
"enfabrikantenjunge.\nHere are some more (2-byte) Unicode characters: "
"äöüÄÖÜß";
CHECK(yatref_Print(rend, text2, f, 3, NULL, NULL, 0, NULL, 0, NULL, 200, 200,
200, SDL_TRUE, NULL, NULL));
yatref_TextureSize size;
CHECK(SDL_QueryTexture(userchar, NULL, NULL, size, size + 1));
const char *text3 =
"This renderer can be used to draw"
"\x11n\x02 big and red \x11n\x01 as well as"
"\x11n\x03 small and green\x11n\x01 text.\n\nIn addition to that, it "
"can draw \x11i\x01 fancy arrows and other user generated characters.";
CHECK(yatref_Print(rend, text3, f, 3, &userchar, &size, 1, NULL, 0, NULL, 200,
10, 200, SDL_TRUE, NULL, NULL));
}
void destroy() {
yatref_DestroyFont(f[0]);
yatref_DestroyFont(f[1]);
yatref_DestroyFont(f[2]);
}
int main() {
CHECK(SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO));
SDL_Window *wind;
SDL_Renderer *rend;
CHECK(SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_SHOWN, &wind, &rend));
SDL_SetWindowTitle(wind, "yatref");
init(rend);
while (SDL_GetTicks() < 10000) {
CHECK(SDL_SetRenderDrawColor(rend, 255, 255, 255, 255));
CHECK(SDL_RenderClear(rend));
render(rend);
SDL_RenderPresent(rend);
}
destroy();
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(wind);
SDL_Quit();
return 0;
}