blob: cc273b1ab370069527d7d577fc653f8da6e2bbbc [file] [log] [blame]
Jason Sams0aa71662009-10-02 18:43:18 -07001#pragma version(1)
2#pragma stateVertex(PV)
3#pragma stateFragment(PFTexLinear)
4#pragma stateStore(PSIcons)
5
6#define PI 3.14159f
7
8
9// Attraction to center values from page edge to page center.
10float g_AttractionTable[9];
11float g_FrictionTable[9];
12float g_PhysicsTableSize;
13
14float g_PosPage;
15float g_PosVelocity;
16float g_LastPositionX;
17int g_LastTouchDown;
18float g_DT;
19int g_LastTime;
20int g_PageCount;
21float g_Zoom;
Jason Sams0f505e52009-10-13 17:18:35 -070022float g_OldPosPage;
23float g_OldPosVelocity;
24float g_OldZoom;
Jason Sams0aa71662009-10-02 18:43:18 -070025
26// Drawing constants, should be parameters ======
27#define VIEW_ANGLE 1.28700222f
28
Jason Sams0f505e52009-10-13 17:18:35 -070029int 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 Sams0aa71662009-10-02 18:43:18 -070056void init() {
57 g_AttractionTable[0] = 6.5f;
58 g_AttractionTable[1] = 6.5f;
59 g_AttractionTable[2] = 7.0f;
60 g_AttractionTable[3] = 6.0f;
61 g_AttractionTable[4] = -6.0f;
62 g_AttractionTable[5] = -7.0f;
63 g_AttractionTable[6] = -6.5f;
64 g_AttractionTable[7] = -6.5f;
65 g_AttractionTable[8] = -6.5f; // dup 7 to avoid a clamp later
66 g_FrictionTable[0] = 3.5f;
67 g_FrictionTable[1] = 3.6f;
68 g_FrictionTable[2] = 4.0f;
69 g_FrictionTable[3] = 5.0f;
70 g_FrictionTable[4] = 5.0f;
71 g_FrictionTable[5] = 4.0f;
72 g_FrictionTable[6] = 3.6f;
73 g_FrictionTable[7] = 3.5f;
74 g_FrictionTable[8] = 3.5f; // dup 7 to avoid a clamp later
75 g_PhysicsTableSize = 7;
76
77 g_PosVelocity = 0;
78 g_PosPage = 0;
79 g_LastTouchDown = 0;
80 g_LastPositionX = 0;
81 g_Zoom = 0;
Jason Sams0aa71662009-10-02 18:43:18 -070082}
83
84void move() {
85 if (g_LastTouchDown) {
86 float dx = -(state->newPositionX - g_LastPositionX);
87 g_PosVelocity = 0;
88 g_PosPage += dx;
89
90 float pmin = -0.25f;
91 float pmax = (g_PageCount - 1) + 0.25f;
92 g_PosPage = clampf(g_PosPage, pmin, pmax);
93 }
94 g_LastTouchDown = state->newTouchDown;
95 g_LastPositionX = state->newPositionX;
96 //debugF("Move P", g_PosPage);
97}
98
99void fling() {
100 g_LastTouchDown = 0;
101 g_PosVelocity = -state->flingVelocityX;
102 float av = fabsf(g_PosVelocity);
103 float minVel = 3.5f;
104
105 minVel *= 1.f - (fabsf(fracf(g_PosPage + 0.5f) - 0.5f) * 0.45f);
106
107 if (av < minVel && av > 0.2f) {
108 if (g_PosVelocity > 0) {
109 g_PosVelocity = minVel;
110 } else {
111 g_PosVelocity = -minVel;
112 }
113 }
114
115 if (g_PosPage <= 0) {
116 g_PosVelocity = maxf(0, g_PosVelocity);
117 }
118 if (g_PosPage > (g_PageCount - 1)) {
119 g_PosVelocity = minf(0, g_PosVelocity);
120 }
121 //debugF("fling v", g_PosVelocity);
122}
123
124void touchUp() {
125 g_LastTouchDown = 0;
126}
127
Jason Sams0aa71662009-10-02 18:43:18 -0700128int
129count_pages(int iconCount)
130{
131 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
132 int pages = iconCount / iconsPerPage;
133 if (pages*iconsPerPage != iconCount) {
134 pages++;
135 }
136 return pages;
137}
138
139float
140modf(float x, float y)
141{
142 return x-(y*floorf(x/y));
143}
144
145void updatePos() {
146 if (g_LastTouchDown) {
147 return;
148 }
149
150 float tablePosNorm = fracf(g_PosPage + 0.5f);
151 float tablePosF = tablePosNorm * g_PhysicsTableSize;
152 int tablePosI = tablePosF;
153 float tablePosFrac = tablePosF - tablePosI;
154 float accel = lerpf(g_AttractionTable[tablePosI],
155 g_AttractionTable[tablePosI + 1],
156 tablePosFrac) * g_DT;
157 float friction = lerpf(g_FrictionTable[tablePosI],
158 g_FrictionTable[tablePosI + 1],
159 tablePosFrac) * g_DT;
160 //debugF(" accel", accel);
161 //debugF(" friction", friction);
162
163 // If our velocity is low OR acceleration is opposing it, apply it.
164 if (fabsf(g_PosVelocity) < 1.0f || (g_PosVelocity * accel) < 0) {
165 g_PosVelocity += accel;
166 }
167
168 if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
169 // Special get back to center and overcome friction physics.
170 float t = tablePosNorm - 0.5f;
171 if (fabsf(t) < (friction * g_DT)) {
172 // really close, just snap
173 g_PosPage = roundf(g_PosPage);
174 g_PosVelocity = 0;
175 } else {
176 if (t > 0) {
177 g_PosVelocity = -friction;
178 } else {
179 g_PosVelocity = friction;
180 }
181 }
182 } else {
183 // Normal physics
184 if (g_PosVelocity > 0) {
185 g_PosVelocity -= friction;
186 g_PosVelocity = maxf(g_PosVelocity, 0);
187 } else {
188 g_PosVelocity += friction;
189 g_PosVelocity = minf(g_PosVelocity, 0);
190 }
191 }
192 g_PosPage += g_PosVelocity * g_DT;
193
194 // Check for out of boundry conditions.
195 if (g_PosPage < 0 && g_PosVelocity < 0) {
196 float damp = 1.0 + (g_PosPage * 4);
197 damp = clampf(damp, 0.f, 0.9f);
198 g_PosVelocity *= damp;
199 }
200 if (g_PosPage > (g_PageCount-1) && g_PosVelocity > 0) {
201 float damp = 1.0 - ((g_PosPage - g_PageCount + 1) * 4);
202 damp = clampf(damp, 0.f, 0.9f);
203 g_PosVelocity *= damp;
204 }
205}
206
207int positionStrip(float row, float column)
208{
209 float mat1[16];
210
211 float y = 1.2f - row * 0.6f;
212
213 float scale = 256.f / getWidth();
214 float xscale = scale * 4.55 / 1.8f / 2;
215
216 matrixLoadTranslate(mat1, 0.f, y, 0.f);
217 matrixScale(mat1, 1.f, scale, 1.f);
218 vpLoadModelMatrix(mat1);
219
220 float soff = -21.8f - (column * 1.25f);
221 matrixLoadScale(mat1, xscale, 1.f, 1.f);
222 matrixTranslate(mat1, soff, 0, 0);
223 vpLoadTextureMatrix(mat1);
224
225 return - soff * 10.f;
226}
227
228void drawIcon(float row, float column, int iconNum)
229{
230 int offset = positionStrip(row, column);
231 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, iconNum));
232
233 if (offset < 0) {
234 offset = 0;
235 }
236 if (offset >= (450 - 20)) {
237 offset = (449 - 20);
238 }
239 drawSimpleMeshRange(NAMED_SMMesh, offset * 6, 20 * 6);
240}
241
242void
243draw_home_button()
244{
245 color(1.0f, 1.0f, 1.0f, 1.0f);
246 bindTexture(NAMED_PFTexLinear, 0, params->homeButtonId);
247
248 float scale = 2.0f / SCREEN_WIDTH_PX;
249
250 float x = 0.0f;
251
252 float y = -(SCREEN_HEIGHT_PX / (float)SCREEN_WIDTH_PX);
253 y += g_Zoom * (scale * params->homeButtonTextureHeight / 2);
254
255 float z = 0.0f;
256 drawSprite(x, y, z, params->homeButtonTextureWidth, params->homeButtonTextureHeight);
257}
258
259int
260main(int launchID)
261{
262 // Compute dt in seconds.
263 int newTime = uptimeMillis();
264 g_DT = (newTime - g_LastTime) / 1000.f;
265 g_LastTime = newTime;
266
Jason Sams0f505e52009-10-13 17:18:35 -0700267 if (!g_DrawLastFrame) {
268 // If we stopped rendering we cannot use DT.
269 // assume 30fps in this case.
270 g_DT = 0.033f;
271 }
272 if (g_DT > 0.2f) {
273 // physics may break if DT is large.
274 g_DT = 0.2f;
275 }
276
277 debugF("zoom", g_Zoom);
278 if (g_Zoom != state->zoomTarget) {
279 float dz = (state->zoomTarget - g_Zoom) * g_DT * 5;
Jason Sams0aa71662009-10-02 18:43:18 -0700280 if (dz && (fabsf(dz) < 0.03f)) {
281 if (dz > 0) {
282 dz = 0.03f;
283 } else {
284 dz = -0.03f;
285 }
286 }
Jason Sams0f505e52009-10-13 17:18:35 -0700287 if (fabsf(g_Zoom - state->zoomTarget) < fabsf(dz)) {
288 g_Zoom = state->zoomTarget;
Jason Sams0aa71662009-10-02 18:43:18 -0700289 } else {
290 g_Zoom += dz;
291 }
Jason Sams0f505e52009-10-13 17:18:35 -0700292 updateReadback();
Jason Sams0aa71662009-10-02 18:43:18 -0700293 }
294
295 // Set clear value to dim the background based on the zoom position.
Jason Sams0f505e52009-10-13 17:18:35 -0700296 if ((g_Zoom < 0.001f) && (state->zoomTarget < 0.001f)) {
Jason Sams0aa71662009-10-02 18:43:18 -0700297 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
298 // When we're zoomed out and not tracking motion events, reset the pos to 0.
299 if (!g_LastTouchDown) {
300 g_PosPage = 0;
301 }
Jason Sams0f505e52009-10-13 17:18:35 -0700302 return lastFrame(0);
303 } else if (g_Zoom < 0.85f) {
Jason Sams0aa71662009-10-02 18:43:18 -0700304 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
305 } else {
Jason Sams0f505e52009-10-13 17:18:35 -0700306 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
Jason Sams0aa71662009-10-02 18:43:18 -0700307 }
308
309 // icons & labels
310 int iconCount = state->iconCount;
311 g_PageCount = count_pages(iconCount);
312
313 updatePos(0.1f);
Jason Sams0f505e52009-10-13 17:18:35 -0700314 updateReadback();
Jason Sams0aa71662009-10-02 18:43:18 -0700315
Jason Sams0f505e52009-10-13 17:18:35 -0700316 debugF(" draw g_PosPage", g_PosPage);
Jason Sams0aa71662009-10-02 18:43:18 -0700317
318 // Draw the icons ========================================
319
320 // Bug makes 1.0f alpha fail.
321 //color(0.2f, 0.2f, 0.2f, 0.99f);
322 //bindProgramFragment(NAMED_PFColor);
323 //positionStrip(0, 0);
324 //drawSimpleMesh(NAMED_SMMesh);
325
326 bindProgramFragment(NAMED_PFTexLinear);
327
328
329 int lastIcon = iconCount-1;
330
331 int page = g_PosPage;
332 float currentPagePosition = g_PosPage - page;
333
334 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
335 float scale = (1 / g_Zoom);
336
337 float pageAngle = VIEW_ANGLE * 1.2f;
338
339 float zoomOffset = 40 * (1 - g_Zoom);
340 int drawPage;
341 //lastIcon = 1;
342 for (drawPage = 0; drawPage < g_PageCount; drawPage++) {
343 int r, c;
344 for (r=0; r < 4; r++) {
345 for (c=0; c < 4; c++) {
346 int iconNum = drawPage * 16 + c + r * 4;
347 if (iconNum <= lastIcon) {
348 float p = (((float)drawPage) - g_PosPage) * 5.f;
349 p += c - 1.5f;
350 p += zoomOffset;
351 if (fabsf(p) > 2) {
352 drawIcon(r, p, iconNum);
353 }
354 }
355 }
356 }
357 }
358
359 for (drawPage = 0; drawPage < g_PageCount; drawPage++) {
360 int r, c;
361 for (r=0; r < 4; r++) {
362 for (c=0; c < 4; c++) {
363 int iconNum = drawPage * 16 + c + r * 4;
364 if (iconNum <= lastIcon) {
365 float p = (((float)drawPage) - g_PosPage) * 5.f;
366 p += c - 1.5f;
367 p += zoomOffset;
368 float x = (p * 1.13f + 1.88f) * getWidth() * 0.2f;
369 float y = 570 - r * 147;
370
371 if (fabsf(p) <= 2) {
372 drawIcon(r, p, iconNum);
373 }
374 if (fabsf(p) <= 2.5) {
375 float a = (1.2f - maxf(scale, 1.0f)) * 5;
376 color(1.0f, 1.0f, 1.0f, a);
377 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_LABEL_IDS, iconNum));
378 drawSpriteScreenspace(x, y, 0,
379 params->bubbleBitmapWidth, params->bubbleBitmapHeight);
380 }
381 }
382 }
383 }
384
385 }
386
Jason Sams0f505e52009-10-13 17:18:35 -0700387 {
388 float mat1[16];
389 matrixLoadIdentity(mat1);
390 vpLoadModelMatrix(mat1);
391 vpLoadTextureMatrix(mat1);
392 }
393
Jason Sams0aa71662009-10-02 18:43:18 -0700394 // Draw the home button ========================================
395 //draw_home_button();
396
397 // Bug workaround where the last frame is not always displayed
398 // So we keep rendering until the bug is fixed.
399 return 1; //(g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != g_ZoomTarget);
400}
401