blob: ed7ba3c58ab6dc60975ec480ed57d68810c64a5e [file] [log] [blame]
Mike Cleronaf45d442009-12-02 02:04:46 -08001// Copyright (C) 2009 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma version(1)
Jason Sams8aeb9832010-05-11 14:02:00 -070016
17#include "../../../../../frameworks/base/libs/rs/scriptc/rs_types.rsh"
18#include "../../../../../frameworks/base/libs/rs/scriptc/rs_math.rsh"
19#include "../../../../../frameworks/base/libs/rs/scriptc/rs_graphics.rsh"
20//#pragma stateVertex(PVOrtho)
21//#pragma stateStore(PSSolid)
Mike Cleronaf45d442009-12-02 02:04:46 -080022
23#define MAX_PULSES 20
24#define MAX_EXTRAS 40
25#define PULSE_SIZE 14 // Size in pixels of a cell
26#define HALF_PULSE_SIZE 7
Daniel Sandlerdeadbf52009-12-03 11:28:35 -050027#define GLOW_SIZE 64 // Size of the leading glow in pixels
Jason Sams0cd53062009-12-08 15:46:00 -080028#define HALF_GLOW_SIZE 32
Mike Cleronaf45d442009-12-02 02:04:46 -080029#define SPEED 0.2f // (200 / 1000) Pixels per ms
30#define SPEED_VARIANCE 0.3f
31#define PULSE_NORMAL 0
32#define PULSE_EXTRA 1
33#define TRAIL_SIZE 40 // Number of cells in a trail
34#define MAX_DELAY 2000 // Delay between a pulse going offscreen and restarting
35
Jason Sams8aeb9832010-05-11 14:02:00 -070036typedef struct pulse_s {
Mike Cleronaf45d442009-12-02 02:04:46 -080037 int pulseType;
38 float originX;
39 float originY;
40 int color;
41 int startTime;
42 float dx;
43 float dy;
44 int active;
Jason Sams8aeb9832010-05-11 14:02:00 -070045} pulse_t;
Mike Cleronaf45d442009-12-02 02:04:46 -080046
Jason Sams8aeb9832010-05-11 14:02:00 -070047static pulse_t gPulses[MAX_PULSES];
48static pulse_t gExtras[MAX_EXTRAS];
49static int gNow;
50static int gWidth;
51static int gHeight;
52static int gRotate;
Mike Cleronaf45d442009-12-02 02:04:46 -080053
Jason Sams8aeb9832010-05-11 14:02:00 -070054
55int gIsPreview;
56float gXOffset;
57int gMode;
58
59rs_program_fragment gPFTexture;
60rs_program_store gPSBlend;
61rs_program_fragment gPFTexture565;
62rs_program_vertex gPVOrtho;
63
64rs_allocation gTBackground;
65rs_allocation gTPulse;
66rs_allocation gTGlow;
67
68#pragma rs export_var(gIsPreview, gXOffset, gMode, gPFTexture, gPSBlend, gPFTexture565, gPVOrtho, gTBackground, gTPulse, gTGlow)
Mike Cleronaf45d442009-12-02 02:04:46 -080069
70
71void setColor(int c) {
Jason Sams8aeb9832010-05-11 14:02:00 -070072 //debugPi(99, 6);
73 if (gMode == 1) {
Jason Sams6a500a12009-12-10 17:32:28 -080074 // sholes red
75 color(0.9f, 0.1f, 0.1f, 0.8f);
76 } else if (c == 0) {
Mike Cleronaf45d442009-12-02 02:04:46 -080077 // red
Jason Sams6a500a12009-12-10 17:32:28 -080078 color(1.0f, 0.0f, 0.0f, 0.8f);
Mike Cleronaf45d442009-12-02 02:04:46 -080079 } else if (c == 1) {
80 // green
Jason Sams6a500a12009-12-10 17:32:28 -080081 color(0.0f, 0.8f, 0.0f, 0.8f);
Mike Cleronaf45d442009-12-02 02:04:46 -080082 } else if (c == 2) {
83 // blue
Jason Sams6a500a12009-12-10 17:32:28 -080084 color(0.0f, 0.4f, 0.9f, 0.8f);
Mike Cleronaf45d442009-12-02 02:04:46 -080085 } else if (c == 3) {
86 // yellow
Jason Sams6a500a12009-12-10 17:32:28 -080087 color(1.0f, 0.8f, 0.0f, 0.8f);
Mike Cleronaf45d442009-12-02 02:04:46 -080088 }
89}
90
91void initPulse(struct pulse_s * pulse, int pulseType) {
Jason Sams8aeb9832010-05-11 14:02:00 -070092 //debugPi(99, 5);
93 if (randf(1.f) > 0.5f) {
94 pulse->originX = (int)randf(getWidth() * 2 / PULSE_SIZE) * PULSE_SIZE;
Mike Cleronaf45d442009-12-02 02:04:46 -080095 pulse->dx = 0;
Jason Sams8aeb9832010-05-11 14:02:00 -070096 if (randf(1.f) > 0.5f) {
Mike Cleronaf45d442009-12-02 02:04:46 -080097 // Top
98 pulse->originY = 0;
99 pulse->dy = randf2(1.0f - SPEED_VARIANCE, 1.0 + SPEED_VARIANCE);
100 } else {
101 // Bottom
Jason Sams8aeb9832010-05-11 14:02:00 -0700102 pulse->originY = gHeight;
Mike Cleronaf45d442009-12-02 02:04:46 -0800103 pulse->dy = -randf2(1.0f - SPEED_VARIANCE, 1.0 + SPEED_VARIANCE);
104 }
105 } else {
Jason Sams8aeb9832010-05-11 14:02:00 -0700106 pulse->originY = (int)randf(getHeight() / PULSE_SIZE) * PULSE_SIZE;
Mike Cleronaf45d442009-12-02 02:04:46 -0800107 pulse->dy = 0;
Jason Sams8aeb9832010-05-11 14:02:00 -0700108 if (randf(1.f) > 0.5f) {
Mike Cleronaf45d442009-12-02 02:04:46 -0800109 // Left
110 pulse->originX = 0;
111 pulse->dx = randf2(1.0f - SPEED_VARIANCE, 1.0 + SPEED_VARIANCE);
112 } else {
113 // Right
Jason Sams8aeb9832010-05-11 14:02:00 -0700114 pulse->originX = getWidth() * 2;
Mike Cleronaf45d442009-12-02 02:04:46 -0800115 pulse->dx = -randf2(1.0f - SPEED_VARIANCE, 1.0 + SPEED_VARIANCE);
116 }
117 }
118 pulse->startTime = gNow + (int)randf(MAX_DELAY);
Jason Sams0cd53062009-12-08 15:46:00 -0800119
Mike Cleronaf45d442009-12-02 02:04:46 -0800120 pulse->color = (int)randf(4.0f);
Jason Sams0cd53062009-12-08 15:46:00 -0800121
Mike Cleronaf45d442009-12-02 02:04:46 -0800122 pulse->pulseType = pulseType;
123 if (pulseType == PULSE_EXTRA) {
124 pulse->active = 0;
125 } else {
126 pulse->active = 1;
127 }
128}
129
130void initPulses() {
Jason Sams8aeb9832010-05-11 14:02:00 -0700131 //debugPi(99, 4);
Mike Cleronaf45d442009-12-02 02:04:46 -0800132 gNow = uptimeMillis();
133 int i;
134 for (i=0; i<MAX_PULSES; i++) {
135 initPulse(&gPulses[i], PULSE_NORMAL);
136 }
137 for (i=0; i<MAX_EXTRAS; i++) {
138 struct pulse_s * p = &gExtras[i];
139 p->pulseType = PULSE_EXTRA;
140 p->active = 0;
141 }
142}
143
Jason Sams8aeb9832010-05-11 14:02:00 -0700144void drawBackground() {
145 //debugPi(99, 3);
146 bindProgramFragment(gPFTexture565);
147 bindTexture(gPFTexture565, 0, gTBackground);
Mike Cleronaf45d442009-12-02 02:04:46 -0800148 color(1.0f, 1.0f, 1.0f, 1.0f);
Jason Sams8aeb9832010-05-11 14:02:00 -0700149 if (gRotate) {
150 drawRect(0.0f, 0.0f, gHeight*2, gWidth, 0.0f);
Mike Cleronaf45d442009-12-02 02:04:46 -0800151 } else {
Jason Sams8aeb9832010-05-11 14:02:00 -0700152 drawRect(0.0f, 0.0f, gWidth*2, gHeight, 0.0f);
153 }
Mike Cleronaf45d442009-12-02 02:04:46 -0800154}
155
Jason Sams8aeb9832010-05-11 14:02:00 -0700156void drawPulses(pulse_t * pulseSet, int setSize) {
157 //debugPi(99, 2);
158 bindProgramFragment(gPFTexture);
159 bindProgramFragmentStore(gPSBlend);
Mike Cleronaf45d442009-12-02 02:04:46 -0800160
161 float matrix[16];
Jason Sams0cd53062009-12-08 15:46:00 -0800162
Mike Cleronaf45d442009-12-02 02:04:46 -0800163 int i;
164 for (i=0; i<setSize; i++) {
165 struct pulse_s * p = &pulseSet[i];
Jason Sams0cd53062009-12-08 15:46:00 -0800166
Mike Cleronaf45d442009-12-02 02:04:46 -0800167 int delta = gNow - p->startTime;
Jason Sams0cd53062009-12-08 15:46:00 -0800168
Mike Cleronaf45d442009-12-02 02:04:46 -0800169 if (p->active != 0 && delta >= 0) {
Jason Sams0cd53062009-12-08 15:46:00 -0800170
Mike Cleronaf45d442009-12-02 02:04:46 -0800171 float x = p->originX + (p->dx * SPEED * delta);
172 float y = p->originY + (p->dy * SPEED * delta);
Jason Sams0cd53062009-12-08 15:46:00 -0800173
Mike Cleronaf45d442009-12-02 02:04:46 -0800174 matrixLoadIdentity(matrix);
175 if (p->dx < 0) {
176 vpLoadTextureMatrix(matrix);
177 float xx = x + (TRAIL_SIZE * PULSE_SIZE);
178 if (xx <= 0) {
179 initPulse(p, p->pulseType);
180 } else {
181 setColor(p->color);
Jason Sams8aeb9832010-05-11 14:02:00 -0700182 bindTexture(gPFTexture, 0, gTPulse);
Mike Cleronaf45d442009-12-02 02:04:46 -0800183 drawRect(x, y, xx, y + PULSE_SIZE, 0.0f);
Jason Sams8aeb9832010-05-11 14:02:00 -0700184 bindTexture(gPFTexture, 0, gTGlow);
Mike Cleronaf45d442009-12-02 02:04:46 -0800185 drawRect(x + HALF_PULSE_SIZE - HALF_GLOW_SIZE,
186 y + HALF_PULSE_SIZE - HALF_GLOW_SIZE,
Jason Sams0cd53062009-12-08 15:46:00 -0800187 x + HALF_PULSE_SIZE + HALF_GLOW_SIZE,
Mike Cleronaf45d442009-12-02 02:04:46 -0800188 y + HALF_PULSE_SIZE + HALF_GLOW_SIZE,
189 0.0f);
190 }
191 } else if (p->dx > 0) {
Daniel Sandlerdeadbf52009-12-03 11:28:35 -0500192 x += PULSE_SIZE; // need to start on the other side of this cell
Mike Cleronaf45d442009-12-02 02:04:46 -0800193 matrixRotate(matrix, 180.0f, 0.0f, 0.0f, 1.0f);
194 vpLoadTextureMatrix(matrix);
195 float xx = x - (TRAIL_SIZE * PULSE_SIZE);
Jason Sams8aeb9832010-05-11 14:02:00 -0700196 if (xx >= gWidth * 2) {
Mike Cleronaf45d442009-12-02 02:04:46 -0800197 initPulse(p, p->pulseType);
198 } else {
199 setColor(p->color);
Jason Sams8aeb9832010-05-11 14:02:00 -0700200 bindTexture(gPFTexture, 0, gTPulse);
Mike Cleronaf45d442009-12-02 02:04:46 -0800201 drawRect(xx, y, x, y + PULSE_SIZE, 0.0f);
Jason Sams8aeb9832010-05-11 14:02:00 -0700202 bindTexture(gPFTexture, 0, gTGlow);
Mike Cleronaf45d442009-12-02 02:04:46 -0800203 drawRect(x - HALF_PULSE_SIZE - HALF_GLOW_SIZE,
204 y + HALF_PULSE_SIZE - HALF_GLOW_SIZE,
Jason Sams0cd53062009-12-08 15:46:00 -0800205 x - HALF_PULSE_SIZE + HALF_GLOW_SIZE,
Mike Cleronaf45d442009-12-02 02:04:46 -0800206 y + HALF_PULSE_SIZE + HALF_GLOW_SIZE,
207 0.0f);
208 }
209 } else if (p->dy < 0) {
210 matrixRotate(matrix, -90.0f, 0.0f, 0.0f, 1.0f);
211 vpLoadTextureMatrix(matrix);
212 float yy = y + (TRAIL_SIZE * PULSE_SIZE);
213 if (yy <= 0) {
214 initPulse(p, p->pulseType);
215 } else {
216 setColor(p->color);
Jason Sams8aeb9832010-05-11 14:02:00 -0700217 bindTexture(gPFTexture, 0, gTPulse);
Mike Cleronaf45d442009-12-02 02:04:46 -0800218 drawRect(x, y, x + PULSE_SIZE, yy, 0.0f);
Jason Sams8aeb9832010-05-11 14:02:00 -0700219 bindTexture(gPFTexture, 0, gTGlow);
Mike Cleronaf45d442009-12-02 02:04:46 -0800220 drawRect(x + HALF_PULSE_SIZE - HALF_GLOW_SIZE,
221 y + HALF_PULSE_SIZE - HALF_GLOW_SIZE,
Jason Sams0cd53062009-12-08 15:46:00 -0800222 x + HALF_PULSE_SIZE + HALF_GLOW_SIZE,
Mike Cleronaf45d442009-12-02 02:04:46 -0800223 y + HALF_PULSE_SIZE + HALF_GLOW_SIZE,
224 0.0f);
225 }
226 } else if (p->dy > 0) {
Daniel Sandlerdeadbf52009-12-03 11:28:35 -0500227 y += PULSE_SIZE; // need to start on the other side of this cell
Mike Cleronaf45d442009-12-02 02:04:46 -0800228 matrixRotate(matrix, 90.0f, 0.0f, 0.0f, 1.0f);
229 vpLoadTextureMatrix(matrix);
230 float yy = y - (TRAIL_SIZE * PULSE_SIZE);
Jason Sams8aeb9832010-05-11 14:02:00 -0700231 if (yy >= gHeight) {
Mike Cleronaf45d442009-12-02 02:04:46 -0800232 initPulse(p, p->pulseType);
233 } else {
234 setColor(p->color);
Jason Sams8aeb9832010-05-11 14:02:00 -0700235 bindTexture(gPFTexture, 0, gTPulse);
Mike Cleronaf45d442009-12-02 02:04:46 -0800236 drawRect(x, yy, x + PULSE_SIZE, y, 0.0f);
Jason Sams8aeb9832010-05-11 14:02:00 -0700237 bindTexture(gPFTexture, 0, gTGlow);
Mike Cleronaf45d442009-12-02 02:04:46 -0800238 drawRect(x + HALF_PULSE_SIZE - HALF_GLOW_SIZE,
239 y - HALF_PULSE_SIZE - HALF_GLOW_SIZE,
Jason Sams0cd53062009-12-08 15:46:00 -0800240 x + HALF_PULSE_SIZE + HALF_GLOW_SIZE,
Mike Cleronaf45d442009-12-02 02:04:46 -0800241 y - HALF_PULSE_SIZE + HALF_GLOW_SIZE,
242 0.0f);
243 }
244 }
245 }
246 }
Jason Sams0cd53062009-12-08 15:46:00 -0800247
Mike Cleronaf45d442009-12-02 02:04:46 -0800248 matrixLoadIdentity(matrix);
249 vpLoadTextureMatrix(matrix);
250}
251
252void addTap(int x, int y) {
Jason Sams8aeb9832010-05-11 14:02:00 -0700253 //debugPi(99, 1);
Mike Cleronaf45d442009-12-02 02:04:46 -0800254 int i;
255 int count = 0;
256 int color = (int)randf(4.0f);
257 x = (int)(x / PULSE_SIZE) * PULSE_SIZE;
Jason Sams0cd53062009-12-08 15:46:00 -0800258 y = (int)(y / PULSE_SIZE) * PULSE_SIZE;
Mike Cleronaf45d442009-12-02 02:04:46 -0800259 for (i=0; i<MAX_EXTRAS; i++) {
260 struct pulse_s * p = &gExtras[i];
261 if (p->active == 0) {
262 p->originX = x;
263 p->originY = y;
Jason Sams0cd53062009-12-08 15:46:00 -0800264
265 if (count == 0) {
Mike Cleronaf45d442009-12-02 02:04:46 -0800266 p->dx = 1.5f;
267 p->dy = 0.0f;
268 } else if (count == 1) {
269 p->dx = -1.5f;
270 p->dy = 0.0f;
271 } else if (count == 2) {
272 p->dx = 0.0f;
273 p->dy = 1.5f;
274 } else if (count == 3) {
275 p->dx = 0.0f;
276 p->dy = -1.5f;
277 }
Jason Sams0cd53062009-12-08 15:46:00 -0800278
Mike Cleronaf45d442009-12-02 02:04:46 -0800279 p->active = 1;
280 p->color = color;
281 color++;
282 if (color >= 4) {
283 color = 0;
284 }
285 p->startTime = gNow;
286 count++;
287 if (count == 4) {
288 break;
289 }
290 }
291 }
292}
293
Jason Sams8aeb9832010-05-11 14:02:00 -0700294int root() {
295 //debugPi(99, 0);
296 gWidth = getWidth();
297 gHeight = getHeight();
298 gRotate = gWidth > gHeight ? 1 : 0;
Mike Cleronaf45d442009-12-02 02:04:46 -0800299
300 gNow = uptimeMillis();
Jason Sams0cd53062009-12-08 15:46:00 -0800301
Jason Sams8aeb9832010-05-11 14:02:00 -0700302 bindProgramVertex(gPVOrtho);
Jason Sams0cd53062009-12-08 15:46:00 -0800303
Mike Cleronaf45d442009-12-02 02:04:46 -0800304 float matrix[16];
305 matrixLoadIdentity(matrix);
Jason Sams8aeb9832010-05-11 14:02:00 -0700306 if (gRotate) {
Mike Cleronaf45d442009-12-02 02:04:46 -0800307 //matrixLoadRotate(matrix, 90.0f, 0.0f, 0.0f, 1.0f);
308 //matrixTranslate(matrix, 0.0f, -height, 1.0f);
Daniel Sandler12290bd2010-02-19 15:43:28 -0500309 // XXX: HAX: do not slide display in landscape
Mike Cleronaf45d442009-12-02 02:04:46 -0800310 } else {
Jason Sams8aeb9832010-05-11 14:02:00 -0700311 matrixTranslate(matrix, -(gXOffset * gWidth), 0, 0);
Mike Cleronaf45d442009-12-02 02:04:46 -0800312 }
Jason Sams0cd53062009-12-08 15:46:00 -0800313
Mike Cleronaf45d442009-12-02 02:04:46 -0800314 vpLoadModelMatrix(matrix);
Jason Sams0cd53062009-12-08 15:46:00 -0800315
Jason Sams8aeb9832010-05-11 14:02:00 -0700316 drawBackground();
Mike Cleronaf45d442009-12-02 02:04:46 -0800317 drawPulses(gPulses, MAX_PULSES);
318 drawPulses(gExtras, MAX_EXTRAS);
Jason Sams0cd53062009-12-08 15:46:00 -0800319
320 return 45;
Mike Cleronaf45d442009-12-02 02:04:46 -0800321}