blob: 752fa86c214707463e9e6c8623d4d02fcf2dc09e [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;
25
26// Drawing constants, should be parameters ======
27#define VIEW_ANGLE 1.28700222f
28
29int g_DrawLastFrame;
30int lastFrame(int draw) {
31 // We draw one extra frame to work around the last frame post bug.
32 // We also need to track if we drew the last frame to deal with large DT
33 // in the physics.
34 int ret = g_DrawLastFrame | draw;
35 g_DrawLastFrame = draw;
36 return ret; // should return draw instead.
37}
38
39void updateReadback() {
40 if ((g_OldPosPage != g_PosPage) ||
41 (g_OldPosVelocity != g_PosVelocity) ||
42 (g_OldZoom != g_Zoom)) {
43
44 g_OldPosPage = g_PosPage;
45 g_OldPosVelocity = g_PosVelocity;
46 g_OldZoom = g_Zoom;
47
48 int i[3];
49 i[0] = g_PosPage * (1 << 16);
50 i[1] = g_PosVelocity * (1 << 16);
51 i[2] = g_OldZoom * (1 << 16);
52 sendToClient(&i[0], 1, 12, 1);
53 }
54}
55
Jason Sams41b61c82009-10-15 15:40:54 -070056void setColor(float r, float g, float b, float a) {
57 if (g_SpecialHWWar) {
58 color(0, 0, 0, 0.001f);
59 } else {
60 color(r, g, b, a);
61 }
62}
Jason Sams0f505e52009-10-13 17:18:35 -070063
64void init() {
65 g_AttractionTable[0] = 6.5f;
66 g_AttractionTable[1] = 6.5f;
67 g_AttractionTable[2] = 7.0f;
68 g_AttractionTable[3] = 6.0f;
69 g_AttractionTable[4] = -6.0f;
70 g_AttractionTable[5] = -7.0f;
71 g_AttractionTable[6] = -6.5f;
72 g_AttractionTable[7] = -6.5f;
73 g_AttractionTable[8] = -6.5f; // dup 7 to avoid a clamp later
Jason Sams0f505e52009-10-13 17:18:35 -070074 g_PhysicsTableSize = 7;
75
76 g_PosVelocity = 0;
77 g_PosPage = 0;
78 g_LastTouchDown = 0;
79 g_LastPositionX = 0;
80 g_Zoom = 0;
Jason Sams41b61c82009-10-15 15:40:54 -070081 g_SpecialHWWar = 1;
82}
83
84void resetHWWar() {
85 g_SpecialHWWar = 1;
Jason Sams0f505e52009-10-13 17:18:35 -070086}
87
88void move() {
89 if (g_LastTouchDown) {
90 float dx = -(state->newPositionX - g_LastPositionX);
91 g_PosVelocity = 0;
92 g_PosPage += dx * 4;
93
94 float pmin = -0.25f;
95 float pmax = g_PosMax + 0.25f;
96 g_PosPage = clampf(g_PosPage, pmin, pmax);
97 }
98 g_LastTouchDown = state->newTouchDown;
99 g_LastPositionX = state->newPositionX;
100 //debugF("Move P", g_PosPage);
101}
102
103void fling() {
104 g_LastTouchDown = 0;
105 g_PosVelocity = -state->flingVelocityX * 2;
106 float av = fabsf(g_PosVelocity);
107 float minVel = 3.5f;
108
109 minVel *= 1.f - (fabsf(fracf(g_PosPage + 0.5f) - 0.5f) * 0.45f);
110
111 if (av < minVel && av > 0.2f) {
112 if (g_PosVelocity > 0) {
113 g_PosVelocity = minVel;
114 } else {
115 g_PosVelocity = -minVel;
116 }
117 }
118
119 if (g_PosPage <= 0) {
120 g_PosVelocity = maxf(0, g_PosVelocity);
121 }
122 if (g_PosPage > g_PosMax) {
123 g_PosVelocity = minf(0, g_PosVelocity);
124 }
125}
126
Jason Sams0f505e52009-10-13 17:18:35 -0700127float
128modf(float x, float y)
129{
130 return x-(y*floorf(x/y));
131}
132
133void updatePos() {
134 if (g_LastTouchDown) {
135 return;
136 }
137
138 int outOfRange = 0;
139 float tablePosNorm = fracf(g_PosPage + 0.5f);
140 float tablePosF = tablePosNorm * g_PhysicsTableSize;
141 int tablePosI = tablePosF;
142 float tablePosFrac = tablePosF - tablePosI;
143 float accel = lerpf(g_AttractionTable[tablePosI],
144 g_AttractionTable[tablePosI + 1],
145 tablePosFrac) * g_DT;
Jason Samsb52dfa02009-10-14 20:16:14 -0700146 float friction = 4.f * g_DT;
Jason Sams0f505e52009-10-13 17:18:35 -0700147
148 if (g_PosPage < -0.5f) {
149 accel = g_AttractionTable[0] * g_DT;
Jason Sams0f505e52009-10-13 17:18:35 -0700150 outOfRange = 1;
151 }
152 if ((g_PosPage - g_PosMax) > 0.5f) {
153 accel = g_AttractionTable[(int)g_PhysicsTableSize] * g_DT;
Jason Sams0f505e52009-10-13 17:18:35 -0700154 outOfRange = 1;
155 }
156
157 // If our velocity is low OR acceleration is opposing it, apply it.
Jason Samsb52dfa02009-10-14 20:16:14 -0700158 if (fabsf(g_PosVelocity) < 1.0f || (g_PosVelocity * accel) < 0 || outOfRange) {
Jason Sams0f505e52009-10-13 17:18:35 -0700159 g_PosVelocity += accel;
160 }
161
162 if ((friction > fabsf(g_PosVelocity)) &&
163 (friction > fabsf(accel)) &&
164 !outOfRange) {
165 // Special get back to center and overcome friction physics.
166 float t = tablePosNorm - 0.5f;
167 if (fabsf(t) < (friction * g_DT)) {
168 // really close, just snap
169 g_PosPage = roundf(g_PosPage);
170 g_PosVelocity = 0;
171 } else {
172 if (t > 0) {
173 g_PosVelocity = -friction;
174 } else {
175 g_PosVelocity = friction;
176 }
177 }
178 } else {
179 // Normal physics
180 if (g_PosVelocity > 0) {
181 g_PosVelocity -= friction;
182 g_PosVelocity = maxf(g_PosVelocity, 0);
183 } else {
184 g_PosVelocity += friction;
185 g_PosVelocity = minf(g_PosVelocity, 0);
186 }
187 }
188 g_PosPage += g_PosVelocity * g_DT;
189
190 // Check for out of boundry conditions.
191 if (g_PosPage < 0 && g_PosVelocity < 0) {
192 g_PosPage = maxf(g_PosPage, -0.49);
193 float damp = 1.0 + (g_PosPage * 4);
194 damp = clampf(damp, 0.f, 0.9f);
195 g_PosVelocity *= damp;
196 }
197 if (g_PosPage > g_PosMax && g_PosVelocity > 0) {
198 g_PosPage = minf(g_PosPage, g_PosMax + 0.49);
199 float damp = 1.0 - ((g_PosPage - g_PosMax) * 4);
200 damp = clampf(damp, 0.f, 0.9f);
201 g_PosVelocity *= damp;
202 }
203}
204
Jason Sams09c6fc02009-10-16 17:23:23 -0700205int positionStrip(float row, float column, int isTop, float p)
Jason Sams0f505e52009-10-13 17:18:35 -0700206{
207 float mat1[16];
Jason Sams0f505e52009-10-13 17:18:35 -0700208 float x = 0.5f * (column - 1.5f);
Jason Sams0f505e52009-10-13 17:18:35 -0700209 float scale = 72.f * 3 / getWidth();
Jason Sams0f505e52009-10-13 17:18:35 -0700210
211 if (isTop) {
212 matrixLoadTranslate(mat1, x, 0.8f, 0.f);
213 matrixScale(mat1, scale, scale, 1.f);
214 } else {
215 matrixLoadTranslate(mat1, x, -0.9f, 0.f);
216 matrixScale(mat1, scale, -scale, 1.f);
Jason Sams09c6fc02009-10-16 17:23:23 -0700217 matrixTranslate(mat1, 0, p * 2, 0.f);
218 matrixRotate(mat1, -p * 50, 1, 0, 0);
Jason Sams0f505e52009-10-13 17:18:35 -0700219 }
220 vpLoadModelMatrix(mat1);
221
Jason Samsb52dfa02009-10-14 20:16:14 -0700222 float soff = -(row * 1.4);
Jason Sams0f505e52009-10-13 17:18:35 -0700223 if (isTop) {
224 matrixLoadScale(mat1, 1.f, -0.85f, 1.f);
Jason Samsb52dfa02009-10-14 20:16:14 -0700225 matrixTranslate(mat1, 0, soff - 0.97f, 0);
Jason Sams0f505e52009-10-13 17:18:35 -0700226 } else {
227 matrixLoadScale(mat1, 1.f, 0.85f, 1.f);
Jason Samsb52dfa02009-10-14 20:16:14 -0700228 matrixTranslate(mat1, 0, soff - 0.45f, 0);
Jason Sams0f505e52009-10-13 17:18:35 -0700229 }
230 vpLoadTextureMatrix(mat1);
Jason Samsb52dfa02009-10-14 20:16:14 -0700231 return -soff * 10.f;
Jason Sams0f505e52009-10-13 17:18:35 -0700232}
233
234void
235draw_home_button()
236{
Jason Sams41b61c82009-10-15 15:40:54 -0700237 setColor(1.0f, 1.0f, 1.0f, 1.0f);
Joe Onoratod63458b2009-10-15 21:19:09 -0700238 bindTexture(NAMED_PFTexLinear, 0, state->homeButtonId);
Jason Sams0f505e52009-10-13 17:18:35 -0700239
240 float scale = 2.0f / SCREEN_WIDTH_PX;
241
242 float x = 0.0f;
243
244 float y = -(SCREEN_HEIGHT_PX / (float)SCREEN_WIDTH_PX);
245 y += g_Zoom * (scale * params->homeButtonTextureHeight / 2);
246
247 float z = 0.0f;
248 drawSprite(x, y, z, params->homeButtonTextureWidth, params->homeButtonTextureHeight);
249}
250
Jason Sams09c6fc02009-10-16 17:23:23 -0700251void drawFrontGrid(float rowOffset, float p)
Jason Sams0f505e52009-10-13 17:18:35 -0700252{
253 float h = getHeight();
254 float w = getWidth();
255
256 int intRowOffset = rowOffset;
257 float rowFrac = rowOffset - intRowOffset;
258 float colWidth = getWidth() / 4;
259 float rowHeight = colWidth + 25.f;
260 float yoff = h - ((h - (rowHeight * 4.f)) / 2);
261
262 yoff -= 110;
263
264 int row, col;
265 int iconNum = intRowOffset * 4;
266 float ymax = yoff;
267 float ymin = yoff - (3 * rowHeight) - 70;
268
269 for (row = 0; row < 5; row++) {
270 float y = yoff - ((-rowFrac + row) * rowHeight);
271
272 for (col=0; col < 4; col++) {
273 if (iconNum >= state->iconCount) {
274 return;
275 }
276
277 if (iconNum >= 0) {
278 float x = colWidth * col - ((128 - colWidth) / 2);
279
280 if ((y >= ymin) && (y <= ymax)) {
Jason Sams41b61c82009-10-15 15:40:54 -0700281 setColor(1.f, 1.f, 1.f, 1.f);
Joe Onorato742d7fc2009-10-15 19:48:16 -0700282
Jason Sams09c6fc02009-10-16 17:23:23 -0700283 if (state->selectedIconIndex == iconNum && !p) {
Joe Onorato742d7fc2009-10-15 19:48:16 -0700284 bindTexture(NAMED_PFTexLinear, 0, state->selectedIconTexture);
285 drawSpriteScreenspace(x, y, 0, 128, 128);
286 }
287
Jason Sams0f505e52009-10-13 17:18:35 -0700288 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, iconNum));
Jason Sams09c6fc02009-10-16 17:23:23 -0700289 if (!p) {
290 drawSpriteScreenspace(x, y, 0, 128, 128);
291 } else {
292 float px = ((x + 64) - (getWidth() / 2)) / (getWidth() / 2);
293 float py = ((y + 64) - (getHeight() / 2)) / (getWidth() / 2);
294 float d = 64.f / (getWidth() / 2);
295 px *= p + 1;
296 py *= p + 1;
297 drawQuadTexCoords(px - d, py - d, -p, 0, 1,
298 px - d, py + d, -p, 0, 0,
299 px + d, py + d, -p, 1, 0,
300 px + d, py - d, -p, 1, 1);
301 }
Jason Sams0f505e52009-10-13 17:18:35 -0700302 }
303
304 float y2 = y - 44;
305 float a = 1.f;
306 if (y2 < ymin) {
307 a = 1.f - (ymin - y2) * 0.02f;
308 }
309 if (y > (ymax + 40)) {
310 a = 1.f - (y - (ymax + 40)) * 0.02f;
311 }
312 a = clampf(a, 0, 1);
Jason Sams09c6fc02009-10-16 17:23:23 -0700313 a *= maxf(0, 1.f - p * 5.f);
Jason Sams0f505e52009-10-13 17:18:35 -0700314
Jason Sams41b61c82009-10-15 15:40:54 -0700315 setColor(1, 1, 1, a);
Jason Sams0f505e52009-10-13 17:18:35 -0700316 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_LABEL_IDS, iconNum));
317 drawSpriteScreenspace(x, y - 44, 0,
318 params->bubbleBitmapWidth, params->bubbleBitmapHeight);
319 }
320 iconNum++;
321 }
322 }
323}
324
Jason Sams09c6fc02009-10-16 17:23:23 -0700325void drawStrip(float row, float column, int isTop, int iconNum, float p)
Jason Samsb52dfa02009-10-14 20:16:14 -0700326{
327 if (iconNum < 0) return;
Jason Sams09c6fc02009-10-16 17:23:23 -0700328 int offset = positionStrip(row, column, isTop, p);
Jason Samsb52dfa02009-10-14 20:16:14 -0700329 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, iconNum));
330 if (offset < -20) return;
331 offset = clamp(offset, 0, 199 - 20);
332 drawSimpleMeshRange(NAMED_SMMesh2, offset * 6, 20 * 6);
333}
334
Jason Sams0f505e52009-10-13 17:18:35 -0700335void drawTop(float rowOffset)
336{
Jason Sams0f505e52009-10-13 17:18:35 -0700337 int row, col;
338 int iconNum = 0;
339 for (row = 0; row < rowOffset; row++) {
340 for (col=0; col < 4; col++) {
341 if (iconNum >= state->iconCount) {
342 return;
343 }
Jason Sams09c6fc02009-10-16 17:23:23 -0700344 drawStrip(rowOffset - row, col, 1, iconNum, 0);
Jason Sams0f505e52009-10-13 17:18:35 -0700345 iconNum++;
346 }
347 }
348}
349
Jason Sams09c6fc02009-10-16 17:23:23 -0700350void drawBottom(float rowOffset, float p)
Jason Sams0f505e52009-10-13 17:18:35 -0700351{
352 float pos = -1.f;
353 int intRowOffset = rowOffset;
354 pos -= rowOffset - intRowOffset;
355
356 int row, col;
357 int iconNum = (intRowOffset + 3) * 4;
358 while (1) {
359 for (col=0; col < 4; col++) {
360 if (iconNum >= state->iconCount) {
361 return;
362 }
363 if (pos > -1) {
Jason Sams09c6fc02009-10-16 17:23:23 -0700364 drawStrip(pos, col, 0, iconNum, p);
Jason Sams0f505e52009-10-13 17:18:35 -0700365 }
366 iconNum++;
367 }
368 pos += 1.f;
369 }
370}
371
372int
373main(int launchID)
374{
375 // Compute dt in seconds.
376 int newTime = uptimeMillis();
377 g_DT = (newTime - g_LastTime) / 1000.f;
378 g_LastTime = newTime;
379
380 if (!g_DrawLastFrame) {
381 // If we stopped rendering we cannot use DT.
382 // assume 30fps in this case.
383 g_DT = 0.033f;
384 }
Jason Samsb52dfa02009-10-14 20:16:14 -0700385 // physics may break if DT is large.
386 g_DT = minf(g_DT, 0.2f);
Jason Sams0f505e52009-10-13 17:18:35 -0700387
388 if (g_Zoom != state->zoomTarget) {
Jason Sams09c6fc02009-10-16 17:23:23 -0700389 float dz;
390 if (state->zoomTarget > 0.5f) {
391 dz = (1 - g_Zoom) * 0.2f;
392 } else {
393 dz = -g_DT - (1 - g_Zoom) * 0.2f;
394 }
395 if (dz && (fabsf(dz) < 0.02f)) {
Jason Sams0f505e52009-10-13 17:18:35 -0700396 if (dz > 0) {
Jason Sams09c6fc02009-10-16 17:23:23 -0700397 dz = 0.02f;
Jason Sams0f505e52009-10-13 17:18:35 -0700398 } else {
Jason Sams09c6fc02009-10-16 17:23:23 -0700399 dz = -0.02f;
Jason Sams0f505e52009-10-13 17:18:35 -0700400 }
401 }
402 if (fabsf(g_Zoom - state->zoomTarget) < fabsf(dz)) {
403 g_Zoom = state->zoomTarget;
404 } else {
405 g_Zoom += dz;
406 }
407 updateReadback();
408 }
409
410 // Set clear value to dim the background based on the zoom position.
Jason Sams41b61c82009-10-15 15:40:54 -0700411 if ((g_Zoom < 0.001f) && (state->zoomTarget < 0.001f) && !g_SpecialHWWar) {
Jason Sams0f505e52009-10-13 17:18:35 -0700412 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
413 // When we're zoomed out and not tracking motion events, reset the pos to 0.
414 if (!g_LastTouchDown) {
415 g_PosPage = 0;
416 }
417 return lastFrame(0);
Jason Sams0f505e52009-10-13 17:18:35 -0700418 } else {
419 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
420 }
421
422 // icons & labels
423 int iconCount = state->iconCount;
424 g_PosMax = ((iconCount + 3) / 4) - 4;
425 if (g_PosMax < 0) g_PosMax = 0;
426
427 updatePos(0.1f);
428 updateReadback();
429
430 //debugF(" draw g_PosPage", g_PosPage);
431
432 // Draw the icons ========================================
433
434 //bindProgramFragment(NAMED_PFColor);
435 //positionStrip(1, 0, 0);
436 //drawSimpleMesh(NAMED_SMMesh2);
Jason Sams0f505e52009-10-13 17:18:35 -0700437
438 bindProgramFragment(NAMED_PFTexLinear);
439
440
Jason Sams09c6fc02009-10-16 17:23:23 -0700441 drawTop(g_PosPage);
442 drawBottom(g_PosPage, 1-g_Zoom);
Jason Sams0f505e52009-10-13 17:18:35 -0700443
444 {
445 float mat1[16];
446 matrixLoadIdentity(mat1);
447 vpLoadModelMatrix(mat1);
448 vpLoadTextureMatrix(mat1);
449 }
Jason Sams09c6fc02009-10-16 17:23:23 -0700450 drawFrontGrid(g_PosPage, 1-g_Zoom);
Jason Samsb52dfa02009-10-14 20:16:14 -0700451 draw_home_button();
Jason Sams0f505e52009-10-13 17:18:35 -0700452
Jason Sams41b61c82009-10-15 15:40:54 -0700453
454 // This is a WAR to do a rendering pass without drawing during init to
455 // force the driver to preload and compile its shaders.
456 // Without this the first animation does not appear due to the time it
457 // takes to init the driver state.
458 if (g_SpecialHWWar) {
459 g_SpecialHWWar = 0;
460 return 1;
461 }
462
Jason Sams0f505e52009-10-13 17:18:35 -0700463 if (0) {
464 float h = getHeight();
465
466 color(1, 1, 1, 1);
467 bindProgramFragment(NAMED_PFColor);
468 bindProgramVertex(NAMED_PVOrtho);
469 float dy = 145.f;
470 float y = h - ((h - (dy * 4.f)) / 2);
471
472 drawLine(0, y, 0, 480, y, 0);
473 y -= dy;
474 drawLine(0, y, 0, 480, y, 0);
475 y -= dy;
476 drawLine(0, y, 0, 480, y, 0);
477 y -= dy;
478 drawLine(0, y, 0, 480, y, 0);
479 y -= dy;
480 drawLine(0, y, 0, 480, y, 0);
481 }
482
Jason Sams0f505e52009-10-13 17:18:35 -0700483 // Bug workaround where the last frame is not always displayed
484 // So we keep rendering until the bug is fixed.
485 return lastFrame((g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != state->zoomTarget);
486}
487