5140f99d4e02e4331ce29f499d340c00e39040d2
[dosdemo] / src / hairball.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "demo.h"
5 #include "3dgfx.h"
6 #include "vmath.h"
7 #include "screen.h"
8 #include "util.h"
9 #include "cfgopt.h"
10
11
12 #ifdef MSDOS
13 #include "dos/gfx.h"    /* for wait_vsync assembly macro */
14 #else
15 void wait_vsync(void);
16 #endif
17
18 #define NSPAWNPOS       128
19 #define SPAWN_RATE      16.0f
20 #define GRAV            (-6.0f)
21 #define HAIR_LENGTH 32
22
23 struct particle {
24         vec3_t pos;
25         vec3_t vel;
26         int r, g, b, a;
27         int life;
28
29         struct particle *next;
30 };
31
32 struct hairball {
33         vec3_t pos;
34         quat_t rot;
35         float xform[16];
36
37         struct particle *plist[NSPAWNPOS];
38 };
39
40 static int init(void);
41 static void destroy(void);
42 static void start(long trans_time);
43 static void draw(void);
44 static void update_hairball(struct hairball *hb, float dt);
45 static void draw_hairball(struct hairball *hb);
46
47 static struct particle *palloc(void);
48 void pfree(struct particle *p);
49
50
51 static struct screen scr = {
52         "hairball",
53         init,
54         destroy,
55         start, 0,
56         draw
57 };
58
59 static long start_time;
60
61 static vec3_t *spawnpos, *spawndir;
62
63 static struct hairball hball;
64
65 struct screen *hairball_screen(void)
66 {
67         return &scr;
68 }
69
70 static int init(void)
71 {
72         int i;
73
74         hball.xform[0] = hball.xform[5] = hball.xform[10] = hball.xform[15] = 1.0f;
75         hball.rot.w = 1.0f;
76
77         if(!(spawnpos = malloc(NSPAWNPOS * sizeof *spawnpos)) ||
78                         !(spawndir = malloc(NSPAWNPOS * sizeof *spawndir))) {
79                 fprintf(stderr, "failed to allocate spawnpos/dir array\n");
80                 return -1;
81         }
82
83         for(i=0; i<NSPAWNPOS; i++) {
84                 spawnpos[i] = spawndir[i] = sphrand(1.0f);
85         }
86
87         return 0;
88 }
89
90 static void destroy(void)
91 {
92 }
93
94 static void start(long trans_time)
95 {
96         g3d_matrix_mode(G3D_PROJECTION);
97         g3d_load_identity();
98         g3d_perspective(50.0, 1.33333, 0.5, 100.0);
99
100         start_time = time_msec;
101 }
102
103 static void update(void)
104 {
105         static float prev_mx, prev_my;
106         static long prev_msec;
107         int mouse_dx, mouse_dy;
108         long msec = time_msec - start_time;
109         float dt = (msec - prev_msec) / 1000.0f;
110         prev_msec = msec;
111
112         mouse_dx = mouse_x - prev_mx;
113         mouse_dy = mouse_y - prev_my;
114         prev_mx = mouse_x;
115         prev_my = mouse_y;
116
117         if(mouse_bmask) {
118                 hball.pos.x += mouse_dx * 0.05;
119                 hball.pos.y -= mouse_dy * 0.05;
120         }
121
122         //hball.rot = quat_rotate(hball.rot, dt * 2.0f, 0, 1, 0);
123
124         quat_to_mat(hball.xform, hball.rot);
125         hball.xform[12] = hball.pos.x;
126         hball.xform[13] = hball.pos.y;
127         hball.xform[14] = hball.pos.z;
128
129         update_hairball(&hball, dt);
130 }
131
132 static void draw(void)
133 {
134         unsigned long msec;
135         static unsigned long last_swap;
136
137         update();
138
139         memset(fb_pixels, 0, fb_width * fb_height * 2);
140
141         g3d_matrix_mode(G3D_MODELVIEW);
142         g3d_load_identity();
143         g3d_translate(0, 0, -8);
144
145         draw_hairball(&hball);
146
147         msec = get_msec();
148         if(msec - last_swap < 16) {
149                 wait_vsync();
150         }
151         if(!opt.vsync) {
152                 wait_vsync();
153         }
154         swap_buffers(fb_pixels);
155         last_swap = get_msec();
156 }
157
158 static void update_hairball(struct hairball *hb, float dt)
159 {
160         int i, j, spawn_count;
161         struct particle pdummy, *prev, *p;
162         static float spawn_acc;
163
164         /* update particles */
165         for(i=0; i<NSPAWNPOS; i++) {
166                 pdummy.next = hb->plist[i];
167                 prev = &pdummy;
168                 while(prev && prev->next) {
169                         p = prev->next;
170                         if(--p->life <= 0) {
171                                 prev->next = p->next;
172                                 pfree(p);
173                                 prev = prev->next;
174                                 continue;
175                         }
176
177                         p->pos.x += p->vel.x * dt;
178                         p->pos.y += p->vel.y * dt;
179                         p->pos.z += p->vel.z * dt;
180                         p->vel.y += GRAV * dt;
181
182                         p->r = p->g = p->b = (p->life << 8) / HAIR_LENGTH - 1;
183
184                         prev = p;
185                 }
186
187                 hb->plist[i] = pdummy.next;
188         }
189
190         /* spawn new particles */
191         spawn_acc += dt;
192         if(spawn_acc >= (1.0f / SPAWN_RATE)) {
193                 for(i=0; i<NSPAWNPOS; i++) {
194                         struct particle *p = palloc();
195                         float *mat = hb->xform;
196                         vec3_t pos = spawnpos[i];
197
198                         p->pos.x = mat[0] * pos.x + mat[4] * pos.y + mat[8] * pos.z + mat[12];
199                         p->pos.y = mat[1] * pos.x + mat[5] * pos.y + mat[9] * pos.z + mat[13];
200                         p->pos.z = mat[2] * pos.x + mat[6] * pos.y + mat[10] * pos.z + mat[14];
201
202                         p->vel = spawndir[i];
203                         p->life = HAIR_LENGTH;
204
205                         p->next = hb->plist[i];
206                         hb->plist[i] = p;
207                 }
208                 spawn_acc -= 1.0f / SPAWN_RATE;
209         }
210 }
211
212 static void draw_hairball(struct hairball *hb)
213 {
214         int i, j, col;
215         struct particle *p;
216         vec3_t prevpos;
217
218         g3d_begin(G3D_LINES);
219         for(i=0; i<NSPAWNPOS; i++) {
220                 p = hb->plist[i];
221                 prevpos.x = hb->xform[0] * spawnpos[i].x + hb->xform[4] * spawnpos[i].y + hb->xform[8] * spawnpos[i].z + hb->xform[12];
222                 prevpos.y = hb->xform[1] * spawnpos[i].x + hb->xform[5] * spawnpos[i].y + hb->xform[9] * spawnpos[i].z + hb->xform[13];
223                 prevpos.z = hb->xform[2] * spawnpos[i].x + hb->xform[6] * spawnpos[i].y + hb->xform[10] * spawnpos[i].z + hb->xform[14];
224
225                 j = 0;
226                 while(p) {
227                         g3d_color3b(p->r, p->g, p->b);
228                         g3d_vertex(prevpos.x, prevpos.y, prevpos.z);
229                         g3d_vertex(p->pos.x, p->pos.y, p->pos.z);
230                         prevpos = p->pos;
231                         p = p->next;
232                 }
233         }
234         g3d_end();
235 }
236
237
238 static struct particle *palloc(void)
239 {
240         return malloc(sizeof(struct particle));
241 }
242
243 void pfree(struct particle *p)
244 {
245         free(p);
246 }