OSDN Git Service

Use 1024x1024 for background. Fix leaf Z-order.
authorMike Cleron <mcleron@google.com>
Fri, 20 Nov 2009 07:23:13 +0000 (23:23 -0800)
committerMike Cleron <mcleron@google.com>
Fri, 20 Nov 2009 07:23:13 +0000 (23:23 -0800)
res/drawable-hdpi/pond.jpg
res/raw/fall.rs
src/com/android/wallpaper/fall/FallRS.java

index 39b1f8f..d1be342 100644 (file)
Binary files a/res/drawable-hdpi/pond.jpg and b/res/drawable-hdpi/pond.jpg differ
index 8dee7dc..e25e04a 100644 (file)
@@ -19,6 +19,7 @@
 
 #define LEAVES_TEXTURES_COUNT 8
 #define LEAF_SIZE 0.55f
+#define LEAVES_COUNT 14
 
 float skyOffsetX;
 float skyOffsetY;
@@ -46,6 +47,27 @@ struct drop_s {
 struct drop_s gDrops[10];
 int gMaxDrops;
 
+struct Leaves_s {
+    float x;
+    float y;
+    float scale;
+    float angle;
+    float spin;
+    float u1;
+    float u2;
+    float altitude;
+    float rippled;
+    float deltaX;
+    float deltaY;
+    int newLeaf;
+};
+
+struct Leaves_s gLeavesStore[LEAVES_COUNT];
+
+struct Leaves_s* gLeaves[LEAVES_COUNT];
+
+struct Leaves_s* gNextLeaves[LEAVES_COUNT];
+
 void init() {
     int ct;
     gMaxDrops = 10;
@@ -60,13 +82,13 @@ void init() {
 }
 
 void initLeaves() {
-    struct Leaves_s *leaf = Leaves;
-    int leavesCount = State->leavesCount;
+    struct Leaves_s *leaf = gLeavesStore;
     float width = State->glWidth * 2;
     float height = State->glHeight;
 
     int i;
-    for (i = 0; i < leavesCount; i ++) {
+    for (i = 0; i < LEAVES_COUNT; i ++) {
+        gLeaves[i] = leaf;
         int sprite = randf(LEAVES_TEXTURES_COUNT);
         leaf->x = randf2(-width * 0.5f, width * 0.5f);
         leaf->y = randf2(-height * 0.5f, height * 0.5f);
@@ -178,7 +200,7 @@ void genLeafDrop(struct Leaves_s *leaf, float amp) {
 
 }
 
-void drawLeaf(struct Leaves_s *leaf) {
+int drawLeaf(struct Leaves_s *leaf) {
 
     float x = leaf->x;
     float y = leaf->y;
@@ -263,6 +285,7 @@ void drawLeaf(struct Leaves_s *leaf) {
         leaf->angle = r;
     }
 
+    int newLeaf = 0;
     if (-LEAF_SIZE * s + x > State->glWidth || LEAF_SIZE * s + x < -State->glWidth ||
             LEAF_SIZE * s + y < -State->glHeight / 2.0f) {
 
@@ -278,7 +301,10 @@ void drawLeaf(struct Leaves_s *leaf) {
         leaf->rippled = -1.0f;
         leaf->deltaX = randf2(-0.02f, 0.02f) / 2.0f;
         leaf->deltaY = -0.08f * randf2(0.9f, 1.1f) / 2.0f;
+        leaf->newLeaf = 1;
+        newLeaf = 1;
     }
+    return newLeaf;
 }
 
 void drawLeaves() {
@@ -289,12 +315,39 @@ void drawLeaves() {
 
     color(1.0f, 1.0f, 1.0f, 1.0f);
 
-    int leavesCount = State->leavesCount;
-    struct Leaves_s *leaf = Leaves;
+    int newLeaves = 0;
     int i = 0;
-    for ( ; i < leavesCount; i += 1) {
-        drawLeaf(leaf);
-        leaf += 1;
+    for ( ; i < LEAVES_COUNT; i += 1) {
+        if (drawLeaf(gLeaves[i])) {
+            newLeaves = 1;
+            
+        }
+    }
+    
+    if (newLeaves > 0) {    
+        int index = 0;
+        
+        // Copy all the old leaves to the beginning of gNextLeaves
+        for (i=0; i < LEAVES_COUNT; i++) {
+            if (gLeaves[i]->newLeaf == 0) {
+                gNextLeaves[index] = gLeaves[i];
+                index++;
+            }
+        }
+        
+        // Now copy all the newly falling leaves to the end of gNextLeaves
+        for (i=0; i < LEAVES_COUNT; i++) {
+            if (gLeaves[i]->newLeaf > 0) {
+                gNextLeaves[index] = gLeaves[i];
+                gNextLeaves[index]->newLeaf = 0;
+                index++;
+            }
+        }
+        
+        // And move everything in gNextLeaves back to gLeaves
+        for (i=0; i < LEAVES_COUNT; i++) {
+            gLeaves[i] = gNextLeaves[i];
+        }
     }
 
     float matrix[16];
@@ -306,7 +359,7 @@ void drawRiverbed() {
     bindTexture(NAMED_PFBackground, 0, NAMED_TRiverbed);
 
     float matrix[16];
-    matrixLoadScale(matrix, 0.5f, -1.0f, 1.0f);
+    matrixLoadScale(matrix, 0.5f * 960.0f / 1024.0f, -1.0f * 800.0f / 1024.0f, 1.0f);
     matrixTranslate(matrix, State->xOffset, 0.0f, 0.0f);
     vpLoadTextureMatrix(matrix);
 
@@ -368,8 +421,8 @@ int main(int index) {
     }
 
     if (add) {
-        int i = (int)randf(State->leavesCount);
-        genLeafDrop(&Leaves[i], randf(0.3f) + 0.1f);
+        int i = (int)randf(LEAVES_COUNT);
+        genLeafDrop(gLeaves[i], randf(0.3f) + 0.1f);
     }
 
     generateRipples();
index 3e00c10..069bedd 100644 (file)
@@ -56,10 +56,8 @@ class FallRS extends RenderScriptScene {
     private static final int RSID_TEXTURE_SKY = 2;
 
     private static final int RSID_RIPPLE_MAP = 1;
-    private static final int RSID_LEAVES = 3;
-    private static final int RSID_DROP = 4;
+    private static final int RSID_DROP = 2;
 
-    private static final int LEAVES_COUNT = 14;
 
     private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options();
 
@@ -91,9 +89,6 @@ class FallRS extends RenderScriptScene {
 
     private Allocation mRippleMap;
 
-    private Allocation mLeaves;
-    private Type mLeavesType;
-
     private float mGlHeight;
 
     public FallRS(int width, int height) {
@@ -154,7 +149,6 @@ class FallRS extends RenderScriptScene {
         ScriptC.Builder sb = new ScriptC.Builder(mRS);
         sb.setType(mStateType, "State", RSID_STATE);
         sb.setType(mDropType, "Drop", RSID_DROP);
-        sb.setType(mLeavesType, "Leaves", RSID_LEAVES);
         sb.setScript(mResources, R.raw.fall);
         Script.Invokable invokable = sb.addInvokable("initLeaves");
         sb.setRoot(true);
@@ -165,7 +159,6 @@ class FallRS extends RenderScriptScene {
 
         script.bindAllocation(mState, RSID_STATE);
         script.bindAllocation(mRippleMap, RSID_RIPPLE_MAP);
-        script.bindAllocation(mLeaves, RSID_LEAVES);
         script.bindAllocation(mDropState, RSID_DROP);
 
         invokable.execute();
@@ -229,12 +222,6 @@ class FallRS extends RenderScriptScene {
 
         createState(rippleMapSize);
         createRippleMap(rippleMapSize);
-        createLeaves();
-    }
-
-    private void createLeaves() {
-        mLeavesType = Type.createFromClass(mRS, Leaf.class, LEAVES_COUNT, "Leaf");
-        mLeaves = Allocation.createTyped(mRS, mLeavesType);
     }
 
     private void createRippleMap(int rippleMapSize) {
@@ -274,7 +261,6 @@ class FallRS extends RenderScriptScene {
         mWorldState.meshHeight = mMeshHeight;
         mWorldState.rippleMapSize = rippleMapSize;
         mWorldState.rippleIndex = 0;
-        mWorldState.leavesCount = LEAVES_COUNT;
         mWorldState.glWidth = 2.0f;
         mWorldState.glHeight = mGlHeight;
         mWorldState.skySpeedX = random(-0.001f, 0.001f);
@@ -294,21 +280,7 @@ class FallRS extends RenderScriptScene {
         mDropState = Allocation.createTyped(mRS, mDropType);
         mDropState.data(mDrop);
     }
-
-    static class Leaf {
-        public float x;
-        public float y;
-        public float scale;
-        public float angle;
-        public float spin;
-        public float u1;
-        public float u2;
-        public float altitude;
-        public float rippled;
-        public float deltaX;
-        public float deltaY;
-    }
-
+    
     private void loadTextures() {
         final Allocation[] textures = new Allocation[TEXTURES_COUNT];
         textures[RSID_TEXTURE_RIVERBED] = loadTexture(R.drawable.pond, "TRiverbed");