blob: abcf0dc4702248ba47ceef35e35b80d19d627aea [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;
23float g_OldPosPage;
24float g_OldPosVelocity;
25float g_OldZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -070026float g_MoveToTotalTime;
27float g_MoveToTime;
28float g_MoveToOldPos;
29
Jason Sams0f505e52009-10-13 17:18:35 -070030
31// Drawing constants, should be parameters ======
32#define VIEW_ANGLE 1.28700222f
33
34int g_DrawLastFrame;
35int lastFrame(int draw) {
36 // We draw one extra frame to work around the last frame post bug.
37 // We also need to track if we drew the last frame to deal with large DT
38 // in the physics.
39 int ret = g_DrawLastFrame | draw;
40 g_DrawLastFrame = draw;
41 return ret; // should return draw instead.
42}
43
44void updateReadback() {
45 if ((g_OldPosPage != g_PosPage) ||
46 (g_OldPosVelocity != g_PosVelocity) ||
47 (g_OldZoom != g_Zoom)) {
48
49 g_OldPosPage = g_PosPage;
50 g_OldPosVelocity = g_PosVelocity;
51 g_OldZoom = g_Zoom;
52
53 int i[3];
54 i[0] = g_PosPage * (1 << 16);
55 i[1] = g_PosVelocity * (1 << 16);
56 i[2] = g_OldZoom * (1 << 16);
57 sendToClient(&i[0], 1, 12, 1);
58 }
59}
60
Jason Sams41b61c82009-10-15 15:40:54 -070061void setColor(float r, float g, float b, float a) {
62 if (g_SpecialHWWar) {
63 color(0, 0, 0, 0.001f);
64 } else {
65 color(r, g, b, a);
66 }
67}
Jason Sams0f505e52009-10-13 17:18:35 -070068
69void init() {
Jason Sams2e19c052009-10-20 18:19:55 -070070 g_AttractionTable[0] = 20.0f;
71 g_AttractionTable[1] = 20.0f;
Jason Samsc8514792009-10-29 14:27:29 -070072 g_AttractionTable[2] = 20.0f;
Jason Sams2e19c052009-10-20 18:19:55 -070073 g_AttractionTable[3] = 10.0f;
74 g_AttractionTable[4] = -10.0f;
Jason Samsc8514792009-10-29 14:27:29 -070075 g_AttractionTable[5] = -20.0f;
76 g_AttractionTable[6] = -20.0f;
Jason Sams2e19c052009-10-20 18:19:55 -070077 g_AttractionTable[7] = -20.0f;
78 g_AttractionTable[8] = -20.0f; // dup 7 to avoid a clamp later
79 g_FrictionTable[0] = 10.0f;
80 g_FrictionTable[1] = 10.0f;
81 g_FrictionTable[2] = 11.0f;
82 g_FrictionTable[3] = 15.0f;
83 g_FrictionTable[4] = 15.0f;
84 g_FrictionTable[5] = 11.0f;
85 g_FrictionTable[6] = 10.0f;
86 g_FrictionTable[7] = 10.0f;
87 g_FrictionTable[8] = 10.0f; // dup 7 to avoid a clamp later
Jason Sams0f505e52009-10-13 17:18:35 -070088 g_PhysicsTableSize = 7;
89
90 g_PosVelocity = 0;
91 g_PosPage = 0;
92 g_LastTouchDown = 0;
93 g_LastPositionX = 0;
94 g_Zoom = 0;
Jason Sams41b61c82009-10-15 15:40:54 -070095 g_SpecialHWWar = 1;
Jason Samsc1c521e2009-10-19 14:45:45 -070096 g_MoveToTime = 0;
97 g_MoveToOldPos = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -070098 g_MoveToTotalTime = 0.2f; // Duration of scrolling 1 line
Jason Sams41b61c82009-10-15 15:40:54 -070099}
100
101void resetHWWar() {
102 g_SpecialHWWar = 1;
Jason Sams0f505e52009-10-13 17:18:35 -0700103}
104
105void move() {
106 if (g_LastTouchDown) {
107 float dx = -(state->newPositionX - g_LastPositionX);
108 g_PosVelocity = 0;
Jason Sams6ec11bc2010-01-19 17:56:52 -0800109 g_PosPage += dx * 5.2f;
Jason Sams0f505e52009-10-13 17:18:35 -0700110
Jason Samsc8514792009-10-29 14:27:29 -0700111 float pmin = -0.49f;
112 float pmax = g_PosMax + 0.49f;
Jason Sams0f505e52009-10-13 17:18:35 -0700113 g_PosPage = clampf(g_PosPage, pmin, pmax);
114 }
115 g_LastTouchDown = state->newTouchDown;
116 g_LastPositionX = state->newPositionX;
Jason Samsc1c521e2009-10-19 14:45:45 -0700117 g_MoveToTime = 0;
Jason Sams0f505e52009-10-13 17:18:35 -0700118 //debugF("Move P", g_PosPage);
119}
120
Jason Samsc1c521e2009-10-19 14:45:45 -0700121void moveTo() {
122 g_MoveToTime = g_MoveToTotalTime;
123 g_PosVelocity = 0;
124 g_MoveToOldPos = g_PosPage;
Jason Sams2e19c052009-10-20 18:19:55 -0700125
Mike Cleron7d5d7462009-10-20 14:06:00 -0700126 // debugF("======= moveTo", state->targetPos);
Jason Samsc1c521e2009-10-19 14:45:45 -0700127}
128
Joe Onorato3a8820b2009-11-10 15:06:42 -0800129void setZoom() {
130 g_Zoom = state->zoomTarget;
131 g_DrawLastFrame = 1;
132 updateReadback();
133}
134
Jason Sams0f505e52009-10-13 17:18:35 -0700135void fling() {
136 g_LastTouchDown = 0;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700137 g_PosVelocity = -state->flingVelocity * 4;
Jason Sams0f505e52009-10-13 17:18:35 -0700138 float av = fabsf(g_PosVelocity);
139 float minVel = 3.5f;
140
141 minVel *= 1.f - (fabsf(fracf(g_PosPage + 0.5f) - 0.5f) * 0.45f);
142
143 if (av < minVel && av > 0.2f) {
144 if (g_PosVelocity > 0) {
145 g_PosVelocity = minVel;
146 } else {
147 g_PosVelocity = -minVel;
148 }
149 }
150
151 if (g_PosPage <= 0) {
152 g_PosVelocity = maxf(0, g_PosVelocity);
153 }
154 if (g_PosPage > g_PosMax) {
155 g_PosVelocity = minf(0, g_PosVelocity);
156 }
157}
158
Jason Sams0f505e52009-10-13 17:18:35 -0700159float
160modf(float x, float y)
161{
162 return x-(y*floorf(x/y));
163}
164
Mike Cleron7d5d7462009-10-20 14:06:00 -0700165
166/*
167 * Interpolates values in the range 0..1 to a curve that eases in
168 * and out.
169 */
170float
171getInterpolation(float input) {
172 return (cosf((input + 1) * PI) / 2.0f) + 0.5f;
173}
174
175
Jason Sams0f505e52009-10-13 17:18:35 -0700176void updatePos() {
177 if (g_LastTouchDown) {
178 return;
179 }
180
Jason Sams0f505e52009-10-13 17:18:35 -0700181 float tablePosNorm = fracf(g_PosPage + 0.5f);
182 float tablePosF = tablePosNorm * g_PhysicsTableSize;
183 int tablePosI = tablePosF;
184 float tablePosFrac = tablePosF - tablePosI;
185 float accel = lerpf(g_AttractionTable[tablePosI],
186 g_AttractionTable[tablePosI + 1],
187 tablePosFrac) * g_DT;
Jason Sams2e19c052009-10-20 18:19:55 -0700188 float friction = lerpf(g_FrictionTable[tablePosI],
189 g_FrictionTable[tablePosI + 1],
190 tablePosFrac) * g_DT;
Jason Sams0f505e52009-10-13 17:18:35 -0700191
Jason Samsc1c521e2009-10-19 14:45:45 -0700192 if (g_MoveToTime) {
Jason Samsc8514792009-10-29 14:27:29 -0700193 // New position is old posiition + (total distance) * (interpolated time)
194 g_PosPage = g_MoveToOldPos + (state->targetPos - g_MoveToOldPos) * getInterpolation((g_MoveToTotalTime - g_MoveToTime) / g_MoveToTotalTime);
Jason Samsc1c521e2009-10-19 14:45:45 -0700195 g_MoveToTime -= g_DT;
196 if (g_MoveToTime <= 0) {
197 g_MoveToTime = 0;
198 g_PosPage = state->targetPos;
199 }
200 return;
201 }
202
Jason Sams0f505e52009-10-13 17:18:35 -0700203 // If our velocity is low OR acceleration is opposing it, apply it.
Jason Samsc8514792009-10-29 14:27:29 -0700204 if (fabsf(g_PosVelocity) < 4.0f || (g_PosVelocity * accel) < 0) {
Jason Sams0f505e52009-10-13 17:18:35 -0700205 g_PosVelocity += accel;
206 }
Jason Samsc8514792009-10-29 14:27:29 -0700207 //debugF("g_PosPage", g_PosPage);
208 //debugF(" g_PosVelocity", g_PosVelocity);
209 //debugF(" friction", friction);
210 //debugF(" accel", accel);
Jason Sams0f505e52009-10-13 17:18:35 -0700211
Jason Samsc8514792009-10-29 14:27:29 -0700212 // Normal physics
213 if (g_PosVelocity > 0) {
214 g_PosVelocity -= friction;
215 g_PosVelocity = maxf(g_PosVelocity, 0);
216 } else {
217 g_PosVelocity += friction;
218 g_PosVelocity = minf(g_PosVelocity, 0);
219 }
220
221 if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
Jason Sams0f505e52009-10-13 17:18:35 -0700222 // Special get back to center and overcome friction physics.
223 float t = tablePosNorm - 0.5f;
224 if (fabsf(t) < (friction * g_DT)) {
225 // really close, just snap
226 g_PosPage = roundf(g_PosPage);
227 g_PosVelocity = 0;
228 } else {
229 if (t > 0) {
230 g_PosVelocity = -friction;
231 } else {
232 g_PosVelocity = friction;
233 }
234 }
Jason Sams0f505e52009-10-13 17:18:35 -0700235 }
Jason Sams0f505e52009-10-13 17:18:35 -0700236
237 // Check for out of boundry conditions.
238 if (g_PosPage < 0 && g_PosVelocity < 0) {
Jason Sams0f505e52009-10-13 17:18:35 -0700239 float damp = 1.0 + (g_PosPage * 4);
240 damp = clampf(damp, 0.f, 0.9f);
241 g_PosVelocity *= damp;
242 }
243 if (g_PosPage > g_PosMax && g_PosVelocity > 0) {
Jason Sams0f505e52009-10-13 17:18:35 -0700244 float damp = 1.0 - ((g_PosPage - g_PosMax) * 4);
245 damp = clampf(damp, 0.f, 0.9f);
246 g_PosVelocity *= damp;
247 }
Jason Samsc8514792009-10-29 14:27:29 -0700248
249 g_PosPage += g_PosVelocity * g_DT;
250 g_PosPage = clampf(g_PosPage, -0.49, g_PosMax + 0.49);
Jason Sams0f505e52009-10-13 17:18:35 -0700251}
252
Jason Sams0f505e52009-10-13 17:18:35 -0700253
254void
255draw_home_button()
256{
Jason Sams41b61c82009-10-15 15:40:54 -0700257 setColor(1.0f, 1.0f, 1.0f, 1.0f);
Jason Samsc8514792009-10-29 14:27:29 -0700258 bindTexture(NAMED_PFTexNearest, 0, state->homeButtonId);
Jason Sams96b49d82009-10-20 14:28:53 -0700259 float x = (SCREEN_WIDTH_PX - params->homeButtonTextureWidth) / 2;
260 float y = (g_Zoom - 1.f) * params->homeButtonTextureHeight;
Jason Sams0f505e52009-10-13 17:18:35 -0700261
Jason Samsc8514792009-10-29 14:27:29 -0700262 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 -0700263 drawSpriteScreenspace(x, y, 0, params->homeButtonTextureWidth, params->homeButtonTextureHeight);
Jason Sams0f505e52009-10-13 17:18:35 -0700264}
265
Jason Sams09c6fc02009-10-16 17:23:23 -0700266void drawFrontGrid(float rowOffset, float p)
Jason Sams0f505e52009-10-13 17:18:35 -0700267{
268 float h = getHeight();
269 float w = getWidth();
270
271 int intRowOffset = rowOffset;
272 float rowFrac = rowOffset - intRowOffset;
273 float colWidth = getWidth() / 4;
274 float rowHeight = colWidth + 25.f;
Jason Samsb4ecab22010-01-19 16:43:26 -0800275 float yoff = 0.5f * h + 1.5f * rowHeight;
Jason Sams0f505e52009-10-13 17:18:35 -0700276
277 int row, col;
Jason Samsb4ecab22010-01-19 16:43:26 -0800278 int iconNum = (intRowOffset - 5) * 4;
Jason Sams0f505e52009-10-13 17:18:35 -0700279
Jason Samsb4ecab22010-01-19 16:43:26 -0800280 bindProgramVertex(NAMED_PVCurve);
281
282 storeF(ALLOC_VP_CONSTANTS, 4, p);
283
284 for (row = -5; row < 15; row++) {
Jason Sams0f505e52009-10-13 17:18:35 -0700285 float y = yoff - ((-rowFrac + row) * rowHeight);
286
287 for (col=0; col < 4; col++) {
288 if (iconNum >= state->iconCount) {
289 return;
290 }
291
292 if (iconNum >= 0) {
Jason Samsb4ecab22010-01-19 16:43:26 -0800293 float x = colWidth * col + (colWidth / 2);
Jason Samsb4ecab22010-01-19 16:43:26 -0800294 storeF(ALLOC_VP_CONSTANTS, 2, x);
Jason Sams1a94ee32010-01-20 13:34:30 -0800295
Jason Sams37f262d2010-01-20 11:19:51 -0800296 if (state->selectedIconIndex == iconNum && !p) {
Jason Sams1a94ee32010-01-20 13:34:30 -0800297 bindProgramFragment(NAMED_PFTexNearest);
298 bindTexture(NAMED_PFTexNearest, 0, state->selectedIconTexture);
299 storeF(ALLOC_VP_CONSTANTS, 0, SELECTION_TEXTURE_WIDTH_PX);
300 storeF(ALLOC_VP_CONSTANTS, 1, SELECTION_TEXTURE_HEIGHT_PX);
301 storeF(ALLOC_VP_CONSTANTS, 3, y - (SELECTION_TEXTURE_HEIGHT_PX - ICON_TEXTURE_HEIGHT_PX) * 0.5f);
302 drawSimpleMesh(NAMED_SMCell);
Jason Sams37f262d2010-01-20 11:19:51 -0800303 }
304
Jason Sams1a94ee32010-01-20 13:34:30 -0800305 bindProgramFragment(NAMED_PFTexMip);
306 storeF(ALLOC_VP_CONSTANTS, 0, ICON_TEXTURE_WIDTH_PX);
307 storeF(ALLOC_VP_CONSTANTS, 1, ICON_TEXTURE_HEIGHT_PX);
308 storeF(ALLOC_VP_CONSTANTS, 3, y);
Jason Samsb4ecab22010-01-19 16:43:26 -0800309 bindTexture(NAMED_PFTexMip, 0, loadI32(ALLOC_ICON_IDS, iconNum));
310 drawSimpleMesh(NAMED_SMCell);
Joe Onorato742d7fc2009-10-15 19:48:16 -0700311
Jason Sams37f262d2010-01-20 11:19:51 -0800312 bindProgramFragment(NAMED_PFTexMipAlpha);
Jason Samsb4ecab22010-01-19 16:43:26 -0800313 storeF(ALLOC_VP_CONSTANTS, 0, 128.f);
314 storeF(ALLOC_VP_CONSTANTS, 1, 64.f);
315 storeF(ALLOC_VP_CONSTANTS, 3, y - 64.f);
Jason Sams37f262d2010-01-20 11:19:51 -0800316 bindTexture(NAMED_PFTexMipAlpha, 0, loadI32(ALLOC_LABEL_IDS, iconNum));
Jason Samsb4ecab22010-01-19 16:43:26 -0800317 drawSimpleMesh(NAMED_SMCell);
Jason Sams0f505e52009-10-13 17:18:35 -0700318 }
319 iconNum++;
320 }
321 }
322}
323
Jason Sams0f505e52009-10-13 17:18:35 -0700324
325int
326main(int launchID)
327{
328 // Compute dt in seconds.
329 int newTime = uptimeMillis();
330 g_DT = (newTime - g_LastTime) / 1000.f;
331 g_LastTime = newTime;
Jason Sams2e19c052009-10-20 18:19:55 -0700332
Jason Sams0f505e52009-10-13 17:18:35 -0700333 if (!g_DrawLastFrame) {
334 // If we stopped rendering we cannot use DT.
335 // assume 30fps in this case.
336 g_DT = 0.033f;
337 }
Jason Samsb52dfa02009-10-14 20:16:14 -0700338 // physics may break if DT is large.
339 g_DT = minf(g_DT, 0.2f);
Jason Sams0f505e52009-10-13 17:18:35 -0700340
341 if (g_Zoom != state->zoomTarget) {
Jason Sams09c6fc02009-10-16 17:23:23 -0700342 float dz;
343 if (state->zoomTarget > 0.5f) {
344 dz = (1 - g_Zoom) * 0.2f;
345 } else {
346 dz = -g_DT - (1 - g_Zoom) * 0.2f;
347 }
348 if (dz && (fabsf(dz) < 0.02f)) {
Jason Sams0f505e52009-10-13 17:18:35 -0700349 if (dz > 0) {
Jason Sams09c6fc02009-10-16 17:23:23 -0700350 dz = 0.02f;
Jason Sams0f505e52009-10-13 17:18:35 -0700351 } else {
Jason Sams09c6fc02009-10-16 17:23:23 -0700352 dz = -0.02f;
Jason Sams0f505e52009-10-13 17:18:35 -0700353 }
354 }
355 if (fabsf(g_Zoom - state->zoomTarget) < fabsf(dz)) {
356 g_Zoom = state->zoomTarget;
357 } else {
358 g_Zoom += dz;
359 }
360 updateReadback();
361 }
362
363 // Set clear value to dim the background based on the zoom position.
Jason Sams41b61c82009-10-15 15:40:54 -0700364 if ((g_Zoom < 0.001f) && (state->zoomTarget < 0.001f) && !g_SpecialHWWar) {
Jason Sams0f505e52009-10-13 17:18:35 -0700365 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
366 // When we're zoomed out and not tracking motion events, reset the pos to 0.
367 if (!g_LastTouchDown) {
368 g_PosPage = 0;
369 }
370 return lastFrame(0);
Jason Sams0f505e52009-10-13 17:18:35 -0700371 } else {
372 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
373 }
374
375 // icons & labels
376 int iconCount = state->iconCount;
377 g_PosMax = ((iconCount + 3) / 4) - 4;
378 if (g_PosMax < 0) g_PosMax = 0;
379
380 updatePos(0.1f);
381 updateReadback();
382
383 //debugF(" draw g_PosPage", g_PosPage);
384
385 // Draw the icons ========================================
Jason Samsb4ecab22010-01-19 16:43:26 -0800386 drawFrontGrid(g_PosPage, 1-g_Zoom);
Jason Samsc8514792009-10-29 14:27:29 -0700387
388 bindProgramFragment(NAMED_PFTexNearest);
Jason Samsb52dfa02009-10-14 20:16:14 -0700389 draw_home_button();
Jason Sams0f505e52009-10-13 17:18:35 -0700390
Jason Sams41b61c82009-10-15 15:40:54 -0700391 // This is a WAR to do a rendering pass without drawing during init to
392 // force the driver to preload and compile its shaders.
393 // Without this the first animation does not appear due to the time it
394 // takes to init the driver state.
395 if (g_SpecialHWWar) {
396 g_SpecialHWWar = 0;
397 return 1;
398 }
399
Jason Sams0f505e52009-10-13 17:18:35 -0700400 // Bug workaround where the last frame is not always displayed
401 // So we keep rendering until the bug is fixed.
Mike Cleron7d5d7462009-10-20 14:06:00 -0700402 return lastFrame((g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != state->zoomTarget || (g_MoveToTime != 0));
Jason Sams0f505e52009-10-13 17:18:35 -0700403}
404