backported build fixes and warnings cleanup from dos
[dosdemo] / src / scr / hairball.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <float.h>
5 #include "demo.h"
6 #include "3dgfx.h"
7 #include "vmath.h"
8 #include "screen.h"
9 #include "util.h"
10 #include "cfgopt.h"
11 #include "mesh.h"
12
13
14 #ifdef MSDOS
15 #include "dos/gfx.h"    /* for wait_vsync assembly macro */
16 #else
17 void wait_vsync(void);
18 #endif
19
20 #define NSPAWNPOS       64
21 #define SPAWN_RATE      16.0f
22 /*#define GRAV          (-6.0f)*/
23 #define GRAV            0.0f
24 #define HAIR_LENGTH 20
25
26 struct particle {
27         vec3_t pos;
28         vec3_t vel;
29         int r, g, b, a;
30         int life;
31
32         struct particle *next;
33 };
34
35 struct hairball {
36         vec3_t pos;
37         quat_t rot;
38         float xform[16];
39
40         struct particle *plist[NSPAWNPOS];
41 };
42
43 static int init(void);
44 static void destroy(void);
45 static void start(long trans_time);
46 static void draw(void);
47 static void update_hairball(struct hairball *hb, float dt);
48 static void draw_hairball(struct hairball *hb);
49
50 static struct particle *palloc(void);
51 void pfree(struct particle *p);
52
53
54 static struct screen scr = {
55         "hairball",
56         init,
57         destroy,
58         start, 0,
59         draw
60 };
61
62 static long start_time;
63
64 static float cam_theta, cam_phi = 15;
65 static float cam_dist = 8;
66
67 static vec3_t *spawnpos, *spawndir;
68 static struct hairball hball;
69 static struct g3d_mesh sphmesh;
70
71 struct screen *hairball_screen(void)
72 {
73         return &scr;
74 }
75
76 static int init(void)
77 {
78         int i, j, numpt = 0;
79
80         gen_sphere_mesh(&sphmesh, 1.0f, 12, 6);
81
82         hball.xform[0] = hball.xform[5] = hball.xform[10] = hball.xform[15] = 1.0f;
83         hball.rot.w = 1.0f;
84
85         if(!(spawnpos = malloc(NSPAWNPOS * sizeof *spawnpos)) ||
86                         !(spawndir = malloc(NSPAWNPOS * sizeof *spawndir))) {
87                 fprintf(stderr, "failed to allocate spawnpos/dir array\n");
88                 return -1;
89         }
90
91         while(numpt < NSPAWNPOS) {
92                 float best_dist = -1.0f;
93                 for(i=0; i<100; i++) {
94                         vec3_t pos = sphrand(1.0f);
95
96                         float mindist = FLT_MAX;
97                         for(j=0; j<numpt; j++) {
98                                 float dx = pos.x - spawnpos[i].x;
99                                 float dy = pos.y - spawnpos[i].y;
100                                 float dz = pos.z - spawnpos[i].z;
101                                 float dsq = dx * dx + dy * dy + dz * dz;
102                                 if(dsq < mindist) {
103                                         mindist = dsq;
104                                 }
105                         }
106
107                         if(mindist > best_dist) {
108                                 spawnpos[numpt] = pos;
109                                 best_dist = mindist;
110                         }
111                 }
112
113                 spawndir[numpt] = spawnpos[numpt];
114                 ++numpt;
115         }
116
117         return 0;
118 }
119
120 static void destroy(void)
121 {
122 }
123
124 static void start(long trans_time)
125 {
126         g3d_matrix_mode(G3D_PROJECTION);
127         g3d_load_identity();
128         g3d_perspective(50.0, 1.33333, 0.5, 100.0);
129
130         start_time = time_msec;
131 }
132
133 static void update(void)
134 {
135         static float prev_mx, prev_my;
136         static long prev_msec;
137         int mouse_dx, mouse_dy;
138         long msec = time_msec - start_time;
139         float dt = (msec - prev_msec) / 1000.0f;
140         prev_msec = msec;
141
142         mouse_dx = mouse_x - prev_mx;
143         mouse_dy = mouse_y - prev_my;
144         prev_mx = mouse_x;
145         prev_my = mouse_y;
146
147
148         if(opt.sball) {
149                 memcpy(hball.xform, sball_matrix, 16 * sizeof(float));
150
151                 hball.pos.x = hball.xform[12];
152                 hball.pos.y = hball.xform[13];
153                 hball.pos.z = hball.xform[14];
154         } else {
155                 if(mouse_bmask & MOUSE_BN_MIDDLE) {
156                         hball.pos.x += mouse_dx * 0.05;
157                         hball.pos.y -= mouse_dy * 0.05;
158                 }
159
160                 quat_to_mat(hball.xform, hball.rot);
161                 hball.xform[12] = hball.pos.x;
162                 hball.xform[13] = hball.pos.y;
163                 hball.xform[14] = hball.pos.z;
164         }
165
166         update_hairball(&hball, dt);
167
168         mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
169 }
170
171 static void draw(void)
172 {
173         unsigned long msec;
174         static unsigned long last_swap;
175
176         update();
177
178         memset(fb_pixels, 0, fb_width * fb_height * 2);
179
180         g3d_matrix_mode(G3D_MODELVIEW);
181         g3d_load_identity();
182         g3d_translate(0, 0, -cam_dist);
183         g3d_rotate(cam_phi, 1, 0, 0);
184         g3d_rotate(cam_theta, 0, 1, 0);
185
186         draw_hairball(&hball);
187
188         msec = get_msec();
189         if(msec - last_swap < 16) {
190                 wait_vsync();
191         }
192         if(!opt.vsync) {
193                 wait_vsync();
194         }
195         swap_buffers(fb_pixels);
196         last_swap = get_msec();
197 }
198
199 static void update_hairball(struct hairball *hb, float dt)
200 {
201         int i, j, spawn_count;
202         struct particle pdummy, *prev, *p;
203         static float spawn_acc;
204
205         /* update particles */
206         for(i=0; i<NSPAWNPOS; i++) {
207                 pdummy.next = hb->plist[i];
208                 prev = &pdummy;
209                 while(prev && prev->next) {
210                         p = prev->next;
211                         if(--p->life <= 0) {
212                                 prev->next = p->next;
213                                 pfree(p);
214                                 prev = prev->next;
215                                 continue;
216                         }
217
218                         p->pos.x += p->vel.x * dt;
219                         p->pos.y += p->vel.y * dt;
220                         p->pos.z += p->vel.z * dt;
221                         p->vel.y += GRAV * dt;
222
223                         p->r = p->g = p->b = (p->life << 8) / HAIR_LENGTH - 1;
224
225                         prev = p;
226                 }
227
228                 hb->plist[i] = pdummy.next;
229         }
230
231         /* spawn new particles */
232         spawn_acc += dt;
233         if(spawn_acc >= (1.0f / SPAWN_RATE)) {
234                 for(i=0; i<NSPAWNPOS; i++) {
235                         struct particle *p = palloc();
236                         float *mat = hb->xform;
237                         vec3_t pos = spawnpos[i];
238                         vec3_t dir = spawndir[i];
239
240                         p->pos.x = mat[0] * pos.x + mat[4] * pos.y + mat[8] * pos.z + mat[12];
241                         p->pos.y = mat[1] * pos.x + mat[5] * pos.y + mat[9] * pos.z + mat[13];
242                         p->pos.z = mat[2] * pos.x + mat[6] * pos.y + mat[10] * pos.z + mat[14];
243
244                         p->vel.x = mat[0] * dir.x + mat[4] * dir.y + mat[8] * dir.z;
245                         p->vel.y = mat[1] * dir.x + mat[5] * dir.y + mat[9] * dir.z;
246                         p->vel.z = mat[2] * dir.x + mat[6] * dir.y + mat[10] * dir.z;
247                         p->life = HAIR_LENGTH;
248
249                         p->next = hb->plist[i];
250                         hb->plist[i] = p;
251                 }
252                 spawn_acc -= 1.0f / SPAWN_RATE;
253         }
254 }
255
256 static void draw_hairball(struct hairball *hb)
257 {
258         int i, col;
259         struct particle *p;
260         vec3_t prevpos;
261         vec3_t start[NSPAWNPOS];
262
263         for(i=0; i<NSPAWNPOS; i++) {
264                 start[i].x = hb->xform[0] * spawnpos[i].x + hb->xform[4] * spawnpos[i].y + hb->xform[8] * spawnpos[i].z + hb->xform[12];
265                 start[i].y = hb->xform[1] * spawnpos[i].x + hb->xform[5] * spawnpos[i].y + hb->xform[9] * spawnpos[i].z + hb->xform[13];
266                 start[i].z = hb->xform[2] * spawnpos[i].x + hb->xform[6] * spawnpos[i].y + hb->xform[10] * spawnpos[i].z + hb->xform[14];
267         }
268
269         g3d_begin(G3D_LINES);
270         for(i=0; i<NSPAWNPOS; i++) {
271                 if(start[i].z >= hb->pos.z) continue;
272                 p = hb->plist[i];
273                 prevpos = start[i];
274                 while(p) {
275                         g3d_color3b(p->r, p->g, p->b);
276                         g3d_vertex(prevpos.x, prevpos.y, prevpos.z);
277                         g3d_vertex(p->pos.x, p->pos.y, p->pos.z);
278                         prevpos = p->pos;
279                         p = p->next;
280                 }
281         }
282         g3d_end();
283
284
285         g3d_push_matrix();
286         g3d_mult_matrix(hb->xform);
287         zsort_mesh(&sphmesh);
288         draw_mesh(&sphmesh);
289         g3d_pop_matrix();
290
291         g3d_begin(G3D_LINES);
292         for(i=0; i<NSPAWNPOS; i++) {
293                 if(start[i].z < hb->pos.z) continue;
294                 p = hb->plist[i];
295                 prevpos = start[i];
296                 while(p) {
297                         g3d_color3b(p->r, p->g, p->b);
298                         g3d_vertex(prevpos.x, prevpos.y, prevpos.z);
299                         g3d_vertex(p->pos.x, p->pos.y, p->pos.z);
300                         prevpos = p->pos;
301                         p = p->next;
302                 }
303         }
304         g3d_end();
305
306 }
307
308
309 static struct particle *palloc(void)
310 {
311         return malloc(sizeof(struct particle));
312 }
313
314 void pfree(struct particle *p)
315 {
316         free(p);
317 }