-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathicebreaker.c
255 lines (204 loc) · 5.54 KB
/
icebreaker.c
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
/*
* IceBreaker
* Copyright (c) 2000-2020 Matthew Miller <[email protected]>
*
* <http://www.mattdm.org/icebreaker/>
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#ifndef __MINGW32__
#include <pwd.h>
#endif
#include <string.h>
#include <sys/types.h>
#include "icebreaker.h"
#include "penguin.h"
#include "line.h"
#include "sound.h"
#include "globals.h"
#include "grid.h"
#include "level.h"
#include "intro.h"
#include "text.h"
#include "transition.h"
#include "hiscore.h"
#include "dialog.h"
#include "options.h"
#include "fullscreen.h"
#include "cursor.h"
#include "themes.h"
#include "titlebar.h"
#include "benchmark.h"
#include "misc.h"
// global
SDL_Surface* screen;
SDL_Surface* fullscreen;
char username[50]; // FIX -- move this into the options struct?
char homedir[255];
SDL_Surface * penguinicon;
// functions
int setup(void);
void cleanup(void);
/************************************************************************/
int setup(void)
{
struct passwd * userinfo;
int newuser=false;
srandom(time(NULL)+getpid());
//stupid buffers
setvbuf(stdout,(char *)NULL, _IOLBF, 0);
userinfo = getpwuid(getuid()); // FIX -- make this part of the options struct; and maybe save in options file
snprintf(username,50,"%s",userinfo->pw_name); // not like it's gonna be fifty characters. but y'know. note: gets chopped to fit in gethighusername().
snprintf(homedir,255,"%s",userinfo->pw_dir); // fix - use OS define for length
inithiscores();
newuser=readoptions();
if (commandline.fullscreen==FULLSCREENOFF)
options.fullscreen=FULLSCREENOFF;
else if (commandline.fullscreen==FULLSCREENON && options.fullscreen==FULLSCREENOFF)
options.fullscreen=FULLSCREENON;
if (SDL_Init(SDL_INIT_VIDEO))
{
fprintf(stderr, "Hey. We're gonna need some graphics.\n"
"SDL error: "
"%s\n\n", SDL_GetError());
exit(1);
}
atexit(cleanup);
penguinicon = SDL_LoadBMP(DATAPREFIX "/" PENGUINICONFILE);
if (penguinicon==NULL) fprintf(stderr, "Icon not loaded!\n\n*** IceBreaker probably wasn't installed properly. ***\n\n");
SDL_WM_SetIcon(penguinicon,NULL);
if (options.fullscreen==FULLSCREENOFF)
{
// gotta do this right away or else we risk getting an ugly "SDL_App"
// in the titlebar for a few milliseconds -- can't have that!
SDL_WM_SetCaption("IceBreaker","IceBreaker");
screen = SDL_SetVideoMode(WIDTH, HEIGHT, VIDEODEPTH, SDL_SWSURFACE);
}
else
{
screen = NULL;
makefullscreen();
}
if (screen == NULL)
{
fprintf(stderr, "Help! Couldn't get a window.\n"
"SDL error: "
"%s\n\n", SDL_GetError());
exit(1);
}
initsound();
inittext();
initgrid();
initcursors();
if (strlen(commandline.theme)>0)
settheme(commandline.theme);
else
settheme(options.theme);
return newuser;
}
void cleanup()
{
quitgrid();
quitsound();
quitcursors();
SDL_Quit();
writeoptions();
writedelayedhiscores();
}
int main(int argc,char** argv)
{
int done = false;
int level=0;
ScoreSheet levelscore;
long totalscore=0;
char windowtitle[39];
LevelExitType levelresult;
int newuser=false;
int rc=0;
#ifdef NEEDCHANGETOARGV0PATH
changetoargv0path(argv[0]);
#endif
rc=parsecommandline(argc,argv);
if (rc) return rc;
newuser=setup();
drawtitlebar("IceBreaker");
#if DEVELRELEASE
if (gameflags.benchmarkmode)
{
drawtitlebar("IceBreaker -- Benchmark mode");
rc=benchmark();
return rc;
}
#endif /* DEVELRELEASE */
done=intro();
if (!done && newuser)
{ // no options file; using the default
setcursor(CURSORCLICK);
if (popuphelp()==POPUPQUITGAME) done=true;
setcursor(CURSORARROW);
}
while(!done)
{
level++;
if (level>=MAXPENGUINS) level=MAXPENGUINS-1;
switch (options.difficulty)
{
case NORMAL:
snprintf(windowtitle,39,"IceBreaker -- Level %d",level);
break;
case HARD:
snprintf(windowtitle,39,"IceBreaker -- Level %d (Hard)",level);
break;
case EASY:
snprintf(windowtitle,39,"IceBreaker -- Level %d (Easy)",level);
break;
}
drawtitlebar(windowtitle);
if (!strcmp(commandline.theme,"random") || (strlen(commandline.theme)==0 && !strcmp(options.theme,"random")))
settheme("random");
levelresult=playlevel(level,totalscore,&levelscore);
drawtitlebar("IceBreaker");
totalscore+= levelscore.basescore + levelscore.clearbonus + levelscore.lifebonus;
switch (levelresult)
{
case QUIT:
done=true;
break;
case DEAD:
done=gameover(totalscore);
// falls through
case ZERO:
// hooray! modifying the index variable in the loop!
// good coding practice at its finest!
level=0;
totalscore=0;
break;
case ERROR:
fprintf(stderr,"Level error -- this should never happen.\n");
break;
case PASS:
// level completed successfully
done=intermission(&levelscore,level+1);
break;
}
}
return rc;
}