source: ttyvaders/trunk/src/main.c

Last change on this file was 3429, checked in by Sam Hocevar, 14 years ago

Spawn fewer bonus items, try to center sprites despite no longer
having handle information in frames, and disable non-working
weapons (the homing missile and the bomb).

  • Property svn:keywords set to Id
File size: 7.5 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 3429 2009-04-29 13:37:29Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15#include "config.h"
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <time.h>
21
22#include "common.h"
23
24static void start_game (game *);
25
26int main (int argc, char **argv)
27{
28    game *g = malloc(sizeof(game));
29    if(g == NULL)
30        exit(1);
31
32    srand(time(NULL));
33
34    g->cv = caca_create_canvas(80, 50);
35    if(!g->cv)
36        return 1;
37
38    g->dp = caca_create_display(g->cv);
39    if(!g->dp)
40        return 1;
41
42    caca_set_display_time(g->dp, 40000);
43
44    /* Initialize our program */
45    g->w = caca_get_canvas_width(g->cv);
46    g->h = caca_get_canvas_height(g->cv);
47
48intro(g);
49
50    /* Go ! */
51    start_game(g);
52
53    /* Clean up */
54    caca_free_display(g->dp);
55    caca_free_canvas(g->cv);
56
57    return 0;
58}
59
60static void start_game (game *g)
61{
62    int quit = 0;
63    int poz = 0;
64    int skip = 0;
65    int purcompteur = 0;
66
67    box *pausebox = NULL;
68
69    g->sf = create_starfield(g);
70    g->wp = malloc(sizeof(weapons));
71    if(g->wp == NULL)
72        exit(1);
73    g->ex = malloc(sizeof(explosions));
74    if(g->ex == NULL)
75        exit(1);
76    g->bo = malloc(sizeof(bonus));
77    if(g->bo == NULL)
78        exit(1);
79    g->t = create_tunnel(g, g->w, g->h);
80    g->p = create_player(g);
81    g->al = malloc(sizeof(aliens));
82    if(g->al == NULL)
83        exit(1);
84
85    init_bonus(g, g->bo);
86    init_weapons(g, g->wp);
87    init_explosions(g, g->ex);
88    init_aliens(g, g->al);
89
90    /* Temporary stuff */
91    g->t->w = 25;
92
93    while(!quit)
94    {
95        caca_event_t ev;
96
97        while(caca_get_event(g->dp, CACA_EVENT_KEY_PRESS, &ev, 0))
98        {
99            switch(ev.data.key.ch)
100            {
101            case 'q':
102                quit = 1;
103                break;
104            case 'p':
105                poz = !poz;
106                if(poz)
107                {
108                    pausebox = create_box(g, g->w / 2, g->h / 2,
109                                              g->w - 16, 8);
110                }
111                else
112                {
113                    free_box(pausebox);
114                }
115                break;
116            case '\t':
117                ceo_alert(g);
118                poz = 1;
119                break;
120            case 's':
121                skip = 1;
122                break;
123            default:
124                if(g->p->dead)
125                {
126                    break;
127                }
128
129                switch(ev.data.key.ch)
130                {
131                case 'h':
132                    g->p->vx = -2;
133                    break;
134                case 'j':
135                    if(g->p->y < g->h - 3) g->p->y += 1;
136                    break;
137                case 'k':
138                    if(g->p->y > 2) g->p->y -= 1;
139                    break;
140                case 'l':
141                    g->p->vx = 2;
142                    break;
143                case 'n':
144                    if(g->p->special >= COST_NUKE)
145                    {
146                        g->p->special -= COST_NUKE;
147                        add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, 0, WEAPON_NUKE);
148                    }
149                    break;
150                case 'f':
151                    if(g->p->special >= COST_FRAGBOMB)
152                    {
153                        g->p->special -= COST_FRAGBOMB;
154                        add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, -16, WEAPON_FRAGBOMB);
155                    }
156                    break;
157                case 'b':
158                    if(g->p->special >= COST_BEAM)
159                    {
160                        g->p->special -= COST_BEAM;
161                        add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, 0, WEAPON_BEAM);
162                    }
163                    break;
164                case ' ':
165                    if(g->p->weapon == 0)
166                    {
167                        g->p->weapon = 4;
168                        add_weapon(g, g->wp, (g->p->x - 2) << 4, g->p->y << 4, 0, -32, WEAPON_LASER);
169                        add_weapon(g, g->wp, (g->p->x + 3) << 4, g->p->y << 4, 0, -32, WEAPON_LASER);
170                        /* Extra schtuph */
171                        //add_weapon(g, g->wp, (g->p->x - 2) << 4, g->p->y << 4, -24, -16, WEAPON_SEEKER);
172                        //add_weapon(g, g->wp, (g->p->x + 3) << 4, g->p->y << 4, 24, -16, WEAPON_SEEKER);
173                        /* More schtuph */
174                        add_weapon(g, g->wp, (g->p->x - 1) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
175                        add_weapon(g, g->wp, (g->p->x + 2) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
176                        /* Even more schtuph */
177                        add_weapon(g, g->wp, g->p->x << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
178                        add_weapon(g, g->wp, (g->p->x + 1) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
179                        /* Extra schtuph */
180                        //add_weapon(g, g->wp, (g->p->x - 2) << 4, g->p->y << 4, -32, -32, WEAPON_SEEKER);
181                        //add_weapon(g, g->wp, (g->p->x + 3) << 4, g->p->y << 4, 32, -32, WEAPON_SEEKER);
182                        /* MORE SCHTUPH! */
183                        //add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, -16, WEAPON_BOMB);
184                    }
185                    break;
186                }
187            }
188        }
189
190        if(!poz || skip)
191        {
192            skip = 0;
193
194            /* XXX: to be removed */
195            if(caca_rand(0, 9) == 0)
196            {
197                int list[3] = { ALIEN_FOO, ALIEN_BAR, ALIEN_BAZ };
198
199                add_alien(g, g->al, 0, rand() % g->h / 2, list[caca_rand(0,3)]);
200            }
201
202            /* Update game rules */
203            if(g->t->right[1] - g->t->left[1] == g->t->w)
204            {
205                g->t->w = 85 - g->t->w;
206            }
207
208            /* Scroll and update positions */
209            collide_player_tunnel(g, g->p, g->t, g->ex);
210            update_player(g, g->p);
211            collide_player_tunnel(g, g->p, g->t, g->ex);
212
213            update_starfield(g, g->sf);
214            update_bonus(g, g->bo);
215            update_aliens(g, g->al);
216
217            collide_weapons_tunnel(g, g->wp, g->t, g->ex);
218            collide_weapons_aliens(g, g->wp, g->al, g->ex);
219            update_weapons(g, g->wp);
220            collide_weapons_tunnel(g, g->wp, g->t, g->ex);
221            collide_weapons_aliens(g, g->wp, g->al, g->ex);
222
223            update_explosions(g, g->ex);
224            update_tunnel(g, g->t);
225        }
226
227        /* Clear screen */
228        caca_clear_canvas(g->cv);
229
230        /* Print starfield, tunnel, aliens, player and explosions */
231        draw_starfield(g, g->sf);
232        draw_aliens(g, g->al);
233        draw_tunnel(g, g->t);
234        draw_bonus(g, g->bo);
235        draw_explosions(g, g->ex);
236        draw_weapons(g, g->wp);
237        draw_player(g, g->p);
238        draw_status(g);
239
240        /* Print pause box if needed */
241        if(poz)
242        {
243            pausebox->frame++;
244            draw_box(g, pausebox);
245        }
246
247        /* Refresh */
248        caca_refresh_display(g->dp);
249
250        purcompteur++;
251    }
252
253    if(pausebox)
254    {
255        free_box(pausebox);
256    }
257
258    free_starfield(g, g->sf);
259    free_tunnel(g->t);
260    free_player(g->p);
261}
262
Note: See TracBrowser for help on using the repository browser.