Changeset 484

Show
Ignore:
Timestamp:
10/13/07 05:20:10 (11 months ago)
Author:
krobillard
Message:

ThuneGL - Added animation & animate.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/thune/gl/boot.c

    r458 r484  
    77  "] func :load\n" 
    88  "[\n" 
    9   "    none :orient none :position\n" 
    10   "    none :viewport 65.0 :fov\n" 
     9  "    none :orient\n" 
     10  "    none :position\n" 
     11  "    none :viewport\n" 
     12  "    65.0 :fov\n" 
    1113  "    0.1 :near\n" 
    1214  "    10.0 :far\n" 
     
    2931  "]\n" 
    3032  "context :widget-proto\n" 
     33  "[\n" 
     34  "    none :value\n" 
     35  "    none :curve\n" 
     36  "    1.0 :period\n" 
     37  "    0.0 :time\n" 
     38  "    1 :behavior\n" 
     39  "]\n" 
     40  "context :animation\n" 
    3141  "script-env/os [\n" 
    3242  "    linux [$100 :mouse-lb\n" 
  • trunk/thune/gl/gx.c

    r458 r484  
    12611261 
    12621262 
     1263/* 
     1264  cv & res may be the same. 
     1265 
     1266  Return zero if error thrown. 
     1267*/ 
     1268static int _curveValue( UThread* ut, UCell* cv, UCell* res, double t ) 
     1269{ 
     1270    UBlock* blk; 
     1271    UCell* it; 
     1272    UCell* end; 
     1273    UCell* v1; 
     1274    int len; 
     1275    double t1, d; 
     1276 
     1277    blk = ur_block( cv ); 
     1278    UR_ITER_BLOCK( it, end, blk, cv ); 
     1279    len = end - it; 
     1280    if( len & 1 ) 
     1281    { 
     1282        --len; 
     1283        --end; 
     1284    } 
     1285    if( len ) 
     1286    { 
     1287        v1 = 0; 
     1288        while( it != end ) 
     1289        { 
     1290            // Assume decimal! for speed. 
     1291            if( t <= ur_decimal(it) ) 
     1292            { 
     1293                if( ! v1 ) 
     1294                { 
     1295                    ur_copyCell( res, it[1] ); 
     1296                    return 1; 
     1297                } 
     1298 
     1299                t1 = ur_decimal(v1); 
     1300                d  = ur_decimal(it) - t1; 
     1301                if( ! d ) 
     1302                    goto copy; 
     1303 
     1304                if( _lerpCells( v1 + 1, it + 1, (t - t1) / d, res ) ) 
     1305                    return 1; 
     1306 
     1307                ur_throwErr( UR_ERR_DATATYPE, 
     1308                         "lerp expected 2 decimal!/vec3!/coord! values" ); 
     1309                return 0; 
     1310            } 
     1311            v1 = it; 
     1312            it += 2; 
     1313        } 
     1314copy: 
     1315        ur_copyCell( res, v1[1] ); 
     1316    } 
     1317    else 
     1318    { 
     1319        ur_setNone( res ); 
     1320    } 
     1321    return 1; 
     1322} 
     1323 
     1324 
    12631325// (curve t -- val) 
    12641326UR_CALL( uc_curve_value ) 
    12651327{ 
    1266     double t, t1, d; 
    1267     int len; 
    1268     UCell* v1; 
     1328    double t; 
    12691329    UCell* cv; 
    1270     UCell* it; 
    1271     UCell* end; 
    1272     UBlock* blk; 
    12731330 
    12741331    if( ur_is(tos, UT_DECIMAL) ) 
     
    12821339    if( ur_is(cv, UT_BLOCK) ) 
    12831340    { 
    1284         blk = ur_block( cv ); 
    1285         UR_ITER_BLOCK( it, end, blk, cv ); 
    1286         len = end - it; 
    1287         if( len & 1 ) 
    1288         { 
    1289             --len; 
    1290             --end; 
    1291         } 
    1292         if( len ) 
    1293         { 
    1294             v1 = 0; 
    1295             while( it != end ) 
    1296             { 
    1297                 // Assume decimal! for speed. 
    1298                 if( t <= ur_decimal(it) ) 
    1299                 { 
    1300                     if( ! v1 ) 
    1301                     { 
    1302                         ur_copyCell( cv, it[1] ); 
    1303                         goto finish; 
    1304                     } 
    1305  
    1306                     t1 = ur_decimal(v1); 
    1307                     d  = ur_decimal(it) - t1; 
    1308                     if( ! d ) 
    1309                         goto copy; 
    1310  
    1311                     if( _lerpCells( v1 + 1, it + 1, (t - t1) / d, cv ) ) 
    1312                         goto finish; 
    1313                     ur_throwErr( UR_ERR_DATATYPE, 
    1314                              "lerp expected 2 decimal!/vec3!/coord! values" ); 
    1315                     return; 
    1316                 } 
    1317                 v1 = it; 
    1318                 it += 2; 
    1319             } 
    1320 copy: 
    1321             ur_copyCell( cv, v1[1] ); 
    1322         } 
    1323         else 
    1324         { 
    1325             ur_setNone( cv ); 
    1326         } 
    1327  
    1328 finish: 
    1329         UR_S_DROP; 
     1341        if( _curveValue( ut, cv, cv, t ) ) 
     1342        { 
     1343            UR_S_DROP; 
     1344        } 
    13301345        return; 
    13311346    } 
     
    13351350    ur_throwErr( UR_ERR_DATATYPE, 
    13361351                 "curve-value expected block! int!/decimal!" ); 
     1352} 
     1353 
     1354 
     1355enum ContextIndexAnimation 
     1356{ 
     1357    CI_ANIM_VALUE = 0, 
     1358    CI_ANIM_CURVE, 
     1359    CI_ANIM_PERIOD, 
     1360    CI_ANIM_TIME, 
     1361    CI_ANIM_BEHAVIOR, 
     1362    CI_ANIM_CELLS 
     1363}; 
     1364 
     1365 
     1366/* 
     1367  Return zero if error thrown. 
     1368*/ 
     1369static int _animate( UThread* ut, UCell* acell, double dt ) 
     1370{ 
     1371    double period; 
     1372    UCell* behav; 
     1373    UCell* curve; 
     1374    UCell* value; 
     1375    UCell* timec; 
     1376    UBlock* vblk = ur_blockPtr( acell->ctx.valBlk ); 
     1377    UCell* vc = vblk->ptr.cells; 
     1378 
     1379    if( vblk->used < CI_ANIM_CELLS ) 
     1380    { 
     1381        ur_throwErr( UR_ERR_SCRIPT, "Invalid animation context" ); 
     1382        return 0; 
     1383    } 
     1384 
     1385    curve = vc + CI_ANIM_CURVE; 
     1386    if( ! ur_is(curve, UT_BLOCK) ) 
     1387    { 
     1388        ur_throwErr( UR_ERR_DATATYPE, "animation curve must be a block!" ); 
     1389        return 0; 
     1390    } 
     1391 
     1392    value = vc + CI_ANIM_VALUE; 
     1393    if( ur_is(value, UT_BLOCK) ) 
     1394    { 
     1395        vblk = ur_block(value); 
     1396        value = vblk->ptr.cells + value->series.it; 
     1397    } 
     1398 
     1399    behav = vc + CI_ANIM_BEHAVIOR; 
     1400    if( ur_is(behav, UT_INT) ) 
     1401    { 
     1402        period = ur_decimal(vc + CI_ANIM_PERIOD); 
     1403 
     1404        timec = vc + CI_ANIM_TIME; 
     1405        dt += ur_decimal(timec); 
     1406        if( dt > period ) 
     1407        { 
     1408            int repeat = ur_int(behav) - 1; 
     1409            if( repeat < 1 )  
     1410            { 
     1411                ur_setNone(behav); 
     1412            } 
     1413            else 
     1414            { 
     1415                dt -= period; 
     1416                ur_int(behav) = repeat; 
     1417            } 
     1418        } 
     1419        else 
     1420        { 
     1421            ur_decimal(timec) = dt; 
     1422        } 
     1423 
     1424        if( (period != 1.0) && (period > 0.0) ) 
     1425            dt /= period; 
     1426 
     1427        return _curveValue( ut, curve, value, dt ); 
     1428    } 
     1429#if 0 
     1430    else if( ur_is(cell, UT_WORD) ) 
     1431    { 
     1432        switch( ur_atom(cell) ) 
     1433        { 
     1434            case UR_ATOM_LOOP: 
     1435            case UR_ATOM_PING_PONG: 
     1436            case UR_ATOM_PONG: 
     1437                break; 
     1438        } 
     1439    } 
     1440#endif 
     1441    return 1; 
     1442} 
     1443 
     1444 
     1445// (anims time -- ) 
     1446UR_CALL( uc_animate ) 
     1447{ 
     1448    UCell* prev; 
     1449    double dt; 
     1450 
     1451    if( ! ur_is(tos, UT_DECIMAL) ) 
     1452    { 
     1453        ur_throwErr( UR_ERR_DATATYPE, "animate expected decimal! time" ); 
     1454        return; 
     1455    } 
     1456    dt = ur_decimal(tos); 
     1457 
     1458    prev = ur_s_prev(tos); 
     1459    if( ur_is(prev, UT_CONTEXT) ) 
     1460    { 
     1461        if( ! _animate( ut, prev, dt ) ) 
     1462            return; 
     1463    } 
     1464    else if( ur_is(prev, UT_BLOCK) ) 
     1465    { 
     1466        UBlock* blk; 
     1467        UCell* it; 
     1468        UCell* end; 
     1469        blk = ur_block(prev); 
     1470        UR_ITER_BLOCK( it, end, blk, prev ); 
     1471        while( it != end ) 
     1472        { 
     1473            if( ur_is(it, UT_CONTEXT) ) 
     1474            { 
     1475                if( ! _animate( ut, it, dt ) ) 
     1476                    return; 
     1477            } 
     1478            ++it; 
     1479        } 
     1480    } 
     1481    else 
     1482    { 
     1483        ur_throwErr( UR_ERR_DATATYPE, "animate expected block!/context!" ); 
     1484        return; 
     1485    } 
     1486    UR_S_DROPN(2); 
    13371487} 
    13381488 
     
    15511701    { uc_lerp,           "lerp" }, 
    15521702    { uc_curve_value,    "curve-value" }, 
     1703    { uc_animate,        "animate" }, 
    15531704    { uc_gl_extensions,  "gl-extensions" }, 
    15541705    { uc_gl_max_textures,"gl-max-textures" }, 
     
    16281779    FIXED_ATOM( "sprite",   6, UR_ATOM_SPRITE ); 
    16291780 
     1781    // Animation 
     1782    FIXED_ATOM( "once",      4, UR_ATOM_ONCE ); 
     1783    //FIXED_ATOM( "loop",      4, UR_ATOM_LOOP ); 
     1784    FIXED_ATOM( "ping-pong", 9, UR_ATOM_PING_PONG ); 
     1785    FIXED_ATOM( "pong",      4, UR_ATOM_PONG ); 
     1786 
    16301787#if MAKE_ATOM_HEADER 
    16311788    exit(0); 
  • trunk/thune/gl/gx.t

    r458 r484  
    4848context :widget-proto 
    4949 
     50[ 
     51    none :value 
     52    none :curve 
     53    1.0  :period 
     54    0.0  :time 
     55    1    :behavior 
     56] 
     57context :animation 
    5058 
    5159/* 
  • trunk/thune/gl/gx_atoms.h

    r483 r484  
    4747#define UR_ATOM_TRANS           359 
    4848#define UR_ATOM_SPRITE          360 
     49#define UR_ATOM_ONCE            361 
     50#define UR_ATOM_PING_PONG               362 
     51#define UR_ATOM_PONG            363