-
Notifications
You must be signed in to change notification settings - Fork 0
/
starslib.c
148 lines (130 loc) · 3.44 KB
/
starslib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright 2016 Philip J Freeman <[email protected]>
*
* This file is part of stars.
*
* stars is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* stars is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with stars. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#ifdef DEBUG
#include <stdio.h>
#endif
#include "starslib.h"
struct points
{
int x, y, z;
struct points *next;
};
void new_universe( struct universe** u, int width, int height, int depth )
{
*u = malloc(sizeof(struct universe));
(*u)->width = width;
(*u)->height = height;
(*u)->depth = depth;
(*u)->iterator = NULL;
(*u)->points = NULL;
#ifdef DEBUG
printf("NEW UNIVERSE: %lx: (%i,%i,%i)\n", (long unsigned int )(*u), (*u)->width, (*u)->height, (*u)->depth);
#endif
return;
}
void new_point( struct universe* u )
{
struct points* p_ptr = malloc(sizeof(struct points));
p_ptr->x = (rand()%u->width - (u->width/2)) * u->depth;
p_ptr->y = (rand()%u->height - (u->height/2)) * u->depth;
p_ptr->z = u->depth;
p_ptr->next = u->points;
u->points = p_ptr;
#ifdef DEBUG
printf("NEW POINT: %lx: (%i,%i,%i) next=%lx\n", (long unsigned int )p_ptr, p_ptr->x, p_ptr->y, p_ptr->z, (long unsigned int) p_ptr->next);
#endif
return;
}
void kill_point( struct universe* universe, struct points* to_kill )
{
struct points *p_ptr, *last_ptr = NULL;
for ( p_ptr = universe->points; p_ptr != NULL; p_ptr = p_ptr->next)
{
if (p_ptr == to_kill)
{
#ifdef DEBUG
printf("KILL POINT: %lx: (%i,%i,%i).\n", (long unsigned int )p_ptr, p_ptr->x, p_ptr->y, p_ptr->z);
#endif
if (last_ptr == NULL)
{
universe->points = p_ptr->next;
} else {
last_ptr->next = p_ptr->next;
}
free(p_ptr);
} else {
last_ptr = p_ptr;
}
}
#ifdef DEBUG
printf("KILL POINT: %lx\n", (long unsigned int )p_ptr);
#endif
return;
}
int process_point( struct universe *u, struct return_point *rp )
{
if ( u->iterator == NULL ) {
if (u->points == NULL){
return 0;
} else {
u->iterator = u->points;
}
}
if ( u->iterator->z == 0 ){
// Delete point that has reached us.
struct points *tmp = u->iterator;
u->iterator = u->iterator->next;
kill_point( u, tmp );
return(-1);
} else {
// Plot the point
int x, y;
x = u->iterator->x / u->iterator->z;
y = u->iterator->y / u->iterator->z;
if ( abs(x) >= u->width/2 || abs(y) >= u->height/2 ){
// Delete point that is off screen
struct points *tmp = u->iterator;
u->iterator = u->iterator->next;
kill_point( u, tmp );
if ( u->iterator == NULL ) {
return(0);
} else {
return(-1);
}
} else {
int m = OPACITY_MAX*((u->depth-u->iterator->z)*4)/u->depth;
if ( m>OPACITY_MAX ){ m=OPACITY_MAX; }
u->iterator->z = u->iterator->z - 1;
#ifdef DEBUG
printf("RETURN POINT: %lx\n", (long unsigned int )u->iterator);
#endif
u->iterator = u->iterator->next;
rp->x = x;
rp->y = y;
rp->opacity = m;
if ( u->iterator == NULL ) {
return(0);
} else {
return(1);
}
}
}
}