1 /* Tower Toppler - Nebulus
2  * Copyright (C) 2000-2012  Andreas R�ver
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18 
19 #include "stars.h"
20 
21 #include "decl.h"
22 #include "sprites.h"
23 #include "screen.h"
24 
25 #include "SDL.h"
26 #include "stdlib.h"
27 
28 #define starstep 5
29 
30 typedef struct {
31   long x, y;
32   int state;
33   int size;
34 } _star;
35 
36 static unsigned short star_spr_nr;
37 static int num_stars;
38 static _star *stars = (_star *)0;
39 
sts_draw(void)40 void sts_draw(void)
41 {
42   for (int t = 0; t < num_stars; t++)
43     scr_blit(objectsprites.data((long)star_spr_nr + stars[t].size - (stars[t].state != 0)), stars[t].x, stars[t].y);
44 }
45 
sts_init(int sn,int nstar)46 void sts_init(int sn, int nstar) {
47   if (stars) {
48     if (nstar <= num_stars) {
49       star_spr_nr = sn;
50       num_stars = nstar;
51       return;
52     } else sts_done();
53   }
54   assert_msg(nstar > 1, "sts_init with too few stars!");
55 
56   stars = new _star[nstar];
57   assert_msg(stars, "Failed to alloc memory!");
58   num_stars = nstar;
59 
60   for (int t = 0; t < num_stars; t++) {
61     stars[t].x = rand() / (RAND_MAX / SCREENWID) - SPR_STARWID;
62     stars[t].y = rand() / (RAND_MAX / SCREENHEI) - SPR_STARHEI;
63     stars[t].state = 0;
64     stars[t].size = rand() / (RAND_MAX / 7);
65   }
66 
67   star_spr_nr = sn;
68 }
69 
sts_done(void)70 void sts_done(void)
71 {
72   if (stars) delete [] stars;
73   num_stars = 0;
74   stars = 0;
75 }
76 
sts_blink(void)77 void sts_blink(void)
78 {
79   for (int t = 0; t < num_stars; t++) {
80     if (stars[t].state > 0) stars[t].state = (stars[t].state + 1) % 4;
81     else if (!(rand() & 0xff)) stars[t].state++;
82   }
83 }
84 
sts_move(long x,long y)85 void sts_move(long x, long y)
86 {
87   int t;
88 
89   for (t = 0; t < num_stars; t++) {
90     stars[t].x += starstep * x;
91     stars[t].y += y;
92     if (stars[t].x > SCREENWID) {
93       stars[t].x = rand() / (RAND_MAX / starstep) - SPR_STARWID;
94       stars[t].y = rand() / (RAND_MAX / SCREENHEI);
95     } else {
96       if (stars[t].x < -SPR_STARWID) {
97         stars[t].x = SCREENWID - rand() / (RAND_MAX / starstep);
98         stars[t].y = rand() / (RAND_MAX / SCREENHEI);
99       }
100     }
101     if (stars[t].y > SCREENHEI) {
102       stars[t].y = -SPR_STARHEI;
103       stars[t].x = rand() / (RAND_MAX / (SCREENWID + SPR_STARWID)) - SPR_STARWID;
104     } else {
105       if (stars[t].y < -SPR_STARHEI) {
106         stars[t].y = SCREENHEI;
107         stars[t].x = rand() / (RAND_MAX / (SCREENWID + SPR_STARWID)) - SPR_STARWID;
108       }
109     }
110   }
111 }
112 
113