blob: ac158de4e31d77fb618647b8f979235203b21334 [file] [log] [blame]
Jason Sams0f505e52009-10-13 17:18:35 -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;
22
23// Drawing constants, should be parameters ======
24#define VIEW_ANGLE 1.28700222f
25
26float g_OldPosPage;
27float g_OldPosVelocity;
28float g_OldZoom;
29
30int g_DrawLastFrame;
31int lastFrame(int draw) {
32 // We draw one extra frame to work around the last frame post bug.
33 // We also need to track if we drew the last frame to deal with large DT
34 // in the physics.
35 int ret = g_DrawLastFrame | draw;
36 g_DrawLastFrame = draw;
37 return ret; // should return draw instead.
38}
39
40void updateReadback() {
41 if ((g_OldPosPage != g_PosPage) ||
42 (g_OldPosVelocity != g_PosVelocity) ||
43 (g_OldZoom != g_Zoom)) {
44
45 g_OldPosPage = g_PosPage;
46 g_OldPosVelocity = g_PosVelocity;
47 g_OldZoom = g_Zoom;
48
49 int i[3];
50 i[0] = g_PosPage * (1 << 16);
51 i[1] = g_PosVelocity * (1 << 16);
52 i[2] = g_OldZoom * (1 << 16);
53 sendToClient(&i[0], 1, 12, 1);
54 }
55}
56
57void init() {
58 g_AttractionTable[0] = 6.5f;
59 g_AttractionTable[1] = 6.5f;
60 g_AttractionTable[2] = 7.0f;
61 g_AttractionTable[3] = 6.0f;
62 g_AttractionTable[4] = -6.0f;
63 g_AttractionTable[5] = -7.0f;
64 g_AttractionTable[6] = -6.5f;
65 g_AttractionTable[7] = -6.5f;
66 g_AttractionTable[8] = -6.5f; // dup 7 to avoid a clamp later
67 g_FrictionTable[0] = 3.5f;
68 g_FrictionTable[1] = 3.6f;
69 g_FrictionTable[2] = 4.0f;
70 g_FrictionTable[3] = 5.0f;
71 g_FrictionTable[4] = 5.0f;
72 g_FrictionTable[5] = 4.0f;
73 g_FrictionTable[6] = 3.6f;
74 g_FrictionTable[7] = 3.5f;
75 g_FrictionTable[8] = 3.5f; // dup 7 to avoid a clamp later
76 g_PhysicsTableSize = 7;
77
78 g_PosVelocity = 0;
79 g_PosPage = 0;
80 g_LastTouchDown = 0;
81 g_LastPositionX = 0;
82 g_Zoom = 0;
83}
84
85void move() {
86 if (g_LastTouchDown) {
87 float dx = -(state->newPositionX - g_LastPositionX);
88 g_PosVelocity = 0;
89 g_PosPage += dx;
90
91 float pmin = -0.25f;
92 float pmax = (g_PageCount - 1) + 0.25f;
93 g_PosPage = clampf(g_PosPage, pmin, pmax);
94 }
95 g_LastTouchDown = state->newTouchDown;
96 g_LastPositionX = state->newPositionX;
97 //debugF("Move P", g_PosPage);
98}
99
100void fling() {
101 g_LastTouchDown = 0;
102 g_PosVelocity = -state->flingVelocityX;
103 float av = fabsf(g_PosVelocity);
104 float minVel = 3.5f;
105
106 minVel *= 1.f - (fabsf(fracf(g_PosPage + 0.5f) - 0.5f) * 0.45f);
107
108 if (av < minVel && av > 0.2f) {
109 if (g_PosVelocity > 0) {
110 g_PosVelocity = minVel;
111 } else {
112 g_PosVelocity = -minVel;
113 }
114 }
115
116 if (g_PosPage <= 0) {
117 g_PosVelocity = maxf(0, g_PosVelocity);
118 }
119 if (g_PosPage > (g_PageCount - 1)) {
120 g_PosVelocity = minf(0, g_PosVelocity);
121 }
122 //debugF("fling v", g_PosVelocity);
123}
124
125void touchUp() {
126 g_LastTouchDown = 0;
127}
128
129int
130count_pages(int iconCount)
131{
132 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
133 int pages = iconCount / iconsPerPage;
134 if (pages*iconsPerPage != iconCount) {
135 pages++;
136 }
137 return pages;
138}
139
140float
141modf(float x, float y)
142{
143 return x-(y*floorf(x/y));
144}
145
146void updatePos() {
147 if (g_LastTouchDown) {
148 return;
149 }
150
151 float tablePosNorm = fracf(g_PosPage + 0.5f);
152 float tablePosF = tablePosNorm * g_PhysicsTableSize;
153 int tablePosI = tablePosF;
154 float tablePosFrac = tablePosF - tablePosI;
155 float accel = lerpf(g_AttractionTable[tablePosI],
156 g_AttractionTable[tablePosI + 1],
157 tablePosFrac) * g_DT;
158 float friction = lerpf(g_FrictionTable[tablePosI],
159 g_FrictionTable[tablePosI + 1],
160 tablePosFrac) * g_DT;
161 //debugF(" accel", accel);
162 //debugF(" friction", friction);
163
164 // If our velocity is low OR acceleration is opposing it, apply it.
165 if (fabsf(g_PosVelocity) < 1.0f || (g_PosVelocity * accel) < 0) {
166 g_PosVelocity += accel;
167 }
168
169 if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
170 // Special get back to center and overcome friction physics.
171 float t = tablePosNorm - 0.5f;
172 if (fabsf(t) < (friction * g_DT)) {
173 // really close, just snap
174 g_PosPage = roundf(g_PosPage);
175 g_PosVelocity = 0;
176 } else {
177 if (t > 0) {
178 g_PosVelocity = -friction;
179 } else {
180 g_PosVelocity = friction;
181 }
182 }
183 } else {
184 // Normal physics
185 if (g_PosVelocity > 0) {
186 g_PosVelocity -= friction;
187 g_PosVelocity = maxf(g_PosVelocity, 0);
188 } else {
189 g_PosVelocity += friction;
190 g_PosVelocity = minf(g_PosVelocity, 0);
191 }
192 }
193 g_PosPage += g_PosVelocity * g_DT;
194
195 // Check for out of boundry conditions.
196 if (g_PosPage < 0 && g_PosVelocity < 0) {
197 float damp = 1.0 + (g_PosPage * 4);
198 damp = clampf(damp, 0.f, 0.9f);
199 g_PosVelocity *= damp;
200 }
201 if (g_PosPage > (g_PageCount-1) && g_PosVelocity > 0) {
202 float damp = 1.0 - ((g_PosPage - g_PageCount + 1) * 4);
203 damp = clampf(damp, 0.f, 0.9f);
204 g_PosVelocity *= damp;
205 }
206}
207
208float
209far_size(float sizeAt0)
210{
211 return sizeAt0 * (RADIUS+2) / 2; // -2 is the camera z=(z-camZ)/z
212}
213
214void
215draw_page(int icon, int lastIcon, float centerAngle, float scale)
216{
217 int row;
218 int col;
219
220 //debugF("center angle", centerAngle);
221
222 float iconTextureWidth = ICON_WIDTH_PX / (float)ICON_TEXTURE_WIDTH_PX;
223 float iconTextureHeight = ICON_HEIGHT_PX / (float)ICON_TEXTURE_HEIGHT_PX;
224
225 float iconWidthAngle = VIEW_ANGLE * ICON_WIDTH_PX / SCREEN_WIDTH_PX;
226 float columnGutterAngle = iconWidthAngle * 0.9f;
227
228 float farIconSize = FAR_ICON_SIZE;
229 float iconGutterHeight = farIconSize * 1.3f;
230
231 float farIconTextureSize = far_size(2 * ICON_TEXTURE_WIDTH_PX / (float)SCREEN_WIDTH_PX);
232
233 float normalizedLabelWidth = 2 * params->bubbleWidth / (float)SCREEN_WIDTH_PX;
234 float farLabelHeight = far_size(params->bubbleHeight * (normalizedLabelWidth / params->bubbleWidth));
235
236 for (row=0; row<ROWS_PER_PAGE && icon<=lastIcon; row++) {
237 float angle = centerAngle;
238 angle -= (columnGutterAngle + iconWidthAngle) * 1.5f;
239
240 float iconTop = (farIconSize + iconGutterHeight) * (1.85f + ICON_TOP_OFFSET)
241 - row * (farIconSize + iconGutterHeight);
242 float iconBottom = iconTop - farIconSize;
243
244 float labelY = iconBottom - farLabelHeight;
245 float iconTextureTop = iconTop + (0.5f * (farIconTextureSize - farIconSize));
246 float iconTextureBottom = iconTextureTop - farIconTextureSize;
247
248 for (col=0; col<COLUMNS_PER_PAGE && icon<=lastIcon; col++) {
249 // icon
250 float sine = sinf(angle);
251 float cosine = cosf(angle);
252
253 float centerX = sine * RADIUS;
254 float centerZ = cosine * RADIUS / scale;
255
256 if (scale > 1.f) {
257 centerX *= scale;
258 }
259
260 float iconLeftX = centerX - (cosine * farIconTextureSize * .5);
261 float iconRightX = centerX + (cosine * farIconTextureSize * .5);
262 float iconLeftZ = centerZ + (sine * farIconTextureSize * .5);
263 float iconRightZ = centerZ - (sine * farIconTextureSize * .5);
264
265 color(1.0f, 1.0f, 1.0f, 0.99f);
266 if (state->selectedIconIndex == icon) {
267 bindTexture(NAMED_PFTexLinear, 0, state->selectedIconTexture);
268 drawQuadTexCoords(
269 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
270 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
271 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
272 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
273 } else {
274 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, icon));
275 drawQuadTexCoords(
276 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
277 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
278 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
279 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
280 }
281
282 // label
283 if (scale < 1.2f) {
284 float a = (1.2f - maxf(scale, 1.0f)) * 5;
285 color(1.0f, 1.0f, 1.0f, a);
286 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_LABEL_IDS, icon));
287 drawSprite(centerX, labelY, centerZ,
288 params->bubbleBitmapWidth, params->bubbleBitmapHeight);
289 }
290
291 angle += columnGutterAngle + iconWidthAngle;
292 icon++;
293 }
294 }
295}
296
297void
298draw_home_button()
299{
300 color(1.0f, 1.0f, 1.0f, 1.0f);
301 bindTexture(NAMED_PFTexLinear, 0, params->homeButtonId);
302
303 float scale = 2.0f / SCREEN_WIDTH_PX;
304
305 float x = 0.0f;
306
307 float y = -(SCREEN_HEIGHT_PX / (float)SCREEN_WIDTH_PX);
308 y += g_Zoom * (scale * params->homeButtonTextureHeight / 2);
309
310 float z = 0.0f;
311 drawSprite(x, y, z, params->homeButtonTextureWidth, params->homeButtonTextureHeight);
312}
313
314int
315main(int launchID)
316{
317 // Compute dt in seconds.
318 int newTime = uptimeMillis();
319 g_DT = (newTime - g_LastTime) / 1000.f;
320 g_LastTime = newTime;
321
322 if (!g_DrawLastFrame) {
323 // If we stopped rendering we cannot use DT.
324 // assume 30fps in this case.
325 g_DT = 0.033f;
326 }
327 if (g_DT > 0.2f) {
328 // physics may break if DT is large.
329 g_DT = 0.2f;
330 }
331
332 //debugF("zoom", g_Zoom);
333 if (g_Zoom != state->zoomTarget) {
334 float dz = (state->zoomTarget - g_Zoom) * g_DT * 5;
335 if (dz && (fabsf(dz) < 0.03f)) {
336 if (dz > 0) {
337 dz = 0.03f;
338 } else {
339 dz = -0.03f;
340 }
341 }
342 if (fabsf(g_Zoom - state->zoomTarget) < fabsf(dz)) {
343 g_Zoom = state->zoomTarget;
344 } else {
345 g_Zoom += dz;
346 }
347 updateReadback();
348 }
349
350 // Set clear value to dim the background based on the zoom position.
351 if ((g_Zoom < 0.001f) && (state->zoomTarget < 0.001f)) {
352 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
353 // When we're zoomed out and not tracking motion events, reset the pos to 0.
354 if (!g_LastTouchDown) {
355 g_PosPage = 0;
356 }
357 return lastFrame(0);
358 } else if (g_Zoom < 0.85f) {
359 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
360 } else {
361 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
362 }
363
364 // icons & labels
365 int iconCount = state->iconCount;
366 g_PageCount = count_pages(iconCount);
367
368 updatePos(0.1f);
369 updateReadback();
370
371 //debugF(" draw g_PosPage", g_PosPage);
372
373 // Draw the icons ========================================
374
375 // Bug makes 1.0f alpha fail.
376 color(1.0f, 1.0f, 1.0f, 0.99f);
377
378 if (iconCount <= 0) {
379 return lastFrame(0);
380 }
381 int lastIcon = iconCount-1;
382
383 int page = g_PosPage;
384 float currentPagePosition = g_PosPage - page;
385
386 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
387 int icon = clamp(iconsPerPage * page, 0, lastIcon);
388
389 float scale = (1 / g_Zoom);
390
391 float pageAngle = VIEW_ANGLE * 1.2f;
392 draw_page(icon, lastIcon, -pageAngle*currentPagePosition, scale);
393 draw_page(icon+iconsPerPage, lastIcon, (-pageAngle*currentPagePosition)+pageAngle, scale);
394
395 // Draw the border lines for debugging ========================================
396 /*
397 bindProgramVertex(NAMED_PVOrtho);
398 bindProgramFragment(NAMED_PFOrtho);
399 bindProgramFragmentStore(NAMED_PFSText);
400
401 color(1.0f, 1.0f, 0.0f, 0.99f);
402 int i;
403 for (i=0; i<ROWS_PER_PAGE+1; i++) {
404 int y = loadI32(ALLOC_Y_BORDERS, i);
405 drawRect(0, y, SCREEN_WIDTH_PX, y+1, 0.0f);
406 }
407 for (i=0; i<COLUMNS_PER_PAGE+1; i++) {
408 int x = loadI32(ALLOC_X_BORDERS, i);
409 drawRect(x, 0, x+1, SCREEN_HEIGHT_PX, 0.0f);
410 }
411 */
412
413 // Draw the home button ========================================
414 draw_home_button();
415
416 /*
417 bindTexture(NAMED_PFOrtho, 0, loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_ID));
418 float handleLeft = 40 + (320 * (scrollXPx/(float)(maxScrollXPx)));
419 float handleTop = 680;
420 float handleWidth = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_WIDTH);
421 float handleHeight = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_HEIGHT);
422 drawRect(handleLeft, handleTop, handleLeft+handleWidth, handleTop+handleHeight, 0.0f);
423 */
424
425 // Bug workaround where the last frame is not always displayed
426 // So we keep rendering until the bug is fixed.
427 return lastFrame((g_PosVelocity != 0) || fracf(g_PosPage) || (g_Zoom != state->zoomTarget));
428}
429