source: libcaca/trunk/src/main.c @ 153

Last change on this file since 153 was 153, checked in by Sam Hocevar, 20 years ago
  • libee/graphics.c: + Renamed ee_color() to ee_set_color(), wrote ee_get_color().
  • libee/line.c: + Implemented draw_polyline() and draw_thin_polyline().
  • libee/sprite.c: + Removed the f member of struct ee_sprite. + Implemented ee_get_sprite_{width|height|dx|dy}(). + Restore the color fater ee_draw_sprite() is called.
  • libee/box.c: + Fixed a bug causing improper box clipping at the right and the bottom.
  • data/foo_fighter: + Fixed bugs in the sprite.
  • src/intro.c: + Test effects for the future game's intro.
  • test/spritedit.c: + Added stuff to the sprite editor. We can now navigate through frames.
  • Property svn:keywords set to Id
File size: 7.4 KB
Line 
1/*
2 *   ttyvaders     Textmode shoot'em up
3 *   Copyright (c) 2002 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: main.c 153 2003-11-12 01:48:58Z sam $
7 *
8 *   This program is free software; you can redistribute it and/or modify
9 *   it under the terms of the GNU General Public License as published by
10 *   the Free Software Foundation; either version 2 of the License, or
11 *   (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include "config.h"
24
25#include <stdio.h>
26#include <stdlib.h>
27
28#include <string.h>
29
30#include <time.h>
31
32#include "common.h"
33
34static void start_game (game *);
35
36int main (int argc, char **argv)
37{
38    game *g = malloc(sizeof(game));
39
40    srand(time(NULL));
41
42    if(ee_init())
43    {
44        return 1;
45    }
46
47    /* Initialize our program */
48    g->w = ee_get_width();
49    g->h = ee_get_height();
50
51intro();
52
53    /* Go ! */
54    start_game(g);
55
56    /* Clean up */
57    ee_end();
58
59    return 0;
60}
61
62static void start_game (game *g)
63{
64    int quit = 0;
65    int poz = 0;
66    int skip = 0;
67    int purcompteur = 0;
68
69    box *pausebox = NULL;
70
71    g->sf = create_starfield(g);
72    g->wp = malloc(sizeof(weapons));
73    g->ex = malloc(sizeof(explosions));
74    g->bo = malloc(sizeof(bonus));
75    g->t = create_tunnel(g, g->w, g->h);
76    g->p = create_player(g);
77    g->al = malloc(sizeof(aliens));
78
79    init_bonus(g, g->bo);
80    init_weapons(g, g->wp);
81    init_explosions(g, g->ex);
82    init_aliens(g, g->al);
83
84    /* Temporary stuff */
85    g->t->w = 25;
86
87    while(!quit)
88    {
89        char key;
90
91        while((key = ee_get_key()))
92        {
93            switch(key)
94            {
95            case 'q':
96                quit = 1;
97                break;
98            case 'p':
99                poz = !poz;
100                if(poz)
101                {
102                    pausebox = create_box(g, g->w / 2, g->h / 2,
103                                              g->w - 16, 8);
104                }
105                else
106                {
107                    free_box(pausebox);
108                }
109                break;
110            case '\t':
111                ceo_alert(g);
112                poz = 1;
113                break;
114            case 's':
115                skip = 1;
116                break;
117            default:
118                if(g->p->dead)
119                {
120                    break;
121                }
122
123                switch(key)
124                {
125                case 'h':
126                    g->p->vx = -2;
127                    break;
128                case 'j':
129                    if(g->p->y < g->h - 3) g->p->y += 1;
130                    break;
131                case 'k':
132                    if(g->p->y > 2) g->p->y -= 1;
133                    break;
134                case 'l':
135                    g->p->vx = 2;
136                    break;
137                case 'n':
138                    if(g->p->special >= COST_NUKE)
139                    {
140                        g->p->special -= COST_NUKE;
141                        add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, 0, WEAPON_NUKE);
142                    }
143                    break;
144                case 'f':
145                    if(g->p->special >= COST_FRAGBOMB)
146                    {
147                        g->p->special -= COST_FRAGBOMB;
148                        add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, -16, WEAPON_FRAGBOMB);
149                    }
150                    break;
151                case 'b':
152                    if(g->p->special >= COST_BEAM)
153                    {
154                        g->p->special -= COST_BEAM;
155                        add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, 0, WEAPON_BEAM);
156                    }
157                    break;
158                case ' ':
159                    if(g->p->weapon == 0)
160                    {
161                        g->p->weapon = 4;
162                        add_weapon(g, g->wp, (g->p->x - 2) << 4, g->p->y << 4, 0, -32, WEAPON_LASER);
163                        add_weapon(g, g->wp, (g->p->x + 3) << 4, g->p->y << 4, 0, -32, WEAPON_LASER);
164                        /* Extra schtuph */
165                        add_weapon(g, g->wp, (g->p->x - 2) << 4, g->p->y << 4, -24, -16, WEAPON_SEEKER);
166                        add_weapon(g, g->wp, (g->p->x + 3) << 4, g->p->y << 4, 24, -16, WEAPON_SEEKER);
167                        /* More schtuph */
168                        add_weapon(g, g->wp, (g->p->x - 1) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
169                        add_weapon(g, g->wp, (g->p->x + 2) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
170                        /* Even more schtuph */
171                        add_weapon(g, g->wp, g->p->x << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
172                        add_weapon(g, g->wp, (g->p->x + 1) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
173                        /* Extra schtuph */
174                        add_weapon(g, g->wp, (g->p->x - 2) << 4, g->p->y << 4, -32, 0, WEAPON_SEEKER);
175                        add_weapon(g, g->wp, (g->p->x + 3) << 4, g->p->y << 4, 32, 0, WEAPON_SEEKER);
176                        /* MORE SCHTUPH! */
177                        add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, -16, WEAPON_BOMB);
178                    }
179                    break;
180                }
181            }
182        }
183
184        if(!poz || skip)
185        {
186            skip = 0;
187
188            /* XXX: to be removed */
189            if(ee_rand(0, 9) == 0)
190            {
191                int list[3] = { ALIEN_FOO, ALIEN_BAR, ALIEN_BAZ };
192
193                add_alien(g, g->al, 0, rand() % g->h / 2, list[ee_rand(0,2)]);
194            }
195
196            /* Update game rules */
197            if(g->t->right[1] - g->t->left[1] == g->t->w)
198            {
199                g->t->w = 85 - g->t->w;
200            }
201
202            /* Scroll and update positions */
203            collide_player_tunnel(g, g->p, g->t, g->ex);
204            update_player(g, g->p);
205            collide_player_tunnel(g, g->p, g->t, g->ex);
206
207            update_starfield(g, g->sf);
208            update_bonus(g, g->bo);
209            update_aliens(g, g->al);
210
211            collide_weapons_tunnel(g, g->wp, g->t, g->ex);
212            collide_weapons_aliens(g, g->wp, g->al, g->ex);
213            update_weapons(g, g->wp);
214            collide_weapons_tunnel(g, g->wp, g->t, g->ex);
215            collide_weapons_aliens(g, g->wp, g->al, g->ex);
216
217            update_explosions(g, g->ex);
218            update_tunnel(g, g->t);
219        }
220
221        /* Clear screen */
222        ee_clear();
223
224        /* Print starfield, tunnel, aliens, player and explosions */
225        draw_starfield(g, g->sf);
226        draw_aliens(g, g->al);
227        draw_tunnel(g, g->t);
228        draw_bonus(g, g->bo);
229        draw_explosions(g, g->ex);
230        draw_weapons(g, g->wp);
231        draw_player(g, g->p);
232        draw_status(g);
233
234        /* Print pause box if needed */
235        if(poz)
236        {
237            pausebox->frame++;
238            draw_box(g, pausebox);
239        }
240
241        /* Refresh */
242        ee_refresh();
243
244        purcompteur++;
245    }
246
247    if(pausebox)
248    {
249        free_box(pausebox);
250    }
251
252    free_starfield(g, g->sf);
253    free_tunnel(g->t);
254    free_player(g->p);
255}
256
Note: See TracBrowser for help on using the repository browser.