Fix Galaxy: some starts were moving in the wrong direction.

Change-Id: Ic761ae7a61c5f6b87253bff22490444747efe18b
diff --git a/res/raw/galaxy.rs b/res/raw/galaxy.rs
index f1a6a77..8e7ac13 100644
--- a/res/raw/galaxy.rs
+++ b/res/raw/galaxy.rs
@@ -19,14 +19,95 @@
 
 #define ELLIPSE_RATIO 0.892f
 
+#define PI 3.1415f
+#define TWO_PI 6.283f
+#define ELLIPSE_TWIST 0.023333333f
+
 float angle;
 float distance;
 
+/**
+ * Script initialization. Called automatically.
+ */
 void init() {
     angle = 37.0f;
     distance = 0.55f;
 }
 
+/**
+ * Helper function to generate the stars.
+ */
+float randomGauss() {
+    float x1;
+    float x2;
+    float w = 2.f;
+
+    while (w >= 1.0f) {
+        x1 = 2.0f * randf2(0.0f, 1.0f) - 1.0f;
+        x2 = 2.0f * randf2(0.0f, 1.0f) - 1.0f;
+        w = x1 * x1 + x2 * x2;
+    }
+
+    w = sqrtf(-2.0 * logf(w) / w);
+    return x1 * w;
+}
+
+/**
+ * Generates the properties for a given star.
+ */
+void createParticle(struct Stars_s *star, struct Particles_s *part, float scale) {
+    float d = fabsf(randomGauss()) * State->galaxyRadius * 0.5f + randf(64.0f);
+    float id = d / State->galaxyRadius;
+    float z = randomGauss() * 0.4f * (1.0f - id);
+    float p = -d * ELLIPSE_TWIST;
+
+    if (d < State->galaxyRadius * 0.33f) {
+        part->r = (int) (220 + id * 35);
+        part->g = 220;
+        part->b = 220;
+    } else {
+        part->r= 180;
+        part->g = 180;
+        part->b = (int) clampf(140.f + id * 115.f, 140.f, 255.f);
+    }
+    part->a = (int) (140 + (1.0f - id) * 115);
+
+    if (d > State->galaxyRadius * 0.15f) {
+        z *= 0.6f * (1.0f - id);
+    } else {
+        z *= 0.72f;
+    }
+
+    // Map to the projection coordinates (viewport.x = -1.0 -> 1.0)
+    d = mapf(-4.0f, State->galaxyRadius + 4.0f, 0.0f, scale, d);
+
+    star->angle = randf(TWO_PI);
+    star->distance = d;
+    star->speed = randf2(0.0015f, 0.0025f) * (0.5f + (scale / d)) * 0.8f;
+    star->s = cosf(p);
+    star->t = sinf(p);
+
+    part->z = z / 5.0f;
+    part->pointSize = randf2(1.2f, 2.1f) * 6;
+}
+
+/**
+ * Initialize all the stars. Called from Java.
+ */
+void initParticles() {
+    struct Stars_s *star = Stars;
+    struct Particles_s *part = Particles;
+    int particlesCount = State->particlesCount;
+    float scale = State->galaxyRadius / (State->width * 0.5f);
+
+    int i;
+    for (i = 0; i < particlesCount; i ++) {
+        createParticle(star, part, scale);
+        star++;
+        part++;
+    }
+}
+
 void drawSpace(float xOffset, int width, int height) {
     bindTexture(NAMED_PFBackground, 0, NAMED_TSpace);
     drawQuadTexCoords(
@@ -71,8 +152,6 @@
     int radius = State->galaxyRadius;
     int particlesCount = State->particlesCount;
 
-    float w = xOffset;
-
     struct Stars_s *star = Stars;
     struct Particles_s *vtx = Particles;
 
@@ -82,9 +161,11 @@
         float x = star->distance * sinf(a);
         float y = star->distance * cosf(a) * ELLIPSE_RATIO;
 
-        vtx->x = star->t * x + star->s * y + w;
+        vtx->x = star->t * x + star->s * y + xOffset;
         vtx->y = star->s * x - star->t * y;
+
         star->angle = a;
+
         star++;
         vtx++;
     }
@@ -118,82 +199,3 @@
 
     return 1;
 }
-
-
-////////////////////////////////////////////////////
-
-float randomGauss()
-{
-    float x1;
-    float x2;
-    float w = 2.f;
-
-    while (w >= 1.0f) {
-        x1 = 2.0f * randf2(0.0f, 1.0f) - 1.0f;
-        x2 = 2.0f * randf2(0.0f, 1.0f) - 1.0f;
-        w = x1 * x1 + x2 * x2;
-    }
-
-    w = sqrtf(-2.0 * logf(w) / w);
-    return x1 * w;
-}
-
-float map(float minStart, float minStop, float maxStart, float maxStop, float value) {
-    return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
-}
-
-#define PI 3.14f
-#define ELLIPSE_TWIST 0.023333333f
-
-void createParticle(struct Stars_s *star, struct Particles_s *part, float scale)
-{
-    float d = absf(randomGauss()) * State->galaxyRadius * 0.5f + randf(64.0f);
-    float id = d / State->galaxyRadius;
-    float z = randomGauss() * 0.4f * (1.0f - id);
-    float p = -d * ELLIPSE_TWIST;
-
-    if (d < State->galaxyRadius * 0.33f) {
-        part->r = (int) (220 + id * 35);
-        part->g = 220;
-        part->b = 220;
-    } else {
-        part->r= 180;
-        part->g = 180;
-        part->b = (int) clampf(140.f + id * 115.f, 140.f, 255.f);
-    }
-    part->a = (int) (140 + (1.0f - id) * 115);
-
-    if (d > State->galaxyRadius * 0.15f) {
-        z *= 0.6f * (1.0f - id);
-    } else {
-        z *= 0.72f;
-    }
-
-    // Map to the projection coordinates (viewport.x = -1.0 -> 1.0)
-    d = map(-4.0f, State->galaxyRadius + 4.0f, 0.0f, scale, d);
-
-    star->angle = randf2(0.0f, (float) (PI * 2.0));
-    star->distance = d;
-    star->speed = randf2(0.0015f, 0.0025f) * (0.5f + (scale / d)) * 0.8f;
-    star->s = cosf(p);
-    star->t = sinf(p);
-
-    part->z = z / 5.0f;
-    part->pointSize = randf2(1.2f, 2.1f) * 6;
-}
-
-void initParticles() {
-    struct Stars_s *star = Stars;
-    struct Particles_s *part = Particles;
-    int particlesCount = State->particlesCount;
-    float scale = State->galaxyRadius / (State->width * 0.5f);
-
-    int i;
-    for (i = 0; i < particlesCount; i ++) {
-        createParticle(star, part, scale);
-        star++;
-        part++;
-    }
-}
-
-
diff --git a/src/com/android/wallpaper/galaxy/GalaxyRS.java b/src/com/android/wallpaper/galaxy/GalaxyRS.java
index 801ad87..e428943 100644
--- a/src/com/android/wallpaper/galaxy/GalaxyRS.java
+++ b/src/com/android/wallpaper/galaxy/GalaxyRS.java
@@ -35,7 +35,6 @@
 import static android.renderscript.ProgramStore.BlendSrcFunc;
 import static android.renderscript.ProgramFragment.EnvMode.*;
 import static android.renderscript.Element.*;
-import static android.util.MathUtils.*;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 
@@ -91,8 +90,6 @@
     @SuppressWarnings({"FieldCanBeLocal"})
     private SimpleMesh mParticlesMesh;
 
-    private final float[] mFloatData = new float[5];
-
     GalaxyRS(int width, int height) {
         super(width, height);
 
@@ -113,7 +110,7 @@
         sb.setType(mStateType, "State", RSID_STATE);
         sb.setType(mParticlesMesh.getVertexType(0), "Particles", RSID_PARTICLES_BUFFER);
         sb.setType(mParticlesType, "Stars", RSID_PARTICLES);
-        ScriptC.Invokable inv = sb.addInvokable("initParticles");
+        ScriptC.Invokable initParticles = sb.addInvokable("initParticles");
         sb.setScript(mResources, R.raw.galaxy);
         sb.setRoot(true);
 
@@ -124,7 +121,7 @@
         script.bindAllocation(mState, RSID_STATE);
         script.bindAllocation(mParticles, RSID_PARTICLES);
         script.bindAllocation(mParticlesBuffer, RSID_PARTICLES_BUFFER);
-        inv.execute();
+        initParticles.execute();
 
         return script;
     }
@@ -206,7 +203,6 @@
     }
 
     private void createParticles() {
-        GalaxyParticle gp = new GalaxyParticle();
         mParticlesType = Type.createFromClass(mRS, GalaxyParticle.class, PARTICLES_COUNT, "Particle");
         mParticles = Allocation.createTyped(mRS, mParticlesType);
     }