1 /* file created on Wed Aug 28 15:38:28 CEST 2002 by doom */
2 
3 #include "scrollingtext.h"
4 #include <string.h>
5 #include <stdlib.h>
6 
7 struct _SCROLLING_TEXT
8 {
9 	char   *text;
10 	SoFont *font;
11 
12 	int     pos;
13 	int     text_width;
14 };
15 
16 static int scrolling_text_width (ScrollingText * st);
17 
18 ScrollingText *
scrolling_text_new(const char * text,SoFont * font)19 scrolling_text_new (const char *text, SoFont * font)
20 {
21 	ScrollingText *st = (ScrollingText*)malloc (sizeof (ScrollingText));
22 
23 	st->text = NULL;
24 	scrolling_text_set_font (st, font);
25 
26 	scrolling_text_set_text (st, text);
27 
28 	st->text_width = scrolling_text_width (st);
29 	st->pos = 640;
30 	return st;
31 }
32 
33 void
scrolling_text_set_font(ScrollingText * st,SoFont * font)34 scrolling_text_set_font (ScrollingText * st, SoFont * font)
35 {
36 	st->font = font;
37 	st->text_width = scrolling_text_width (st);
38 }
39 
40 void
scrolling_text_set_text(ScrollingText * st,const char * text)41 scrolling_text_set_text (ScrollingText * st, const char *text)
42 {
43 	if (st->text)
44 		free (st->text);
45 	if (text) {
46 		st->text = (char*)malloc (strlen (text) + 1);
47 		strcpy (st->text, text);
48 	}
49 	else
50 		st->text = NULL;
51 	st->text_width = scrolling_text_width (st);
52 }
53 
54 void
scrolling_text_update(ScrollingText * st,SDL_Surface * surf)55 scrolling_text_update (ScrollingText * st, SDL_Surface * surf)
56 {
57 	if ((st->text == NULL) || (st->font == NULL))
58 		return;
59 
60 	st->pos -= 3;
61 	if (st->pos < -st->text_width)
62 		st->pos = surf->w;
63 }
64 
65 void
scrolling_text_draw(ScrollingText * st,SDL_Surface * surf,int y)66 scrolling_text_draw (ScrollingText * st, SDL_Surface * surf, int y)
67 {
68 	if ((st->text == NULL) || (st->font == NULL))
69 		return;
70 
71 	SoFont_PutString (st->font, surf, st->pos, y, st->text, NULL);
72 }
73 
74 static int
scrolling_text_width(ScrollingText * st)75 scrolling_text_width (ScrollingText * st)
76 {
77 	if ((st->text == NULL) || (st->font == NULL))
78 		return 0;
79 
80 	return SoFont_TextWidth (st->font, st->text) + 400;
81 }
82