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 2990 2008-10-18 21:42:24Z 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 | |
---|
21 | caca_canvas_t *cv; |
---|
22 | caca_display_t *dp; |
---|
23 | |
---|
24 | caca_canvas_t *ship, *alien; |
---|
25 | |
---|
26 | unsigned int shipx, shipy; |
---|
27 | |
---|
28 | unsigned int frame, w, h; |
---|
29 | int ground[81]; |
---|
30 | |
---|
31 | static void run_game(void); |
---|
32 | |
---|
33 | int main(int argc, char **argv) |
---|
34 | { |
---|
35 | cv = caca_create_canvas(80, 24); |
---|
36 | if(!cv) |
---|
37 | return 1; |
---|
38 | |
---|
39 | dp = caca_create_display(cv); |
---|
40 | if(!dp) |
---|
41 | return 1; |
---|
42 | |
---|
43 | caca_set_display_time(dp, 80000); |
---|
44 | |
---|
45 | /* Initialize our program */ |
---|
46 | w = caca_get_canvas_width(cv); |
---|
47 | h = caca_get_canvas_height(cv); |
---|
48 | |
---|
49 | /* Load data */ |
---|
50 | ship = caca_create_canvas(0, 0); |
---|
51 | caca_import_file(ship, "data/ship.ans", ""); |
---|
52 | alien = caca_create_canvas(0, 0); |
---|
53 | caca_import_file(alien, "data/alien.ans", ""); |
---|
54 | |
---|
55 | /* Go ! */ |
---|
56 | run_game(); |
---|
57 | |
---|
58 | /* Clean up */ |
---|
59 | caca_free_display(dp); |
---|
60 | caca_free_canvas(cv); |
---|
61 | |
---|
62 | return 0; |
---|
63 | } |
---|
64 | |
---|
65 | static void update_ground(void) |
---|
66 | { |
---|
67 | int i; |
---|
68 | |
---|
69 | for(i = 0; i < 80; i++) |
---|
70 | ground[i] = ground[i + 1]; |
---|
71 | |
---|
72 | ground[80] = ground[79]; |
---|
73 | |
---|
74 | if(frame % 3) |
---|
75 | return; |
---|
76 | |
---|
77 | ground[80] += caca_rand(-1, 2); |
---|
78 | if(ground[80] < 2) |
---|
79 | ground[80] = 3; |
---|
80 | else if(ground[80] > 7) |
---|
81 | ground[80] = 7; |
---|
82 | } |
---|
83 | |
---|
84 | static void fill_ground(void) |
---|
85 | { |
---|
86 | int i; |
---|
87 | |
---|
88 | ground[80] = 5; |
---|
89 | for(i = 0; i < 80; i++) |
---|
90 | update_ground(); |
---|
91 | } |
---|
92 | |
---|
93 | static void display_ground(void) |
---|
94 | { |
---|
95 | unsigned int i, j; |
---|
96 | |
---|
97 | for(i = 0; i < 80; i++) |
---|
98 | { |
---|
99 | /* Draw the sky */ |
---|
100 | caca_set_color_ansi(cv, CACA_LIGHTBLUE, CACA_LIGHTCYAN); |
---|
101 | for(j = h - 24; j < h - 18 + ((40 - i) * (40 - i) / (40 * 40 / 10)) + (i & 1); j++) |
---|
102 | caca_put_char(cv, i, j, ' '); |
---|
103 | caca_put_char(cv, i, j++, 0x2591); |
---|
104 | caca_put_char(cv, i, j++, 0x2591); |
---|
105 | caca_put_char(cv, i, j++, 0x2591); |
---|
106 | caca_put_char(cv, i, j++, 0x2592); |
---|
107 | caca_put_char(cv, i, j++, 0x2592); |
---|
108 | caca_put_char(cv, i, j++, 0x2592); |
---|
109 | caca_put_char(cv, i, j++, 0x2593); |
---|
110 | caca_put_char(cv, i, j++, 0x2593); |
---|
111 | caca_put_char(cv, i, j++, 0x2593); |
---|
112 | caca_set_color_ansi(cv, CACA_LIGHTBLUE, CACA_LIGHTBLUE); |
---|
113 | for( ; j < h; j++) |
---|
114 | caca_put_char(cv, i, j, ' '); |
---|
115 | |
---|
116 | /* TODO: Draw the mountains */ |
---|
117 | |
---|
118 | /* Draw the ground */ |
---|
119 | j = h - ground[i]; |
---|
120 | caca_set_color_ansi(cv, CACA_BLACK, CACA_LIGHTBLUE); |
---|
121 | /* if(i >= 4 && ground[i] == ground[i - 6] |
---|
122 | && ground[i] != ground[i - 7]) |
---|
123 | { |
---|
124 | caca_putstr(cv, i - 3, j - 2, "Omm"); |
---|
125 | caca_putstr(cv, i - 6, j - 1, "(/)-(/)"); |
---|
126 | }*/ |
---|
127 | caca_set_color_ansi(cv, CACA_RED, CACA_GREEN); |
---|
128 | if(ground[i + 1] > ground[i]) |
---|
129 | caca_put_char(cv, i, j++, 0x2588); // UTF-8: 0x259f |
---|
130 | else if(ground[i + 1] < ground[i]) |
---|
131 | { |
---|
132 | j++; |
---|
133 | caca_put_char(cv, i, j++, 0x2588); // UTF-8: 0x2599 |
---|
134 | } |
---|
135 | else |
---|
136 | caca_put_char(cv, i, j++, 0x2584); |
---|
137 | caca_set_color_ansi(cv, CACA_RED, CACA_BROWN); |
---|
138 | caca_put_char(cv, i, j++, 0x2593); |
---|
139 | caca_put_char(cv, i, j++, 0x2592); |
---|
140 | caca_put_char(cv, i, j++, 0x2591); |
---|
141 | for( ; j < h; j++) |
---|
142 | caca_put_char(cv, i, j, ' '); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | static void display_stuff(void) |
---|
147 | { |
---|
148 | caca_blit(cv, shipx, shipy, ship, NULL); |
---|
149 | caca_blit(cv, 68, h - 22, alien, NULL); |
---|
150 | caca_blit(cv, 52, h - 16, alien, NULL); |
---|
151 | caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE); |
---|
152 | caca_printf(cv, 2, h - 2, " %i fps ", 1000000 / (1 + caca_get_display_time(dp))); |
---|
153 | } |
---|
154 | |
---|
155 | static void run_game(void) |
---|
156 | { |
---|
157 | fill_ground(); |
---|
158 | shipx = 5; |
---|
159 | shipy = h - 20; |
---|
160 | |
---|
161 | for(;;) |
---|
162 | { |
---|
163 | caca_event_t ev; |
---|
164 | |
---|
165 | update_ground(); |
---|
166 | display_ground(); |
---|
167 | display_stuff(); |
---|
168 | caca_refresh_display(dp); |
---|
169 | frame++; |
---|
170 | |
---|
171 | while(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0)) |
---|
172 | { |
---|
173 | switch(ev.data.key.ch) |
---|
174 | { |
---|
175 | case CACA_KEY_ESCAPE: goto end; break; |
---|
176 | case CACA_KEY_UP: shipy -= 1; break; |
---|
177 | case CACA_KEY_DOWN: shipy += 1; break; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | continue; end: break; |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|