File: wl_text.cpp

package info (click to toggle)
wolf4sdl 1.7+svn262+dfsg1-5
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye, buster, sid
  • size: 4,112 kB
  • sloc: cpp: 34,696; ansic: 1,257; objc: 246; makefile: 159; sh: 44
file content (854 lines) | stat: -rw-r--r-- 16,847 bytes parent folder | download | duplicates (4)
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
// WL_TEXT.C

#include "wl_def.h"

/*
=============================================================================

TEXT FORMATTING COMMANDS
------------------------
^C<hex digit>           Change text color
^E[enter]               End of layout (all pages)
^G<y>,<x>,<pic>[enter]  Draw a graphic and push margins
^P[enter]               start new page, must be the first chars in a layout
^L<x>,<y>[ENTER]        Locate to a specific spot, x in pixels, y in lines

=============================================================================
*/

/*
=============================================================================

                                                 LOCAL CONSTANTS

=============================================================================
*/

#ifndef SPEAR

#define BACKCOLOR       0x11


#define WORDLIMIT       80
#define FONTHEIGHT      10
#define TOPMARGIN       16
#define BOTTOMMARGIN    32
#define LEFTMARGIN      16
#define RIGHTMARGIN     16
#define PICMARGIN       8
#define TEXTROWS        ((200-TOPMARGIN-BOTTOMMARGIN)/FONTHEIGHT)
#define SPACEWIDTH      7
#define SCREENPIXWIDTH  320
#define SCREENMID       (SCREENPIXWIDTH/2)

/*
=============================================================================

                                LOCAL VARIABLES

=============================================================================
*/

static int pagenum;
static int numpages;

static unsigned leftmargin[TEXTROWS];
static unsigned rightmargin[TEXTROWS];
static char*    text;
static unsigned rowon;

static int     picx;
static int     picy;
static int     picnum;
static int     picdelay;
static boolean layoutdone;

//===========================================================================

#ifndef JAPAN
/*
=====================
=
= RipToEOL
=
=====================
*/

void RipToEOL (void)
{
    while (*text++ != '\n')         // scan to end of line
        ;
}


/*
=====================
=
= ParseNumber
=
=====================
*/

int ParseNumber (void)
{
    char  ch;
    char  num[80];
    char *numptr;

    //
    // scan until a number is found
    //
    ch = *text;
    while (ch < '0' || ch >'9')
        ch = *++text;

    //
    // copy the number out
    //
    numptr = num;
    do
    {
        *numptr++ = ch;
        ch = *++text;
    } while (ch >= '0' && ch <= '9');
    *numptr = 0;

    return atoi (num);
}



/*
=====================
=
= ParsePicCommand
=
= Call with text pointing just after a ^P
= Upon exit text points to the start of next line
=
=====================
*/

void ParsePicCommand (void)
{
    picy=ParseNumber();
    picx=ParseNumber();
    picnum=ParseNumber();
    RipToEOL ();
}


void ParseTimedCommand (void)
{
    picy=ParseNumber();
    picx=ParseNumber();
    picnum=ParseNumber();
    picdelay=ParseNumber();
    RipToEOL ();
}


/*
=====================
=
= TimedPicCommand
=
= Call with text pointing just after a ^P
= Upon exit text points to the start of next line
=
=====================
*/

void TimedPicCommand (void)
{
    ParseTimedCommand ();

    //
    // update the screen, and wait for time delay
    //
    VW_UpdateScreen ();

    //
    // wait for time
    //
    Delay(picdelay);

    //
    // draw pic
    //
    VWB_DrawPic (picx&~7,picy,picnum);
}


/*
=====================
=
= HandleCommand
=
=====================
*/

void HandleCommand (void)
{
    int     i,margin,top,bottom;
    int     picwidth,picheight,picmid;

    switch (toupper(*++text))
    {
        case 'B':
            picy=ParseNumber();
            picx=ParseNumber();
            picwidth=ParseNumber();
            picheight=ParseNumber();
            VWB_Bar(picx,picy,picwidth,picheight,BACKCOLOR);
            RipToEOL();
            break;
        case ';':               // comment
            RipToEOL();
            break;
        case 'P':               // ^P is start of next page, ^E is end of file
        case 'E':
            layoutdone = true;
            text--;             // back up to the '^'
            break;

        case 'C':               // ^c<hex digit> changes text color
            i = toupper(*++text);
            if (i>='0' && i<='9')
                fontcolor = i-'0';
            else if (i>='A' && i<='F')
                fontcolor = i-'A'+10;

            fontcolor *= 16;
            i = toupper(*++text);
            if (i>='0' && i<='9')
                fontcolor += i-'0';
            else if (i>='A' && i<='F')
                fontcolor += i-'A'+10;
            text++;
            break;

        case '>':
            px = 160;
            text++;
            break;

        case 'L':
            py=ParseNumber();
            rowon = (py-TOPMARGIN)/FONTHEIGHT;
            py = TOPMARGIN+rowon*FONTHEIGHT;
            px=ParseNumber();
            while (*text++ != '\n')         // scan to end of line
                ;
            break;

        case 'T':               // ^Tyyy,xxx,ppp,ttt waits ttt tics, then draws pic
            TimedPicCommand ();
            break;

        case 'G':               // ^Gyyy,xxx,ppp draws graphic
            ParsePicCommand ();
            VWB_DrawPic (picx&~7,picy,picnum);
            picwidth = pictable[picnum-STARTPICS].width;
            picheight = pictable[picnum-STARTPICS].height;
            //
            // adjust margins
            //
            picmid = picx + picwidth/2;
            if (picmid > SCREENMID)
                margin = picx-PICMARGIN;                        // new right margin
            else
                margin = picx+picwidth+PICMARGIN;       // new left margin

            top = (picy-TOPMARGIN)/FONTHEIGHT;
            if (top<0)
                top = 0;
            bottom = (picy+picheight-TOPMARGIN)/FONTHEIGHT;
            if (bottom>=TEXTROWS)
                bottom = TEXTROWS-1;

            for (i=top;i<=bottom;i++)
                if (picmid > SCREENMID)
                    rightmargin[i] = margin;
                else
                    leftmargin[i] = margin;

            //
            // adjust this line if needed
            //
            if (px < (int) leftmargin[rowon])
                px = leftmargin[rowon];
            break;
    }
}


/*
=====================
=
= NewLine
=
=====================
*/

void NewLine (void)
{
    char    ch;

    if (++rowon == TEXTROWS)
    {
        //
        // overflowed the page, so skip until next page break
        //
        layoutdone = true;
        do
        {
            if (*text == '^')
            {
                ch = toupper(*(text+1));
                if (ch == 'E' || ch == 'P')
                {
                    layoutdone = true;
                    return;
                }
            }
            text++;
        } while (1);
    }
    px = leftmargin[rowon];
    py+= FONTHEIGHT;
}



/*
=====================
=
= HandleCtrls
=
=====================
*/

void HandleCtrls (void)
{
    char    ch;

    ch = *text++;                   // get the character and advance

    if (ch == '\n')
    {
        NewLine ();
        return;
    }
}


/*
=====================
=
= HandleWord
=
=====================
*/

void HandleWord (void)
{
    char    wword[WORDLIMIT];
    int     wordindex;
    word    wwidth,wheight,newpos;


    //
    // copy the next word into [word]
    //
    wword[0] = *text++;
    wordindex = 1;
    while (*text>32)
    {
        wword[wordindex] = *text++;
        if (++wordindex == WORDLIMIT)
            Quit ("PageLayout: Word limit exceeded");
    }
    wword[wordindex] = 0;            // stick a null at end for C

    //
    // see if it fits on this line
    //
    VW_MeasurePropString (wword,&wwidth,&wheight);

    while (px+wwidth > (int) rightmargin[rowon])
    {
        NewLine ();
        if (layoutdone)
            return;         // overflowed page
    }

    //
    // print it
    //
    newpos = px+wwidth;
    VWB_DrawPropString (wword);
    px = newpos;

    //
    // suck up any extra spaces
    //
    while (*text == ' ')
    {
        px += SPACEWIDTH;
        text++;
    }
}

/*
=====================
=
= PageLayout
=
= Clears the screen, draws the pics on the page, and word wraps the text.
= Returns a pointer to the terminating command
=
=====================
*/

void PageLayout (boolean shownumber)
{
    int     i,oldfontcolor;
    char    ch;

    oldfontcolor = fontcolor;

    fontcolor = 0;

    //
    // clear the screen
    //
    VWB_Bar (0,0,320,200,BACKCOLOR);
    VWB_DrawPic (0,0,H_TOPWINDOWPIC);
    VWB_DrawPic (0,8,H_LEFTWINDOWPIC);
    VWB_DrawPic (312,8,H_RIGHTWINDOWPIC);
    VWB_DrawPic (8,176,H_BOTTOMINFOPIC);


    for (i=0; i<TEXTROWS; i++)
    {
        leftmargin[i] = LEFTMARGIN;
        rightmargin[i] = SCREENPIXWIDTH-RIGHTMARGIN;
    }

    px = LEFTMARGIN;
    py = TOPMARGIN;
    rowon = 0;
    layoutdone = false;

    //
    // make sure we are starting layout text (^P first command)
    //
    while (*text <= 32)
        text++;

    if (*text != '^' || toupper(*++text) != 'P')
        Quit ("PageLayout: Text not headed with ^P");

    while (*text++ != '\n')
        ;


    //
    // process text stream
    //
    do
    {
        ch = *text;

        if (ch == '^')
            HandleCommand ();
        else
            if (ch == 9)
            {
                px = (px+8)&0xf8;
                text++;
            }
            else if (ch <= 32)
                HandleCtrls ();
            else
                HandleWord ();

    } while (!layoutdone);

    pagenum++;

    if (shownumber)
    {
#ifdef SPANISH
        sprintf(str, "Hoja %d de %d", pagenum, numpages);
        px = 208;
#else
        sprintf(str, "pg %d of %d", pagenum, numpages);
        px = 213;
#endif
        py = 183;
        fontcolor = 0x4f;                          //12^BACKCOLOR;

        VWB_DrawPropString (str);
    }

    fontcolor = oldfontcolor;
}

//===========================================================================

/*
=====================
=
= BackPage
=
= Scans for a previous ^P
=
=====================
*/

void BackPage (void)
{
    pagenum--;
    do
    {
        text--;
        if (*text == '^' && toupper(*(text+1)) == 'P')
            return;
    } while (1);
}


//===========================================================================


/*
=====================
=
= CacheLayoutGraphics
=
= Scans an entire layout file (until a ^E) marking all graphics used, and
= counting pages, then caches the graphics in
=
=====================
*/
void CacheLayoutGraphics (void)
{
    char    *bombpoint, *textstart;
    char    ch;

    textstart = text;
    bombpoint = text+30000;
    numpages = pagenum = 0;

    do
    {
        if (*text == '^')
        {
            ch = toupper(*++text);
            if (ch == 'P')          // start of a page
                numpages++;
            if (ch == 'E')          // end of file, so load graphics and return
            {
#ifndef SPEAR
                CA_CacheGrChunk(H_TOPWINDOWPIC);
                CA_CacheGrChunk(H_LEFTWINDOWPIC);
                CA_CacheGrChunk(H_RIGHTWINDOWPIC);
                CA_CacheGrChunk(H_BOTTOMINFOPIC);
#endif
                //                              CA_CacheMarks ();
                text = textstart;
                return;
            }
            if (ch == 'G')          // draw graphic command, so mark graphics
            {
                ParsePicCommand ();
                CA_CacheGrChunk (picnum);
            }
            if (ch == 'T')          // timed draw graphic command, so mark graphics
            {
                ParseTimedCommand ();
                CA_CacheGrChunk (picnum);
            }
        }
        else
            text++;

    } while (text<bombpoint);

    Quit ("CacheLayoutGraphics: No ^E to terminate file!");
}
#endif


/*
=====================
=
= ShowArticle
=
=====================
*/

#ifdef JAPAN
void ShowArticle (int which)
#else
void ShowArticle (char *article)
#endif
{
#ifdef JAPAN
    int snames[10] = {
        H_HELP1PIC,
        H_HELP2PIC,
        H_HELP3PIC,
        H_HELP4PIC,
        H_HELP5PIC,
        H_HELP6PIC,
        H_HELP7PIC,
        H_HELP8PIC,
        H_HELP9PIC,
        H_HELP10PIC};
    int enames[14] = {
        0,0,
#ifndef JAPDEMO
        C_ENDGAME1APIC,
        C_ENDGAME1BPIC,
        C_ENDGAME2APIC,
        C_ENDGAME2BPIC,
        C_ENDGAME3APIC,
        C_ENDGAME3BPIC,
        C_ENDGAME4APIC,
        C_ENDGAME4BPIC,
        C_ENDGAME5APIC,
        C_ENDGAME5BPIC,
        C_ENDGAME6APIC,
        C_ENDGAME6BPIC
#endif
    };
#endif
    unsigned    oldfontnumber;
    boolean     newpage,firstpage;
    ControlInfo ci;

#ifdef JAPAN
    pagenum = 1;
    if (!which)
        numpages = 10;
    else
        numpages = 2;
#else
    text = article;
    oldfontnumber = fontnumber;
    fontnumber = 0;
    CA_CacheGrChunk(STARTFONT);
    VWB_Bar (0,0,320,200,BACKCOLOR);
    CacheLayoutGraphics ();
#endif

    newpage = true;
    firstpage = true;

    do
    {
        if (newpage)
        {
            newpage = false;
#ifdef JAPAN
            if (!which)
                CA_CacheScreen(snames[pagenum - 1]);
            else
                CA_CacheScreen(enames[which*2 + pagenum - 1]);
#else
            PageLayout (true);
#endif
            VW_UpdateScreen ();
            if (firstpage)
            {
                VL_FadeIn(0,255,gamepal,10);
                firstpage = false;
            }
        }
        SDL_Delay(5);

        LastScan = 0;
        ReadAnyControl(&ci);
        Direction dir = ci.dir;
        switch(dir)
        {
            case dir_North:
            case dir_South:
                break;

            default:
                if(ci.button0) dir = dir_South;
                switch(LastScan)
                {
                    case sc_UpArrow:
                    case sc_PgUp:
                    case sc_LeftArrow:
                        dir = dir_North;
                        break;

                    case sc_Enter:
                    case sc_DownArrow:
                    case sc_PgDn:
                    case sc_RightArrow:
                        dir = dir_South;
                        break;
                }
                break;
        }

        switch(dir)
        {
            case dir_North:
            case dir_West:
                if (pagenum>1)
                {
#ifndef JAPAN
                    BackPage ();
                    BackPage ();
#else
                  pagenum--;
#endif
                    newpage = true;
                }
                TicDelay(20);
                break;

            case dir_South:
            case dir_East:
                if (pagenum<numpages)
                {
                    newpage = true;
#ifdef JAPAN
                    pagenum++;
#endif
                }
                TicDelay(20);
                break;
        }
    } while (LastScan != sc_Escape && !ci.button1);

    IN_ClearKeysDown ();
    fontnumber = oldfontnumber;
}


//===========================================================================

#ifndef JAPAN
#ifdef ARTSEXTERN
int     endextern = T_ENDART1;
#ifndef SPEAR
int     helpextern = T_HELPART;
#endif
#endif
char helpfilename[13] = "HELPART.",
    endfilename[13] = "ENDART1.";
#endif

/*
=================
=
= HelpScreens
=
=================
*/
#ifndef SPEAR
void HelpScreens (void)
{
    int     artnum;
    char    *text;
#ifndef ARTSEXTERN
    memptr  layout;
#endif


#ifdef JAPAN
    ShowArticle (0);
    VW_FadeOut();
    FreeMusic ();
#else

#ifdef ARTSEXTERN
    artnum = helpextern;
    CA_CacheGrChunk (artnum);
    text = (char *)grsegs[artnum];
#else
    CA_LoadFile (helpfilename,&layout);
    text = (char *)layout;
#endif

    ShowArticle (text);

#ifdef ARTSEXTERN
    UNCACHEGRCHUNK(artnum);
#else
    free(layout);
#endif

    VW_FadeOut();

    FreeMusic ();
#endif
}
#endif

//
// END ARTICLES
//
void EndText (void)
{
    int     artnum;
    char    *text;
#ifndef ARTSEXTERN
    memptr  layout;
#endif

    ClearMemory ();

#ifdef JAPAN
    ShowArticle(gamestate.episode + 1);

    VW_FadeOut();

    SETFONTCOLOR(0,15);
    IN_ClearKeysDown();
    if (MousePresent && IN_IsInputGrabbed())
        IN_CenterMouse();  // Clear accumulated mouse movement

    FreeMusic ();
#else



#ifdef ARTSEXTERN
    artnum = endextern+gamestate.episode;
    CA_CacheGrChunk (artnum);
    text = (char *)grsegs[artnum];
#else
    endfilename[6] = '1'+gamestate.episode;
    CA_LoadFile (endfilename,&layout);
    text = (char *)layout;
#endif

    ShowArticle (text);

#ifdef ARTSEXTERN
    UNCACHEGRCHUNK(artnum);
#else
    free(layout);
#endif


    VW_FadeOut();
    SETFONTCOLOR(0,15);
    IN_ClearKeysDown();
    if (MousePresent && IN_IsInputGrabbed())
        IN_CenterMouse();  // Clear accumulated mouse movement

    FreeMusic ();
#endif
}
#endif