~ubuntu-branches/ubuntu/feisty/penguin-command/feisty

« back to all changes in this revision

Viewing changes to src/game.c

  • Committer: Bazaar Package Importer
  • Author(s): Karl Bartel
  • Date: 2001-10-13 17:35:43 UTC
  • Revision ID: james.westby@ubuntu.com-20011013173543-dn27xb78b2gco6qi
Tags: upstream-1.6.2
ImportĀ upstreamĀ versionĀ 1.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "main.h"
 
2
#include "joystick.h"
 
3
 
 
4
int endgame=0,MouseX,MouseY,MissilesCreated=0,NewMissile=10000,WaveNum=0,
 
5
    NewCity=0,CityNum,pause=0;
 
6
Uint32 Now,TimeStart,FrameCount=0;
 
7
double Speed,FramesPerSecond;
 
8
char text[200];
 
9
 
 
10
void AddScore(int ScoreBonus)
 
11
{
 
12
    SDL_Rect rect;
 
13
    
 
14
    Score+=ScoreBonus*(WaveNum/2+1);
 
15
    sprintf(text, "Score: %d", Score);
 
16
    rect.x=0;
 
17
    rect.y=0;
 
18
    rect.w=TextWidth(text)+30;
 
19
    rect.h=Font->h;
 
20
    SDL_BlitSurface( BackPic, &rect, BackBuffer, &rect);
 
21
    PutString(BackBuffer,0,0,text);
 
22
    SDL_SetAlpha(BackBuffer,(SDL_SRCALPHA), 255);
 
23
    SDL_BlitSurface( BackBuffer, &rect, Screen, &rect);
 
24
    AddThisRect(rect);
 
25
}
 
26
 
 
27
void GetSpeed()
 
28
{
 
29
    FrameCount++;
 
30
    FramesPerSecond=FrameCount*(float)1000/(SDL_GetTicks()-TimeStart);
 
31
    Speed=SDL_GetTicks()-Now;
 
32
    // limit speed to 10ms per frame
 
33
    while (Speed<10) {
 
34
        SDL_Delay(1);
 
35
        Speed=(SDL_GetTicks()-Now);
 
36
    }
 
37
    Now=SDL_GetTicks();
 
38
}
 
39
 
 
40
 
 
41
void Pause(const SDL_Event *event)
 
42
{
 
43
    pause=1;    
 
44
 
 
45
    // clean input queue
 
46
    while ( event->type == SDL_KEYDOWN )
 
47
        SDL_PollEvent((SDL_Event *)event);
 
48
        
 
49
    // write message to screen
 
50
    SDL_BlitSurface(Screen,NULL,FadeBuffer,NULL);
 
51
    XCenteredString(Screen,250,"Game Paused");
 
52
    SDL_UpdateRect(Screen,0,0,0,0);
 
53
 
 
54
    // wait
 
55
    while ( event->type != SDL_KEYDOWN )
 
56
        SDL_PollEvent((SDL_Event *)event);
 
57
 
 
58
    // Clean pic
 
59
    SDL_BlitSurface(FadeBuffer,NULL,Screen,NULL);
 
60
    SDL_UpdateRect(Screen,0,0,0,0);
 
61
    Now=SDL_GetTicks();
 
62
    pause=0;
 
63
}
 
64
 
 
65
int NormalEvents(const SDL_Event *event)
 
66
{
 
67
    switch (event->type) {
 
68
        case SDL_QUIT:
 
69
            printf("Quit Requested\n");
 
70
            Quit=1;
 
71
        break;
 
72
        case SDL_KEYDOWN:
 
73
            if ( event->key.keysym.sym == SDLK_ESCAPE )
 
74
                Quit=1;
 
75
            if ( (event->key.keysym.sym == SDLK_RETURN) &&
 
76
                (event->key.keysym.mod & KMOD_ALT) )
 
77
                    SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
 
78
        break;
 
79
    }
 
80
    
 
81
    return 1;
 
82
}
 
83
 
 
84
void MenuEvents()
 
85
{
 
86
    SDL_SetEventFilter(NormalEvents);
 
87
}
 
88
 
 
89
int GameEvents(const SDL_Event *event)
 
90
{
 
91
    int button=-1;
 
92
    Uint8* keys;
 
93
 
 
94
    switch (event->type) {
 
95
        case SDL_QUIT:
 
96
            printf("Quit Requested\n");
 
97
            Quit=1;
 
98
        break;
 
99
#ifdef JOYSTICK
 
100
        /* JOYAXISMOTION is handled elsewhere. */
 
101
        case SDL_JOYBUTTONDOWN:
 
102
                /* TODO: Let user configure which button does what. */
 
103
                if (event->jbutton.state == SDL_PRESSED) {
 
104
                        SDL_GetMouseState(&MouseX, &MouseY);
 
105
                        /* Joystick buttons are numbered from 0, mouse
 
106
                          buttons are numbered from 1 */
 
107
                        CreateShot(MouseX,MouseY,event->button.button+1);
 
108
                }
 
109
                break;
 
110
#endif /* JOYSTICK */   
 
111
        case SDL_MOUSEBUTTONDOWN:
 
112
            SDL_GetMouseState(&MouseX, &MouseY);
 
113
            CreateShot(MouseX,MouseY,event->button.button);
 
114
        break;    
 
115
        case SDL_KEYDOWN:
 
116
            keys = SDL_GetKeyState(NULL);
 
117
            if ( event->key.keysym.sym == SDLK_a ) button=1;
 
118
            if ( event->key.keysym.sym == SDLK_s ) button=2;
 
119
            if ( event->key.keysym.sym == SDLK_d ) button=3;
 
120
            if ( button > 0 ) {
 
121
                CreateShot(MouseX,MouseY,button);
 
122
            }
 
123
            if ( event->key.keysym.sym == SDLK_ESCAPE ) {
 
124
                printf("Quit Requested\n");
 
125
                endgame=1;
 
126
            }
 
127
            // Fullscreen toggle
 
128
            if ( (event->key.keysym.sym == SDLK_RETURN) &&
 
129
                (event->key.keysym.mod & KMOD_ALT) )
 
130
                    SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
 
131
            // Pause
 
132
            if (( ( event->key.keysym.sym == SDLK_PAUSE ) ||
 
133
                ( event->key.keysym.sym == SDLK_p ) ) &&
 
134
                ( !pause) )
 
135
                    Pause(event);
 
136
        break;
 
137
    }    
 
138
    return 1;
 
139
 
 
140
}
 
141
 
 
142
void UndrawCannon(int x, int y)
 
143
{
 
144
    SDL_Rect rect;
 
145
    
 
146
    rect.x=x-(int)(Cannon[0]->w/2);
 
147
    rect.y=y;
 
148
    rect.w=Cannon[0]->w;
 
149
    rect.h=Cannon[0]->h;
 
150
    SDL_SetAlpha(BackBuffer,(SDL_SRCALPHA),255);
 
151
    BlitPart(rect.x,rect.y,BackBuffer,rect);
 
152
}
 
153
 
 
154
void DrawCannon(int x, int y)
 
155
{
 
156
    int add=0;
 
157
    float deg;
 
158
 
 
159
    SDL_GetMouseState(&MouseX, &MouseY);
 
160
    deg=atan((float)(MouseY-(y+20))/(float)(MouseX-x));
 
161
    if (MouseX<x) {
 
162
        add+=20;
 
163
    }
 
164
    if (MouseY>y+20)
 
165
        Blit(x-(int)(Cannon[0]->w/2),y,Cannon[0+add]);
 
166
    else    
 
167
        Blit(x-(int)(Cannon[0]->w/2),y,Cannon[add-(int)(deg*0.6366*10)]);
 
168
}
 
169
 
 
170
void DrawCenteredText(int y, char *text)
 
171
{
 
172
    SDL_Rect rect;
 
173
 
 
174
    rect.x=400-TextWidth(text)/2;
 
175
    rect.y=y;
 
176
    rect.w=TextWidth(text);
 
177
    rect.h=Font->h;
 
178
    SDL_SetAlpha(BackBuffer,(SDL_SRCALPHA), 255);
 
179
    SDL_BlitSurface( BackBuffer, &rect, Screen, &rect);
 
180
    PutString(Screen,rect.x,rect.y,text);
 
181
    AddThisRect(rect);
 
182
}
 
183
 
 
184
void StartWave()
 
185
{
 
186
    int i,dummy=0;
 
187
 
 
188
    NewMissile=0;
 
189
    MissilesCreated=0;
 
190
    BlitToBB(0,0,BackPic);
 
191
    // New City?
 
192
    for (i=0;i<6;i++)
 
193
        if (!CityDestroyed[i]) dummy++;
 
194
    i=abrand(0,5);
 
195
    if (dummy<6) {
 
196
        if ((!NewCity)||(ArcadeMode)) {
 
197
            while (!CityDestroyed[i])
 
198
                i=abrand(0,5);
 
199
            CityDestroyed[i]=0;
 
200
            if (i<3)
 
201
                Blit(70+i*100,490,CityPic);
 
202
            else
 
203
                Blit(130+i*100,490,CityPic);
 
204
            PlaySound(CitySound);
 
205
            WaitWithMouse(100);
 
206
            Update();
 
207
            WaitWithMouse(200);
 
208
            NewCity=1;
 
209
        }
 
210
        else NewCity=0;
 
211
    }
 
212
    CityNum=0;
 
213
    for (i=0;i<6;i++)
 
214
        if (!CityDestroyed[i]) CityNum++;
 
215
    // Draw Cities
 
216
    dummy=0;
 
217
    for (i=0;i<3;i++)
 
218
        if (CityDestroyed[i])
 
219
            BlitToBB(70+i*100,490,CityHitPic);
 
220
        else
 
221
            BlitToBB(70+i*100,490,CityPic);
 
222
    dummy=0;
 
223
    for (i=0;i<3;i++)
 
224
        if (CityDestroyed[i+3])
 
225
            BlitToBB(430+i*100,490,CityHitPic);
 
226
        else
 
227
            BlitToBB(430+i*100,490,CityPic);
 
228
    // Printf "Staring Wave" message
 
229
    AddScore(0);
 
230
    Blit(0,0,BackBuffer);
 
231
    sprintf(text,"Starting Wave #%d",WaveNum+1);
 
232
    PutString(Screen, 400-TextWidth(text)/2, 170, text);
 
233
    sprintf(text,"Score %d x",WaveNum/2+1);
 
234
    PutString(Screen, 400-TextWidth(text)/2, 220, text);
 
235
    DrawCenteredText(370, "Defend Cities!");
 
236
    for (i=0;i<3;i++) {
 
237
        if ( CannonDestroyed[i] ) {
 
238
            ShotNum[i]=3;
 
239
            CannonDestroyed[i]=0;
 
240
        } else
 
241
            ShotNum[i]=10;
 
242
        DrawShotNum(i);
 
243
    }
 
244
    Update();
 
245
    // Wait 2 seconds
 
246
    WaitWithMouse(2000-1000*ArcadeMode);
 
247
    Blit(0,0,BackBuffer);
 
248
    for (i=0;i<3;i++) DrawShotNum(i);
 
249
    SDL_SetEventFilter(GameEvents);
 
250
}
 
251
 
 
252
void WaveScoring()
 
253
{
 
254
    int x,i,OldScore;
 
255
 
 
256
    UndrawMouse();
 
257
    SDL_SetEventFilter(NormalEvents);
 
258
    DrawCenteredText(140,"Wave Scoring");
 
259
    x=400-(ShotNum[0]+ShotNum[1]+ShotNum[2])*4;
 
260
//    printf("1: %d\t2: %d\t3: %d\tE: %d\t\n",ShotNum[0],ShotNum[1],ShotNum[2],x);
 
261
    OldScore=Score;
 
262
    if (!ArcadeMode)
 
263
    for (i=0;i<3;i++) {
 
264
        while(ShotNum[i]>0) {
 
265
            Blit(x,200,ShotPic);
 
266
            x+=8;
 
267
            AddScore(10);
 
268
            DrawShotNum(i);
 
269
            sprintf(text,"  %d  ", Score-OldScore);
 
270
            DrawCenteredText(235, text);
 
271
            Update();
 
272
            WaitWithMouse(70);
 
273
            ShotNum[i]--;
 
274
        }
 
275
        DrawShotNum(i);
 
276
    }
 
277
    x=400;
 
278
    OldScore=Score;
 
279
    for (i=0;i<6;i++)
 
280
        if (!CityDestroyed[i]) x-=50;
 
281
    for (i=0;i<6;i++)
 
282
        if (!CityDestroyed[i]) {
 
283
            Blit(x,285-ArcadeMode*100,CityPic);
 
284
            x+=100;
 
285
            AddScore(80);
 
286
            sprintf(text,"  %d  ", Score-OldScore);
 
287
            DrawCenteredText(380-ArcadeMode*100, text);
 
288
            Update();
 
289
            WaitWithMouse(400-ArcadeMode*100);
 
290
        }
 
291
    WaitWithMouse(3000-ArcadeMode*1500);
 
292
    Now=SDL_GetTicks();
 
293
//    sprintf(text,"Starting Wave #%d",WaveNum+1);
 
294
//    PutString(Screen, 400-TextWidth(text)/2, 300, text);
 
295
}
 
296
 
 
297
void ProcessEvents()
 
298
{
 
299
    int i,Rand,b;
 
300
 
 
301
    SDL_PumpEvents();
 
302
    if (HighDetail) UndrawShot2(); else UndrawShot();
 
303
    UndrawFlyer();
 
304
    UndrawMissile();
 
305
    UndrawExplosion();
 
306
    UndrawBomb();
 
307
    if (!CannonDestroyed[0]) UndrawCannon(40,510);
 
308
    if (!CannonDestroyed[1]) UndrawCannon(400,510);
 
309
    if (!CannonDestroyed[2]) UndrawCannon(760,510);
 
310
    UndrawMouse();
 
311
    HandleMissile();
 
312
    if (HighDetail) HandleShot2(); else HandleShot();
 
313
    HandleFlyer();
 
314
    HandleExplosion(1);
 
315
    HandleBomb();
 
316
    HandleCursor();
 
317
    DrawMissile();
 
318
    if (HighDetail) DrawShot2(); else DrawShot();
 
319
    DrawBomb();
 
320
    DrawFlyer();
 
321
    DrawExplosion();
 
322
    if (!CannonDestroyed[0]) DrawCannon(40,510);
 
323
    if (!CannonDestroyed[1]) DrawCannon(400,510);
 
324
    if (!CannonDestroyed[2]) DrawCannon(760,510);
 
325
#ifdef JOYSTICK
 
326
    JoystickMove();
 
327
#endif
 
328
    DrawMouse();
 
329
    // Are all cities destroyed?
 
330
    if (!endgame) {
 
331
        endgame=1;
 
332
        for (i=0;i<6;i++)
 
333
            if (CityDestroyed[i]==0)
 
334
                endgame=0;
 
335
    }
 
336
    // New Missiles
 
337
    if ((NewMissile<0)&&(Wave[WaveNum].MissilesToDestroy>MissilesCreated)) {
 
338
        Rand=abrand(0,99);
 
339
        if ((Rand<Wave[WaveNum].MirvChance)&&(MissileCount>0)) {
 
340
            i=abrand(0,MissileCount-1);
 
341
            b=0;
 
342
            while ((Split(i))&&(b<100)) {
 
343
                i=abrand(0,MissileCount-1);
 
344
                b++;
 
345
            }
 
346
        } else if (Rand>99-Wave[WaveNum].FlyerChance) {
 
347
            CreateFlyer();
 
348
        } else if (Rand>99-Wave[WaveNum].BombChance-Wave[WaveNum].FlyerChance) {
 
349
            CreateBomb();
 
350
        } else
 
351
            CreateMissile(abrand(100,700),0);
 
352
        NewMissile+=Wave[WaveNum].TimeBetweenShots;
 
353
        MissilesCreated++;
 
354
    }
 
355
    NewMissile-=Speed;
 
356
    // New Wave
 
357
    if (
 
358
        (MissilesCreated==Wave[WaveNum].MissilesToDestroy)
 
359
        &&(MissileCount==0)
 
360
        &&(ExplosionCount==0)
 
361
        &&(BombCount==0)
 
362
        &&(FlyerCount==0)
 
363
        &&(ShotCount==0)
 
364
        ) {
 
365
        WaveScoring();
 
366
        WaveNum++;
 
367
        StartWave();
 
368
    }    
 
369
}
 
370
 
 
371
void ShowRules()
 
372
{
 
373
    int i;
 
374
 
 
375
    Blit(0,0,BackPic);
 
376
    for (i=0;i<6;i++)
 
377
        if (i<3)
 
378
            Blit(70+i*100,490,CityPic);
 
379
        else
 
380
            Blit(130+i*100,490,CityPic);
 
381
    
 
382
    XCenteredString(Screen,50,"< Arcade Mode >");
 
383
    PutString(Screen,10,160,"-No extra points for remaining shots.");
 
384
    PutString(Screen,10,200,"-More cities result in faster shot regeneration.");
 
385
    PutString(Screen,10,240,"-One city is regenerated per wave.");
 
386
    PutString(Screen,10,280,"-The game is more difficult.");
 
387
    XCenteredString(Screen,400,"click to continue");
 
388
    SDL_BlitSurface(Screen, NULL, BackBuffer, NULL);
 
389
    Update();
 
390
    while (SDL_GetMouseState(NULL,NULL)==SDL_BUTTON(1)) {
 
391
        SDL_PollEvent(&event);
 
392
        SDL_Delay(10);
 
393
    }
 
394
    while ((event.type!=SDL_KEYDOWN)&&(event.type!=SDL_MOUSEBUTTONDOWN)) {
 
395
        SDL_PollEvent(&event);
 
396
        UndrawMouse();
 
397
        DrawMouse();
 
398
        GetSpeed();
 
399
        Update();
 
400
    }
 
401
}
 
402
 
 
403
void StartGame()
 
404
{
 
405
    int i;
 
406
    
 
407
    InitWaves();
 
408
    for (i=0;i<6;i++) CityDestroyed[i]=0;
 
409
    for (i=0;i<3;i++) CannonDestroyed[i]=0;
 
410
    ShotCount=0;
 
411
    MissileCount=0;
 
412
    ExplosionCount=0;
 
413
    BombCount=0;
 
414
    FlyerCount=0;
 
415
    WaveNum=StartLevel;
 
416
    Score=0;
 
417
    NewCity=0;
 
418
    MissilesCreated=0;
 
419
 
 
420
    if (ArcadeMode) ShowRules();
 
421
    SDL_SetEventFilter(GameEvents);
 
422
    StartWave();
 
423
    Now=SDL_GetTicks();
 
424
    Flicker=1;endgame=0;
 
425
    while ((!endgame)&&(!Quit)) {
 
426
        FullUpdate();
 
427
        GetSpeed();
 
428
        SDL_PollEvent(&event);
 
429
        ProcessEvents();
 
430
        //SDL_Delay(60);
 
431
    }
 
432
    Mix_FadeOutMusic(4000);
 
433
    PlaySound(GameoverSound);
 
434
    i=0;
 
435
    if (!Quit) 
 
436
        while (i<2000) {
 
437
            Update();
 
438
            GetSpeed();
 
439
            ProcessEvents();
 
440
            i+=Speed;
 
441
        }
 
442
    Flicker=0;
 
443
    printf("FPS %f\n",FramesPerSecond);
 
444
    SDL_SetEventFilter(NormalEvents);
 
445
}
 
446