blob: fa6a8bf0410e05f79ff96e171ffb6dab558b4259 [file] [log] [blame]
Jason Sams0f505e52009-10-13 17:18:35 -07001#pragma version(1)
2#pragma stateVertex(PV)
Jason Samsc8514792009-10-29 14:27:29 -07003#pragma stateFragment(PFTexNearest)
Jason Sams0f505e52009-10-13 17:18:35 -07004#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 Sams2e19c052009-10-20 18:19:55 -070012float g_FrictionTable[9];
Jason Sams0f505e52009-10-13 17:18:35 -070013float g_PhysicsTableSize;
14
15float g_PosPage;
16float g_PosVelocity;
17float g_LastPositionX;
18int g_LastTouchDown;
19float g_DT;
20int g_LastTime;
21int g_PosMax;
22float g_Zoom;
Jason Sams6471c8b2010-01-20 14:15:22 -080023float g_Animation;
Jason Sams0f505e52009-10-13 17:18:35 -070024float g_OldPosPage;
25float g_OldPosVelocity;
26float g_OldZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -070027float g_MoveToTotalTime;
28float g_MoveToTime;
29float g_MoveToOldPos;
30
Jason Sams0f505e52009-10-13 17:18:35 -070031
32// Drawing constants, should be parameters ======
33#define VIEW_ANGLE 1.28700222f
34
35int g_DrawLastFrame;
36int lastFrame(int draw) {
37 // We draw one extra frame to work around the last frame post bug.
38 // We also need to track if we drew the last frame to deal with large DT
39 // in the physics.
40 int ret = g_DrawLastFrame | draw;
41 g_DrawLastFrame = draw;
42 return ret; // should return draw instead.
43}
44
45void updateReadback() {
46 if ((g_OldPosPage != g_PosPage) ||
47 (g_OldPosVelocity != g_PosVelocity) ||
48 (g_OldZoom != g_Zoom)) {
49
50 g_OldPosPage = g_PosPage;
51 g_OldPosVelocity = g_PosVelocity;
52 g_OldZoom = g_Zoom;
53
54 int i[3];
55 i[0] = g_PosPage * (1 << 16);
56 i[1] = g_PosVelocity * (1 << 16);
57 i[2] = g_OldZoom * (1 << 16);
58 sendToClient(&i[0], 1, 12, 1);
59 }
60}
61
Jason Sams41b61c82009-10-15 15:40:54 -070062void setColor(float r, float g, float b, float a) {
63 if (g_SpecialHWWar) {
64 color(0, 0, 0, 0.001f);
65 } else {
66 color(r, g, b, a);
67 }
68}
Jason Sams0f505e52009-10-13 17:18:35 -070069
70void init() {
Jason Sams2e19c052009-10-20 18:19:55 -070071 g_AttractionTable[0] = 20.0f;
72 g_AttractionTable[1] = 20.0f;
Jason Samsc8514792009-10-29 14:27:29 -070073 g_AttractionTable[2] = 20.0f;
Jason Sams2e19c052009-10-20 18:19:55 -070074 g_AttractionTable[3] = 10.0f;
75 g_AttractionTable[4] = -10.0f;
Jason Samsc8514792009-10-29 14:27:29 -070076 g_AttractionTable[5] = -20.0f;
77 g_AttractionTable[6] = -20.0f;
Jason Sams2e19c052009-10-20 18:19:55 -070078 g_AttractionTable[7] = -20.0f;
79 g_AttractionTable[8] = -20.0f; // dup 7 to avoid a clamp later
80 g_FrictionTable[0] = 10.0f;
81 g_FrictionTable[1] = 10.0f;
82 g_FrictionTable[2] = 11.0f;
83 g_FrictionTable[3] = 15.0f;
84 g_FrictionTable[4] = 15.0f;
85 g_FrictionTable[5] = 11.0f;
86 g_FrictionTable[6] = 10.0f;
87 g_FrictionTable[7] = 10.0f;
88 g_FrictionTable[8] = 10.0f; // dup 7 to avoid a clamp later
Jason Sams0f505e52009-10-13 17:18:35 -070089 g_PhysicsTableSize = 7;
90
91 g_PosVelocity = 0;
92 g_PosPage = 0;
93 g_LastTouchDown = 0;
94 g_LastPositionX = 0;
95 g_Zoom = 0;
Jason Sams41b61c82009-10-15 15:40:54 -070096 g_SpecialHWWar = 1;
Jason Samsc1c521e2009-10-19 14:45:45 -070097 g_MoveToTime = 0;
98 g_MoveToOldPos = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -070099 g_MoveToTotalTime = 0.2f; // Duration of scrolling 1 line
Jason Sams41b61c82009-10-15 15:40:54 -0700100}
101
102void resetHWWar() {
103 g_SpecialHWWar = 1;
Jason Sams0f505e52009-10-13 17:18:35 -0700104}
105
106void move() {
107 if (g_LastTouchDown) {
108 float dx = -(state->newPositionX - g_LastPositionX);
109 g_PosVelocity = 0;
Jason Sams6ec11bc2010-01-19 17:56:52 -0800110 g_PosPage += dx * 5.2f;
Jason Sams0f505e52009-10-13 17:18:35 -0700111
Jason Samsc8514792009-10-29 14:27:29 -0700112 float pmin = -0.49f;
113 float pmax = g_PosMax + 0.49f;
Jason Sams0f505e52009-10-13 17:18:35 -0700114 g_PosPage = clampf(g_PosPage, pmin, pmax);
115 }
116 g_LastTouchDown = state->newTouchDown;
117 g_LastPositionX = state->newPositionX;
Jason Samsc1c521e2009-10-19 14:45:45 -0700118 g_MoveToTime = 0;
Jason Sams0f505e52009-10-13 17:18:35 -0700119 //debugF("Move P", g_PosPage);
120}
121
Jason Samsc1c521e2009-10-19 14:45:45 -0700122void moveTo() {
123 g_MoveToTime = g_MoveToTotalTime;
124 g_PosVelocity = 0;
125 g_MoveToOldPos = g_PosPage;
Jason Sams2e19c052009-10-20 18:19:55 -0700126
Mike Cleron7d5d7462009-10-20 14:06:00 -0700127 // debugF("======= moveTo", state->targetPos);
Jason Samsc1c521e2009-10-19 14:45:45 -0700128}
129
Joe Onorato3a8820b2009-11-10 15:06:42 -0800130void setZoom() {
131 g_Zoom = state->zoomTarget;
132 g_DrawLastFrame = 1;
133 updateReadback();
134}
135
Jason Sams0f505e52009-10-13 17:18:35 -0700136void fling() {
137 g_LastTouchDown = 0;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700138 g_PosVelocity = -state->flingVelocity * 4;
Jason Sams0f505e52009-10-13 17:18:35 -0700139 float av = fabsf(g_PosVelocity);
140 float minVel = 3.5f;
141
142 minVel *= 1.f - (fabsf(fracf(g_PosPage + 0.5f) - 0.5f) * 0.45f);
143
144 if (av < minVel && av > 0.2f) {
145 if (g_PosVelocity > 0) {
146 g_PosVelocity = minVel;
147 } else {
148 g_PosVelocity = -minVel;
149 }
150 }
151
152 if (g_PosPage <= 0) {
153 g_PosVelocity = maxf(0, g_PosVelocity);
154 }
155 if (g_PosPage > g_PosMax) {
156 g_PosVelocity = minf(0, g_PosVelocity);
157 }
158}
159
Jason Sams0f505e52009-10-13 17:18:35 -0700160float
161modf(float x, float y)
162{
163 return x-(y*floorf(x/y));
164}
165
Mike Cleron7d5d7462009-10-20 14:06:00 -0700166
167/*
168 * Interpolates values in the range 0..1 to a curve that eases in
169 * and out.
170 */
171float
172getInterpolation(float input) {
173 return (cosf((input + 1) * PI) / 2.0f) + 0.5f;
174}
175
176
Jason Sams0f505e52009-10-13 17:18:35 -0700177void updatePos() {
178 if (g_LastTouchDown) {
179 return;
180 }
181
Jason Sams0f505e52009-10-13 17:18:35 -0700182 float tablePosNorm = fracf(g_PosPage + 0.5f);
183 float tablePosF = tablePosNorm * g_PhysicsTableSize;
184 int tablePosI = tablePosF;
185 float tablePosFrac = tablePosF - tablePosI;
186 float accel = lerpf(g_AttractionTable[tablePosI],
187 g_AttractionTable[tablePosI + 1],
188 tablePosFrac) * g_DT;
Jason Sams2e19c052009-10-20 18:19:55 -0700189 float friction = lerpf(g_FrictionTable[tablePosI],
190 g_FrictionTable[tablePosI + 1],
191 tablePosFrac) * g_DT;
Jason Sams0f505e52009-10-13 17:18:35 -0700192
Jason Samsc1c521e2009-10-19 14:45:45 -0700193 if (g_MoveToTime) {
Jason Samsc8514792009-10-29 14:27:29 -0700194 // New position is old posiition + (total distance) * (interpolated time)
195 g_PosPage = g_MoveToOldPos + (state->targetPos - g_MoveToOldPos) * getInterpolation((g_MoveToTotalTime - g_MoveToTime) / g_MoveToTotalTime);
Jason Samsc1c521e2009-10-19 14:45:45 -0700196 g_MoveToTime -= g_DT;
197 if (g_MoveToTime <= 0) {
198 g_MoveToTime = 0;
199 g_PosPage = state->targetPos;
200 }
201 return;
202 }
203
Jason Sams0f505e52009-10-13 17:18:35 -0700204 // If our velocity is low OR acceleration is opposing it, apply it.
Jason Samsc8514792009-10-29 14:27:29 -0700205 if (fabsf(g_PosVelocity) < 4.0f || (g_PosVelocity * accel) < 0) {
Jason Sams0f505e52009-10-13 17:18:35 -0700206 g_PosVelocity += accel;
207 }
Jason Samsc8514792009-10-29 14:27:29 -0700208 //debugF("g_PosPage", g_PosPage);
209 //debugF(" g_PosVelocity", g_PosVelocity);
210 //debugF(" friction", friction);
211 //debugF(" accel", accel);
Jason Sams0f505e52009-10-13 17:18:35 -0700212
Jason Samsc8514792009-10-29 14:27:29 -0700213 // Normal physics
214 if (g_PosVelocity > 0) {
215 g_PosVelocity -= friction;
216 g_PosVelocity = maxf(g_PosVelocity, 0);
217 } else {
218 g_PosVelocity += friction;
219 g_PosVelocity = minf(g_PosVelocity, 0);
220 }
221
222 if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
Jason Sams0f505e52009-10-13 17:18:35 -0700223 // Special get back to center and overcome friction physics.
224 float t = tablePosNorm - 0.5f;
225 if (fabsf(t) < (friction * g_DT)) {
226 // really close, just snap
227 g_PosPage = roundf(g_PosPage);
228 g_PosVelocity = 0;
229 } else {
230 if (t > 0) {
231 g_PosVelocity = -friction;
232 } else {
233 g_PosVelocity = friction;
234 }
235 }
Jason Sams0f505e52009-10-13 17:18:35 -0700236 }
Jason Sams0f505e52009-10-13 17:18:35 -0700237
238 // Check for out of boundry conditions.
239 if (g_PosPage < 0 && g_PosVelocity < 0) {
Jason Sams0f505e52009-10-13 17:18:35 -0700240 float damp = 1.0 + (g_PosPage * 4);
241 damp = clampf(damp, 0.f, 0.9f);
242 g_PosVelocity *= damp;
243 }
244 if (g_PosPage > g_PosMax && g_PosVelocity > 0) {
Jason Sams0f505e52009-10-13 17:18:35 -0700245 float damp = 1.0 - ((g_PosPage - g_PosMax) * 4);
246 damp = clampf(damp, 0.f, 0.9f);
247 g_PosVelocity *= damp;
248 }
Jason Samsc8514792009-10-29 14:27:29 -0700249
250 g_PosPage += g_PosVelocity * g_DT;
251 g_PosPage = clampf(g_PosPage, -0.49, g_PosMax + 0.49);
Jason Sams0f505e52009-10-13 17:18:35 -0700252}
253
Jason Sams0f505e52009-10-13 17:18:35 -0700254
255void
256draw_home_button()
257{
Jason Sams41b61c82009-10-15 15:40:54 -0700258 setColor(1.0f, 1.0f, 1.0f, 1.0f);
Jason Samsc8514792009-10-29 14:27:29 -0700259 bindTexture(NAMED_PFTexNearest, 0, state->homeButtonId);
Jason Sams0f505e52009-10-13 17:18:35 -0700260
Jason Sams76512482010-02-04 16:31:35 -0800261 float w = getWidth();
262 float h = getHeight();
263
264 float x;
265 float y;
266 if (getWidth() > getHeight()) {
267 x = w - (params->homeButtonTextureWidth * (1 - g_Animation)) + 20;
268 y = (h - params->homeButtonTextureHeight) * 0.5f;
269 } else {
270 x = (w - params->homeButtonTextureWidth) / 2;
271 y = -g_Animation * params->homeButtonTextureHeight;
272 y -= 30; // move the house to the edge of the screen as it doesn't fill the texture.
273 }
274
Jason Sams96b49d82009-10-20 14:28:53 -0700275 drawSpriteScreenspace(x, y, 0, params->homeButtonTextureWidth, params->homeButtonTextureHeight);
Jason Sams0f505e52009-10-13 17:18:35 -0700276}
277
Jason Sams09c6fc02009-10-16 17:23:23 -0700278void drawFrontGrid(float rowOffset, float p)
Jason Sams0f505e52009-10-13 17:18:35 -0700279{
280 float h = getHeight();
281 float w = getWidth();
282
283 int intRowOffset = rowOffset;
284 float rowFrac = rowOffset - intRowOffset;
Jason Sams76512482010-02-04 16:31:35 -0800285 float colWidth = 120.f;//getWidth() / 4;
Jason Sams0f505e52009-10-13 17:18:35 -0700286 float rowHeight = colWidth + 25.f;
Jason Samsb4ecab22010-01-19 16:43:26 -0800287 float yoff = 0.5f * h + 1.5f * rowHeight;
Jason Sams0f505e52009-10-13 17:18:35 -0700288
289 int row, col;
Jason Sams76512482010-02-04 16:31:35 -0800290 int colCount = 4;
291 if (w > h) {
292 colCount = 6;
293 rowHeight -= 12.f;
294 yoff = 0.47f * h + 1.0f * rowHeight;
295 }
296
297 int iconNum = (intRowOffset - 5) * colCount;
298
Jason Sams0f505e52009-10-13 17:18:35 -0700299
Jason Samsb4ecab22010-01-19 16:43:26 -0800300 bindProgramVertex(NAMED_PVCurve);
301
Jason Sams76512482010-02-04 16:31:35 -0800302 vpConstants->Position.z = p;
Jason Samsb4ecab22010-01-19 16:43:26 -0800303
Jason Samse78ace92010-02-02 17:37:44 -0800304 setColor(1.0f, 1.0f, 1.0f, 1.0f);
Jason Samsb4ecab22010-01-19 16:43:26 -0800305 for (row = -5; row < 15; row++) {
Jason Sams0f505e52009-10-13 17:18:35 -0700306 float y = yoff - ((-rowFrac + row) * rowHeight);
307
Jason Sams76512482010-02-04 16:31:35 -0800308 for (col=0; col < colCount; col++) {
Jason Sams0f505e52009-10-13 17:18:35 -0700309 if (iconNum >= state->iconCount) {
310 return;
311 }
312
313 if (iconNum >= 0) {
Jason Samsb4ecab22010-01-19 16:43:26 -0800314 float x = colWidth * col + (colWidth / 2);
Jason Sams6b08ffe2010-02-22 15:41:30 -0800315 vpConstants->Position.x = x + 0.2f;
Jason Sams1a94ee32010-01-20 13:34:30 -0800316
Jason Sams37f262d2010-01-20 11:19:51 -0800317 if (state->selectedIconIndex == iconNum && !p) {
Jason Sams1a94ee32010-01-20 13:34:30 -0800318 bindProgramFragment(NAMED_PFTexNearest);
319 bindTexture(NAMED_PFTexNearest, 0, state->selectedIconTexture);
Jason Sams76512482010-02-04 16:31:35 -0800320 vpConstants->ImgSize.x = SELECTION_TEXTURE_WIDTH_PX;
321 vpConstants->ImgSize.y = SELECTION_TEXTURE_HEIGHT_PX;
322 vpConstants->Position.y = y - (SELECTION_TEXTURE_HEIGHT_PX - ICON_TEXTURE_HEIGHT_PX) * 0.5f;
Jason Sams1a94ee32010-01-20 13:34:30 -0800323 drawSimpleMesh(NAMED_SMCell);
Jason Sams37f262d2010-01-20 11:19:51 -0800324 }
325
Jason Sams1a94ee32010-01-20 13:34:30 -0800326 bindProgramFragment(NAMED_PFTexMip);
Jason Sams76512482010-02-04 16:31:35 -0800327 vpConstants->ImgSize.x = ICON_TEXTURE_WIDTH_PX;
328 vpConstants->ImgSize.y = ICON_TEXTURE_HEIGHT_PX;
Jason Sams6b08ffe2010-02-22 15:41:30 -0800329 vpConstants->Position.y = y - 0.2f;
Jason Samsb4ecab22010-01-19 16:43:26 -0800330 bindTexture(NAMED_PFTexMip, 0, loadI32(ALLOC_ICON_IDS, iconNum));
331 drawSimpleMesh(NAMED_SMCell);
Joe Onorato742d7fc2009-10-15 19:48:16 -0700332
Jason Sams37f262d2010-01-20 11:19:51 -0800333 bindProgramFragment(NAMED_PFTexMipAlpha);
Jason Sams76512482010-02-04 16:31:35 -0800334 vpConstants->ImgSize.x = 120.f;
335 vpConstants->ImgSize.y = 64.f;
Jason Sams6b08ffe2010-02-22 15:41:30 -0800336 vpConstants->Position.y = y - 64.f - 0.2f;
Jason Sams37f262d2010-01-20 11:19:51 -0800337 bindTexture(NAMED_PFTexMipAlpha, 0, loadI32(ALLOC_LABEL_IDS, iconNum));
Jason Samsb4ecab22010-01-19 16:43:26 -0800338 drawSimpleMesh(NAMED_SMCell);
Jason Sams0f505e52009-10-13 17:18:35 -0700339 }
340 iconNum++;
341 }
342 }
343}
344
Jason Sams0f505e52009-10-13 17:18:35 -0700345
346int
347main(int launchID)
348{
349 // Compute dt in seconds.
350 int newTime = uptimeMillis();
351 g_DT = (newTime - g_LastTime) / 1000.f;
352 g_LastTime = newTime;
Jason Sams2e19c052009-10-20 18:19:55 -0700353
Jason Sams0f505e52009-10-13 17:18:35 -0700354 if (!g_DrawLastFrame) {
355 // If we stopped rendering we cannot use DT.
356 // assume 30fps in this case.
357 g_DT = 0.033f;
358 }
Jason Samsb52dfa02009-10-14 20:16:14 -0700359 // physics may break if DT is large.
360 g_DT = minf(g_DT, 0.2f);
Jason Sams0f505e52009-10-13 17:18:35 -0700361
362 if (g_Zoom != state->zoomTarget) {
Jason Sams6471c8b2010-01-20 14:15:22 -0800363 float dz = g_DT * 1.7f;
364 if (state->zoomTarget < 0.5f) {
365 dz = -dz;
Jason Sams0f505e52009-10-13 17:18:35 -0700366 }
367 if (fabsf(g_Zoom - state->zoomTarget) < fabsf(dz)) {
368 g_Zoom = state->zoomTarget;
369 } else {
370 g_Zoom += dz;
371 }
Jason Sams6471c8b2010-01-20 14:15:22 -0800372 g_Animation = powf(1-g_Zoom, 3);
Jason Sams0f505e52009-10-13 17:18:35 -0700373 updateReadback();
374 }
375
376 // Set clear value to dim the background based on the zoom position.
Jason Sams41b61c82009-10-15 15:40:54 -0700377 if ((g_Zoom < 0.001f) && (state->zoomTarget < 0.001f) && !g_SpecialHWWar) {
Jason Sams0f505e52009-10-13 17:18:35 -0700378 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
379 // When we're zoomed out and not tracking motion events, reset the pos to 0.
380 if (!g_LastTouchDown) {
381 g_PosPage = 0;
382 }
383 return lastFrame(0);
Jason Sams0f505e52009-10-13 17:18:35 -0700384 } else {
385 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
386 }
387
388 // icons & labels
389 int iconCount = state->iconCount;
390 g_PosMax = ((iconCount + 3) / 4) - 4;
391 if (g_PosMax < 0) g_PosMax = 0;
392
393 updatePos(0.1f);
394 updateReadback();
395
396 //debugF(" draw g_PosPage", g_PosPage);
397
398 // Draw the icons ========================================
Jason Sams6471c8b2010-01-20 14:15:22 -0800399 drawFrontGrid(g_PosPage, g_Animation);
Jason Samsc8514792009-10-29 14:27:29 -0700400
401 bindProgramFragment(NAMED_PFTexNearest);
Jason Samsb52dfa02009-10-14 20:16:14 -0700402 draw_home_button();
Jason Sams0f505e52009-10-13 17:18:35 -0700403
Jason Sams41b61c82009-10-15 15:40:54 -0700404 // This is a WAR to do a rendering pass without drawing during init to
405 // force the driver to preload and compile its shaders.
406 // Without this the first animation does not appear due to the time it
407 // takes to init the driver state.
408 if (g_SpecialHWWar) {
409 g_SpecialHWWar = 0;
410 return 1;
411 }
412
Jason Sams0f505e52009-10-13 17:18:35 -0700413 // Bug workaround where the last frame is not always displayed
414 // So we keep rendering until the bug is fixed.
Mike Cleron7d5d7462009-10-20 14:06:00 -0700415 return lastFrame((g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != state->zoomTarget || (g_MoveToTime != 0));
Jason Sams0f505e52009-10-13 17:18:35 -0700416}
417