-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrollingtext.c
82 lines (66 loc) · 1.63 KB
/
scrollingtext.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
/* file created on Wed Aug 28 15:38:28 CEST 2002 by doom */
#include <stdbool.h>
#include "scrollingtext.h"
#include <string.h>
#include <stdlib.h>
struct _SCROLLING_TEXT
{
char *text;
SoFont *font;
int pos;
int text_width;
};
static int scrolling_text_width (ScrollingText * st);
ScrollingText *
scrolling_text_new (const char *text, SoFont * font)
{
ScrollingText *st = (ScrollingText*)malloc (sizeof (ScrollingText));
st->text = NULL;
scrolling_text_set_font (st, font);
scrolling_text_set_text (st, text);
st->text_width = scrolling_text_width (st);
st->pos = 640;
return st;
}
void
scrolling_text_set_font (ScrollingText * st, SoFont * font)
{
st->font = font;
st->text_width = scrolling_text_width (st);
}
void
scrolling_text_set_text (ScrollingText * st, const char *text)
{
if (st->text)
free (st->text);
if (text) {
st->text = (char*)malloc (strlen (text) + 1);
strcpy (st->text, text);
}
else
st->text = NULL;
st->text_width = scrolling_text_width (st);
}
void
scrolling_text_update (ScrollingText * st, SDL_Surface * surf)
{
if ((st->text == NULL) || (st->font == NULL))
return;
st->pos -= 3;
if (st->pos < -st->text_width)
st->pos = surf->w;
}
void
scrolling_text_draw (ScrollingText * st, SDL_Surface * surf, int y)
{
if ((st->text == NULL) || (st->font == NULL))
return;
SoFont_PutString (st->font, surf, st->pos, y, st->text, NULL);
}
static int
scrolling_text_width (ScrollingText * st)
{
if ((st->text == NULL) || (st->font == NULL))
return 0;
return SoFont_TextWidth (st->font, st->text) + 400;
}