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