File: txtscroll.cc

package info (click to toggle)
exult 0.98rc1-2
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,924 kB
  • ctags: 8,928
  • sloc: cpp: 83,768; sh: 7,643; ansic: 4,328; makefile: 890; yacc: 618; lex: 255; xml: 19
file content (238 lines) | stat: -rw-r--r-- 5,579 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 *  Copyright (C) 2000-2001  The Exult Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#ifndef ALPHA_LINUX_CXX
#  include <cstring>
#  include <cstdlib>
#endif
#include "exult_types.h"
#include "files/U7file.h"
#include "gamewin.h"
#include "txtscroll.h"
#include "font.h"
#include "game.h"

#include "SDL_timer.h"
#include "SDL_events.h"
#include "SDL_keysym.h"

using std::atoi;
using std::size_t;
using std::strchr;
using std::string;
using std::strlen;
using std::strncmp;
using std::vector;

TextScroller::TextScroller(const char *archive, int index, Font *fnt, Shape *shp)
{
	font = fnt;
	shapes = shp;
	U7object txtobj(archive, index);
	size_t len;
	const char	CR = '\r', LF = '\n';
		
	char *txt, *ptr, *end;
	txt = txtobj.retrieve(len);
	ptr = txt;
	end = ptr+len;

	text = new vector<string>();
	while(ptr<end) {
		char *start = ptr;
		ptr = strchr(ptr, LF);
		if(ptr) {
			if(*(ptr-1)==CR) // It's CR/LF
				*(ptr-1) = 0;
			else
				*ptr = 0;
			text->push_back(string(start));
			ptr += 1;
		} else
			break;
	}
	delete [] txt;
}

TextScroller::~TextScroller()
{
	delete text;
}

int TextScroller::show_line(Game_window *gwin, int left, int right, int y, int index)
{
	//The texts used in the main menu contains backslashed sequences that
	//indicates the output format of the lines:
	// \Px   include picture number x (frame nr. of shape passed to constructor)
	// \C    center line
	// \L    left aligned to right center line
	// \R	 right aligned to left center line
	// |	 carriage return (stay on same line)
	// #xxx	 display character with number xxx
	string str = (*text)[index];
	const char * ptr = str.c_str();
	char *txt = new char[strlen(ptr)+1];
	
	char *txtptr = txt;
	int ypos = y;
	int vspace = 2; // 2 extra pixels between lines
	// Align text to the left by default
	int align = -1;
	int xpos = left;
	int center = (right+left)/2;
	bool add_line = true;
	
	while(*ptr) {
		if(!strncmp(ptr,"\\P",2)) {
			int pix = *(ptr+2)-'0';
			ptr +=3;
			Shape_frame *frame = shapes->get_frame(pix);
			if (frame) {
			        gwin->paint_shape(center-frame->get_width()/2,
						  ypos, frame);
				ypos += frame->get_height()+vspace;
			}
		} else if(!strncmp(ptr,"\\C",2)) {
			ptr += 2;
			align = 0;
		} else if(!strncmp(ptr,"\\L",2)) {
			ptr += 2;
			align = 1;
		} else if(!strncmp(ptr,"\\R",2)) {
			ptr += 2;
			align = -1;
		} else if(*ptr=='|' || *(ptr+1)==0) {
			if(*(ptr+1)==0 && *ptr!='|')
			{
				*txtptr++ = *ptr;
				add_line = false;
			}
			*txtptr = 0;
			
			if(align<0)
				xpos = center-font->get_text_width(txt);
			else if(align==0)
				xpos = center-font->get_text_width(txt)/2;
			else
				xpos = center;
			font->draw_text(gwin->get_win()->get_ib8(),
							xpos,ypos,txt);
			if(*ptr!='|') ypos += font->get_text_height()+vspace;
			txtptr = txt;	// Go to beginning of string
			++ptr;
		} else if(*ptr=='#') {
			ptr++;
			if(*ptr=='#') {	// Double hash
				*txtptr++ = *ptr++;
				continue;
			}
			char numerical[4] = {0,0,0,0};
			char *num = numerical;
			while (*ptr >= '0' && *ptr <= '9')
				*num++ = *ptr++;
			*txtptr++ = atoi(numerical);
		} else
			*txtptr++ = *ptr++;
	}
	
	delete [] txt;
	if(add_line)
		ypos += font->get_text_height();
	return ypos;
}


bool TextScroller::run(Game_window *gwin, Palette& pal)
{
	gwin->clear_screen();
	gwin->show(1);

	int topx = (gwin->get_width()-320)/2;
	int topy = (gwin->get_height()-200)/2;
	int endy = topy+200;
	uint32 starty = endy;
	uint32 startline = 0;
	unsigned int maxlines = text->size();
	bool looping = true;
	bool complete = false;
	SDL_Event event;
	uint32 next_time = SDL_GetTicks() + 200;
	uint32 incr = 120;
	pal.apply();

	while(looping) {
		int ypos = starty;
		uint32 curline = startline;
		gwin->clear_screen();
		do {
			if(curline==maxlines)
				break;
			ypos = show_line(gwin, topx, topx+320, ypos, curline++);
			if(ypos<topy) {		// If this line doesn't appear, don't show it next time
				++startline;
				starty = ypos;
				if(startline>=maxlines) {
					looping = false;
					complete = true;
					break;
				}
			}
		} while (ypos<endy);
//		pal.apply();
		gwin->show(1);
		do {
		  // this could be a problem when too many events are produced
		  while (SDL_PollEvent(&event)) {
			switch(event.type) {
			case SDL_KEYDOWN:
				if(event.key.keysym.sym==SDLK_RSHIFT || event.key.keysym.sym==SDLK_LSHIFT)
					incr = 0;
				else
					looping = false;
				break;
			
			case SDL_KEYUP:
				incr = 120;
				next_time = SDL_GetTicks();
				break;
			case SDL_MOUSEBUTTONUP:
				looping = false;
				break;
			default:
				break;
			}
		  }
		} while (next_time > SDL_GetTicks());
		next_time = SDL_GetTicks() + incr;
		if(!looping)
			pal.fade_out(c_fade_out_time);
		starty--;
	}
	gwin->clear_screen();
	gwin->show(1);
	return complete;
}

int TextScroller::get_count()
{
	return text->size();
}