File: text.c

package info (click to toggle)
freedroidrpg 0.16.1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 260,740 kB
  • sloc: ansic: 63,915; sh: 4,588; makefile: 503; python: 178; xml: 54; sed: 16
file content (865 lines) | stat: -rw-r--r-- 24,689 bytes parent folder | download | duplicates (3)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
/* 
 *
 *   Copyright (c) 2004-2007 Arthur Huillet 
 *   Copyright (c) 1994, 2002, 2003 Johannes Prix
 *   Copyright (c) 1994, 2002 Reinhard Prix
 *
 *
 *  This file is part of Freedroid
 *
 *  Freedroid 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.
 *
 *  Freedroid 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Freedroid; see the file COPYING. If not, write to the 
 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *  MA  02111-1307  USA
 *
 */

/**
 * This file contains all functions dealing with texts on the screen,
 * that have to be blitted somehow, using bitmaps or OpenGL texturers,
 * but at least strongly rely on graphics concepts, not pure internal
 * text-processing.
 */

#define _text_c

#include "system.h"

#include "defs.h"
#include "struct.h"
#include "proto.h"
#include "global.h"

#include "lvledit/lvledit_display.h"

// current text insertion position
static int MyCursorX;
static int MyCursorY;

static int display_char_disabled;

/**
 * This function determines how many lines of text it takes to write a string
 * with the current font into the given rectangle, provided one would start at
 * the left side.
 */
int get_lines_needed(const char *text, SDL_Rect t_rect, float line_height_factor)
{
	t_rect.h = 32767; // arbitrary large number
	display_char_disabled = TRUE;
	int lines = display_text(text, t_rect.x, t_rect.y, &t_rect, line_height_factor);
	display_char_disabled = FALSE;
	return lines;
}

/**
 *
 *
 */
void show_backgrounded_label_at_pixel_position(char *LabelText, int pos_x, int pos_y)
{
	SDL_Rect background_rect;

	background_rect.x = pos_x - 1;
	background_rect.y = pos_y;
	background_rect.w = text_width(get_current_font(), LabelText) + 2;
	background_rect.h = 20;

	draw_rectangle(&background_rect, 0, 0, 0, 255);

	set_current_font(FPS_Display_Font);
	put_string(get_current_font(), pos_x, pos_y, LabelText);

};				// void show_backgrounded_label_at_pixel_position ( char* LabelText , float fill_status , int pos_x , int pos_y )

/**
 *
 *
 */
void show_backgrounded_label_at_map_position(char *LabelText, float fill_status, float pos_x, float pos_y, int zoom_is_on)
{
	int pixel_x, pixel_y;

	translate_map_point_to_screen_pixel(pos_x, pos_y, &pixel_x, &pixel_y);
	show_backgrounded_label_at_pixel_position(LabelText, pixel_x, pixel_y);

};				// void show_backgrounded_label_at_map_position ( char* LabelText , float fill_status , float pos_x , float pos_y )

#define IN_WINDOW_TEXT_OFFSET 15

/**
 * Show text inside a filled rectangle. The specified height is taken as a
 * minimum value, and it will be expanded vertically to fit the given text.
 * @return The height of the rectangle
 */
int show_backgrounded_text_rectangle(const char *text, struct font *font, int x, int y, int w, int h)
{
	struct font *old_font = get_current_font();
	set_current_font(font);

	SDL_Rect t_rect;
	t_rect.x = x;
	t_rect.y = y;
	
	// Find out the number of lines the text will occupy. This is done by
	// using the drawing function with drawing temporarily disabled.
	t_rect.w = w - (IN_WINDOW_TEXT_OFFSET * 2);
	t_rect.h = GameConfig.screen_height;
	int lines = get_lines_needed(text, t_rect, 1.0);

	// Calculate the rectangle height
	int f_height = get_font_height(get_current_font());
	int r_height = (lines * f_height) + (IN_WINDOW_TEXT_OFFSET * 2);

	// Set up and fill the rectangle.
	t_rect.w = w;
	t_rect.h = r_height;
	draw_rectangle(&t_rect, 0, 0, 0, 255);
	
	// Show the text inside our newly drawn rectangle.
	t_rect.w -= IN_WINDOW_TEXT_OFFSET * 2;
	t_rect.h -= IN_WINDOW_TEXT_OFFSET;
	t_rect.x += IN_WINDOW_TEXT_OFFSET;
	t_rect.y += IN_WINDOW_TEXT_OFFSET;
	display_text(text, t_rect.x, t_rect.y, &t_rect, 1.0);

	set_current_font(old_font);
	return r_height;
}

/**
 * 
 */
int CutDownStringToMaximalSize(char *StringToCut, int LengthInPixels)
{
	int StringIndex = 0;
	int i;

	if (text_width(get_current_font(), StringToCut) <= LengthInPixels)
		return FALSE;

	StringIndex = limit_text_width(get_current_font(), StringToCut, LengthInPixels);
	if (StringIndex < 1)
		return FALSE;

	for (i = 0; i < 3; i++) {
		if (StringToCut[StringIndex + i] != 0) {
			StringToCut[StringIndex + i] = '.';
		} else
			return TRUE;
	}
	StringToCut[StringIndex + 3] = 0;

	return TRUE;
};				// void CutDownStringToMaximalSize ( char* StringToCut , int LengthInPixels )

/* -----------------------------------------------------------------
 *
 * This function scrolls a given text down inside the User-window, 
 * defined by the global SDL_Rect User_Rect
 *
 * ----------------------------------------------------------------- */
int ScrollText(char *Text, const char *background_name)
{
	int Number_Of_Line_Feeds = 0;	// number of lines used for the text
	int StartInsertLine, InsertLine;
	int speed = +1;
	int maxspeed = 8;
	int done = 0;
	SDL_Event event;

	Activate_Conservative_Frame_Computation();

	SDL_Rect ScrollRect = { User_Rect.x + 10, User_Rect.y, User_Rect.w - 20, User_Rect.h };
	set_current_font(Para_Font);
	StartInsertLine = ScrollRect.y + ScrollRect.h;
	InsertLine = StartInsertLine;

	while (!done) {
		
		if (background_name)
			blit_background(background_name);
		Number_Of_Line_Feeds = display_text(Text, ScrollRect.x, InsertLine, &ScrollRect, 1.0);
		// We might add some buttons to be displayed here, so that, if you don't have
		// a mouse wheel and don't know about cursor keys, you can still click on these
		// buttons to control the scrolling speed of the text.
		//
		ShowGenericButtonFromList(SCROLL_TEXT_UP_BUTTON);
		ShowGenericButtonFromList(SCROLL_TEXT_DOWN_BUTTON);
		blit_mouse_cursor();
		our_SDL_flip_wrapper();

		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_MOUSEBUTTONDOWN) {
				//mange the event to make it look like an arrow key press
				event.type = SDL_KEYDOWN;
				switch (event.button.button) {
					case SDL_BUTTON_WHEELUP:
						event.key.keysym.sym = SDLK_UP;
						break;
					case SDL_BUTTON_WHEELDOWN:
						event.key.keysym.sym = SDLK_DOWN;
						break;
					case SDL_BUTTON_LEFT:
						if (MouseCursorIsOnButton(SCROLL_TEXT_UP_BUTTON, GetMousePos_x(), GetMousePos_y())) {
							event.key.keysym.sym = SDLK_UP;
						} else if (MouseCursorIsOnButton(SCROLL_TEXT_DOWN_BUTTON, GetMousePos_x(), GetMousePos_y())) {
							event.key.keysym.sym = SDLK_DOWN;
						} else {
							done = 1;
						}
						break;	
					default:
						;
				}
			}

			if (event.type == SDL_KEYDOWN) {
				switch (event.key.keysym.sym) {
					case SDLK_UP:
						speed--;
						if (speed < -maxspeed)
							speed = -maxspeed;
						break;
					case SDLK_DOWN:
						speed++;
						if (speed > maxspeed)
							speed = maxspeed;
						break;
					case SDLK_ESCAPE:
						done = 1;
						break;
					default:
						;
				}
			}

		}

		InsertLine -= speed;

		// impose some limit on the amount to scroll away downwards and upwards
		//
		if (InsertLine > StartInsertLine && (speed < 0)) {
			InsertLine = StartInsertLine;
			speed = 0;
		}
		if (InsertLine + (Number_Of_Line_Feeds + 1) * get_font_height(get_current_font()) < ScrollRect.y
		    && (speed > 0)) {
			InsertLine = ScrollRect.y - (Number_Of_Line_Feeds + 1) * get_font_height(get_current_font());
			speed = 0;
		}

		SDL_Delay(20);
	}

	while (MouseLeftPressed()) ;	// so that we don't touch again immediately.

	return OK;
}

/**
 * This function sets a new text, that will be displayed in huge font 
 * directly over the combat window for a fixed duration of time, where
 * only the time in midst of combat and with no other windows opened
 * is counted.
 */
void SetNewBigScreenMessage(const char *ScreenMessageText)
{
	int i = MAX_BIG_SCREEN_MESSAGES - 1;

	/* Free the last message that's going to be overwritten */
	if (Me.BigScreenMessage[i]) {
		free(Me.BigScreenMessage[i]);
		Me.BigScreenMessage[i] = NULL;
	}

	while (i > 0) {
		Me.BigScreenMessage[i] = Me.BigScreenMessage[i - 1];
		Me.BigScreenMessageDuration[i] = Me.BigScreenMessageDuration[i - 1];
		i--;
	}

	Me.BigScreenMessage[0] = MyMalloc(strlen(ScreenMessageText) + 1);
	strcpy(Me.BigScreenMessage[0], ScreenMessageText);
	Me.BigScreenMessageDuration[0] = 0;

};				// void SetNewBigScreenMessage( char* ScreenMessageText )

/**
 * This function displays the currently defined Bigscreenmessage on the
 * screen.  It will be called by AssembleCombatWindow.
 */
void DisplayBigScreenMessage(void)
{
	int i;
	int y_pos = 30;

	for (i = 0; i < GameConfig.number_of_big_screen_messages; i++) {
		if (!Me.BigScreenMessage[i])
			continue;

		char *text = Me.BigScreenMessage[i];

		if (Me.BigScreenMessageDuration[i] < GameConfig.delay_for_big_screen_messages) {
			SDL_SetClipRect(Screen, NULL);

			set_current_font(Menu_Font);
			int x_pos = GameConfig.screen_width/2 - text_width(get_current_font(), text)/2;
			display_text(text, x_pos, y_pos, NULL /* clip */, 1.0);

			if (!GameConfig.Inventory_Visible && !GameConfig.SkillScreen_Visible && !GameConfig.CharacterScreen_Visible)
				Me.BigScreenMessageDuration[i] += Frame_Time();

			y_pos += get_font_height(Menu_Font);
		}
	}

};				// void DisplayBigScreenMessage( void )

static void display_countdown(int pixel_x, int pixel_y, const char* message, float duration)
{
	char text[100];
	int minutes = ((int) duration) / 60;
	int seconds = ((int) duration) - (minutes * 60);

	if (minutes >= 60) {
		sprintf(text, "%s", message);
	} else if (minutes) {
		sprintf(text, "%s %dm%ds", message, minutes, seconds);
	} else {
		sprintf(text, "%s %.1fs", message, duration);
	}

	pixel_x -= text_width(get_current_font(), text) / 2;

	display_text(text, pixel_x, pixel_y, NULL /* clip */, 1.0);
}

/**
 * This function displays the remaining seconds of effect on Tux above him.
 */
void display_effect_countdowns(void)
{
	int pixel_x, pixel_y;
	translate_map_point_to_screen_pixel(Me.pos.x, Me.pos.y, &pixel_x, &pixel_y);
	pixel_y -= 128; /* tux height... */

	if (Me.slowdown_duration > 0.0) {
		set_current_font(Blue_Font);
		display_countdown(pixel_x, pixel_y, _("slowed"), Me.slowdown_duration);
		pixel_y -= get_font_height(Blue_Font);
	}

	if (Me.paralyze_duration > 0.0) {
		set_current_font(Red_Font);
		display_countdown(pixel_x, pixel_y, _("paralyzed"), Me.paralyze_duration);
		pixel_y -= get_font_height(Red_Font);
	}

	if (Me.invisible_duration > 0.0) {
		set_current_font(FPS_Display_Font);
		display_countdown(pixel_x, pixel_y, _("invisible"), Me.invisible_duration);
		pixel_y -= get_font_height(FPS_Display_Font);
	}

	if (Me.nmap_duration > 0.0) {
		set_current_font(FPS_Display_Font);
		display_countdown(pixel_x, pixel_y, _("scanning"), Me.nmap_duration);
	}
};		// void display_effect_countdowns( void )

static int display_text_with_cursor(const char *text, int startx, int starty, const SDL_Rect *clip, float line_height_factor, int curpos)
{
	char *tmp;		// mobile pointer to the current position in the string to be printed
	SDL_Rect Temp_Clipping_Rect;	// adding this to prevent segfault in case of NULL as parameter
	SDL_Rect store_clip;
	short int nblines = 1;
	int empty_lines_started = 0;
	int current_curpos = 0;

	if (text == NULL)
		return 0;

	int letter_spacing = get_letter_spacing(get_current_font());
	int tab_width = TABWIDTH * (font_char_width(get_current_font(), TABCHAR) + letter_spacing);

	if (text[0]=='\0')
		nblines = 0;

	// We position the internal text cursor on the right spot for
	// the first character to be printed.
	//
	MyCursorX = startx;
	MyCursorY = starty;

	int cursor_x = MyCursorX;
	int cursor_y = MyCursorY;

	// We make a backup of the current clipping rect, so we can restore
	// it later.
	//
	SDL_GetClipRect(Screen, &store_clip);

	// If we did receive some clipping rect in the parameter list (like e.g. it's
	// always the case with dialog output) we enforce this new clipping rect, otherwise
	// we just set the clipping rect to contain the whole screen.
	//
	if (clip != NULL) {
		SDL_SetClipRect(Screen, clip);
	} else {
		clip = &Temp_Clipping_Rect;
		Temp_Clipping_Rect.x = 0;
		Temp_Clipping_Rect.y = 0;
		Temp_Clipping_Rect.w = GameConfig.screen_width;
		Temp_Clipping_Rect.h = GameConfig.screen_height;
	}

	set_gl_clip_rect(clip);

	start_image_batch();

	// Now we can start to print the actual text to the screen.
	//
	// The running text pointer must be initialized.
	//
	tmp = (char *)text;	// this is no longer a 'const' char*, but only a char*
	while (*tmp && (MyCursorY < clip->y + clip->h)) {
		if (current_curpos == curpos) {
			cursor_x = MyCursorX;
			cursor_y = MyCursorY;
		}

		if (handle_switch_font_char(&tmp)) {
			current_curpos++;
			continue;
		}

		if (((*tmp == ' ') || (*tmp == '\t'))
		    && (ImprovedCheckLineBreak(tmp, clip, line_height_factor) == 1))	// don't write over right border 
		{		/*THE CALL ABOVE HAS DONE THE CARRIAGE RETURN FOR US !!! */
			empty_lines_started++;
			tmp++;
			current_curpos++;
			continue;
		}

		// carriage return in the middle of a word if it is too big to fit on one line
		if (isgraph(*tmp) && (MyCursorX + font_char_width(get_current_font(), *tmp) + letter_spacing) > (clip->x + clip->w)) {
			MyCursorX = clip->x;
			MyCursorY += (int)(get_font_height(get_current_font()) * line_height_factor);
			empty_lines_started++;
		}

		switch (*tmp) {
		case '\n':
			MyCursorX = clip->x;
			MyCursorY += (int)(get_font_height(get_current_font()) * line_height_factor);
			empty_lines_started++;
			break;
		case '\t':
			MyCursorX = (int)ceilf((float)MyCursorX / (float)(tab_width)) * (tab_width);
			break;
		default:
			if (MyCursorY > clip->y - (int)(get_font_height(get_current_font()) * line_height_factor)
				&& !display_char_disabled)
				put_char(get_current_font(), MyCursorX, MyCursorY, *tmp);

			MyCursorX += font_char_width(get_current_font(), *tmp)
				+ get_letter_spacing(get_current_font());

			// At least one visible character must follow a line break or else
			// the line is a trailing empty line not visible to the user. Such
			// empty lines don't contribute to the visual height of the string.
			if (isgraph(*tmp)) {
				nblines += empty_lines_started;
				empty_lines_started = 0;
			}
		}
		tmp++;
		current_curpos++;
	}

	end_image_batch();

	SDL_SetClipRect(Screen, &store_clip);	// restore previous clip-rect 

	if (curpos != -1) {
		if (curpos != 0 && current_curpos == curpos) {
			cursor_x = MyCursorX;
			cursor_y = MyCursorY;
		}

		SDL_Rect cursor_rect;
		cursor_rect.x = cursor_x;
		cursor_rect.y = cursor_y;
		cursor_rect.h = get_font_height(get_current_font());
		cursor_rect.w = 8;
		HighlightRectangle(Screen, cursor_rect);
	}
	
	if (use_open_gl)
		unset_gl_clip_rect();

	return nblines;
}

/**
 * Prints text at given positions, automatically word-wrapping at the
 * edges of clip_rect.  If clip_rect is NULL, no clipping is performed.
 *      
 * @return number of lines written (from the first text line up to the
 *         last displayed line)
 */
int display_text(const char *text, int startx, int starty, const SDL_Rect *clip, float line_height_factor)
{
	return display_text_with_cursor(text, startx, starty, clip, line_height_factor, -1);
}

/**
 * This function checks if the next word still fits in this line
 * of text and initiates a carriage return/line feed if not.
 * Very handy and convenient, for that means it is no longer necessary
 * to enter \n in the text every time its time for a newline. cool.
 *  
 * rp: added argument clip, which contains the text-window we're writing in
 *     (formerly known as "TextBorder")
 *
 * ah: added return value : 1 if carriage return was done, FALSE otherwise
 */
int ImprovedCheckLineBreak(char *Resttext, const SDL_Rect * clip, float line_height_factor)
{
	int NeededSpace = 0;

	int letter_spacing = get_letter_spacing(get_current_font());

	if (*Resttext == ' ')
		NeededSpace = font_char_width(get_current_font(), ' ') + letter_spacing;
	else if (*Resttext == '\t')
		NeededSpace = TABWIDTH * (font_char_width(get_current_font(), TABCHAR) + letter_spacing);

	Resttext++;
	while ((*Resttext != ' ') && (*Resttext != '\t') && (*Resttext != '\n') && (*Resttext != 0)) {
		NeededSpace += font_char_width(get_current_font(), *Resttext) + letter_spacing;
		Resttext++;
	}

	if ((MyCursorX + NeededSpace) > (clip->x + clip->w)) {
		MyCursorX = clip->x;
		MyCursorY += (int)(get_font_height(get_current_font()) * line_height_factor);
		return 1;
	}

	return 0;
};				// int ImprovedCheckLineBreak()

/**
 * Prompt the user for a string no longer than MaxLen (excluding terminating \0).
 */
char *get_string(int MaxLen, const char *background_name, const char *text_for_overhead_promt)
{
	char *input;		// pointer to the string entered by the user
	int key;		// last 'character' entered 
	int curpos;		// counts the characters entered so far
	int finished;
	int x0, y0;

	display_text(text_for_overhead_promt, 50, 50, NULL, 1.0);

	// allocate memory for the users input
	input = MyMalloc(MaxLen + 5);

	memset(input, '.', MaxLen);
	input[MaxLen] = 0;

	finished = FALSE;
	curpos = 0;

	while (!finished) {
		blit_background(background_name);
		display_text(text_for_overhead_promt, 50, 50, NULL, 1.0);

		x0 = MyCursorX;
		y0 = MyCursorY;

		put_string(get_current_font(), x0, y0, input);
		our_SDL_flip_wrapper();

		key = getchar_raw(NULL);

		if (key == SDLK_RETURN || key == SDLK_KP_ENTER) {
			// Display the image again so both buffers are in sync
			// useful for GL drivers that do true pageflipping (win32, nvidia 173.x, ...)
			if (use_open_gl) {
				blit_background(background_name);
				display_text(text_for_overhead_promt, 50, 50, NULL, 1.0);
				x0 = MyCursorX;
				y0 = MyCursorY;
				put_string(get_current_font(), x0, y0, input);
				our_SDL_flip_wrapper();
			}
			input[curpos] = 0;
			finished = TRUE;
		} else if ((key < SDLK_DELETE) && isprint(key) && (curpos < MaxLen)) {
			/* printable characters are entered in string */
			input[curpos] = (char)key;
			curpos++;
		} else if ((key <= SDLK_KP9) && (key >= SDLK_KP0) && (curpos < MaxLen)) {
			key -= SDLK_KP0;
			key += '0';

			input[curpos] = (char)key;
			curpos++;
		} else if (key == SDLK_BACKSPACE) {
			if (curpos > 0)
				curpos--;
			input[curpos] = '.';
		} else if (key == SDLK_ESCAPE) {
			free(input);
			while (EscapePressed()) ;
			return (NULL);
		}
	}

	return (input);
}

/* -----------------------------------------------------------------
 * This function reads a string of "MaxLen" from User-input.
 *
 * NOTE: MaxLen is the maximal _strlen_ of the string (excl. \0 !)
 * 
 * ----------------------------------------------------------------- */
char *GetEditableStringInPopupWindow(int MaxLen, const char *PopupWindowTitle, const char *DefaultString)
{
	char *input;		// pointer to the string entered by the user
	int key;		// last 'character' entered 
	int curpos;		// counts the characters entered so far
	int finished;
	int x0, y0;
	SDL_Rect TargetRect;
	int i;

#define EDIT_WINDOW_TEXT_OFFSET 15

	input = MyMalloc(MaxLen + 5);
	strncpy(input, DefaultString, MaxLen - 1);
	input[MaxLen] = 0;
	curpos = strlen(input);

	// Now we prepare a rectangle for our popup window...
	//
	TargetRect.w = 440;
	TargetRect.h = 340;
	TargetRect.x = (640 - TargetRect.w) / 2;
	TargetRect.y = (480 - TargetRect.h) / 2;
	TargetRect.w -= EDIT_WINDOW_TEXT_OFFSET;
	TargetRect.h -= EDIT_WINDOW_TEXT_OFFSET;
	TargetRect.x += EDIT_WINDOW_TEXT_OFFSET;
	TargetRect.y += EDIT_WINDOW_TEXT_OFFSET;

	// Now we find the right position for the new string to start by writing
	// out the title text once, just to get the cursor positioned right...
	//
	display_text(PopupWindowTitle, TargetRect.x, TargetRect.y, &TargetRect, 1.0);
	x0 = MyCursorX;
	y0 = MyCursorY;

	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
	// Now we can start the enter-string cycle...
	//
	finished = FALSE;
	while (!finished) {

		TargetRect.w = 440;
		TargetRect.h = 340;
		TargetRect.x = (640 - TargetRect.w) / 2;
		TargetRect.y = (480 - TargetRect.h) / 2;
		draw_rectangle(&TargetRect, 0, 0, 0, 255);

		TargetRect.w -= EDIT_WINDOW_TEXT_OFFSET;
		TargetRect.h -= EDIT_WINDOW_TEXT_OFFSET;
		TargetRect.x += EDIT_WINDOW_TEXT_OFFSET;
		TargetRect.y += EDIT_WINDOW_TEXT_OFFSET;

		set_current_font(FPS_Display_Font);

		display_text(PopupWindowTitle, TargetRect.x, TargetRect.y, &TargetRect, 1.0);

		if (PopupWindowTitle[strlen(PopupWindowTitle) - 1] != '\n')
			display_text("\n\n", x0, y0, &TargetRect, 1.0);

		TargetRect.x = MyCursorX;
		TargetRect.y = MyCursorY;
		display_text_with_cursor(input, TargetRect.x, TargetRect.y, &TargetRect, 1.0, curpos);

		our_SDL_flip_wrapper();

		key = getchar_raw(NULL);

		if (key == SDLK_RETURN || key == SDLK_KP_ENTER) {
			// input[curpos] = 0;
			finished = TRUE;
		} else if (key == SDLK_ESCAPE) {
			while (EscapePressed())
				SDL_Delay(1);
			free(input);
			return NULL;
		} else if (isprint(key) && (curpos < MaxLen)) {
			// If a printable character has been entered, it is either appended to
			// the end of the current input string or the rest of the string is being
			// moved and the new character inserted at the end.
			//
			if (curpos == ((int)strlen(input))) {
				input[curpos] = (char)key;
				curpos++;
				input[curpos] = 0;
			} else {
				if (((int)strlen(input)) == MaxLen - 1)
					input[MaxLen - 2] = 0;
				for (i = strlen(input); i >= curpos; i--) {
					input[i + 1] = input[i];
				}
				input[curpos] = (char)key;
				curpos++;
			}
		} else if (key == SDLK_LEFT) {
			if (curpos > 0)
				curpos--;
			// input[curpos] = '.';
		} else if (key == SDLK_RIGHT) {
			if (curpos < ((int)strlen(input)))
				curpos++;
			// input[curpos] = '.';
		} else if (key == SDLK_BACKSPACE) {
			if (curpos > 0) {
				i = curpos;
				while (input[i - 1] != 0) {
					input[i - 1] = input[i];
					i++;
				}
				curpos--;
			}
		} else if (key == SDLK_DELETE) {
			if (curpos > 0) {
				i = curpos;
				while (input[i] != 0) {
					input[i] = input[i + 1];
					i++;
				}
			}

		} else if ((key <= SDLK_KP9) && (key >= SDLK_KP0) && (curpos < MaxLen)) {
			key -= SDLK_KP0;
			key += '0';

			input[curpos] = (char)key;
			curpos++;
		}

	}			// while ( ! finished ) 

	SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);

	return input;
}

/* -----------------------------------------------------------------
 * behaves similarly as gl_printf() of svgalib, using the BFont
 * print function PrintString().
 *  
 *  sets current position of MyCursor[XY],  
 *     if last char is '\n': to same x, next line y
 *     to end of string otherwise
 *
 * Added functionality to PrintString() is: 
 *  o) passing -1 as coord uses previous x and next-line y for printing
 *  o) Screen is updated immediately after print, using SDL_flip()                       
 *
 * ----------------------------------------------------------------- */
void printf_SDL(SDL_Surface * screen, int x, int y, const char *fmt, ...)
{
	va_list args;
	int i;

	char *tmp;
	va_start(args, fmt);

	if (x == -1)
		x = MyCursorX;
	else
		MyCursorX = x;

	if (y == -1)
		y = MyCursorY;
	else
		MyCursorY = y;

	tmp = (char *)MyMalloc(10000 + 1);
	vsprintf(tmp, fmt, args);
	put_string(get_current_font(), x, y, tmp);

	// our_SDL_flip_wrapper (screen);

	if (tmp[strlen(tmp) - 1] == '\n') {
		MyCursorX = x;
		MyCursorY = y + 1.1 * (get_font_height(get_current_font()));
	} else {
		for (i = 0; i < ((int)strlen(tmp)); i++)
			MyCursorX += font_char_width(get_current_font(), tmp[i]);
		MyCursorY = y;
	}

	free(tmp);
	va_end(args);

};				// void printf_SDL (SDL_Surface *screen, int x, int y, char *fmt, ...)

/**
 * Find the longest line in text, taking letter-spacing into account. Return the
 * minimum line width needed to print that line, using the current font.
 */
int longest_line_width(char *text)
{
	int width = 0;
	char *line_start = text;

	for (; TRUE; text++) {
		// Find the end of the current line.
		if (*text != '\n' && *text != '\0') {
			continue;
		}

		// Get the width of the current line.
		char tmp = *text;
		*text = '\0';
		int line_width = text_width(get_current_font(), line_start);
		*text = tmp;

		// Update the width of the longest line.
		if (line_width > width) {
			width = line_width;
		}

		// Handle the next line, if any remain.
		if (*text != '\0') {
			line_start = text + 1;
		} else {
			break;
		}
	}

	return width;
}

#undef _text_c