blob: 312800fc4e4a74fc426e094dfde86f6cbbd9775c [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 Sams2a131552010-02-26 13:50:31 -080096 g_Animation = 1.f;
Jason Sams41b61c82009-10-15 15:40:54 -070097 g_SpecialHWWar = 1;
Jason Samsc1c521e2009-10-19 14:45:45 -070098 g_MoveToTime = 0;
99 g_MoveToOldPos = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700100 g_MoveToTotalTime = 0.2f; // Duration of scrolling 1 line
Jason Sams41b61c82009-10-15 15:40:54 -0700101}
102
103void resetHWWar() {
104 g_SpecialHWWar = 1;
Jason Sams0f505e52009-10-13 17:18:35 -0700105}
106
107void move() {
108 if (g_LastTouchDown) {
109 float dx = -(state->newPositionX - g_LastPositionX);
110 g_PosVelocity = 0;
Jason Sams6ec11bc2010-01-19 17:56:52 -0800111 g_PosPage += dx * 5.2f;
Jason Sams0f505e52009-10-13 17:18:35 -0700112
Jason Samsc8514792009-10-29 14:27:29 -0700113 float pmin = -0.49f;
114 float pmax = g_PosMax + 0.49f;
Jason Sams0f505e52009-10-13 17:18:35 -0700115 g_PosPage = clampf(g_PosPage, pmin, pmax);
116 }
117 g_LastTouchDown = state->newTouchDown;
118 g_LastPositionX = state->newPositionX;
Jason Samsc1c521e2009-10-19 14:45:45 -0700119 g_MoveToTime = 0;
Jason Sams0f505e52009-10-13 17:18:35 -0700120 //debugF("Move P", g_PosPage);
121}
122
Jason Samsc1c521e2009-10-19 14:45:45 -0700123void moveTo() {
124 g_MoveToTime = g_MoveToTotalTime;
125 g_PosVelocity = 0;
126 g_MoveToOldPos = g_PosPage;
Jason Sams2e19c052009-10-20 18:19:55 -0700127
Mike Cleron7d5d7462009-10-20 14:06:00 -0700128 // debugF("======= moveTo", state->targetPos);
Jason Samsc1c521e2009-10-19 14:45:45 -0700129}
130
Joe Onorato3a8820b2009-11-10 15:06:42 -0800131void setZoom() {
132 g_Zoom = state->zoomTarget;
133 g_DrawLastFrame = 1;
134 updateReadback();
135}
136
Jason Sams0f505e52009-10-13 17:18:35 -0700137void fling() {
138 g_LastTouchDown = 0;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700139 g_PosVelocity = -state->flingVelocity * 4;
Jason Sams0f505e52009-10-13 17:18:35 -0700140 float av = fabsf(g_PosVelocity);
141 float minVel = 3.5f;
142
143 minVel *= 1.f - (fabsf(fracf(g_PosPage + 0.5f) - 0.5f) * 0.45f);
144
145 if (av < minVel && av > 0.2f) {
146 if (g_PosVelocity > 0) {
147 g_PosVelocity = minVel;
148 } else {
149 g_PosVelocity = -minVel;
150 }
151 }
152
153 if (g_PosPage <= 0) {
154 g_PosVelocity = maxf(0, g_PosVelocity);
155 }
156 if (g_PosPage > g_PosMax) {
157 g_PosVelocity = minf(0, g_PosVelocity);
158 }
159}
160
Jason Sams0f505e52009-10-13 17:18:35 -0700161float
162modf(float x, float y)
163{
164 return x-(y*floorf(x/y));
165}
166
Mike Cleron7d5d7462009-10-20 14:06:00 -0700167
168/*
169 * Interpolates values in the range 0..1 to a curve that eases in
170 * and out.
171 */
172float
173getInterpolation(float input) {
174 return (cosf((input + 1) * PI) / 2.0f) + 0.5f;
175}
176
177
Jason Sams0f505e52009-10-13 17:18:35 -0700178void updatePos() {
179 if (g_LastTouchDown) {
180 return;
181 }
182
Jason Sams0f505e52009-10-13 17:18:35 -0700183 float tablePosNorm = fracf(g_PosPage + 0.5f);
184 float tablePosF = tablePosNorm * g_PhysicsTableSize;
185 int tablePosI = tablePosF;
186 float tablePosFrac = tablePosF - tablePosI;
187 float accel = lerpf(g_AttractionTable[tablePosI],
188 g_AttractionTable[tablePosI + 1],
189 tablePosFrac) * g_DT;
Jason Sams2e19c052009-10-20 18:19:55 -0700190 float friction = lerpf(g_FrictionTable[tablePosI],
191 g_FrictionTable[tablePosI + 1],
192 tablePosFrac) * g_DT;
Jason Sams0f505e52009-10-13 17:18:35 -0700193
Jason Samsc1c521e2009-10-19 14:45:45 -0700194 if (g_MoveToTime) {
Jason Samsc8514792009-10-29 14:27:29 -0700195 // New position is old posiition + (total distance) * (interpolated time)
196 g_PosPage = g_MoveToOldPos + (state->targetPos - g_MoveToOldPos) * getInterpolation((g_MoveToTotalTime - g_MoveToTime) / g_MoveToTotalTime);
Jason Samsc1c521e2009-10-19 14:45:45 -0700197 g_MoveToTime -= g_DT;
198 if (g_MoveToTime <= 0) {
199 g_MoveToTime = 0;
200 g_PosPage = state->targetPos;
201 }
202 return;
203 }
204
Jason Sams0f505e52009-10-13 17:18:35 -0700205 // If our velocity is low OR acceleration is opposing it, apply it.
Jason Samsc8514792009-10-29 14:27:29 -0700206 if (fabsf(g_PosVelocity) < 4.0f || (g_PosVelocity * accel) < 0) {
Jason Sams0f505e52009-10-13 17:18:35 -0700207 g_PosVelocity += accel;
208 }
Jason Samsc8514792009-10-29 14:27:29 -0700209 //debugF("g_PosPage", g_PosPage);
210 //debugF(" g_PosVelocity", g_PosVelocity);
211 //debugF(" friction", friction);
212 //debugF(" accel", accel);
Jason Sams0f505e52009-10-13 17:18:35 -0700213
Jason Samsc8514792009-10-29 14:27:29 -0700214 // Normal physics
215 if (g_PosVelocity > 0) {
216 g_PosVelocity -= friction;
217 g_PosVelocity = maxf(g_PosVelocity, 0);
218 } else {
219 g_PosVelocity += friction;
220 g_PosVelocity = minf(g_PosVelocity, 0);
221 }
222
223 if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
Jason Sams0f505e52009-10-13 17:18:35 -0700224 // Special get back to center and overcome friction physics.
225 float t = tablePosNorm - 0.5f;
226 if (fabsf(t) < (friction * g_DT)) {
227 // really close, just snap
228 g_PosPage = roundf(g_PosPage);
229 g_PosVelocity = 0;
230 } else {
231 if (t > 0) {
232 g_PosVelocity = -friction;
233 } else {
234 g_PosVelocity = friction;
235 }
236 }
Jason Sams0f505e52009-10-13 17:18:35 -0700237 }
Jason Sams0f505e52009-10-13 17:18:35 -0700238
239 // Check for out of boundry conditions.
240 if (g_PosPage < 0 && g_PosVelocity < 0) {
Jason Sams0f505e52009-10-13 17:18:35 -0700241 float damp = 1.0 + (g_PosPage * 4);
242 damp = clampf(damp, 0.f, 0.9f);
243 g_PosVelocity *= damp;
244 }
245 if (g_PosPage > g_PosMax && g_PosVelocity > 0) {
Jason Sams0f505e52009-10-13 17:18:35 -0700246 float damp = 1.0 - ((g_PosPage - g_PosMax) * 4);
247 damp = clampf(damp, 0.f, 0.9f);
248 g_PosVelocity *= damp;
249 }
Jason Samsc8514792009-10-29 14:27:29 -0700250
251 g_PosPage += g_PosVelocity * g_DT;
252 g_PosPage = clampf(g_PosPage, -0.49, g_PosMax + 0.49);
Jason Sams0f505e52009-10-13 17:18:35 -0700253}
254
Jason Sams0f505e52009-10-13 17:18:35 -0700255
256void
257draw_home_button()
258{
Jason Sams41b61c82009-10-15 15:40:54 -0700259 setColor(1.0f, 1.0f, 1.0f, 1.0f);
Jason Samsc8514792009-10-29 14:27:29 -0700260 bindTexture(NAMED_PFTexNearest, 0, state->homeButtonId);
Jason Sams0f505e52009-10-13 17:18:35 -0700261
Jason Sams76512482010-02-04 16:31:35 -0800262 float w = getWidth();
263 float h = getHeight();
264
265 float x;
266 float y;
267 if (getWidth() > getHeight()) {
268 x = w - (params->homeButtonTextureWidth * (1 - g_Animation)) + 20;
269 y = (h - params->homeButtonTextureHeight) * 0.5f;
270 } else {
271 x = (w - params->homeButtonTextureWidth) / 2;
272 y = -g_Animation * params->homeButtonTextureHeight;
273 y -= 30; // move the house to the edge of the screen as it doesn't fill the texture.
274 }
275
Jason Sams96b49d82009-10-20 14:28:53 -0700276 drawSpriteScreenspace(x, y, 0, params->homeButtonTextureWidth, params->homeButtonTextureHeight);
Jason Sams0f505e52009-10-13 17:18:35 -0700277}
278
Jason Sams09c6fc02009-10-16 17:23:23 -0700279void drawFrontGrid(float rowOffset, float p)
Jason Sams0f505e52009-10-13 17:18:35 -0700280{
281 float h = getHeight();
282 float w = getWidth();
283
284 int intRowOffset = rowOffset;
285 float rowFrac = rowOffset - intRowOffset;
Jason Sams76512482010-02-04 16:31:35 -0800286 float colWidth = 120.f;//getWidth() / 4;
Jason Sams0f505e52009-10-13 17:18:35 -0700287 float rowHeight = colWidth + 25.f;
Jason Samsb4ecab22010-01-19 16:43:26 -0800288 float yoff = 0.5f * h + 1.5f * rowHeight;
Jason Sams0f505e52009-10-13 17:18:35 -0700289
290 int row, col;
Jason Sams76512482010-02-04 16:31:35 -0800291 int colCount = 4;
292 if (w > h) {
293 colCount = 6;
294 rowHeight -= 12.f;
295 yoff = 0.47f * h + 1.0f * rowHeight;
296 }
297
298 int iconNum = (intRowOffset - 5) * colCount;
299
Jason Sams0f505e52009-10-13 17:18:35 -0700300
Jason Samsb4ecab22010-01-19 16:43:26 -0800301 bindProgramVertex(NAMED_PVCurve);
302
Jason Sams76512482010-02-04 16:31:35 -0800303 vpConstants->Position.z = p;
Jason Samsb4ecab22010-01-19 16:43:26 -0800304
Jason Samse78ace92010-02-02 17:37:44 -0800305 setColor(1.0f, 1.0f, 1.0f, 1.0f);
Jason Samsb4ecab22010-01-19 16:43:26 -0800306 for (row = -5; row < 15; row++) {
Jason Sams0f505e52009-10-13 17:18:35 -0700307 float y = yoff - ((-rowFrac + row) * rowHeight);
308
Jason Sams76512482010-02-04 16:31:35 -0800309 for (col=0; col < colCount; col++) {
Jason Sams0f505e52009-10-13 17:18:35 -0700310 if (iconNum >= state->iconCount) {
311 return;
312 }
313
314 if (iconNum >= 0) {
Jason Samsb4ecab22010-01-19 16:43:26 -0800315 float x = colWidth * col + (colWidth / 2);
Jason Sams6b08ffe2010-02-22 15:41:30 -0800316 vpConstants->Position.x = x + 0.2f;
Jason Sams1a94ee32010-01-20 13:34:30 -0800317
Jason Sams37f262d2010-01-20 11:19:51 -0800318 if (state->selectedIconIndex == iconNum && !p) {
Jason Sams1a94ee32010-01-20 13:34:30 -0800319 bindProgramFragment(NAMED_PFTexNearest);
320 bindTexture(NAMED_PFTexNearest, 0, state->selectedIconTexture);
Jason Sams76512482010-02-04 16:31:35 -0800321 vpConstants->ImgSize.x = SELECTION_TEXTURE_WIDTH_PX;
322 vpConstants->ImgSize.y = SELECTION_TEXTURE_HEIGHT_PX;
323 vpConstants->Position.y = y - (SELECTION_TEXTURE_HEIGHT_PX - ICON_TEXTURE_HEIGHT_PX) * 0.5f;
Jason Sams1a94ee32010-01-20 13:34:30 -0800324 drawSimpleMesh(NAMED_SMCell);
Jason Sams37f262d2010-01-20 11:19:51 -0800325 }
326
Jason Sams1a94ee32010-01-20 13:34:30 -0800327 bindProgramFragment(NAMED_PFTexMip);
Jason Sams76512482010-02-04 16:31:35 -0800328 vpConstants->ImgSize.x = ICON_TEXTURE_WIDTH_PX;
329 vpConstants->ImgSize.y = ICON_TEXTURE_HEIGHT_PX;
Jason Sams6b08ffe2010-02-22 15:41:30 -0800330 vpConstants->Position.y = y - 0.2f;
Jason Samsb4ecab22010-01-19 16:43:26 -0800331 bindTexture(NAMED_PFTexMip, 0, loadI32(ALLOC_ICON_IDS, iconNum));
332 drawSimpleMesh(NAMED_SMCell);
Joe Onorato742d7fc2009-10-15 19:48:16 -0700333
Jason Sams37f262d2010-01-20 11:19:51 -0800334 bindProgramFragment(NAMED_PFTexMipAlpha);
Jason Sams76512482010-02-04 16:31:35 -0800335 vpConstants->ImgSize.x = 120.f;
336 vpConstants->ImgSize.y = 64.f;
Jason Sams6b08ffe2010-02-22 15:41:30 -0800337 vpConstants->Position.y = y - 64.f - 0.2f;
Jason Sams37f262d2010-01-20 11:19:51 -0800338 bindTexture(NAMED_PFTexMipAlpha, 0, loadI32(ALLOC_LABEL_IDS, iconNum));
Jason Samsb4ecab22010-01-19 16:43:26 -0800339 drawSimpleMesh(NAMED_SMCell);
Jason Sams0f505e52009-10-13 17:18:35 -0700340 }
341 iconNum++;
342 }
343 }
344}
345
Jason Sams0f505e52009-10-13 17:18:35 -0700346
347int
348main(int launchID)
349{
350 // Compute dt in seconds.
351 int newTime = uptimeMillis();
352 g_DT = (newTime - g_LastTime) / 1000.f;
353 g_LastTime = newTime;
Jason Sams2e19c052009-10-20 18:19:55 -0700354
Jason Sams0f505e52009-10-13 17:18:35 -0700355 if (!g_DrawLastFrame) {
356 // If we stopped rendering we cannot use DT.
357 // assume 30fps in this case.
358 g_DT = 0.033f;
359 }
Jason Samsb52dfa02009-10-14 20:16:14 -0700360 // physics may break if DT is large.
361 g_DT = minf(g_DT, 0.2f);
Jason Sams0f505e52009-10-13 17:18:35 -0700362
363 if (g_Zoom != state->zoomTarget) {
Jason Sams6471c8b2010-01-20 14:15:22 -0800364 float dz = g_DT * 1.7f;
365 if (state->zoomTarget < 0.5f) {
366 dz = -dz;
Jason Sams0f505e52009-10-13 17:18:35 -0700367 }
368 if (fabsf(g_Zoom - state->zoomTarget) < fabsf(dz)) {
369 g_Zoom = state->zoomTarget;
370 } else {
371 g_Zoom += dz;
372 }
373 updateReadback();
374 }
Jason Sams638a93d2010-03-08 17:03:51 -0800375 g_Animation = powf(1-g_Zoom, 3);
Jason Sams0f505e52009-10-13 17:18:35 -0700376
377 // Set clear value to dim the background based on the zoom position.
Jason Sams41b61c82009-10-15 15:40:54 -0700378 if ((g_Zoom < 0.001f) && (state->zoomTarget < 0.001f) && !g_SpecialHWWar) {
Jason Sams0f505e52009-10-13 17:18:35 -0700379 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
380 // When we're zoomed out and not tracking motion events, reset the pos to 0.
381 if (!g_LastTouchDown) {
382 g_PosPage = 0;
383 }
384 return lastFrame(0);
Jason Sams0f505e52009-10-13 17:18:35 -0700385 } else {
386 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
387 }
388
389 // icons & labels
390 int iconCount = state->iconCount;
391 g_PosMax = ((iconCount + 3) / 4) - 4;
392 if (g_PosMax < 0) g_PosMax = 0;
393
394 updatePos(0.1f);
395 updateReadback();
396
397 //debugF(" draw g_PosPage", g_PosPage);
398
399 // Draw the icons ========================================
Jason Sams6471c8b2010-01-20 14:15:22 -0800400 drawFrontGrid(g_PosPage, g_Animation);
Jason Samsc8514792009-10-29 14:27:29 -0700401
402 bindProgramFragment(NAMED_PFTexNearest);
Jason Samsb52dfa02009-10-14 20:16:14 -0700403 draw_home_button();
Jason Sams0f505e52009-10-13 17:18:35 -0700404
Jason Sams41b61c82009-10-15 15:40:54 -0700405 // This is a WAR to do a rendering pass without drawing during init to
406 // force the driver to preload and compile its shaders.
407 // Without this the first animation does not appear due to the time it
408 // takes to init the driver state.
409 if (g_SpecialHWWar) {
410 g_SpecialHWWar = 0;
411 return 1;
412 }
413
Jason Sams0f505e52009-10-13 17:18:35 -0700414 // Bug workaround where the last frame is not always displayed
415 // So we keep rendering until the bug is fixed.
Mike Cleron7d5d7462009-10-20 14:06:00 -0700416 return lastFrame((g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != state->zoomTarget || (g_MoveToTime != 0));
Jason Sams0f505e52009-10-13 17:18:35 -0700417}
418