File: title.c

package info (click to toggle)
xgalaga 2.1.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,708 kB
  • ctags: 1,371
  • sloc: ansic: 16,119; sh: 1,548; perl: 816; makefile: 177
file content (302 lines) | stat: -rw-r--r-- 8,195 bytes parent folder | download | duplicates (2)
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
/* $Id: title.c,v 1.4 1998/05/11 06:52:59 mrogre Exp $ */
/* Copyright (c) 1998 Joe Rumsey (mrogre@mediaone.net) */
#include "copyright.h"
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#include "math.h"
#include "Wlib.h"
#include "defs.h"
#include "images.h"
#include "struct.h"
#include "data.h"
#include "proto.h"
#include <unistd.h>

/* for scrolling credits */
const int LINESIZE= 70;
const int VISIBLELINES=7;
const int SPEEDFACTOR=5;
char *gchLBuf;
int giCreditLines=0, giActCreditLine=0;

W_Image *pauseImage, *titleImage;
int pausex, pausey, pauseyspeed=1;

void undo_pause()
{
    W_CacheClearArea(baseWin, pausex-(pauseImage->width/2), pausey-(pauseImage->height/2), 
		     pauseImage->width, pauseImage->height);
}

void do_pause()
{
    if((pausey < pauseImage->height/2) || ((pausey+(pauseImage->height/2)) >= WINHEIGHT))
	pauseyspeed = -pauseyspeed;
    pausey+=pauseyspeed;

    W_DrawImage(baseWin, pausex-(pauseImage->width/2), pausey-(pauseImage->height/2), 0, pauseImage, W_White);
}

void 
center_text(char *text, int y, W_Color color)
{
/*
    W_MaskText(baseWin, WINWIDTH/2 - ((strlen(text)*W_Textwidth)/2)-1, y+1, W_Black,
	       text, strlen(text), W_RegularFont);
*/
    W_MaskText(baseWin, WINWIDTH/2 - ((strlen(text)*W_Textwidth)/2), y, color,
	       text, strlen(text), W_RegularFont);
}

#if 0 /* ships vary from level to level now */
static void show_points()
{
    int i, ty;
    int a_images[6] = {
	I_ALIEN1,
	I_ALIEN2,
	I_ALIEN3,
	I_ALIEN4,
	I_ALIEN5,
	I_ALIEN6
    };

    char buf[40];

    for (i=0;i<6;i++) {
	W_DrawImage(baseWin, WINWIDTH/2 - 30, 220+i*21,
		    0, getImage(a_images[5-i]), W_Red);
	if(i < 5)
	    sprintf(buf, "- %d", (i+1)*100);
	else
	    sprintf(buf, "- ???");
	W_MaskText(baseWin, WINWIDTH/2, 225+i*21,
		   W_Yellow, buf, strlen(buf), W_RegularFont);
    }
    ty = 220+i*21;
    center_text("Ships in convoy are worth 50 points", ty, W_Green); ty += W_Textheight;
    center_text("Bonus ships at 20,000, 50,000, then every 50,000.", ty, W_Green);
}
#endif

static void show_help()
{
    int top = 270;

    center_text("         Keyboard controls           ", top, W_Red);
    center_text("  left/right arrow   move            ", top+10, W_Yellow);
    center_text("  space bar          fire            ", top+20, W_Yellow);
    center_text("  p                  pause           ", top+30, W_Cyan);
    center_text("  q                  end game        ", top+40, W_Cyan);
    center_text("  Q                  quick quit      ", top+50, W_Cyan);
    center_text("  k                  keyboard control", top+60, W_Cyan);
    center_text("  m                  mouse control   ", top+70, W_Cyan);
#ifndef ORIGINAL_XGALAGA
    center_text("  x                  toggle shields  ", top+80, W_Cyan);
#endif
#ifdef SOUND
    center_text("  s                  toggle sound    ", top+90, W_Cyan);
#endif
	  
    center_text("Bonus ships at 20,000, 50,000, then every 50,000.", top+120, W_Green);
    center_text("XGalaga Home page: http://rumsey.org/xgal.html",
		top+150, W_Cyan);
}

void read_credits()
{
    int hsf;
    int i,j,l,lines;
    int nSize;
    char *chFBuf, chLBuf[LINESIZE+1];

    j = 0; lines = 0;
    chFBuf = (char *) malloc (8000+1);
    if (chFBuf == 0) return;
    gchLBuf = (char *) malloc (LINESIZE+1);
    if (gchLBuf == 0) return;
    hsf = open (CREDITSFILE, O_RDONLY);
    if ( hsf > 0 ) 
    {
      l = read (hsf, chFBuf, 8000);
      chFBuf[l] = 0x00;
      if ( l > 0 )
      {
        for (i=0; i<l; i++)
        {
          switch (chFBuf[i])
          {
          case 10 :     // Linefeed
          case 0 :
            chLBuf[j] = 0x00;
            j = 0;
            strncpy (gchLBuf+(lines*(LINESIZE+1)), chLBuf, LINESIZE);
            lines++;
            nSize = (lines+1) * (LINESIZE+1);
            gchLBuf = (char *) realloc (gchLBuf, nSize);        // get space for next line
            break;
          case 1: case 2: case 3: case 4:case 5:
          case 6: case 7: case 8: case 9:
          case 11: case 12: case 13: case 14:case 15:
          case 16: case 17: case 18: case 19:
            // ignore control characters 
            break;
          default:
            chLBuf[j++] = chFBuf[i];            // visible character
            if (j>LINESIZE)
            {
              // line too long -> cut without grace
              chLBuf[j] = 0x00;
              j = 0;
              strncpy (gchLBuf+(lines*(LINESIZE+1)), chLBuf, LINESIZE);
              lines++;
              nSize = (lines+1) * (LINESIZE+1);
              gchLBuf = (char *) realloc (gchLBuf, nSize);        // get space for next line
            }
            break;
          } // end switch
        } // end for
      }
    }
    else
    {
//      printf (chLBuf, sizeof(chLBuf)-1, "Sorry! Could not read CREDITSFILE");
    }
    close(hsf);
    free (chFBuf);
    giCreditLines = lines-1;
  }


void undo_credits()
{
  int i,j,lines;
  int top = 460;
  char chLBuf[LINESIZE+1];

  if ( gchLBuf > 0 )
  {
    j = 0; lines = 0;
    for ( i=giActCreditLine; i<giCreditLines; i++ )
    {
      strncpy (chLBuf, gchLBuf+i*(LINESIZE+1), LINESIZE);
      center_text (chLBuf, top+lines*W_Textheight + ((pagetimer-1) % (W_Textheight*SPEEDFACTOR))/SPEEDFACTOR, W_Black);
      lines++;
      if ( lines > VISIBLELINES ) break;
    } // end for
  }
  else
  {
    snprintf (chLBuf, sizeof(chLBuf)-1, "Sorry no CREDITS data");
    center_text (chLBuf, top+10, W_Black);
  }
}


void show_credits()
{
  int i,j,lines;
  int top = 460;
  char chLBuf[LINESIZE+1];

  if ( gchLBuf > 0 )
  {
    j = 0; lines = 0;
    for ( i=giActCreditLine; i<giCreditLines; i++ )
    {
      strncpy (chLBuf, gchLBuf+i*(LINESIZE+1), LINESIZE);
      j = top + lines*W_Textheight + ((pagetimer-1) % (W_Textheight*SPEEDFACTOR))/SPEEDFACTOR ; 
      center_text (chLBuf,j, W_Green);
      lines++;
      if ( lines > VISIBLELINES ) break;
    } // end for
    W_ClearArea(baseWin,
                0, top,
                WINWIDTH, W_Textheight);
    W_ClearArea(baseWin,
                0, top+(VISIBLELINES+1)*W_Textheight,
                WINWIDTH, W_Textheight);
   }
  else
  {
    snprintf (chLBuf, sizeof(chLBuf)-1, "Sorry no CREDITS data");
    center_text (chLBuf, top+10, W_Green);
  }
}


void do_title()
{
    int ty;
    char vbuf[10];

    W_DrawImage (baseWin, WINWIDTH/2 - titleImage->width/2, 50, 0, titleImage, W_Red);
    sprintf (vbuf, "v%s", VERSION);
    center_text (vbuf, 40+titleImage->height, W_Yellow);

    ty = 60 + titleImage->height;
    if (getting_name)
	title_page = 0;

    switch (title_page) 
    {
    case 0:
	center_text("Copyright (c) 1995-1998   Joe Rumsey", ty, W_Green); ty+= W_Textheight;
	center_text("Maintenance 2008 by Hermann Riedel", ty, W_Green); ty+= 2*W_Textheight;
#ifndef ORIGINAL_XGALAGA
	center_text("XGalaga: Hyperspace 0.9", ty, W_Green); ty+= W_Textheight;
#endif
        show_help();
//        show_credits();
	break;
    case 1:
    default:
	show_scores();
//        show_credits();
	break;
    }

#ifdef __linux__
    if (js_device)
	    center_text("Press k for keyboard or joystick control, m for mouse", WINHEIGHT - 2*W_Textheight, W_Yellow);
    else /* no joystick, so use message below */
#endif
    center_text("Press k for keyboard control, m for mouse", WINHEIGHT - 2*W_Textheight, W_Yellow);
    center_text("Or q to quit", WINHEIGHT - W_Textheight, W_Yellow);

    undo_credits();     // erase with painting black
    pagetimer--;
//    if (!(pagetimer % 50))
    if (!(pagetimer % (W_Textheight*SPEEDFACTOR)))
    {
//printf("%d %d %25s\n", pagetimer, (pagetimer % (W_Textheight*SPEEDFACTOR)),  gchLBuf+giActCreditLine*(LINESIZE+1));
      giActCreditLine++;
      if (giActCreditLine > giCreditLines)
        giActCreditLine = 0; 
    } 
    if (!pagetimer) 
    {
	W_ClearWindow(baseWin);
	title_page++;
	if (title_page > 1)
	  title_page = 0;
	pagetimer = 299;
    }
    show_credits();     // write text
}

void init_titles()
{
    titleImage = getImage(I_TITLE);
    pauseImage = getImage(I_PAUSE);
    pausex = WINWIDTH/2;
    pausey = WINHEIGHT/2;
    pauseyspeed = 3;
    read_credits();
}