~ubuntu-branches/ubuntu/hardy/orbital-eunuchs-sniper/hardy

« back to all changes in this revision

Viewing changes to src/highscores.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-05-29 09:32:48 UTC
  • mfrom: (1.1.1 upstream) (2.1.2 gutsy)
  • Revision ID: james.westby@ubuntu.com-20070529093248-laj1bsm2dffohdf9
Tags: 1.30+svn20070601-1
Fix broken "upstream" rule to generate correctly versioned orig.tar.gz
to avoid native package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "snipe2d.h"
 
2
#include <pwd.h>
 
3
 
 
4
extern PREFS gPrefs;
 
5
char uName[9];
 
6
char names[10][9];
 
7
int scores[10];
 
8
 
 
9
extern void fillrect (int x1, int y1, int x2, int y2, int color);
 
10
 
 
11
 
 
12
void
 
13
oes_fillrect (SDL_Surface *screen, int x1, int y1, int x2, int y2, int color)
 
14
{
 
15
    SDL_Rect r;
 
16
    r.x = x1;
 
17
    r.y = y1;
 
18
    r.w = x2-x1;
 
19
    r.h = y2-y1;
 
20
    int sdlcolor = SDL_MapRGB(screen->format,
 
21
                              (color >> 16) & 0xff,
 
22
                              (color >> 8) & 0xff,
 
23
                              (color >> 0) & 0xff);
 
24
    SDL_FillRect(screen, &r, sdlcolor);
 
25
}
 
26
 
 
27
 
 
28
int
 
29
draw_hiscores (SDL_Surface *screen, const SDL_Rect *r)
 
30
{
 
31
  char score[256];
 
32
  FILE *f;
 
33
  int i;
 
34
  int row, toprow;
 
35
  int col;
 
36
  int x, y, w, h;
 
37
  int xp, xq;  /* horizontal scale factor (p/q) */
 
38
  int yp, yq;  /* vertical scale factor (p/q) */
 
39
 
 
40
  if ((f = fopen(gPrefs.scorepath, "rb")))
 
41
    {
 
42
      fread(&Game.HiScore, sizeof(SCORES), 1, f);
 
43
      fclose(f);
 
44
    }
 
45
 
 
46
  x = r->x; y = r->y; w = r->w; h = r->h;
 
47
  /* map  640 * p/q  =>  screen->w */
 
48
  xp = screen->w;
 
49
  yp = screen->h;
 
50
  xq = 640;
 
51
  yq = 480;
 
52
#define X(n) (x + (n * xp / xq))
 
53
#define Y(n) (y + (n * yp / yq))
 
54
  oes_fillrect(screen, X(0), Y(0), X(640), Y(480), 0);
 
55
  oes_fillrect(screen, X(19), Y(59), X(621), Y(421), 0x007f00);
 
56
  oes_fillrect(screen, X(20), Y(60), X(620), Y(420), 0x003f00);
 
57
  oes_fillrect(screen, X(19), Y(80), X(621), Y(81), 0x007f00);
 
58
  print(X(224 + 12), Y(412), COLOR_GREEN, "click to play | press ESC to quit");
 
59
  toprow = 88 + 40;
 
60
 
 
61
  row = toprow;
 
62
  printShadow(X(30), Y(row), "  Top Ten (Difficulty: Easy");
 
63
  row += 8;
 
64
  for (i = 0; i < 10; i ++)
 
65
    {
 
66
      col = 24;
 
67
      printShadow(X(col), Y(row), Game.HiScore.easy_n[i]);
 
68
      snprintf(score, sizeof(score), "%d", Game.HiScore.easy_s[i]);
 
69
      if (Game.HiScore.easy_s[i])
 
70
        printShadow(X(col + 48), Y(row), score);
 
71
      row += 8;
 
72
    }
 
73
 
 
74
  row = toprow;
 
75
  printShadow(X(250), Y(row), "Top Ten (Difficulty: Medium)");
 
76
  row += 8;
 
77
  for (i = 0; i < 10; i++)
 
78
    {
 
79
      col = 250;
 
80
      printShadow(X(col), Y(row), Game.HiScore.medium_n[i]);
 
81
      snprintf(score, sizeof(score), "%d", Game.HiScore.medium_s[i]);
 
82
      if (Game.HiScore.medium_s[i])
 
83
          printShadow(X(col + 48), Y(row), score);
 
84
      row += 8;
 
85
    }
 
86
 
 
87
  row = toprow;
 
88
  printShadow(X(476), Y(row), " Top Ten (Difficulty: Hard)");
 
89
  row += 8;
 
90
  for (i = 0; i < 10; i++)
 
91
    {
 
92
      col = 476;
 
93
      printShadow(X(col), Y(row), Game.HiScore.hard_n[i]);
 
94
      snprintf(score, sizeof(score), "%d", Game.HiScore.hard_s[i]);
 
95
      if (Game.HiScore.hard_s[i])
 
96
          printShadow(X(col + 48), Y(row), score);
 
97
      row += 8;
 
98
    }
 
99
 
 
100
  row = toprow;
 
101
 
 
102
#undef X
 
103
#undef Y
 
104
  return 1;
 
105
}
 
106
 
 
107
#if 0
 
108
void show_hiscores()
 
109
{
 
110
    char score[256];
 
111
    FILE *f;
 
112
    int i;
 
113
 
 
114
    Game.GameState = 0;
 
115
    SDL_ShowCursor (1);
 
116
    if ((f = fopen (gPrefs.scorepath, "rb")))
 
117
        {
 
118
            fread (&Game.HiScore, sizeof(SCORES), 1, f);
 
119
            fclose (f);
 
120
        }
 
121
 
 
122
    Game.GameState = 0;
 
123
    fillrect(0,0,640,480,0);
 
124
    fillrect(19,59,621,421,0x007f00);
 
125
    fillrect(20,60,620,420,0x003f00);
 
126
    fillrect(19,80,621,81,0x007f00);
 
127
    print(224 + 12, 412, COLOR_GREEN, "click to play | press ESC to quit");
 
128
    int row = 88 +40;
 
129
 
 
130
    printShadow (30, row, "  Top Ten (Difficulty: Easy");
 
131
    row += 8;
 
132
    for (i = 0; i < 10; i++)
 
133
        {
 
134
            printShadow (24, row, Game.HiScore.easy_n[i]);
 
135
            snprintf (score, sizeof(score), "%d", Game.HiScore.easy_s[i]);
 
136
            if (Game.HiScore.easy_s[i])
 
137
                printShadow (24+48, row, score);
 
138
            row += 8;
 
139
        }
 
140
    row = 88+40;
 
141
 
 
142
    printShadow (250, row, "Top Ten (Difficulty: Medium)");
 
143
    row += 8;
 
144
    for (i = 0; i < 10; i++)
 
145
        {
 
146
            printShadow (250, row, Game.HiScore.medium_n[i]);
 
147
            snprintf (score, sizeof(score), "%d", Game.HiScore.medium_s[i]);
 
148
            if (Game.HiScore.medium_s[i])
 
149
                printShadow (298, row, score);
 
150
            row += 8;
 
151
        }
 
152
    row = 88+40;
 
153
 
 
154
    printShadow (476, row, " Top Ten (Difficulty: Hard)");
 
155
    row += 8;
 
156
    for (i = 0; i < 10; i++)
 
157
        {
 
158
            printShadow (476, row, Game.HiScore.hard_n[i]);
 
159
            snprintf (score, sizeof(score), "%d", Game.HiScore.hard_s[i]);
 
160
            if (Game.HiScore.hard_s[i])
 
161
                printShadow (476+48, row, score);
 
162
            row += 8;
 
163
        }
 
164
    row = 88+40;
 
165
 
 
166
    SDL_Flip (Game.Screen);
 
167
}
 
168
#endif /* 0 */
 
169
 
 
170
void shove_hiscores(int pos) // Shift scorecard down, bump last one.
 
171
{
 
172
    int i;
 
173
 
 
174
    for (i = 9; i > pos; i--)
 
175
        if (i > 0)
 
176
            {
 
177
                snprintf (names[i], 9, "%s", names[i-1]);
 
178
                names[i][8] = '\0';
 
179
                scores[i] = scores[i-1];
 
180
            }
 
181
 
 
182
    scores[i] = Game.Score;
 
183
    snprintf (names[i], 9, "%s", uName);
 
184
    names[i][8] = '\0';
 
185
}
 
186
 
 
187
void init_hiscores()
 
188
{
 
189
    char path[256];
 
190
    struct passwd *pw_ent = getpwuid (geteuid());
 
191
    FILE *f;
 
192
 
 
193
    memset (&Game.HiScore, 0, sizeof(SCORES));
 
194
    snprintf (uName, 9, "%s", getenv("USER"));
 
195
    uName[8] = '\0';
 
196
 
 
197
    snprintf (path, 256, "%s/.oes/scores.bin", pw_ent->pw_dir);
 
198
    path[255] = '\0';
 
199
    f = fopen (path, "rb");
 
200
    if (f)
 
201
        {
 
202
            fread (&Game.HiScore, sizeof(SCORES), 1, f);
 
203
            fclose (f);
 
204
        }
 
205
}
 
206
 
 
207
void save_hiscores()
 
208
{
 
209
    FILE *f;
 
210
    if (!(f = fopen(gPrefs.scorepath, "wb")))
 
211
        {
 
212
            fprintf (stderr, "Unable to open hiscore file.  Ignoring...\n");
 
213
            return;
 
214
        }
 
215
    fwrite (&Game.HiScore, sizeof (SCORES), 1, f);
 
216
    fclose (f);
 
217
}
 
218
 
 
219
void process_hiscore()
 
220
{
 
221
    int i;
 
222
 
 
223
    if (Game.Score < 0)
 
224
        return;
 
225
    if (gPrefs.difficulty == 1)
 
226
        {
 
227
            memcpy (&scores, &Game.HiScore.easy_s, sizeof(int)*10);
 
228
            memcpy (&names, &Game.HiScore.easy_n, sizeof(char)*90);
 
229
        }
 
230
    else if (gPrefs.difficulty == 2)
 
231
        {
 
232
            memcpy (&scores, &Game.HiScore.medium_s, sizeof(int)*10);
 
233
            memcpy (&names, &Game.HiScore.medium_n, sizeof(char)*90);
 
234
        }
 
235
    else
 
236
        {
 
237
            memcpy (&scores, &Game.HiScore.hard_s, sizeof(int)*10);
 
238
            memcpy (&names, &Game.HiScore.hard_n, sizeof(char)*90);
 
239
        }
 
240
    for (i = 0; i < 10; i++)
 
241
        if (Game.Score > scores[i])
 
242
            break;
 
243
 
 
244
    if (i > 9)
 
245
        return;
 
246
 
 
247
    shove_hiscores(i);
 
248
 
 
249
    if (gPrefs.difficulty == 1)
 
250
        {
 
251
            memcpy (&Game.HiScore.easy_s, &scores, sizeof(int)*10);
 
252
            memcpy (&Game.HiScore.easy_n, &names, sizeof(char)*90);
 
253
        }
 
254
    else if (gPrefs.difficulty == 2)
 
255
        {
 
256
            memcpy (&Game.HiScore.medium_s, &scores, sizeof(int)*10);
 
257
            memcpy (&Game.HiScore.medium_n, &names, sizeof(char)*90);
 
258
        }
 
259
    else
 
260
        {
 
261
            memcpy (&Game.HiScore.hard_s, &scores, sizeof(int)*10);
 
262
            memcpy (&Game.HiScore.hard_n, &names, sizeof(char)*90);
 
263
        }
 
264
 
 
265
    save_hiscores();
 
266
}
 
267
 
 
268