blob: eb87063d606cac8a9a013e5d86f008064ca8bbd6 [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
Jason Sams41b61c82009-10-15 15:40:54 -070084void resetHWWar() {
85}
86
Jason Sams0aa71662009-10-02 18:43:18 -070087void move() {
88 if (g_LastTouchDown) {
89 float dx = -(state->newPositionX - g_LastPositionX);
90 g_PosVelocity = 0;
91 g_PosPage += dx;
92
93 float pmin = -0.25f;
94 float pmax = (g_PageCount - 1) + 0.25f;
95 g_PosPage = clampf(g_PosPage, pmin, pmax);
96 }
97 g_LastTouchDown = state->newTouchDown;
98 g_LastPositionX = state->newPositionX;
99 //debugF("Move P", g_PosPage);
100}
101
102void fling() {
103 g_LastTouchDown = 0;
104 g_PosVelocity = -state->flingVelocityX;
105 float av = fabsf(g_PosVelocity);
106 float minVel = 3.5f;
107
108 minVel *= 1.f - (fabsf(fracf(g_PosPage + 0.5f) - 0.5f) * 0.45f);
109
110 if (av < minVel && av > 0.2f) {
111 if (g_PosVelocity > 0) {
112 g_PosVelocity = minVel;
113 } else {
114 g_PosVelocity = -minVel;
115 }
116 }
117
118 if (g_PosPage <= 0) {
119 g_PosVelocity = maxf(0, g_PosVelocity);
120 }
121 if (g_PosPage > (g_PageCount - 1)) {
122 g_PosVelocity = minf(0, g_PosVelocity);
123 }
124 //debugF("fling v", g_PosVelocity);
125}
126
127void touchUp() {
128 g_LastTouchDown = 0;
129}
130
Jason Sams0aa71662009-10-02 18:43:18 -0700131int
132count_pages(int iconCount)
133{
134 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
135 int pages = iconCount / iconsPerPage;
136 if (pages*iconsPerPage != iconCount) {
137 pages++;
138 }
139 return pages;
140}
141
142float
143modf(float x, float y)
144{
145 return x-(y*floorf(x/y));
146}
147
148void updatePos() {
149 if (g_LastTouchDown) {
150 return;
151 }
152
153 float tablePosNorm = fracf(g_PosPage + 0.5f);
154 float tablePosF = tablePosNorm * g_PhysicsTableSize;
155 int tablePosI = tablePosF;
156 float tablePosFrac = tablePosF - tablePosI;
157 float accel = lerpf(g_AttractionTable[tablePosI],
158 g_AttractionTable[tablePosI + 1],
159 tablePosFrac) * g_DT;
160 float friction = lerpf(g_FrictionTable[tablePosI],
161 g_FrictionTable[tablePosI + 1],
162 tablePosFrac) * g_DT;
163 //debugF(" accel", accel);
164 //debugF(" friction", friction);
165
166 // If our velocity is low OR acceleration is opposing it, apply it.
167 if (fabsf(g_PosVelocity) < 1.0f || (g_PosVelocity * accel) < 0) {
168 g_PosVelocity += accel;
169 }
170
171 if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
172 // Special get back to center and overcome friction physics.
173 float t = tablePosNorm - 0.5f;
174 if (fabsf(t) < (friction * g_DT)) {
175 // really close, just snap
176 g_PosPage = roundf(g_PosPage);
177 g_PosVelocity = 0;
178 } else {
179 if (t > 0) {
180 g_PosVelocity = -friction;
181 } else {
182 g_PosVelocity = friction;
183 }
184 }
185 } else {
186 // Normal physics
187 if (g_PosVelocity > 0) {
188 g_PosVelocity -= friction;
189 g_PosVelocity = maxf(g_PosVelocity, 0);
190 } else {
191 g_PosVelocity += friction;
192 g_PosVelocity = minf(g_PosVelocity, 0);
193 }
194 }
195 g_PosPage += g_PosVelocity * g_DT;
196
197 // Check for out of boundry conditions.
198 if (g_PosPage < 0 && g_PosVelocity < 0) {
199 float damp = 1.0 + (g_PosPage * 4);
200 damp = clampf(damp, 0.f, 0.9f);
201 g_PosVelocity *= damp;
202 }
203 if (g_PosPage > (g_PageCount-1) && g_PosVelocity > 0) {
204 float damp = 1.0 - ((g_PosPage - g_PageCount + 1) * 4);
205 damp = clampf(damp, 0.f, 0.9f);
206 g_PosVelocity *= damp;
207 }
208}
209
210int positionStrip(float row, float column)
211{
212 float mat1[16];
213
214 float y = 1.2f - row * 0.6f;
215
216 float scale = 256.f / getWidth();
217 float xscale = scale * 4.55 / 1.8f / 2;
218
219 matrixLoadTranslate(mat1, 0.f, y, 0.f);
220 matrixScale(mat1, 1.f, scale, 1.f);
221 vpLoadModelMatrix(mat1);
222
223 float soff = -21.8f - (column * 1.25f);
224 matrixLoadScale(mat1, xscale, 1.f, 1.f);
225 matrixTranslate(mat1, soff, 0, 0);
226 vpLoadTextureMatrix(mat1);
227
228 return - soff * 10.f;
229}
230
231void drawIcon(float row, float column, int iconNum)
232{
233 int offset = positionStrip(row, column);
234 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, iconNum));
235
236 if (offset < 0) {
237 offset = 0;
238 }
239 if (offset >= (450 - 20)) {
240 offset = (449 - 20);
241 }
242 drawSimpleMeshRange(NAMED_SMMesh, offset * 6, 20 * 6);
243}
244
245void
246draw_home_button()
247{
248 color(1.0f, 1.0f, 1.0f, 1.0f);
Joe Onoratod63458b2009-10-15 21:19:09 -0700249 bindTexture(NAMED_PFTexLinear, 0, state->homeButtonId);
Jason Sams0aa71662009-10-02 18:43:18 -0700250
251 float scale = 2.0f / SCREEN_WIDTH_PX;
252
253 float x = 0.0f;
254
255 float y = -(SCREEN_HEIGHT_PX / (float)SCREEN_WIDTH_PX);
256 y += g_Zoom * (scale * params->homeButtonTextureHeight / 2);
257
258 float z = 0.0f;
259 drawSprite(x, y, z, params->homeButtonTextureWidth, params->homeButtonTextureHeight);
260}
261
262int
263main(int launchID)
264{
265 // Compute dt in seconds.
266 int newTime = uptimeMillis();
267 g_DT = (newTime - g_LastTime) / 1000.f;
268 g_LastTime = newTime;
269
Jason Sams0f505e52009-10-13 17:18:35 -0700270 if (!g_DrawLastFrame) {
271 // If we stopped rendering we cannot use DT.
272 // assume 30fps in this case.
273 g_DT = 0.033f;
274 }
275 if (g_DT > 0.2f) {
276 // physics may break if DT is large.
277 g_DT = 0.2f;
278 }
279
280 debugF("zoom", g_Zoom);
281 if (g_Zoom != state->zoomTarget) {
282 float dz = (state->zoomTarget - g_Zoom) * g_DT * 5;
Jason Sams0aa71662009-10-02 18:43:18 -0700283 if (dz && (fabsf(dz) < 0.03f)) {
284 if (dz > 0) {
285 dz = 0.03f;
286 } else {
287 dz = -0.03f;
288 }
289 }
Jason Sams0f505e52009-10-13 17:18:35 -0700290 if (fabsf(g_Zoom - state->zoomTarget) < fabsf(dz)) {
291 g_Zoom = state->zoomTarget;
Jason Sams0aa71662009-10-02 18:43:18 -0700292 } else {
293 g_Zoom += dz;
294 }
Jason Sams0f505e52009-10-13 17:18:35 -0700295 updateReadback();
Jason Sams0aa71662009-10-02 18:43:18 -0700296 }
297
298 // Set clear value to dim the background based on the zoom position.
Jason Sams0f505e52009-10-13 17:18:35 -0700299 if ((g_Zoom < 0.001f) && (state->zoomTarget < 0.001f)) {
Jason Sams0aa71662009-10-02 18:43:18 -0700300 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
301 // When we're zoomed out and not tracking motion events, reset the pos to 0.
302 if (!g_LastTouchDown) {
303 g_PosPage = 0;
304 }
Jason Sams0f505e52009-10-13 17:18:35 -0700305 return lastFrame(0);
306 } else if (g_Zoom < 0.85f) {
Jason Sams0aa71662009-10-02 18:43:18 -0700307 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
308 } else {
Jason Sams0f505e52009-10-13 17:18:35 -0700309 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
Jason Sams0aa71662009-10-02 18:43:18 -0700310 }
311
312 // icons & labels
313 int iconCount = state->iconCount;
314 g_PageCount = count_pages(iconCount);
315
316 updatePos(0.1f);
Jason Sams0f505e52009-10-13 17:18:35 -0700317 updateReadback();
Jason Sams0aa71662009-10-02 18:43:18 -0700318
Jason Sams0f505e52009-10-13 17:18:35 -0700319 debugF(" draw g_PosPage", g_PosPage);
Jason Sams0aa71662009-10-02 18:43:18 -0700320
321 // Draw the icons ========================================
322
323 // Bug makes 1.0f alpha fail.
324 //color(0.2f, 0.2f, 0.2f, 0.99f);
325 //bindProgramFragment(NAMED_PFColor);
326 //positionStrip(0, 0);
327 //drawSimpleMesh(NAMED_SMMesh);
328
329 bindProgramFragment(NAMED_PFTexLinear);
330
331
332 int lastIcon = iconCount-1;
333
334 int page = g_PosPage;
335 float currentPagePosition = g_PosPage - page;
336
337 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
338 float scale = (1 / g_Zoom);
339
340 float pageAngle = VIEW_ANGLE * 1.2f;
341
342 float zoomOffset = 40 * (1 - g_Zoom);
343 int drawPage;
344 //lastIcon = 1;
345 for (drawPage = 0; drawPage < g_PageCount; drawPage++) {
346 int r, c;
347 for (r=0; r < 4; r++) {
348 for (c=0; c < 4; c++) {
349 int iconNum = drawPage * 16 + c + r * 4;
350 if (iconNum <= lastIcon) {
351 float p = (((float)drawPage) - g_PosPage) * 5.f;
352 p += c - 1.5f;
353 p += zoomOffset;
354 if (fabsf(p) > 2) {
355 drawIcon(r, p, iconNum);
356 }
357 }
358 }
359 }
360 }
361
362 for (drawPage = 0; drawPage < g_PageCount; drawPage++) {
363 int r, c;
364 for (r=0; r < 4; r++) {
365 for (c=0; c < 4; c++) {
366 int iconNum = drawPage * 16 + c + r * 4;
367 if (iconNum <= lastIcon) {
368 float p = (((float)drawPage) - g_PosPage) * 5.f;
369 p += c - 1.5f;
370 p += zoomOffset;
371 float x = (p * 1.13f + 1.88f) * getWidth() * 0.2f;
372 float y = 570 - r * 147;
373
374 if (fabsf(p) <= 2) {
375 drawIcon(r, p, iconNum);
376 }
377 if (fabsf(p) <= 2.5) {
378 float a = (1.2f - maxf(scale, 1.0f)) * 5;
379 color(1.0f, 1.0f, 1.0f, a);
380 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_LABEL_IDS, iconNum));
381 drawSpriteScreenspace(x, y, 0,
382 params->bubbleBitmapWidth, params->bubbleBitmapHeight);
383 }
384 }
385 }
386 }
387
388 }
389
Jason Sams0f505e52009-10-13 17:18:35 -0700390 {
391 float mat1[16];
392 matrixLoadIdentity(mat1);
393 vpLoadModelMatrix(mat1);
394 vpLoadTextureMatrix(mat1);
395 }
396
Jason Sams0aa71662009-10-02 18:43:18 -0700397 // Draw the home button ========================================
398 //draw_home_button();
399
400 // Bug workaround where the last frame is not always displayed
401 // So we keep rendering until the bug is fixed.
402 return 1; //(g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != g_ZoomTarget);
403}
404