~ubuntu-branches/ubuntu/saucy/dd2/saucy-proposed

« back to all changes in this revision

Viewing changes to src/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Gürkan Sengün
  • Date: 2006-04-23 14:52:00 UTC
  • Revision ID: james.westby@ubuntu.com-20060423145200-wr4obk2gvkcishho
Tags: upstream-0.2.1
Import upstream version 0.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
    Dodgin' Diamond 2, a shot'em up arcade
 
4
    Copyright (C) 2003,2004 Juan J. Martinez <jjm@usebox.net>
 
5
 
 
6
    This program is free software; you can redistribute it and/or modify
 
7
    it under the terms of the GNU General Public License Version 2 as
 
8
    published by the Free Software Foundation.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program; if not, write to the Free Software
 
17
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 
 
19
*/
 
20
#include<stdlib.h>
 
21
#include<stdio.h>
 
22
#include"main.h"
 
23
#include"SDL.h"
 
24
#include"SDL_mixer.h"
 
25
#include"menu.h"
 
26
 
 
27
#define APP_NAME        "Dodgin' Diamond ]["
 
28
 
 
29
#define FPS     60
 
30
 
 
31
#include "control.h"
 
32
#include "SDL_plus.h"
 
33
#include "engine.h"
 
34
#include "cfg.h"
 
35
 
 
36
#ifdef WIN32
 
37
static const char COPYRIGHT[]="Dodgin' Diamond 2 - Copyright (c) 2003,2004 Juan J. Martinez <jjm@usebox.net> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License Version 2 as published by the Free Software Foundation (www.fsf.org).";
 
38
#endif
 
39
 
 
40
SDL_Surface *screen, *gfx;
 
41
extern pDesc player[2];
 
42
SDL_Joystick *joy[2]={ NULL, NULL };
 
43
SDL_Event event;
 
44
Uint32 tick, ntick;
 
45
float scroll=0,scroll2=0;
 
46
 
 
47
bool pause;
 
48
Uint32 pause_tick;
 
49
 
 
50
extern bool boss;
 
51
 
 
52
cfg conf;
 
53
score hiscore[10];
 
54
 
 
55
Mix_Chunk *efx[8]={ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
 
56
Mix_Music *bgm=NULL, *bgm_boss=NULL;
 
57
int sound;
 
58
bool done;
 
59
 
 
60
/* load all the sound stuff */
 
61
void
 
62
soundLoad()
 
63
{
 
64
    int i;
 
65
    char buffer[512];
 
66
 
 
67
        sprintf(buffer,"%s/bgm1.xm",DD2_DATA);
 
68
        bgm=Mix_LoadMUS(buffer);
 
69
        if(!bgm)
 
70
                fprintf(stderr,"Unable load bgm: %s\n", SDL_GetError());
 
71
 
 
72
        sprintf(buffer,"%s/bgm2.xm",DD2_DATA);
 
73
        bgm_boss=Mix_LoadMUS(buffer);
 
74
        if(!bgm_boss)
 
75
                fprintf(stderr,"Unable load bgm_boss: %s\n", SDL_GetError());
 
76
 
 
77
        for(i=0;i<NUM_EFX;i++) {
 
78
                sprintf(buffer,"%s/efx%i.wav",DD2_DATA,i+1);
 
79
                efx[i]=Mix_LoadWAV(buffer);
 
80
                if(!efx[i]) {
 
81
                        fprintf(stderr,"Unable load efx: %s\n", SDL_GetError());
 
82
                } else
 
83
                        Mix_VolumeChunk(efx[i],MIX_MAX_VOLUME/2);
 
84
        }
 
85
 
 
86
}
 
87
 
 
88
void
 
89
gameLoop()
 
90
{
 
91
        int afterdeath=0;
 
92
 
 
93
        for(done=false,tick=SDL_GetTicks();!done && afterdeath<400;) {
 
94
 
 
95
                while(SDL_PollEvent(&event)) {
 
96
                        if (event.type==SDL_QUIT)
 
97
                                done=true;
 
98
 
 
99
                        /* joystick control for the menu */
 
100
                        if(player[0].joy && joy[0])
 
101
                        {
 
102
                                SDL_JoystickUpdate();
 
103
 
 
104
                                if(SDL_JoystickGetButton(joy[0], 1))
 
105
                                {
 
106
                                        event.type=SDL_KEYDOWN;
 
107
                                        event.key.keysym.sym=SDLK_p;
 
108
                                }
 
109
                        }
 
110
                        else
 
111
                                if(player[1].joy && joy[1])
 
112
                                {
 
113
                                        SDL_JoystickUpdate();
 
114
 
 
115
                                        if(SDL_JoystickGetButton(joy[1], 1))
 
116
                                        {
 
117
                                                event.type=SDL_KEYDOWN;
 
118
                                                event.key.keysym.sym=SDLK_p;
 
119
                                        }
 
120
                                }
 
121
 
 
122
                        if(event.type==SDL_KEYDOWN) {
 
123
                                if(event.key.keysym.sym==SDLK_ESCAPE) {
 
124
                                        done=true;
 
125
                                        continue;
 
126
                                }
 
127
                                else
 
128
                                {
 
129
                                        if(event.key.keysym.sym==SDLK_p && pause_tick<SDL_GetTicks())
 
130
                                        {
 
131
                                                writeCString(gfx, screen, 98, 20, "game paused", 0);
 
132
                                                SDL_Flip(screen);
 
133
                                                pause=pause ? false : true;
 
134
                                                pause_tick=SDL_GetTicks()+200;
 
135
                                                continue;
 
136
                                        }
 
137
                                        else
 
138
                                                if(event.key.keysym.sym==SDLK_F12)
 
139
                                                        SDL_SaveBMP(screen,"scnshot.bmp");
 
140
                                }
 
141
                        }
 
142
                }
 
143
 
 
144
                /* player control */
 
145
                if(player[0].shield) {
 
146
                        if(joy[0] && player[0].joy)
 
147
                                control_player_joy(joy[0],&player[0]);
 
148
                        else
 
149
                                control_player(&player[0]);
 
150
                }
 
151
 
 
152
                if(player[1].shield) {
 
153
                        if(joy[1] && player[1].joy)
 
154
                                control_player_joy(joy[1],&player[1]);
 
155
                        else
 
156
                                control_player(&player[1]);
 
157
                }
 
158
 
 
159
                if(pause)
 
160
                        continue;
 
161
                
 
162
                /* frame rate calculation */
 
163
                ntick=SDL_GetTicks();
 
164
                if(ntick-tick>=1000/FPS) {
 
165
                        tick=ntick;
 
166
 
 
167
                                /* scroll here */
 
168
                        {
 
169
                                SDL_Rect a,b;
 
170
 
 
171
                                if(scroll>0)
 
172
                                        scroll-=0.5;
 
173
                                else
 
174
                                        scroll=200;
 
175
 
 
176
                                b.x=1;
 
177
                                b.w=SCREENW;
 
178
                                a.x=0;
 
179
 
 
180
                                if(!scroll) {
 
181
                                        a.y=0;
 
182
                                        b.y=204;
 
183
                                        b.h=SCREENH;
 
184
                                        SDL_BlitSurface(gfx, &b, screen, &a);
 
185
                                } else {
 
186
                                        a.y=0;
 
187
                                        b.y=204+(int)scroll;
 
188
                                        b.h=SCREENH-(int)scroll;
 
189
                                        SDL_BlitSurface(gfx, &b, screen, &a);
 
190
                                        a.y=SCREENH-(int)scroll;
 
191
                                        b.y=204;
 
192
                                        b.h=(int)scroll;
 
193
                                        SDL_BlitSurface(gfx, &b, screen, &a);
 
194
                                }
 
195
 
 
196
                                /* scroll parallax here */
 
197
 
 
198
                                if(scroll2>0)
 
199
                                        scroll2-=2;
 
200
                                else
 
201
                                        scroll2=200;
 
202
 
 
203
                                b.x=324;
 
204
                                b.w=25;
 
205
                                a.x=0;
 
206
 
 
207
                                if(!scroll2) {
 
208
                                        a.y=0;
 
209
                                        b.y=204;
 
210
                                        b.h=SCREENH;
 
211
                                        SDL_BlitSurface(gfx, &b, screen, &a);
 
212
 
 
213
                                        b.x=358;
 
214
                                        a.x=SCREENW-25;
 
215
                                        SDL_BlitSurface(gfx, &b, screen, &a);                           
 
216
                                } else {
 
217
                                        a.y=0;
 
218
                                        b.y=204+(int)scroll2;
 
219
                                        b.h=SCREENH-(int)scroll2;
 
220
                                        SDL_BlitSurface(gfx, &b, screen, &a);
 
221
                                        a.y=SCREENH-(int)scroll2;
 
222
                                        b.y=204;
 
223
                                        b.h=(int)scroll2;
 
224
                                        SDL_BlitSurface(gfx, &b, screen, &a);
 
225
 
 
226
                                        b.x=358;
 
227
                                        a.x=SCREENW-25;
 
228
                                        a.y=0;
 
229
                                        b.y=204+(int)scroll2;
 
230
                                        b.h=SCREENH-(int)scroll2;
 
231
                                        SDL_BlitSurface(gfx, &b, screen, &a);
 
232
                                        a.y=SCREENH-(int)scroll2;
 
233
                                        b.y=204;
 
234
                                        b.h=(int)scroll2;
 
235
                                        SDL_BlitSurface(gfx, &b, screen, &a);   
 
236
                                }
 
237
                        }
 
238
                        /* enemy here */
 
239
                        engine_enemy();
 
240
 
 
241
                        /* fire here */
 
242
                        engine_fire();
 
243
 
 
244
                        /* character here */
 
245
                        if(player[0].shield)
 
246
                                engine_player(&player[0]);
 
247
 
 
248
                        if(player[1].shield)
 
249
                                engine_player(&player[1]);
 
250
 
 
251
                        if(!(player[0].shield | player[1].shield))
 
252
                                afterdeath++;
 
253
                                
 
254
                        engine_obj();
 
255
 
 
256
                        engine_vefx();
 
257
 
 
258
                        /* panel */
 
259
                        drawPanel(gfx,screen,player);
 
260
 
 
261
                        SDL_Flip(screen);
 
262
                }
 
263
        }
 
264
}
 
265
 
 
266
int
 
267
main (int argc, char *argv[])
 
268
{
 
269
    int i,j,k;
 
270
    char buffer[512];
 
271
        SDL_VideoInfo *vi;
 
272
        unsigned char bpp=8;
 
273
 
 
274
#ifndef WIN32
 
275
        if(argc==2)
 
276
                if(argv[1][0]=='-' && argv[1][1]=='v') {
 
277
                        printf("%s v%s\nCopyright (c) 2003,2004 Juan J. Martinez <jjm@usebox.net>\n", PACKAGE, VERSION);
 
278
                        printf("This is free software, and you are welcome\nto redistribute it"
 
279
                   " under certain conditions; read COPYING for details.\n");
 
280
                        return 1;
 
281
                }
 
282
 
 
283
    /* try local configuration */
 
284
        sprintf(buffer,"%.500s/.dd2rc",getenv("HOME"));
 
285
        if(!loadCFG(buffer,&conf)) {
 
286
                /* if there's no local, use global */
 
287
                sprintf(buffer,"%s/dd2.cfg",DD2_DATA);
 
288
                if(!loadCFG(buffer,&conf))
 
289
                        fprintf(stderr,"unable to read configuration, using defaults\n");
 
290
        }
 
291
#else
 
292
        sprintf(buffer,"%s/dd2.cfg",DD2_DATA);
 
293
        if(!loadCFG(buffer,&conf))
 
294
                fprintf(stderr,"unable to read configuration, using defaults\n");
 
295
#endif
 
296
 
 
297
        /* read hi-scores */
 
298
        sprintf(buffer,"%s/dd2-hiscore",DD2_DATA);
 
299
        if(!loadScore(buffer,hiscore))
 
300
                fprintf(stderr,"unable to read hi-scores, using defaults\n");
 
301
 
 
302
        /* don't init sound if it's not needed */
 
303
        if(conf.sound!=NO_SOUND)
 
304
                i=SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK);
 
305
        else
 
306
                i=SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK);
 
307
        if(i<0) {
 
308
                fprintf(stderr,"Unable to init SDL: %s\n", SDL_GetError());
 
309
                return 1;
 
310
        }
 
311
        atexit(SDL_Quit);
 
312
 
 
313
        sound=SDL_WasInit(SDL_INIT_AUDIO) & SDL_INIT_AUDIO;
 
314
 
 
315
        /* no sound, 16000, 22050, 44100 */
 
316
        if(sound && conf.sound!=NO_SOUND) {
 
317
                switch(conf.sound) {
 
318
                        default:
 
319
                        case SOUND_HI:
 
320
                                i=44100;
 
321
                        break;
 
322
                        case SOUND_MED:
 
323
                                i=22050;
 
324
                        break;
 
325
                        case SOUND_LOW:
 
326
                                i=16000;
 
327
                        break;
 
328
                }
 
329
                if(Mix_OpenAudio(i, MIX_DEFAULT_FORMAT, 2, 2048)<0) {
 
330
                        fprintf(stderr, "Unable to set audio: %s\n", SDL_GetError());
 
331
                        sound=0;
 
332
                } else
 
333
                        soundLoad();
 
334
        }
 
335
 
 
336
        vi=(SDL_VideoInfo *)SDL_GetVideoInfo();
 
337
        if(vi)
 
338
                bpp=vi->vfmt->BitsPerPixel;
 
339
 
 
340
        if(conf.fullscreen)
 
341
                screen=SDL_SetVideoMode(SCREENW, SCREENH, bpp, SDL_HWSURFACE|SDL_HWPALETTE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
 
342
        else
 
343
                screen=SDL_SetVideoMode(SCREENW, SCREENH, bpp, SDL_HWSURFACE|SDL_HWPALETTE|SDL_DOUBLEBUF);
 
344
 
 
345
        if(!screen) {
 
346
                fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
 
347
                return 1;
 
348
        }
 
349
 
 
350
        /* init the joystick */
 
351
        if(SDL_WasInit(SDL_INIT_JOYSTICK) & SDL_INIT_JOYSTICK)
 
352
                if(SDL_NumJoysticks()>=1)
 
353
                {
 
354
                        joy[0]=SDL_JoystickOpen(0);
 
355
                        if(SDL_NumJoysticks()>1)
 
356
                                joy[1]=SDL_JoystickOpen(1);
 
357
                }
 
358
 
 
359
        /* hide the mouse */
 
360
        SDL_ShowCursor(SDL_DISABLE);
 
361
 
 
362
        /* set the caption */
 
363
        SDL_WM_SetCaption(APP_NAME,NULL);
 
364
 
 
365
        /* load console gfx */
 
366
        sprintf(buffer,"%s/gfx.bmp",DD2_DATA);
 
367
        gfx=loadBMP(buffer);
 
368
        if(!gfx) {
 
369
                fprintf(stderr,"Unable load gfx: %s\n", SDL_GetError());
 
370
                return 1;
 
371
        }
 
372
        /* set transparent color */
 
373
        if(SDL_SetColorKey(gfx, SDL_SRCCOLORKEY, SDL_MapRGB(gfx->format, 255, 0, 255))<0) {
 
374
                        fprintf(stderr,"Unable to setup gfx: %s\n", SDL_GetError());
 
375
                        return 1;
 
376
        }
 
377
 
 
378
        /* main LOOP */
 
379
        while(menu()) {
 
380
 
 
381
                /* init the engine */
 
382
                engine_init();
 
383
 
 
384
                SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
 
385
                SDL_Flip(screen);
 
386
 
 
387
                if(sound && bgm) {
 
388
                        Mix_VolumeMusic(MIX_MAX_VOLUME);
 
389
                        Mix_FadeInMusic(bgm,-1,2000);
 
390
                        SDL_Delay(2000);
 
391
                }
 
392
 
 
393
                player[0].joy=(int)conf.control[0]==JOYSTICK;
 
394
                player[1].joy=(int)conf.control[1]==JOYSTICK;
 
395
 
 
396
                pause=0;
 
397
                pause_tick=0;
 
398
                boss=0;
 
399
                gameLoop();
 
400
 
 
401
                if(sound && bgm) {
 
402
                        Mix_FadeOutMusic(2000);
 
403
                        SDL_Delay(3000);
 
404
                }
 
405
 
 
406
                for(i=0;i<2;i++) {
 
407
                        /* check if there's a place for this score */
 
408
                        for(j=9;j>=0 && hiscore[j].score<player[i].score;j--);
 
409
 
 
410
                        /* the player will be in the hall of fame? */
 
411
                        if(j<9) {
 
412
                                for(k=8;k>j;k--)
 
413
                                        hiscore[k+1]=hiscore[k];
 
414
 
 
415
                                /* put the new score */
 
416
                                hiscore[j+1].score=player[i].score;
 
417
                                hiscore[j+1].stage=player[i].stage;
 
418
 
 
419
                                hiscore[j+1].name[0]=0;
 
420
                                if(!getName(hiscore[j+1].name, j+2,i+1))
 
421
                                        break; /* probably a problem if the user closes the window */
 
422
 
 
423
                                /* show the hall of fame */
 
424
                                hiscores();
 
425
                        }
 
426
                }
 
427
        }
 
428
 
 
429
        if(sound) {
 
430
                if(bgm)
 
431
                        Mix_FreeMusic(bgm);
 
432
                if(bgm_boss)
 
433
                        Mix_FreeMusic(bgm_boss);
 
434
 
 
435
                for(i=0;i<NUM_EFX;i++)
 
436
                        if(efx[i])
 
437
                                Mix_FreeChunk(efx[i]);
 
438
 
 
439
                Mix_CloseAudio();
 
440
        }
 
441
 
 
442
        /* release the joystick */
 
443
        if(SDL_JoystickOpened(0))
 
444
                SDL_JoystickClose(joy[0]);
 
445
        if(SDL_JoystickOpened(1))
 
446
                SDL_JoystickClose(joy[1]);
 
447
 
 
448
        /* free all! */
 
449
        SDL_FreeSurface(gfx);
 
450
 
 
451
        /* free engine memory */
 
452
        engine_release();
 
453
 
 
454
        /* now update conf changes */
 
455
#ifdef WIN32
 
456
        /* update cnf file */
 
457
        sprintf(buffer,"%s/dd2.cfg",DD2_DATA);
 
458
#else
 
459
        /* save conf into local */
 
460
        sprintf(buffer,"%.500s/.dd2rc",getenv("HOME"));
 
461
#endif
 
462
        saveCFG(buffer,&conf);
 
463
 
 
464
        /* save hi-scores */
 
465
        sprintf(buffer,"%s/dd2-hiscore",DD2_DATA);
 
466
        if(!saveScore(buffer,hiscore))
 
467
                fprintf(stderr,"unable to save hi-scores\ndo you have permissions to write into %s?\n"
 
468
                        ,buffer);
 
469
 
 
470
        return 0;
 
471
}