source: ttyvaders/trunk/src/starfield.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: 1.8 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: starfield.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 <stdlib.h>
18
19#include "common.h"
20
21starfield * 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 = cucul_rand(0, g->w - 1);
33        s[i].y = cucul_rand(0, g->h - 1);
34        s[i].z = cucul_rand(1, 3);
35        s[i].c = cucul_rand(0, 1) ? CUCUL_COLOR_LIGHTGRAY : CUCUL_COLOR_DARKGRAY;
36        s[i].ch = cucul_rand(0, 1) ? '.' : '\'';
37    }
38
39    return s;
40}
41
42void 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            cucul_set_color(g->cv, s[i].c, CUCUL_COLOR_BLACK);
51            cucul_putchar(g->cv, s[i].x, s[i].y, s[i].ch);
52        }
53    }
54}
55
56void 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 = cucul_rand(0, g->w - 1);
65            s[i].y = 0;
66            s[i].z = cucul_rand(1, 2);
67            s[i].c = cucul_rand(0, 1) ? CUCUL_COLOR_LIGHTGRAY : CUCUL_COLOR_DARKGRAY;
68            s[i].ch = cucul_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
81void free_starfield(game *g, starfield *s)
82{
83    free(s);
84}
85
Note: See TracBrowser for help on using the repository browser.