/* stars.c - Starfield particle module - Don Yang (uguu.org) v1.01 (9/9/98) 08/30/98: InitStars UninitStars GetStarParticle GetAllStarParticles TranslateStars 09/09/98: MorphStars GetStarParticle deleted */ /********************************* Header **********************************/ /* Includes */ #include #include #include #include"particle.h" #include"stars.h" /* Globals */ static int _far *StarX; static int _far *StarY; static int _far *StarZ; /*************************** GetAllStarParticles *************************** Copy all particle coordinates. * * IN: (global) (StarX[], StarY[], StarZ[]) = source. * (particle.c) NumberOfParticles = number of particles. * OUT: (particle.c) (ParticleX[], ParticleY[], ParticleZ[]) = target. */ void GetAllStarParticles(void) { _fmemcpy(ParticleX, StarX, NumberOfParticles * sizeof(int)); _fmemcpy(ParticleY, StarY, NumberOfParticles * sizeof(int)); _fmemcpy(ParticleZ, StarZ, NumberOfParticles * sizeof(int)); } /* GetAllStarParticles() */ /******************************** InitStars ******************************** Initialize particles. Particles are distributed randomly in predefined * rectangular space. * * IN: (particles.c) NumberOfParticles = number of stars. * OUT: 0 = success. * (global) (StarX[], StarY[], StarZ[]) = particle coordinates. * 1 = not enough memory. */ int InitStars(void) { int i; /* Allocate memory */ StarX = _fcalloc((size_t)NumberOfParticles, sizeof(int)); StarY = _fcalloc((size_t)NumberOfParticles, sizeof(int)); StarZ = _fcalloc((size_t)NumberOfParticles, sizeof(int)); if( StarX == NULL || StarY == NULL || StarZ == NULL ) { _ffree(StarX); _ffree(StarY); _ffree(StarZ); return 1; } /* Generate coordinates */ for(i = 0; i < NumberOfParticles; i++) { StarX[i] = Generate(rand(), SPACE_W); /* (particle.h) */ StarY[i] = Generate(rand(), SPACE_H); StarZ[i] = Generate(rand(), SPACE_L); } return 0; } /* InitStars() */ /******************************** MorphStars ******************************* Blend particle systems. * * IN: frame = current morphing animation frame. * target = end animation frame. * (global) (StarX[], StarY[], StarZ[]) = target. * (particle.c) NumberOfParticles = number of particles. * (particle.c) (ParticleX[], ParticleY[], ParticleZ[]) = source. * OUT: (particle.c) (ParticleX[], ParticleY[], ParticleZ[]) updated. */ void MorphStars(int frame, int target) { int i; for(i = 0; i < NumberOfParticles; i++) { ParticleX[i] = Morph(ParticleX[i], StarX[i], frame, target); ParticleY[i] = Morph(ParticleY[i], StarY[i], frame, target); ParticleZ[i] = Morph(ParticleZ[i], StarZ[i], frame, target); } /* (particle.h) */ } /* MorphStars() */ /***************************** TranslateStars ****************************** Move star particles. * * IN: (particle.c) NumberOfParticles = number of particles. * OUT: (global) (StarX[], StarY[], StarZ[]) = star coordinates. */ void TranslateStars(void) { static int dx = 0, dy = 0, dz = -STAR_VELOCITY; static int lon = 180, lat = 270, dlon = 0, dlat = 0; static int frame = 0, target = STAR_PHASE_M, phase = STAR_PHASE_M; int i, r; /* Move particles */ for(i = 0; i < NumberOfParticles; i++) { StarX[i] += dx; StarY[i] += dy; StarZ[i] += dz; StarX[i] = WrapMin(StarX[i], SPACE_W); /* (particle.h) */ StarX[i] = WrapMax(StarX[i], SPACE_W); StarY[i] = WrapMin(StarY[i], SPACE_H); StarY[i] = WrapMax(StarY[i], SPACE_H); StarZ[i] = WrapMin(StarZ[i], SPACE_L); StarZ[i] = WrapMax(StarZ[i], SPACE_L); } /* Calculate new velocity */ if( phase == STAR_PHASE_M ) { frame++; if( frame == target ) { i = rand() % 3; if( i == 0 ) dlon = 0; if( i == 1 ) dlon = 1; if( i == 2 ) dlon = 359; i = rand() % 3; if( i == 0 ) dlat = 0; if( i == 1 ) dlat = 1; if( i == 2 ) dlat = 359; phase = STAR_PHASE_T; frame = 0; target = rand() % STAR_PHASE_E + STAR_PHASE_T; } } else { frame++; lon += dlon; lat += dlat; if( lon >= 360 ) lon -= 360; if( lat >= 360 ) lat -= 360; r = (int)(STAR_VELOCITY * Cosine(lat) / SINE_SCALE); /* (particle.h) */ dx = (int)(r * Cosine(lon) / SINE_SCALE); dy = (int)(r * Sine(lon) / SINE_SCALE); dz = (int)(STAR_VELOCITY * Sine(lat) / SINE_SCALE); if( frame == target ) { phase = STAR_PHASE_M; frame = 0; target = rand() % STAR_PHASE_E + STAR_PHASE_M; } } } /* TranslateStars() */ /******************************* UninitStars ******************************* Uninitialize (reverses InitStars). * * IN: (global) (StarX[], StarY[], StarZ[]) = star coordinates. * OUT: None. */ void UninitStars(void) { _ffree(StarX); _ffree(StarY); _ffree(StarZ); } /* UninitStars() */