source: ttyvaders/trunk/src/ttyvaders.c @ 1057

Last change on this file since 1057 was 1057, checked in by Sam Hocevar, 14 years ago
  • Bring the tree back in sync with the new libcaca API.
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1/*
2 *   ttyvaders     Textmode shoot'em up
3 *   Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: ttyvaders.c 1057 2006-09-18 16:54:08Z 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
27#include <caca.h>
28
29cucul_canvas_t *cv;
30caca_display_t *dp;
31
32cucul_canvas_t *ship, *alien;
33
34unsigned int shipx, shipy;
35
36unsigned int frame, w, h;
37int ground[80];
38
39static void run_game(void);
40
41int main(int argc, char **argv)
42{
43    cucul_buffer_t *b;
44
45    cv = cucul_create_canvas(80, 24);
46    if(!cv)
47        return 1;
48
49    dp = caca_create_display(cv);
50    if(!dp)
51        return 1;
52
53    caca_set_display_time(dp, 80000);
54
55    /* Initialize our program */
56    w = cucul_get_canvas_width(cv);
57    h = cucul_get_canvas_height(cv);
58
59    /* Load data */
60    b = cucul_load_file("data/ship.ans");
61    ship = cucul_import_canvas(b, "");
62    cucul_free_buffer(b);
63    b = cucul_load_file("data/alien.ans");
64    alien = cucul_import_canvas(b, "");
65    cucul_free_buffer(b);
66
67    /* Go ! */
68    run_game();
69
70    /* Clean up */
71    caca_free_display(dp);
72    cucul_free_canvas(cv);
73
74    return 0;
75}
76
77static void update_ground(void)
78{
79    int i;
80
81    for(i = 0; i < 80; i++)
82        ground[i] = ground[i + 1];
83
84    ground[80] = ground[79];
85
86    if(frame % 3)
87        return;
88
89    ground[80] += cucul_rand(-1, 2);
90    if(ground[80] < 2)
91        ground[80] = 3;
92    else if(ground[80] > 7)
93        ground[80] = 7;
94}
95
96static void fill_ground(void)
97{
98    int i;
99
100    ground[80] = 5;
101    for(i = 0; i < 80; i++)
102        update_ground();
103}
104
105static void display_ground(void)
106{
107    unsigned int i, j;
108
109    for(i = 0; i < 80; i++)
110    {
111        /* Draw the sky */
112        cucul_set_color(cv, CUCUL_COLOR_LIGHTBLUE, CUCUL_COLOR_LIGHTCYAN);
113        for(j = h - 24; j < h - 18 + ((40 - i) * (40 - i) / (40 * 40 / 10)) + (i & 1); j++)
114            cucul_putchar(cv, i, j, ' ');
115        cucul_putchar(cv, i, j++, 0x2591);
116        cucul_putchar(cv, i, j++, 0x2591);
117        cucul_putchar(cv, i, j++, 0x2591);
118        cucul_putchar(cv, i, j++, 0x2592);
119        cucul_putchar(cv, i, j++, 0x2592);
120        cucul_putchar(cv, i, j++, 0x2592);
121        cucul_putchar(cv, i, j++, 0x2593);
122        cucul_putchar(cv, i, j++, 0x2593);
123        cucul_putchar(cv, i, j++, 0x2593);
124        cucul_set_color(cv, CUCUL_COLOR_LIGHTBLUE, CUCUL_COLOR_LIGHTBLUE);
125        for( ; j < h; j++)
126            cucul_putchar(cv, i, j, ' ');
127
128        /* TODO: Draw the mountains */
129
130        /* Draw the ground */
131        j = h - ground[i];
132        cucul_set_color(cv, CUCUL_COLOR_BLACK, CUCUL_COLOR_LIGHTBLUE);
133        if(i >= 4 && ground[i] == ground[i - 6]
134                  && ground[i] != ground[i - 7])
135        {
136            cucul_putstr(cv, i - 3, j - 2,    "Omm");
137            cucul_putstr(cv, i - 6, j - 1, "(/)-(/)");
138        }
139        cucul_set_color(cv, CUCUL_COLOR_RED, CUCUL_COLOR_GREEN);
140        if(ground[i + 1] > ground[i])
141            cucul_putchar(cv, i, j++, 0x2588); // UTF-8: 0x259f
142        else if(ground[i + 1] < ground[i])
143        {
144            j++;
145            cucul_putchar(cv, i, j++, 0x2588); // UTF-8: 0x2599
146        }
147        else
148            cucul_putchar(cv, i, j++, 0x2584);
149        cucul_set_color(cv, CUCUL_COLOR_RED, CUCUL_COLOR_BROWN);
150        cucul_putchar(cv, i, j++, 0x2593);
151        cucul_putchar(cv, i, j++, 0x2592);
152        cucul_putchar(cv, i, j++, 0x2591);
153        for( ; j < h; j++)
154            cucul_putchar(cv, i, j, ' ');
155    }
156}
157
158static void display_stuff(void)
159{
160    cucul_blit(cv, shipx, shipy, ship, NULL);
161    cucul_blit(cv, 68, h - 22, alien, NULL);
162    cucul_blit(cv, 52, h - 16, alien, NULL);
163    cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
164    cucul_printf(cv, 2, h - 2, " %i fps ", 1000000 / (1 + caca_get_display_time(dp)));
165}
166
167static void run_game(void)
168{
169    fill_ground();
170    shipx = 5;
171    shipy = h - 20;
172
173    for(;;)
174    {
175        caca_event_t ev;
176
177        update_ground();
178        display_ground();
179        display_stuff();
180        caca_refresh_display(dp);
181        frame++;
182
183        while(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0))
184        {
185            switch(ev.data.key.ch)
186            {
187                case CACA_KEY_ESCAPE: goto end; break;
188                case CACA_KEY_UP: shipy -= 1; break;
189                case CACA_KEY_DOWN: shipy += 1; break;
190            }
191        }
192
193        continue; end: break;
194    }
195}
196
Note: See TracBrowser for help on using the repository browser.