Home | History | Annotate | Download | only in fountain
      1 // Fountain test script
      2 #pragma version(1)
      3 
      4 #pragma rs java_package_name(com.example.android.rs.fountain)
      5 
      6 #pragma stateFragment(parent)
      7 
      8 #include "rs_graphics.rsh"
      9 
     10 static int newPart = 0;
     11 rs_mesh partMesh;
     12 
     13 typedef struct __attribute__((packed, aligned(4))) Point {
     14     float2 delta;
     15     float2 position;
     16     uchar4 color;
     17 } Point_t;
     18 Point_t *point;
     19 
     20 int root() {
     21     float dt = min(rsGetDt(), 0.1f);
     22     rsgClearColor(0.f, 0.f, 0.f, 1.f);
     23     const float height = rsgGetHeight();
     24     const int size = rsAllocationGetDimX(rsGetAllocation(point));
     25     float dy2 = dt * (10.f);
     26     Point_t * p = point;
     27     for (int ct=0; ct < size; ct++) {
     28         p->delta.y += dy2;
     29         p->position += p->delta;
     30         if ((p->position.y > height) && (p->delta.y > 0)) {
     31             p->delta.y *= -0.3f;
     32         }
     33         p++;
     34     }
     35 
     36     rsgDrawMesh(partMesh);
     37     return 1;
     38 }
     39 
     40 static float4 partColor[10];
     41 void addParticles(int rate, float x, float y, int index, bool newColor)
     42 {
     43     if (newColor) {
     44         partColor[index].x = rsRand(0.5f, 1.0f);
     45         partColor[index].y = rsRand(1.0f);
     46         partColor[index].z = rsRand(1.0f);
     47     }
     48     float rMax = ((float)rate) * 0.02f;
     49     int size = rsAllocationGetDimX(rsGetAllocation(point));
     50     uchar4 c = rsPackColorTo8888(partColor[index]);
     51 
     52     Point_t * np = &point[newPart];
     53     float2 p = {x, y};
     54     while (rate--) {
     55         float angle = rsRand(3.14f * 2.f);
     56         float len = rsRand(rMax);
     57         np->delta.x = len * sin(angle);
     58         np->delta.y = len * cos(angle);
     59         np->position = p;
     60         np->color = c;
     61         newPart++;
     62         np++;
     63         if (newPart >= size) {
     64             newPart = 0;
     65             np = &point[newPart];
     66         }
     67     }
     68 }
     69 
     70