-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrolling_text.c
150 lines (124 loc) · 3.64 KB
/
scrolling_text.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
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
145
146
147
148
149
150
#include "scrolling_text.h"
static scrolling_text *first_text = NULL;
static scrolling_text *last_text = NULL;
static TTF_Font *scrolling_font[s_text_frames];
static void scrolling_text_exit(void)
{
int counter;
for(counter = 0; counter < s_text_frames; counter++)
TTF_CloseFont(scrolling_font[counter]);
free_all_scrolling_texts();
}
int init_scrolling_text()
{
int counter = 0;
while(counter < s_text_frames)
{
if(!(scrolling_font[counter] = TTF_OpenFont("gfx/LiberationSans-Regular.ttf", counter + 15)))
return 0;
counter++;
}
atexit(scrolling_text_exit);
return 1;
}
scrolling_text *new_scrolling_text(const double x, const double y, const char *text,
const Uint8 r, const Uint8 g, const Uint8 b)
{
/* allocate memory and set new text to last text */
if(!first_text)
{
first_text = (scrolling_text *)malloc(sizeof(scrolling_text));
if(!first_text)
return NULL;
first_text->next = NULL;
first_text->prev = NULL;
last_text = first_text;
}
else
{
last_text->next = (scrolling_text *)malloc(sizeof(scrolling_text));
if(!last_text->next)
return NULL;
last_text->next->prev = last_text;
last_text->next->next = NULL;
last_text = last_text->next;
}
/* set text properties */
strncpy(last_text->text, text, TEXT_BUFFER);
last_text->color.r = r;
last_text->color.g = g;
last_text->color.b = b;
last_text->x = x;
last_text->y = y;
last_text->start_time = SDL_GetTicks();
return last_text;
}
scrolling_text *new_scrolling_int(const double x, const double y, const int value,
const Uint8 r, const Uint8 g, const Uint8 b)
{
char tmp_string[TEXT_BUFFER] = {0};
sprintf(tmp_string, "%i", value);
return new_scrolling_text(x, y, tmp_string, r, g, b);
}
void free_scrolling_text(scrolling_text *scr_text)
{
if(!scr_text)
return;
if(scr_text == first_text)
{
first_text = scr_text->next;
if(scr_text->next)
scr_text->next->prev = NULL;
else
last_text = NULL;
}
else if(scr_text == last_text)
{
scr_text->prev->next = NULL;
last_text = scr_text->prev;
}
else
{
scr_text->next->prev = scr_text->prev;
scr_text->prev->next = scr_text->next;
}
free(scr_text);
}
void blit_all_scrolling_texts(SDL_Surface *dst)
{
SDL_Rect tmp_dst;
SDL_Surface *cache_surface = NULL;
scrolling_text *tmp_text = first_text;
scrolling_text *cache_text = NULL;
while(tmp_text)
{
/* remove timed out text */
if(SDL_GetTicks() - tmp_text->start_time > s_text_duration)
{
cache_text = tmp_text;
tmp_text = tmp_text->next;
free_scrolling_text(cache_text);
continue;
}
/* render damage text */
cache_surface = TTF_RenderText_Blended(scrolling_font[(int)((SDL_GetTicks() - tmp_text->start_time)/(s_text_duration/s_text_frames + 1))],
tmp_text->text, tmp_text->color);
/* blit current damage_list element */
tmp_dst.x = tmp_text->x - cache_surface->w/2;
tmp_dst.y = tmp_text->y - cache_surface->h/2 - (int)((SDL_GetTicks() - tmp_text->start_time)/(s_text_duration/s_text_frames + 1)) * 1.5;
SDL_BlitSurface(cache_surface, NULL, dst, &tmp_dst);
SDL_FreeSurface(cache_surface);
tmp_text = tmp_text->next;
}
}
void extend_all_scrolling_texts_timestamp(const Uint32 time)
{
scrolling_text *tmp_text = NULL;
for(tmp_text = first_text; tmp_text; tmp_text = tmp_text->next)
tmp_text->start_time += time;
}
void free_all_scrolling_texts(void)
{
while(first_text)
free_scrolling_text(first_text);
}