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

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