blob: 1f279a9b6974d0d1a05b650e9dc58c056224be28 [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 Sams96b49d82009-10-20 14:28:53 -0700260 float x = (SCREEN_WIDTH_PX - params->homeButtonTextureWidth) / 2;
Jason Sams6471c8b2010-01-20 14:15:22 -0800261 float y = -g_Animation * params->homeButtonTextureHeight;
Jason Sams0f505e52009-10-13 17:18:35 -0700262
Jason Samsc8514792009-10-29 14:27:29 -0700263 y -= 30; // move the house to the edge of the screen as it doesn't fill the texture.
Jason Sams96b49d82009-10-20 14:28:53 -0700264 drawSpriteScreenspace(x, y, 0, params->homeButtonTextureWidth, params->homeButtonTextureHeight);
Jason Sams0f505e52009-10-13 17:18:35 -0700265}
266
Jason Sams09c6fc02009-10-16 17:23:23 -0700267void drawFrontGrid(float rowOffset, float p)
Jason Sams0f505e52009-10-13 17:18:35 -0700268{
269 float h = getHeight();
270 float w = getWidth();
271
272 int intRowOffset = rowOffset;
273 float rowFrac = rowOffset - intRowOffset;
274 float colWidth = getWidth() / 4;
275 float rowHeight = colWidth + 25.f;
Jason Samsb4ecab22010-01-19 16:43:26 -0800276 float yoff = 0.5f * h + 1.5f * rowHeight;
Jason Sams0f505e52009-10-13 17:18:35 -0700277
278 int row, col;
Jason Samsb4ecab22010-01-19 16:43:26 -0800279 int iconNum = (intRowOffset - 5) * 4;
Jason Sams0f505e52009-10-13 17:18:35 -0700280
Jason Samsb4ecab22010-01-19 16:43:26 -0800281 bindProgramVertex(NAMED_PVCurve);
282
283 storeF(ALLOC_VP_CONSTANTS, 4, p);
284
285 for (row = -5; row < 15; row++) {
Jason Sams0f505e52009-10-13 17:18:35 -0700286 float y = yoff - ((-rowFrac + row) * rowHeight);
287
288 for (col=0; col < 4; col++) {
289 if (iconNum >= state->iconCount) {
290 return;
291 }
292
293 if (iconNum >= 0) {
Jason Samsb4ecab22010-01-19 16:43:26 -0800294 float x = colWidth * col + (colWidth / 2);
Jason Samsb4ecab22010-01-19 16:43:26 -0800295 storeF(ALLOC_VP_CONSTANTS, 2, x);
Jason Sams1a94ee32010-01-20 13:34:30 -0800296
Jason Sams37f262d2010-01-20 11:19:51 -0800297 if (state->selectedIconIndex == iconNum && !p) {
Jason Sams1a94ee32010-01-20 13:34:30 -0800298 bindProgramFragment(NAMED_PFTexNearest);
299 bindTexture(NAMED_PFTexNearest, 0, state->selectedIconTexture);
300 storeF(ALLOC_VP_CONSTANTS, 0, SELECTION_TEXTURE_WIDTH_PX);
301 storeF(ALLOC_VP_CONSTANTS, 1, SELECTION_TEXTURE_HEIGHT_PX);
302 storeF(ALLOC_VP_CONSTANTS, 3, y - (SELECTION_TEXTURE_HEIGHT_PX - ICON_TEXTURE_HEIGHT_PX) * 0.5f);
303 drawSimpleMesh(NAMED_SMCell);
Jason Sams37f262d2010-01-20 11:19:51 -0800304 }
305
Jason Sams1a94ee32010-01-20 13:34:30 -0800306 bindProgramFragment(NAMED_PFTexMip);
307 storeF(ALLOC_VP_CONSTANTS, 0, ICON_TEXTURE_WIDTH_PX);
308 storeF(ALLOC_VP_CONSTANTS, 1, ICON_TEXTURE_HEIGHT_PX);
309 storeF(ALLOC_VP_CONSTANTS, 3, y);
Jason Samsb4ecab22010-01-19 16:43:26 -0800310 bindTexture(NAMED_PFTexMip, 0, loadI32(ALLOC_ICON_IDS, iconNum));
311 drawSimpleMesh(NAMED_SMCell);
Joe Onorato742d7fc2009-10-15 19:48:16 -0700312
Jason Sams37f262d2010-01-20 11:19:51 -0800313 bindProgramFragment(NAMED_PFTexMipAlpha);
Jason Samsb4ecab22010-01-19 16:43:26 -0800314 storeF(ALLOC_VP_CONSTANTS, 0, 128.f);
315 storeF(ALLOC_VP_CONSTANTS, 1, 64.f);
316 storeF(ALLOC_VP_CONSTANTS, 3, y - 64.f);
Jason Sams37f262d2010-01-20 11:19:51 -0800317 bindTexture(NAMED_PFTexMipAlpha, 0, loadI32(ALLOC_LABEL_IDS, iconNum));
Jason Samsb4ecab22010-01-19 16:43:26 -0800318 drawSimpleMesh(NAMED_SMCell);
Jason Sams0f505e52009-10-13 17:18:35 -0700319 }
320 iconNum++;
321 }
322 }
323}
324
Jason Sams0f505e52009-10-13 17:18:35 -0700325
326int
327main(int launchID)
328{
329 // Compute dt in seconds.
330 int newTime = uptimeMillis();
331 g_DT = (newTime - g_LastTime) / 1000.f;
332 g_LastTime = newTime;
Jason Sams2e19c052009-10-20 18:19:55 -0700333
Jason Sams0f505e52009-10-13 17:18:35 -0700334 if (!g_DrawLastFrame) {
335 // If we stopped rendering we cannot use DT.
336 // assume 30fps in this case.
337 g_DT = 0.033f;
338 }
Jason Samsb52dfa02009-10-14 20:16:14 -0700339 // physics may break if DT is large.
340 g_DT = minf(g_DT, 0.2f);
Jason Sams0f505e52009-10-13 17:18:35 -0700341
342 if (g_Zoom != state->zoomTarget) {
Jason Sams6471c8b2010-01-20 14:15:22 -0800343 float dz = g_DT * 1.7f;
344 if (state->zoomTarget < 0.5f) {
345 dz = -dz;
Jason Sams0f505e52009-10-13 17:18:35 -0700346 }
347 if (fabsf(g_Zoom - state->zoomTarget) < fabsf(dz)) {
348 g_Zoom = state->zoomTarget;
349 } else {
350 g_Zoom += dz;
351 }
Jason Sams6471c8b2010-01-20 14:15:22 -0800352 g_Animation = powf(1-g_Zoom, 3);
Jason Sams0f505e52009-10-13 17:18:35 -0700353 updateReadback();
354 }
355
356 // Set clear value to dim the background based on the zoom position.
Jason Sams41b61c82009-10-15 15:40:54 -0700357 if ((g_Zoom < 0.001f) && (state->zoomTarget < 0.001f) && !g_SpecialHWWar) {
Jason Sams0f505e52009-10-13 17:18:35 -0700358 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
359 // When we're zoomed out and not tracking motion events, reset the pos to 0.
360 if (!g_LastTouchDown) {
361 g_PosPage = 0;
362 }
363 return lastFrame(0);
Jason Sams0f505e52009-10-13 17:18:35 -0700364 } else {
365 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
366 }
367
368 // icons & labels
369 int iconCount = state->iconCount;
370 g_PosMax = ((iconCount + 3) / 4) - 4;
371 if (g_PosMax < 0) g_PosMax = 0;
372
373 updatePos(0.1f);
374 updateReadback();
375
376 //debugF(" draw g_PosPage", g_PosPage);
377
378 // Draw the icons ========================================
Jason Sams6471c8b2010-01-20 14:15:22 -0800379 drawFrontGrid(g_PosPage, g_Animation);
Jason Samsc8514792009-10-29 14:27:29 -0700380
381 bindProgramFragment(NAMED_PFTexNearest);
Jason Samsb52dfa02009-10-14 20:16:14 -0700382 draw_home_button();
Jason Sams0f505e52009-10-13 17:18:35 -0700383
Jason Sams41b61c82009-10-15 15:40:54 -0700384 // This is a WAR to do a rendering pass without drawing during init to
385 // force the driver to preload and compile its shaders.
386 // Without this the first animation does not appear due to the time it
387 // takes to init the driver state.
388 if (g_SpecialHWWar) {
389 g_SpecialHWWar = 0;
390 return 1;
391 }
392
Jason Sams0f505e52009-10-13 17:18:35 -0700393 // Bug workaround where the last frame is not always displayed
394 // So we keep rendering until the bug is fixed.
Mike Cleron7d5d7462009-10-20 14:06:00 -0700395 return lastFrame((g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != state->zoomTarget || (g_MoveToTime != 0));
Jason Sams0f505e52009-10-13 17:18:35 -0700396}
397