blob: 77db74a69bb819795aabb3bbd2d8555d797ca371 [file] [log] [blame]
Jason Sams0f505e52009-10-13 17:18:35 -07001#pragma version(1)
2#pragma stateVertex(PV)
3#pragma stateFragment(PFTexLinear)
4#pragma stateStore(PSIcons)
5
6#define PI 3.14159f
7
Jason Sams41b61c82009-10-15 15:40:54 -07008int g_SpecialHWWar;
Jason Sams0f505e52009-10-13 17:18:35 -07009
10// Attraction to center values from page edge to page center.
11float g_AttractionTable[9];
Jason Sams0f505e52009-10-13 17:18:35 -070012float g_PhysicsTableSize;
13
14float g_PosPage;
15float g_PosVelocity;
16float g_LastPositionX;
17int g_LastTouchDown;
18float g_DT;
19int g_LastTime;
20int g_PosMax;
21float g_Zoom;
22float g_OldPosPage;
23float g_OldPosVelocity;
24float g_OldZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -070025float g_MoveToTotalTime;
26float g_MoveToTime;
27float g_MoveToOldPos;
28
Jason Sams0f505e52009-10-13 17:18:35 -070029
30// Drawing constants, should be parameters ======
31#define VIEW_ANGLE 1.28700222f
32
33int g_DrawLastFrame;
34int lastFrame(int draw) {
35 // We draw one extra frame to work around the last frame post bug.
36 // We also need to track if we drew the last frame to deal with large DT
37 // in the physics.
38 int ret = g_DrawLastFrame | draw;
39 g_DrawLastFrame = draw;
40 return ret; // should return draw instead.
41}
42
43void updateReadback() {
44 if ((g_OldPosPage != g_PosPage) ||
45 (g_OldPosVelocity != g_PosVelocity) ||
46 (g_OldZoom != g_Zoom)) {
47
48 g_OldPosPage = g_PosPage;
49 g_OldPosVelocity = g_PosVelocity;
50 g_OldZoom = g_Zoom;
51
52 int i[3];
53 i[0] = g_PosPage * (1 << 16);
54 i[1] = g_PosVelocity * (1 << 16);
55 i[2] = g_OldZoom * (1 << 16);
56 sendToClient(&i[0], 1, 12, 1);
57 }
58}
59
Jason Sams41b61c82009-10-15 15:40:54 -070060void setColor(float r, float g, float b, float a) {
61 if (g_SpecialHWWar) {
62 color(0, 0, 0, 0.001f);
63 } else {
64 color(r, g, b, a);
65 }
66}
Jason Sams0f505e52009-10-13 17:18:35 -070067
68void init() {
69 g_AttractionTable[0] = 6.5f;
70 g_AttractionTable[1] = 6.5f;
71 g_AttractionTable[2] = 7.0f;
72 g_AttractionTable[3] = 6.0f;
73 g_AttractionTable[4] = -6.0f;
74 g_AttractionTable[5] = -7.0f;
75 g_AttractionTable[6] = -6.5f;
76 g_AttractionTable[7] = -6.5f;
77 g_AttractionTable[8] = -6.5f; // dup 7 to avoid a clamp later
Jason Sams0f505e52009-10-13 17:18:35 -070078 g_PhysicsTableSize = 7;
79
80 g_PosVelocity = 0;
81 g_PosPage = 0;
82 g_LastTouchDown = 0;
83 g_LastPositionX = 0;
84 g_Zoom = 0;
Jason Sams41b61c82009-10-15 15:40:54 -070085 g_SpecialHWWar = 1;
Jason Samsc1c521e2009-10-19 14:45:45 -070086 g_MoveToTime = 0;
87 g_MoveToOldPos = 0;
88 g_MoveToTotalTime = 0.5f;
Jason Sams41b61c82009-10-15 15:40:54 -070089}
90
91void resetHWWar() {
92 g_SpecialHWWar = 1;
Jason Sams0f505e52009-10-13 17:18:35 -070093}
94
95void move() {
96 if (g_LastTouchDown) {
97 float dx = -(state->newPositionX - g_LastPositionX);
98 g_PosVelocity = 0;
99 g_PosPage += dx * 4;
100
101 float pmin = -0.25f;
102 float pmax = g_PosMax + 0.25f;
103 g_PosPage = clampf(g_PosPage, pmin, pmax);
104 }
105 g_LastTouchDown = state->newTouchDown;
106 g_LastPositionX = state->newPositionX;
Jason Samsc1c521e2009-10-19 14:45:45 -0700107 g_MoveToTime = 0;
Jason Sams0f505e52009-10-13 17:18:35 -0700108 //debugF("Move P", g_PosPage);
109}
110
Jason Samsc1c521e2009-10-19 14:45:45 -0700111void moveTo() {
112 g_MoveToTime = g_MoveToTotalTime;
113 g_PosVelocity = 0;
114 g_MoveToOldPos = g_PosPage;
115}
116
Jason Sams0f505e52009-10-13 17:18:35 -0700117void fling() {
118 g_LastTouchDown = 0;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700119 g_PosVelocity = -state->flingVelocity * 4;
Jason Sams0f505e52009-10-13 17:18:35 -0700120 float av = fabsf(g_PosVelocity);
121 float minVel = 3.5f;
122
123 minVel *= 1.f - (fabsf(fracf(g_PosPage + 0.5f) - 0.5f) * 0.45f);
124
125 if (av < minVel && av > 0.2f) {
126 if (g_PosVelocity > 0) {
127 g_PosVelocity = minVel;
128 } else {
129 g_PosVelocity = -minVel;
130 }
131 }
132
133 if (g_PosPage <= 0) {
134 g_PosVelocity = maxf(0, g_PosVelocity);
135 }
136 if (g_PosPage > g_PosMax) {
137 g_PosVelocity = minf(0, g_PosVelocity);
138 }
139}
140
Jason Sams0f505e52009-10-13 17:18:35 -0700141float
142modf(float x, float y)
143{
144 return x-(y*floorf(x/y));
145}
146
147void updatePos() {
148 if (g_LastTouchDown) {
149 return;
150 }
151
152 int outOfRange = 0;
153 float tablePosNorm = fracf(g_PosPage + 0.5f);
154 float tablePosF = tablePosNorm * g_PhysicsTableSize;
155 int tablePosI = tablePosF;
156 float tablePosFrac = tablePosF - tablePosI;
157 float accel = lerpf(g_AttractionTable[tablePosI],
158 g_AttractionTable[tablePosI + 1],
159 tablePosFrac) * g_DT;
Jason Samsb52dfa02009-10-14 20:16:14 -0700160 float friction = 4.f * g_DT;
Jason Sams0f505e52009-10-13 17:18:35 -0700161
Jason Samsc1c521e2009-10-19 14:45:45 -0700162 if (g_MoveToTime) {
163 float a = 2.f * (state->targetPos - g_MoveToOldPos) /
164 (g_MoveToTotalTime * g_MoveToTotalTime);
165 if (g_MoveToTime > (g_MoveToTotalTime * 0.5f)) {
166 // slowing
167 g_PosPage = state->targetPos - 0.5f * a * (g_MoveToTime * g_MoveToTime);
168 } else {
169 // accelerating.
170 float t = g_MoveToTotalTime - g_MoveToTime;
171 g_PosPage = g_MoveToOldPos + 0.5f * a * (t * t);
172 }
173 g_MoveToTime -= g_DT;
174 if (g_MoveToTime <= 0) {
175 g_MoveToTime = 0;
176 g_PosPage = state->targetPos;
177 }
178 return;
179 }
180
Jason Sams0f505e52009-10-13 17:18:35 -0700181 if (g_PosPage < -0.5f) {
182 accel = g_AttractionTable[0] * g_DT;
Jason Sams0f505e52009-10-13 17:18:35 -0700183 outOfRange = 1;
184 }
185 if ((g_PosPage - g_PosMax) > 0.5f) {
186 accel = g_AttractionTable[(int)g_PhysicsTableSize] * g_DT;
Jason Sams0f505e52009-10-13 17:18:35 -0700187 outOfRange = 1;
188 }
189
190 // If our velocity is low OR acceleration is opposing it, apply it.
Jason Samsb52dfa02009-10-14 20:16:14 -0700191 if (fabsf(g_PosVelocity) < 1.0f || (g_PosVelocity * accel) < 0 || outOfRange) {
Jason Sams0f505e52009-10-13 17:18:35 -0700192 g_PosVelocity += accel;
193 }
194
195 if ((friction > fabsf(g_PosVelocity)) &&
196 (friction > fabsf(accel)) &&
197 !outOfRange) {
198 // Special get back to center and overcome friction physics.
199 float t = tablePosNorm - 0.5f;
200 if (fabsf(t) < (friction * g_DT)) {
201 // really close, just snap
202 g_PosPage = roundf(g_PosPage);
203 g_PosVelocity = 0;
204 } else {
205 if (t > 0) {
206 g_PosVelocity = -friction;
207 } else {
208 g_PosVelocity = friction;
209 }
210 }
211 } else {
212 // Normal physics
213 if (g_PosVelocity > 0) {
214 g_PosVelocity -= friction;
215 g_PosVelocity = maxf(g_PosVelocity, 0);
216 } else {
217 g_PosVelocity += friction;
218 g_PosVelocity = minf(g_PosVelocity, 0);
219 }
220 }
221 g_PosPage += g_PosVelocity * g_DT;
222
223 // Check for out of boundry conditions.
224 if (g_PosPage < 0 && g_PosVelocity < 0) {
225 g_PosPage = maxf(g_PosPage, -0.49);
226 float damp = 1.0 + (g_PosPage * 4);
227 damp = clampf(damp, 0.f, 0.9f);
228 g_PosVelocity *= damp;
229 }
230 if (g_PosPage > g_PosMax && g_PosVelocity > 0) {
231 g_PosPage = minf(g_PosPage, g_PosMax + 0.49);
232 float damp = 1.0 - ((g_PosPage - g_PosMax) * 4);
233 damp = clampf(damp, 0.f, 0.9f);
234 g_PosVelocity *= damp;
235 }
236}
237
Jason Sams09c6fc02009-10-16 17:23:23 -0700238int positionStrip(float row, float column, int isTop, float p)
Jason Sams0f505e52009-10-13 17:18:35 -0700239{
240 float mat1[16];
Jason Sams0f505e52009-10-13 17:18:35 -0700241 float x = 0.5f * (column - 1.5f);
Jason Sams0f505e52009-10-13 17:18:35 -0700242 float scale = 72.f * 3 / getWidth();
Jason Sams0f505e52009-10-13 17:18:35 -0700243
244 if (isTop) {
245 matrixLoadTranslate(mat1, x, 0.8f, 0.f);
246 matrixScale(mat1, scale, scale, 1.f);
247 } else {
248 matrixLoadTranslate(mat1, x, -0.9f, 0.f);
249 matrixScale(mat1, scale, -scale, 1.f);
250 }
Jason Sams461073b2009-10-20 12:06:28 -0700251 matrixTranslate(mat1, 0, p * 2, 0.f);
252 matrixRotate(mat1, -p * 50, 1, 0, 0);
Jason Sams0f505e52009-10-13 17:18:35 -0700253 vpLoadModelMatrix(mat1);
254
Jason Samsb52dfa02009-10-14 20:16:14 -0700255 float soff = -(row * 1.4);
Jason Sams0f505e52009-10-13 17:18:35 -0700256 if (isTop) {
257 matrixLoadScale(mat1, 1.f, -0.85f, 1.f);
Jason Samsb52dfa02009-10-14 20:16:14 -0700258 matrixTranslate(mat1, 0, soff - 0.97f, 0);
Jason Sams0f505e52009-10-13 17:18:35 -0700259 } else {
260 matrixLoadScale(mat1, 1.f, 0.85f, 1.f);
Jason Samsb52dfa02009-10-14 20:16:14 -0700261 matrixTranslate(mat1, 0, soff - 0.45f, 0);
Jason Sams0f505e52009-10-13 17:18:35 -0700262 }
263 vpLoadTextureMatrix(mat1);
Jason Samsb52dfa02009-10-14 20:16:14 -0700264 return -soff * 10.f;
Jason Sams0f505e52009-10-13 17:18:35 -0700265}
266
267void
268draw_home_button()
269{
Jason Sams41b61c82009-10-15 15:40:54 -0700270 setColor(1.0f, 1.0f, 1.0f, 1.0f);
Joe Onoratod63458b2009-10-15 21:19:09 -0700271 bindTexture(NAMED_PFTexLinear, 0, state->homeButtonId);
Jason Sams0f505e52009-10-13 17:18:35 -0700272
273 float scale = 2.0f / SCREEN_WIDTH_PX;
274
275 float x = 0.0f;
276
277 float y = -(SCREEN_HEIGHT_PX / (float)SCREEN_WIDTH_PX);
278 y += g_Zoom * (scale * params->homeButtonTextureHeight / 2);
279
280 float z = 0.0f;
281 drawSprite(x, y, z, params->homeButtonTextureWidth, params->homeButtonTextureHeight);
282}
283
Jason Sams09c6fc02009-10-16 17:23:23 -0700284void drawFrontGrid(float rowOffset, float p)
Jason Sams0f505e52009-10-13 17:18:35 -0700285{
286 float h = getHeight();
287 float w = getWidth();
288
289 int intRowOffset = rowOffset;
290 float rowFrac = rowOffset - intRowOffset;
291 float colWidth = getWidth() / 4;
292 float rowHeight = colWidth + 25.f;
293 float yoff = h - ((h - (rowHeight * 4.f)) / 2);
294
295 yoff -= 110;
296
297 int row, col;
298 int iconNum = intRowOffset * 4;
299 float ymax = yoff;
300 float ymin = yoff - (3 * rowHeight) - 70;
301
302 for (row = 0; row < 5; row++) {
303 float y = yoff - ((-rowFrac + row) * rowHeight);
304
305 for (col=0; col < 4; col++) {
306 if (iconNum >= state->iconCount) {
307 return;
308 }
309
310 if (iconNum >= 0) {
311 float x = colWidth * col - ((128 - colWidth) / 2);
312
313 if ((y >= ymin) && (y <= ymax)) {
Jason Sams41b61c82009-10-15 15:40:54 -0700314 setColor(1.f, 1.f, 1.f, 1.f);
Joe Onorato742d7fc2009-10-15 19:48:16 -0700315
Jason Sams09c6fc02009-10-16 17:23:23 -0700316 if (state->selectedIconIndex == iconNum && !p) {
Joe Onorato742d7fc2009-10-15 19:48:16 -0700317 bindTexture(NAMED_PFTexLinear, 0, state->selectedIconTexture);
318 drawSpriteScreenspace(x, y, 0, 128, 128);
319 }
320
Jason Sams0f505e52009-10-13 17:18:35 -0700321 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, iconNum));
Jason Sams09c6fc02009-10-16 17:23:23 -0700322 if (!p) {
323 drawSpriteScreenspace(x, y, 0, 128, 128);
324 } else {
325 float px = ((x + 64) - (getWidth() / 2)) / (getWidth() / 2);
326 float py = ((y + 64) - (getHeight() / 2)) / (getWidth() / 2);
327 float d = 64.f / (getWidth() / 2);
328 px *= p + 1;
329 py *= p + 1;
330 drawQuadTexCoords(px - d, py - d, -p, 0, 1,
331 px - d, py + d, -p, 0, 0,
332 px + d, py + d, -p, 1, 0,
333 px + d, py - d, -p, 1, 1);
334 }
Jason Sams0f505e52009-10-13 17:18:35 -0700335 }
336
337 float y2 = y - 44;
338 float a = 1.f;
339 if (y2 < ymin) {
340 a = 1.f - (ymin - y2) * 0.02f;
341 }
342 if (y > (ymax + 40)) {
343 a = 1.f - (y - (ymax + 40)) * 0.02f;
344 }
345 a = clampf(a, 0, 1);
Jason Sams09c6fc02009-10-16 17:23:23 -0700346 a *= maxf(0, 1.f - p * 5.f);
Jason Sams0f505e52009-10-13 17:18:35 -0700347
Jason Sams41b61c82009-10-15 15:40:54 -0700348 setColor(1, 1, 1, a);
Jason Sams0f505e52009-10-13 17:18:35 -0700349 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_LABEL_IDS, iconNum));
350 drawSpriteScreenspace(x, y - 44, 0,
351 params->bubbleBitmapWidth, params->bubbleBitmapHeight);
352 }
353 iconNum++;
354 }
355 }
356}
357
Jason Sams09c6fc02009-10-16 17:23:23 -0700358void drawStrip(float row, float column, int isTop, int iconNum, float p)
Jason Samsb52dfa02009-10-14 20:16:14 -0700359{
360 if (iconNum < 0) return;
Jason Sams09c6fc02009-10-16 17:23:23 -0700361 int offset = positionStrip(row, column, isTop, p);
Jason Samsb52dfa02009-10-14 20:16:14 -0700362 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, iconNum));
363 if (offset < -20) return;
364 offset = clamp(offset, 0, 199 - 20);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700365 drawSimpleMeshRange(NAMED_SMMesh, offset * 6, 20 * 6);
Jason Samsb52dfa02009-10-14 20:16:14 -0700366}
367
Jason Sams461073b2009-10-20 12:06:28 -0700368void drawTop(float rowOffset, float p)
Jason Sams0f505e52009-10-13 17:18:35 -0700369{
Jason Sams0f505e52009-10-13 17:18:35 -0700370 int row, col;
371 int iconNum = 0;
372 for (row = 0; row < rowOffset; row++) {
373 for (col=0; col < 4; col++) {
374 if (iconNum >= state->iconCount) {
375 return;
376 }
Jason Sams461073b2009-10-20 12:06:28 -0700377 drawStrip(rowOffset - row, col, 1, iconNum, p);
Jason Sams0f505e52009-10-13 17:18:35 -0700378 iconNum++;
379 }
380 }
381}
382
Jason Sams09c6fc02009-10-16 17:23:23 -0700383void drawBottom(float rowOffset, float p)
Jason Sams0f505e52009-10-13 17:18:35 -0700384{
385 float pos = -1.f;
386 int intRowOffset = rowOffset;
387 pos -= rowOffset - intRowOffset;
388
389 int row, col;
390 int iconNum = (intRowOffset + 3) * 4;
391 while (1) {
392 for (col=0; col < 4; col++) {
393 if (iconNum >= state->iconCount) {
394 return;
395 }
396 if (pos > -1) {
Jason Sams09c6fc02009-10-16 17:23:23 -0700397 drawStrip(pos, col, 0, iconNum, p);
Jason Sams0f505e52009-10-13 17:18:35 -0700398 }
399 iconNum++;
400 }
401 pos += 1.f;
402 }
403}
404
405int
406main(int launchID)
407{
408 // Compute dt in seconds.
409 int newTime = uptimeMillis();
410 g_DT = (newTime - g_LastTime) / 1000.f;
411 g_LastTime = newTime;
412
413 if (!g_DrawLastFrame) {
414 // If we stopped rendering we cannot use DT.
415 // assume 30fps in this case.
416 g_DT = 0.033f;
417 }
Jason Samsb52dfa02009-10-14 20:16:14 -0700418 // physics may break if DT is large.
419 g_DT = minf(g_DT, 0.2f);
Jason Sams0f505e52009-10-13 17:18:35 -0700420
421 if (g_Zoom != state->zoomTarget) {
Jason Sams09c6fc02009-10-16 17:23:23 -0700422 float dz;
423 if (state->zoomTarget > 0.5f) {
424 dz = (1 - g_Zoom) * 0.2f;
425 } else {
426 dz = -g_DT - (1 - g_Zoom) * 0.2f;
427 }
428 if (dz && (fabsf(dz) < 0.02f)) {
Jason Sams0f505e52009-10-13 17:18:35 -0700429 if (dz > 0) {
Jason Sams09c6fc02009-10-16 17:23:23 -0700430 dz = 0.02f;
Jason Sams0f505e52009-10-13 17:18:35 -0700431 } else {
Jason Sams09c6fc02009-10-16 17:23:23 -0700432 dz = -0.02f;
Jason Sams0f505e52009-10-13 17:18:35 -0700433 }
434 }
435 if (fabsf(g_Zoom - state->zoomTarget) < fabsf(dz)) {
436 g_Zoom = state->zoomTarget;
437 } else {
438 g_Zoom += dz;
439 }
440 updateReadback();
441 }
442
443 // Set clear value to dim the background based on the zoom position.
Jason Sams41b61c82009-10-15 15:40:54 -0700444 if ((g_Zoom < 0.001f) && (state->zoomTarget < 0.001f) && !g_SpecialHWWar) {
Jason Sams0f505e52009-10-13 17:18:35 -0700445 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
446 // When we're zoomed out and not tracking motion events, reset the pos to 0.
447 if (!g_LastTouchDown) {
448 g_PosPage = 0;
449 }
450 return lastFrame(0);
Jason Sams0f505e52009-10-13 17:18:35 -0700451 } else {
452 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
453 }
454
455 // icons & labels
456 int iconCount = state->iconCount;
457 g_PosMax = ((iconCount + 3) / 4) - 4;
458 if (g_PosMax < 0) g_PosMax = 0;
459
460 updatePos(0.1f);
461 updateReadback();
462
463 //debugF(" draw g_PosPage", g_PosPage);
464
465 // Draw the icons ========================================
466
467 //bindProgramFragment(NAMED_PFColor);
468 //positionStrip(1, 0, 0);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700469 //drawSimpleMesh(NAMED_SMMesh);
Jason Sams0f505e52009-10-13 17:18:35 -0700470
471 bindProgramFragment(NAMED_PFTexLinear);
472
473
Jason Sams461073b2009-10-20 12:06:28 -0700474 drawTop(g_PosPage, 1-g_Zoom);
Jason Sams09c6fc02009-10-16 17:23:23 -0700475 drawBottom(g_PosPage, 1-g_Zoom);
Jason Sams0f505e52009-10-13 17:18:35 -0700476
477 {
478 float mat1[16];
479 matrixLoadIdentity(mat1);
480 vpLoadModelMatrix(mat1);
481 vpLoadTextureMatrix(mat1);
482 }
Jason Sams09c6fc02009-10-16 17:23:23 -0700483 drawFrontGrid(g_PosPage, 1-g_Zoom);
Jason Samsb52dfa02009-10-14 20:16:14 -0700484 draw_home_button();
Jason Sams0f505e52009-10-13 17:18:35 -0700485
Jason Sams41b61c82009-10-15 15:40:54 -0700486
487 // This is a WAR to do a rendering pass without drawing during init to
488 // force the driver to preload and compile its shaders.
489 // Without this the first animation does not appear due to the time it
490 // takes to init the driver state.
491 if (g_SpecialHWWar) {
492 g_SpecialHWWar = 0;
493 return 1;
494 }
495
Jason Sams0f505e52009-10-13 17:18:35 -0700496 // Bug workaround where the last frame is not always displayed
497 // So we keep rendering until the bug is fixed.
498 return lastFrame((g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != state->zoomTarget);
499}
500