1 /*
2 Copyright notice:
3 
4 This is mine.  I'm only letting you use it.  Period.  Feel free to rip off
5 any of the code you see fit, but have the courtesy to give me credit.
6 Otherwise great hairy beasties will rip your eyes out and eat your flesh
7 when you least expect it.
8 
9 Jonny Goldman <jonathan@think.com>
10 
11 Wed May  8 1991
12 */
13 
14 /* score.c -- Print the score. */
15 
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19 
20 #include "vaders.h"
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 
24 #define SCORELABEL	10
25 #define SCOREPOS	(SCORELABEL+15)
26 #define HILABEL		(SCOREPOS+20)
27 #define HIPOS		(HILABEL+15)
28 
29 char *scorestr=NULL,*highstr=NULL;
30 
31 static int painthiscore=0;
32 int hiscore = -1;
33 #ifdef SCOREFILE
34 static int initial_hiscore=0;
35 #endif
36 
PaintScore()37 void PaintScore()
38 {
39   char scorestring[8];
40   if (!scorestr) {
41      scorestr=strdup(_("Score"));
42      highstr=strdup(_("High"));
43   }
44   XDrawImageString(dpy, labelwindow, scoregc, 0, SCORELABEL, scorestr, strlen(scorestr));
45   sprintf(scorestring, "%6d ", score);
46   XDrawImageString(dpy, labelwindow, scoregc, 0, SCOREPOS, scorestring, 7);
47   if (nextbonus && score >= nextbonus) {
48     basesleft++;
49     ShowBase(basesleft-1, basegc);
50     bases = basesleft;
51     nextbonus = 0;
52   }
53   if (painthiscore || (score > hiscore && (hiscore = score)) )
54     painthiscore=0;
55     sprintf(scorestring, "%6d ", hiscore);
56     XDrawImageString(dpy, labelwindow, scoregc, 0, HILABEL, highstr, strlen(highstr));
57     XDrawImageString(dpy, labelwindow, scoregc, 0, HIPOS, scorestring, 7);
58 }
59 
InitScore()60 void InitScore()
61 {
62     score = 0;
63     if (hiscore == -1)
64     {
65 #ifdef SCOREFILE
66     	FILE *f;
67     	char l[15];
68     	painthiscore = 1;
69     	f=fopen(SCOREFILE,"r");
70     	if (f)
71     	{
72                if (fgets(l,14,f) != NULL)
73                        initial_hiscore=hiscore=atoi(l);
74                else
75                        hiscore=0;
76     		fclose(f);
77     	} else
78     		hiscore=0;
79 #else
80         hiscore = 0;
81         painthiscore = 1;
82 #endif
83     }
84     basesleft = 3;
85     nextbonus = 1500;
86 }
87 
88 
89 #ifdef SCOREFILE
SaveScore()90 void SaveScore()
91 {
92 	FILE *f;
93 	if((initial_hiscore != hiscore) && (f=fopen(SCOREFILE,"w")))
94 	{
95 		fchmod(fileno(f),S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
96 		fprintf(f,"%d\n",hiscore);
97 		fclose(f);
98 	}
99 }
100 #endif
101