blob: 3c3ebf02bf5f5ced25eb0bf9457ce9bb9bc44656 [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
205int positionStrip(float row, float column, int isTop)
206{
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);
217 }
218 vpLoadModelMatrix(mat1);
219
Jason Samsb52dfa02009-10-14 20:16:14 -0700220 float soff = -(row * 1.4);
Jason Sams0f505e52009-10-13 17:18:35 -0700221 if (isTop) {
222 matrixLoadScale(mat1, 1.f, -0.85f, 1.f);
Jason Samsb52dfa02009-10-14 20:16:14 -0700223 matrixTranslate(mat1, 0, soff - 0.97f, 0);
Jason Sams0f505e52009-10-13 17:18:35 -0700224 } else {
225 matrixLoadScale(mat1, 1.f, 0.85f, 1.f);
Jason Samsb52dfa02009-10-14 20:16:14 -0700226 matrixTranslate(mat1, 0, soff - 0.45f, 0);
Jason Sams0f505e52009-10-13 17:18:35 -0700227 }
228 vpLoadTextureMatrix(mat1);
Jason Samsb52dfa02009-10-14 20:16:14 -0700229 return -soff * 10.f;
Jason Sams0f505e52009-10-13 17:18:35 -0700230}
231
232void
233draw_home_button()
234{
Jason Sams41b61c82009-10-15 15:40:54 -0700235 setColor(1.0f, 1.0f, 1.0f, 1.0f);
Joe Onoratod63458b2009-10-15 21:19:09 -0700236 bindTexture(NAMED_PFTexLinear, 0, state->homeButtonId);
Jason Sams0f505e52009-10-13 17:18:35 -0700237
238 float scale = 2.0f / SCREEN_WIDTH_PX;
239
240 float x = 0.0f;
241
242 float y = -(SCREEN_HEIGHT_PX / (float)SCREEN_WIDTH_PX);
243 y += g_Zoom * (scale * params->homeButtonTextureHeight / 2);
244
245 float z = 0.0f;
246 drawSprite(x, y, z, params->homeButtonTextureWidth, params->homeButtonTextureHeight);
247}
248
249void drawFrontGrid(float rowOffset)
250{
251 float h = getHeight();
252 float w = getWidth();
253
254 int intRowOffset = rowOffset;
255 float rowFrac = rowOffset - intRowOffset;
256 float colWidth = getWidth() / 4;
257 float rowHeight = colWidth + 25.f;
258 float yoff = h - ((h - (rowHeight * 4.f)) / 2);
259
260 yoff -= 110;
261
262 int row, col;
263 int iconNum = intRowOffset * 4;
264 float ymax = yoff;
265 float ymin = yoff - (3 * rowHeight) - 70;
266
267 for (row = 0; row < 5; row++) {
268 float y = yoff - ((-rowFrac + row) * rowHeight);
269
270 for (col=0; col < 4; col++) {
271 if (iconNum >= state->iconCount) {
272 return;
273 }
274
275 if (iconNum >= 0) {
276 float x = colWidth * col - ((128 - colWidth) / 2);
277
278 if ((y >= ymin) && (y <= ymax)) {
Jason Sams41b61c82009-10-15 15:40:54 -0700279 setColor(1.f, 1.f, 1.f, 1.f);
Joe Onorato742d7fc2009-10-15 19:48:16 -0700280
281 if (state->selectedIconIndex == iconNum) {
282 bindTexture(NAMED_PFTexLinear, 0, state->selectedIconTexture);
283 drawSpriteScreenspace(x, y, 0, 128, 128);
284 }
285
Jason Sams0f505e52009-10-13 17:18:35 -0700286 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, iconNum));
287 drawSpriteScreenspace(x, y, 0, 128, 128);
288 }
289
290 float y2 = y - 44;
291 float a = 1.f;
292 if (y2 < ymin) {
293 a = 1.f - (ymin - y2) * 0.02f;
294 }
295 if (y > (ymax + 40)) {
296 a = 1.f - (y - (ymax + 40)) * 0.02f;
297 }
298 a = clampf(a, 0, 1);
299
Jason Sams41b61c82009-10-15 15:40:54 -0700300 setColor(1, 1, 1, a);
Jason Sams0f505e52009-10-13 17:18:35 -0700301 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_LABEL_IDS, iconNum));
302 drawSpriteScreenspace(x, y - 44, 0,
303 params->bubbleBitmapWidth, params->bubbleBitmapHeight);
304 }
305 iconNum++;
306 }
307 }
308}
309
Jason Samsb52dfa02009-10-14 20:16:14 -0700310void drawStrip(float row, float column, int isTop, int iconNum)
311{
312 if (iconNum < 0) return;
313 int offset = positionStrip(row, column, isTop);
314 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, iconNum));
315 if (offset < -20) return;
316 offset = clamp(offset, 0, 199 - 20);
317 drawSimpleMeshRange(NAMED_SMMesh2, offset * 6, 20 * 6);
318}
319
Jason Sams0f505e52009-10-13 17:18:35 -0700320void drawTop(float rowOffset)
321{
Jason Sams0f505e52009-10-13 17:18:35 -0700322 int row, col;
323 int iconNum = 0;
324 for (row = 0; row < rowOffset; row++) {
325 for (col=0; col < 4; col++) {
326 if (iconNum >= state->iconCount) {
327 return;
328 }
Jason Samsb52dfa02009-10-14 20:16:14 -0700329 drawStrip(rowOffset - row, col, 1, iconNum);
Jason Sams0f505e52009-10-13 17:18:35 -0700330 iconNum++;
331 }
332 }
333}
334
335void drawBottom(float rowOffset)
336{
337 float pos = -1.f;
338 int intRowOffset = rowOffset;
339 pos -= rowOffset - intRowOffset;
340
341 int row, col;
342 int iconNum = (intRowOffset + 3) * 4;
343 while (1) {
344 for (col=0; col < 4; col++) {
345 if (iconNum >= state->iconCount) {
346 return;
347 }
348 if (pos > -1) {
Jason Samsb52dfa02009-10-14 20:16:14 -0700349 drawStrip(pos, col, 0, iconNum);
Jason Sams0f505e52009-10-13 17:18:35 -0700350 }
351 iconNum++;
352 }
353 pos += 1.f;
354 }
355}
356
357int
358main(int launchID)
359{
360 // Compute dt in seconds.
361 int newTime = uptimeMillis();
362 g_DT = (newTime - g_LastTime) / 1000.f;
363 g_LastTime = newTime;
364
365 if (!g_DrawLastFrame) {
366 // If we stopped rendering we cannot use DT.
367 // assume 30fps in this case.
368 g_DT = 0.033f;
369 }
Jason Samsb52dfa02009-10-14 20:16:14 -0700370 // physics may break if DT is large.
371 g_DT = minf(g_DT, 0.2f);
Jason Sams0f505e52009-10-13 17:18:35 -0700372
373 if (g_Zoom != state->zoomTarget) {
374 float dz = (state->zoomTarget - g_Zoom) * g_DT * 5;
375 if (dz && (fabsf(dz) < 0.03f)) {
376 if (dz > 0) {
377 dz = 0.03f;
378 } else {
379 dz = -0.03f;
380 }
381 }
382 if (fabsf(g_Zoom - state->zoomTarget) < fabsf(dz)) {
383 g_Zoom = state->zoomTarget;
384 } else {
385 g_Zoom += dz;
386 }
387 updateReadback();
388 }
389
390 // Set clear value to dim the background based on the zoom position.
Jason Sams41b61c82009-10-15 15:40:54 -0700391 if ((g_Zoom < 0.001f) && (state->zoomTarget < 0.001f) && !g_SpecialHWWar) {
Jason Sams0f505e52009-10-13 17:18:35 -0700392 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
393 // When we're zoomed out and not tracking motion events, reset the pos to 0.
394 if (!g_LastTouchDown) {
395 g_PosPage = 0;
396 }
397 return lastFrame(0);
Jason Sams0f505e52009-10-13 17:18:35 -0700398 } else {
399 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
400 }
401
402 // icons & labels
403 int iconCount = state->iconCount;
404 g_PosMax = ((iconCount + 3) / 4) - 4;
405 if (g_PosMax < 0) g_PosMax = 0;
406
407 updatePos(0.1f);
408 updateReadback();
409
410 //debugF(" draw g_PosPage", g_PosPage);
411
412 // Draw the icons ========================================
413
414 //bindProgramFragment(NAMED_PFColor);
415 //positionStrip(1, 0, 0);
416 //drawSimpleMesh(NAMED_SMMesh2);
Jason Sams0f505e52009-10-13 17:18:35 -0700417
418 bindProgramFragment(NAMED_PFTexLinear);
419
420
Jason Samsb52dfa02009-10-14 20:16:14 -0700421 float zoomOffset = 8.f * (1 - g_Zoom);
422 drawTop(g_PosPage - zoomOffset);
423 drawBottom(g_PosPage - zoomOffset);
424 drawFrontGrid(g_PosPage - zoomOffset);
Jason Sams0f505e52009-10-13 17:18:35 -0700425
426 {
427 float mat1[16];
428 matrixLoadIdentity(mat1);
429 vpLoadModelMatrix(mat1);
430 vpLoadTextureMatrix(mat1);
431 }
Jason Samsb52dfa02009-10-14 20:16:14 -0700432 draw_home_button();
Jason Sams0f505e52009-10-13 17:18:35 -0700433
Jason Sams41b61c82009-10-15 15:40:54 -0700434
435 // This is a WAR to do a rendering pass without drawing during init to
436 // force the driver to preload and compile its shaders.
437 // Without this the first animation does not appear due to the time it
438 // takes to init the driver state.
439 if (g_SpecialHWWar) {
440 g_SpecialHWWar = 0;
441 return 1;
442 }
443
Jason Sams0f505e52009-10-13 17:18:35 -0700444 if (0) {
445 float h = getHeight();
446
447 color(1, 1, 1, 1);
448 bindProgramFragment(NAMED_PFColor);
449 bindProgramVertex(NAMED_PVOrtho);
450 float dy = 145.f;
451 float y = h - ((h - (dy * 4.f)) / 2);
452
453 drawLine(0, y, 0, 480, y, 0);
454 y -= dy;
455 drawLine(0, y, 0, 480, y, 0);
456 y -= dy;
457 drawLine(0, y, 0, 480, y, 0);
458 y -= dy;
459 drawLine(0, y, 0, 480, y, 0);
460 y -= dy;
461 drawLine(0, y, 0, 480, y, 0);
462 }
463
Jason Sams0f505e52009-10-13 17:18:35 -0700464 // Bug workaround where the last frame is not always displayed
465 // So we keep rendering until the bug is fixed.
466 return lastFrame((g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != state->zoomTarget);
467}
468