blob: 2aefa291a8644ffabdbc162d39bd65e191dcd3cc [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);
Jason Sams0f505e52009-10-13 17:18:35 -0700236 bindTexture(NAMED_PFTexLinear, 0, params->homeButtonId);
237
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);
Jason Sams0f505e52009-10-13 17:18:35 -0700280 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, iconNum));
281 drawSpriteScreenspace(x, y, 0, 128, 128);
282 }
283
284 float y2 = y - 44;
285 float a = 1.f;
286 if (y2 < ymin) {
287 a = 1.f - (ymin - y2) * 0.02f;
288 }
289 if (y > (ymax + 40)) {
290 a = 1.f - (y - (ymax + 40)) * 0.02f;
291 }
292 a = clampf(a, 0, 1);
293
Jason Sams41b61c82009-10-15 15:40:54 -0700294 setColor(1, 1, 1, a);
Jason Sams0f505e52009-10-13 17:18:35 -0700295 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_LABEL_IDS, iconNum));
296 drawSpriteScreenspace(x, y - 44, 0,
297 params->bubbleBitmapWidth, params->bubbleBitmapHeight);
298 }
299 iconNum++;
300 }
301 }
302}
303
Jason Samsb52dfa02009-10-14 20:16:14 -0700304void drawStrip(float row, float column, int isTop, int iconNum)
305{
306 if (iconNum < 0) return;
307 int offset = positionStrip(row, column, isTop);
308 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, iconNum));
309 if (offset < -20) return;
310 offset = clamp(offset, 0, 199 - 20);
311 drawSimpleMeshRange(NAMED_SMMesh2, offset * 6, 20 * 6);
312}
313
Jason Sams0f505e52009-10-13 17:18:35 -0700314void drawTop(float rowOffset)
315{
Jason Sams0f505e52009-10-13 17:18:35 -0700316 int row, col;
317 int iconNum = 0;
318 for (row = 0; row < rowOffset; row++) {
319 for (col=0; col < 4; col++) {
320 if (iconNum >= state->iconCount) {
321 return;
322 }
Jason Samsb52dfa02009-10-14 20:16:14 -0700323 drawStrip(rowOffset - row, col, 1, iconNum);
Jason Sams0f505e52009-10-13 17:18:35 -0700324 iconNum++;
325 }
326 }
327}
328
329void drawBottom(float rowOffset)
330{
331 float pos = -1.f;
332 int intRowOffset = rowOffset;
333 pos -= rowOffset - intRowOffset;
334
335 int row, col;
336 int iconNum = (intRowOffset + 3) * 4;
337 while (1) {
338 for (col=0; col < 4; col++) {
339 if (iconNum >= state->iconCount) {
340 return;
341 }
342 if (pos > -1) {
Jason Samsb52dfa02009-10-14 20:16:14 -0700343 drawStrip(pos, col, 0, iconNum);
Jason Sams0f505e52009-10-13 17:18:35 -0700344 }
345 iconNum++;
346 }
347 pos += 1.f;
348 }
349}
350
351int
352main(int launchID)
353{
354 // Compute dt in seconds.
355 int newTime = uptimeMillis();
356 g_DT = (newTime - g_LastTime) / 1000.f;
357 g_LastTime = newTime;
358
359 if (!g_DrawLastFrame) {
360 // If we stopped rendering we cannot use DT.
361 // assume 30fps in this case.
362 g_DT = 0.033f;
363 }
Jason Samsb52dfa02009-10-14 20:16:14 -0700364 // physics may break if DT is large.
365 g_DT = minf(g_DT, 0.2f);
Jason Sams0f505e52009-10-13 17:18:35 -0700366
367 if (g_Zoom != state->zoomTarget) {
368 float dz = (state->zoomTarget - g_Zoom) * g_DT * 5;
369 if (dz && (fabsf(dz) < 0.03f)) {
370 if (dz > 0) {
371 dz = 0.03f;
372 } else {
373 dz = -0.03f;
374 }
375 }
376 if (fabsf(g_Zoom - state->zoomTarget) < fabsf(dz)) {
377 g_Zoom = state->zoomTarget;
378 } else {
379 g_Zoom += dz;
380 }
381 updateReadback();
382 }
383
384 // Set clear value to dim the background based on the zoom position.
Jason Sams41b61c82009-10-15 15:40:54 -0700385 if ((g_Zoom < 0.001f) && (state->zoomTarget < 0.001f) && !g_SpecialHWWar) {
Jason Sams0f505e52009-10-13 17:18:35 -0700386 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
387 // When we're zoomed out and not tracking motion events, reset the pos to 0.
388 if (!g_LastTouchDown) {
389 g_PosPage = 0;
390 }
391 return lastFrame(0);
Jason Sams0f505e52009-10-13 17:18:35 -0700392 } else {
393 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
394 }
395
396 // icons & labels
397 int iconCount = state->iconCount;
398 g_PosMax = ((iconCount + 3) / 4) - 4;
399 if (g_PosMax < 0) g_PosMax = 0;
400
401 updatePos(0.1f);
402 updateReadback();
403
404 //debugF(" draw g_PosPage", g_PosPage);
405
406 // Draw the icons ========================================
407
408 //bindProgramFragment(NAMED_PFColor);
409 //positionStrip(1, 0, 0);
410 //drawSimpleMesh(NAMED_SMMesh2);
Jason Sams0f505e52009-10-13 17:18:35 -0700411
412 bindProgramFragment(NAMED_PFTexLinear);
413
414
Jason Samsb52dfa02009-10-14 20:16:14 -0700415 float zoomOffset = 8.f * (1 - g_Zoom);
416 drawTop(g_PosPage - zoomOffset);
417 drawBottom(g_PosPage - zoomOffset);
418 drawFrontGrid(g_PosPage - zoomOffset);
Jason Sams0f505e52009-10-13 17:18:35 -0700419
420 {
421 float mat1[16];
422 matrixLoadIdentity(mat1);
423 vpLoadModelMatrix(mat1);
424 vpLoadTextureMatrix(mat1);
425 }
Jason Samsb52dfa02009-10-14 20:16:14 -0700426 draw_home_button();
Jason Sams0f505e52009-10-13 17:18:35 -0700427
Jason Sams41b61c82009-10-15 15:40:54 -0700428
429 // This is a WAR to do a rendering pass without drawing during init to
430 // force the driver to preload and compile its shaders.
431 // Without this the first animation does not appear due to the time it
432 // takes to init the driver state.
433 if (g_SpecialHWWar) {
434 g_SpecialHWWar = 0;
435 return 1;
436 }
437
Jason Sams0f505e52009-10-13 17:18:35 -0700438 if (0) {
439 float h = getHeight();
440
441 color(1, 1, 1, 1);
442 bindProgramFragment(NAMED_PFColor);
443 bindProgramVertex(NAMED_PVOrtho);
444 float dy = 145.f;
445 float y = h - ((h - (dy * 4.f)) / 2);
446
447 drawLine(0, y, 0, 480, y, 0);
448 y -= dy;
449 drawLine(0, y, 0, 480, y, 0);
450 y -= dy;
451 drawLine(0, y, 0, 480, y, 0);
452 y -= dy;
453 drawLine(0, y, 0, 480, y, 0);
454 y -= dy;
455 drawLine(0, y, 0, 480, y, 0);
456 }
457
Jason Sams0f505e52009-10-13 17:18:35 -0700458 // Bug workaround where the last frame is not always displayed
459 // So we keep rendering until the bug is fixed.
460 return lastFrame((g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != state->zoomTarget);
461}
462