1 | /* |
---|
2 | * ttyvaders Textmode shoot'em up |
---|
3 | * Copyright (c) 2002 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: starfield.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 <stdlib.h> |
---|
18 | |
---|
19 | #include "common.h" |
---|
20 | |
---|
21 | starfield * create_starfield(game *g) |
---|
22 | { |
---|
23 | int i; |
---|
24 | starfield *s; |
---|
25 | |
---|
26 | s = malloc(STARS * sizeof(starfield)); |
---|
27 | if(s == NULL) |
---|
28 | exit(1); |
---|
29 | |
---|
30 | for(i = 0; i < STARS; i++) |
---|
31 | { |
---|
32 | s[i].x = caca_rand(0, g->w - 1); |
---|
33 | s[i].y = caca_rand(0, g->h - 1); |
---|
34 | s[i].z = caca_rand(1, 3); |
---|
35 | s[i].c = caca_rand(0, 1) ? CACA_COLOR_LIGHTGRAY : CACA_COLOR_DARKGRAY; |
---|
36 | s[i].ch = caca_rand(0, 1) ? '.' : '\''; |
---|
37 | } |
---|
38 | |
---|
39 | return s; |
---|
40 | } |
---|
41 | |
---|
42 | void draw_starfield(game *g, starfield *s) |
---|
43 | { |
---|
44 | int i; |
---|
45 | |
---|
46 | for(i = 0; i < STARS; i++) |
---|
47 | { |
---|
48 | if(s[i].x >= 0) |
---|
49 | { |
---|
50 | caca_set_color(g->cv, s[i].c, CACA_COLOR_BLACK); |
---|
51 | caca_putchar(g->cv, s[i].x, s[i].y, s[i].ch); |
---|
52 | } |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | void update_starfield(game *g, starfield *s) |
---|
57 | { |
---|
58 | int i; |
---|
59 | |
---|
60 | for(i = 0; i < STARS; i++) |
---|
61 | { |
---|
62 | if(s[i].x < 0) |
---|
63 | { |
---|
64 | s[i].x = caca_rand(0, g->w - 1); |
---|
65 | s[i].y = 0; |
---|
66 | s[i].z = caca_rand(1, 2); |
---|
67 | s[i].c = caca_rand(0, 1) ? CACA_COLOR_LIGHTGRAY : CACA_COLOR_DARKGRAY; |
---|
68 | s[i].ch = caca_rand(0, 1) ? '.' : '\''; |
---|
69 | } |
---|
70 | else if(s[i].y < g->h-1) |
---|
71 | { |
---|
72 | s[i].y += s[i].z; |
---|
73 | } |
---|
74 | else |
---|
75 | { |
---|
76 | s[i].x = -1; |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | void free_starfield(game *g, starfield *s) |
---|
82 | { |
---|
83 | free(s); |
---|
84 | } |
---|
85 | |
---|