JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
* dust.c (zero): correct lower bound for wrapping to avoid loss of precision.
[vor.git] / dust.c
1 #include <SDL.h>
2 #include <math.h>
3
4 #include "config.h"
5 #include "globals.h"
6 #include "dust.h"
7 #include "mt.h"
8
9 struct dust_mote {
10         float x,y,z;
11         Uint16 color;
12 };
13
14 struct dust_mote motes[N_DUST_MOTES];
15
16 // lower bound to ensure that we don't lose precision when wrapping.
17 static float zero;
18
19 void
20 init_dust(void)
21 {
22         int i, b;
23         for(i=0; i<N_DUST_MOTES; i++) {
24                 motes[i].x = frnd()*(XSIZE-5);
25                 motes[i].y = frnd()*(YSIZE-5);
26                 motes[i].z = MAX_DUST_DEPTH*sqrt(frnd());
27                 b = (MAX_DUST_DEPTH - motes[i].z) * 255.0 / MAX_DUST_DEPTH;
28                 motes[i].color = SDL_MapRGB(surf_screen->format, b, b, b);
29         }
30
31         zero = pow(-10, ceil(log10(XSIZE)) - FLT_DIG);
32 }
33
34 void
35 move_dust(float ticks)
36 {
37         int i;
38         float xscroll = screendx * ticks;
39         float yscroll = screendy * ticks;
40         
41         for(i=0; i<N_DUST_MOTES; i++) {
42                 motes[i].x -= xscroll / (1.3 + motes[i].z);
43                 if(motes[i].x >= XSIZE) motes[i].x -= XSIZE;
44                 else if(motes[i].x < zero) motes[i].x += XSIZE;
45
46                 motes[i].y -= yscroll / (1.3 + motes[i].z);
47                 if(motes[i].y >= YSIZE) motes[i].y -= YSIZE;
48                 else if(motes[i].y < zero) motes[i].y += YSIZE;
49         }
50 }
51
52 void
53 draw_dust(SDL_Surface *s)
54 {
55         int i;
56         uint16_t *pixels = s->pixels;
57         for(i=0; i<N_DUST_MOTES; i++) {
58                 pixels[s->pitch/2*(int)motes[i].y + (int)motes[i].x] = motes[i].color;
59         }
60 }