Changeset 81

Show
Ignore:
Timestamp:
08/03/08 16:08:01 (4 months ago)
Author:
JoeCoder
Message:

More surface changes.
Preliminary skeletal animatin support for Milshape 3D models. (Collada model loading support will come eventually)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/demo1/gameobj.d

    r73 r81  
    6767        if (timer is null) 
    6868        {   timer = new Timer(); 
    69             timer.set(1); 
     69            timer.set(1);           
    7070        } 
    7171 
  • trunk/src/demo1/main.d

    r75 r81  
    124124    Timer delta = new Timer(); 
    125125    Log.write("Starting rendering loop."); 
    126     std.gc.fullCollect(); 
     126    std.gc.fullCollect();   
    127127    while(1) 
    128128    { 
  • trunk/src/demo2/main.d

    r73 r81  
    2323int main() 
    2424{ 
    25     /* 
    26     Object[] array1; 
    27     Object[uint] array2; 
    28      
    29     Timer a = new Timer(); 
    30     for (int j=0; j<100; j++) 
    31     for (int i=0; i<1000; i++) 
    32         array1 ~= new Object(); 
    33     writefln("array create: " ~ a.toString()); 
    34  
    35      
    36     a.reset(); 
    37     for (int j=0; j<100; j++) 
    38     for (int i=0; i<1000; i++) 
    39     {};  
    40     writefln("array loop: " ~ a.toString()); 
    41      
    42     a.reset(); 
    43     for (int j=0; j<100; j++) 
    44     for (int i=0; i<1000; i++) 
    45     {   Object b = new Object; 
    46         array2[b.toHash()] = b;  
    47     } 
    48     writefln("aa create: " ~ a.toString()); 
    49     //array2.rehash; 
    50  
    51     Timer c = new Timer(); 
    52     for (int j=0; j<100; j++) 
    53     foreach (i; array2) 
    54     {} 
    55     writefln("aa loop: " ~ c.toString()); 
    56      
    57     return 0; 
    58     */ 
    59      
    6025    // Init (resolution, depth, fullscreen, aa-samples) 
    6126    Device.init(800, 600, 32, false, 1); 
     27    //Device.init(1024, 768, 32, true); 
     28    //Device.init(1440, 900, 32, true); 
    6229     
    6330    // Paths 
     
    7340    Scene scene = new Scene(); 
    7441    scene.start(60); // update 60 times per second 
    75      
    7642    Device.onExit = &scene.stop; 
    7743 
     
    9258    camera.setView(2, 20000, 60, 0, 1); // wide angle view 
    9359     
    94     Surface bg = new Surface(null); 
    95     bg.setTexture(camera.getTexture()); 
    96     bg.topLeft = Vec2f(0,0); 
    97     bg.bottomRight = Vec2f(1, 1); 
    98     bg.setVisibility(true); 
    99          
    100     void onMousedown(Surface self, byte buttons, Vec2i coordinates){ 
    101         self.grabMouse(!ship.input); 
    102         ship.input = !ship.input; 
    103     } 
    104      
    105     void onMousemove(Surface self, byte buttons, Vec2i rel){ 
    106         if(ship.input){ 
    107             ship.mouseDelta = ship.mouseDelta.add(rel); 
    108         } 
    109     } 
    110      
    111     void onResize(Surface self){ 
    112         camera.setResolution(self.size.x, self.size.y); 
    113         writefln("Camera resolution changed to ", self.size.x, " x ", self.size.y); 
    114     } 
    115      
    116     void onKeydown(Surface self, byte key){ 
     60     
     61    // Main surface where camera output is rendered. 
     62    Surface view = new Surface(null); 
     63    view.style.backgroundMaterial = camera.getTexture(); 
     64    view.style.set("bottom: 0; right: 0");   
     65    Device.setSurface(view); 
     66     
     67    // Events for main surface. 
     68    view.onKeyDown = delegate void (Surface self, byte key){ 
    11769        if (key == SDLK_ESCAPE) 
    11870            Device.exit(0); 
    11971         
    120         if (key == SDLK_SPACE){ 
    121           Flare flare = new Flare(ship.getScene()); 
     72        if (key == SDLK_SPACE) 
     73        { Flare flare = new Flare(ship.getScene()); 
    12274            flare.setPosition(ship.getAbsolutePosition()); 
    12375            flare.setVelocity(Vec3f(0, 0, -150).rotate(ship.ship.getAbsoluteTransform())+ship.getVelocity()); 
     
    13082        } 
    13183         
    132         if(key == SDLK_c){ 
    133           std.gc.fullCollect();  
     84        if(key == SDLK_c) // Perform garbage collection, which used to crash. 
     85        { std.gc.fullCollect();  
    13486            writefln("garbage collected"); 
    13587        } 
    136     } 
    137      
    138     bg.onMousedown = &onMousedown; 
    139     bg.onResize = &onResize; 
    140     bg.onMousemove = &onMousemove; 
    141     bg.onKeydown = &onKeydown; 
    142      
    143     GPUTexture active = new GPUTexture("test/clear.png"); 
    144     GPUTexture inactive = new GPUTexture("test/clearInactive.png"); 
    145     GPUTexture inactive2 = new GPUTexture("test/clearInactive2.png"); 
    146      
    147     void onMousedown2(Surface self, byte buttons, Vec2i coordinates){ 
     88    }; 
     89    view.onMouseDown = delegate void (Surface self, byte buttons, Vec2i coordinates){ 
     90        self.grabMouse(!ship.input); 
     91        ship.input = !ship.input; 
     92    }; 
     93    view.onMouseMove = delegate void (Surface self, byte buttons, Vec2i rel){ 
     94        if(ship.input) 
     95            ship.mouseDelta = ship.mouseDelta + rel;         
     96    }; 
     97    view.onResize = delegate void (Surface self, Vec2f amount){ 
     98        camera.setResolution(cast(int)self.width, cast(int)self.height); 
     99    }; 
     100 
     101     
     102     
     103    // Create some windows 
     104    void onMouseDown2(Surface self, byte buttons, Vec2i coordinates){ 
    148105        self.raise(); 
    149         self.startDrag(); 
    150     } 
    151     void onMouseup2(Surface self, byte buttons, Vec2i coordinates){ 
    152         self.endDrag(); 
    153     } 
    154     void onMousemove2(Surface self, byte buttons, Vec2i diff){ 
    155         if(buttons == 1) self.drag(diff); 
    156     } 
    157     void onMouseenter(Surface self, byte buttons, Vec2i coordinates){ 
    158         self.setTexture(active); 
    159     } 
    160     void onMouseleave(Surface self, byte buttons, Vec2i coordinates){ 
    161         self.setTexture(inactive2); 
    162     } 
    163      
    164     Surface clear = new Surface(bg); 
    165     clear.setTexture(inactive2); 
    166     clear.topLeft = Vec2f(.65,0); 
    167     clear.bottomRight = Vec2f(1, .25); 
    168     clear.fill = stretched; 
    169     clear.setVisibility(true); 
    170     clear.onMousedown = &onMousedown2; 
    171     clear.onMousemove = &onMousemove2; 
    172     clear.onMouseup = &onMouseup2; 
    173     clear.onMouseenter = &onMouseenter; 
    174     clear.onMouseleave = &onMouseleave; 
    175      
    176     Surface clear2 = new Surface(clear); 
    177     clear2.setTexture(inactive2); 
    178     clear2.topLeft = Vec2f(.65,0); 
    179     clear2.bottomRight = Vec2f(1, .25); 
    180     clear2.fill = stretched; 
    181     clear2.setVisibility(true); 
    182     clear2.onMousedown = &onMousedown2; 
    183     clear2.onMousemove = &onMousemove2; 
    184     clear2.onMouseup = &onMouseup2; 
    185     clear2.onMouseenter = &onMouseenter; 
    186     clear2.onMouseleave = &onMouseleave; 
    187  
    188     Surface clear3 = new Surface(bg); 
    189     clear3.setTexture(inactive2); 
    190     clear3.topLeft = Vec2f(.65,0); 
    191     clear3.bottomRight = Vec2f(1, .25); 
    192     clear3.fill = stretched; 
    193     clear3.setVisibility(true); 
    194     clear3.onMousedown = &onMousedown2; 
    195     clear3.onMousemove = &onMousemove2; 
    196     clear3.onMouseup = &onMouseup2; 
    197     clear3.onMouseenter = &onMouseenter; 
    198     clear3.onMouseleave = &onMouseleave; 
    199  
    200     Surface clear4 = new Surface(bg); 
    201     clear4.setTexture(inactive2); 
    202     clear4.topLeft = Vec2f(.4,0); 
    203     clear4.bottomRight = Vec2f(1, .25); 
    204     clear4.fill = stretched; 
    205     clear4.setVisibility(true); 
    206     clear4.onMousedown = &onMousedown2; 
    207     clear4.onMousemove = &onMousemove2; 
    208     clear4.onMouseup = &onMouseup2; 
    209     clear4.onMouseenter = &onMouseenter; 
    210     clear4.onMouseleave = &onMouseleave; 
    211  
    212     Surface clear5 = new Surface(bg); 
    213     clear5.setTexture(inactive); 
    214     clear5.topLeft = Vec2f(.65,0); 
    215     clear5.bottomRight = Vec2f(1, .4); 
    216     clear5.fill = stretched; 
    217     clear5.setVisibility(true); 
    218     clear5.onMousedown = &onMousedown2; 
    219     clear5.onMousemove = &onMousemove2; 
    220     clear5.onMouseup = &onMouseup2; 
    221     clear5.onMouseenter = &onMouseenter; 
    222     clear5.onMouseleave = &onMouseleave; 
     106        self.focus(); 
     107    } 
     108    void onMouseUp2(Surface self, byte buttons, Vec2i coordinates){ 
     109        self.blur(); 
     110    } 
     111    void onMouseMove2(Surface self, byte buttons, Vec2i diff){ 
     112        if(buttons == 1)  
     113            self.move(cast(Vec2f)diff, true); 
     114    } 
     115    void onMouseOver(Surface self, byte buttons, Vec2i coordinates){ 
     116        self.style.set("background-material: url('gui/skin/clear3.png')"); 
     117    } 
     118    void onMouseOut(Surface self, byte buttons, Vec2i coordinates){ 
     119        self.style.set("background-material: url('gui/skin/clear2.png')"); 
     120    } 
     121 
     122    auto window1 = new Surface(view);    
     123    window1.style.set("top: 0; right: 0; width: 400; height: 60; background-position: 5px 5px; " ~  
     124        "background-repeat: nineslice; background-material: url('gui/skin/clear2.png')"); 
     125    window1.onMouseDown = &onMouseDown2; 
     126    window1.onMouseMove = &onMouseMove2; 
     127    window1.onMouseUp = &onMouseUp2; 
     128    //window1.onMouseOver = &onMouseOver; 
     129    //window1.onMouseOut = &onMouseOut; 
     130     
     131    auto window2 = new Surface(window1); 
     132    window2.style.set("top: 30; right: 0; width: 50; height: 30; background-position: 5px 5px; " ~  
     133        "background-repeat: nineslice; background-material: url('gui/skin/clear2.png')"); 
     134    window2.onMouseDown = &onMouseDown2; 
     135    window2.onMouseMove = &onMouseMove2; 
     136    window2.onMouseUp = &onMouseUp2; 
     137    window2.onMouseOver = &onMouseOver; 
     138    window2.onMouseOut = &onMouseOut;    
     139 
     140    window1.style.fontFamily = Resource.font("gui/font/Vera.ttf"); 
     141    window1.style.fontSize = 12; 
     142    window1.style.color = Color("black"); 
     143     
     144    // Beast 
     145     
     146    auto beast = new ModelNode(scene); 
     147    beast.setModel("beast/beast.ms3d"); 
     148    beast.setPosition(Vec3f(0, 25, -1000)); 
     149    beast.setRotation(Vec3f(0, 1.507, 0)); 
     150     
     151     
     152    void animate(Node self) 
     153    {   ModelNode node = cast(ModelNode)self; 
     154        node.getModel().animate(node.getScene().getStartTime()); 
     155    } 
     156    beast.onUpdate(&animate); 
     157 
    223158     
    224159    // Music 
    225     SoundNode music = new SoundNode(camera); 
     160    auto music = new SoundNode(camera); 
    226161    music.setSound("music/celery - pages.ogg"); 
    227162    music.setLooping(true); 
     
    229164 
    230165    // Lights 
    231     LightNode l1 = new LightNode(scene); 
     166    auto l1 = new LightNode(scene); 
    232167    l1.setDiffuse(Color(1, .85, .7)); 
    233168    l1.setLightRadius(7000); 
     
    235170 
    236171    // Star 
    237     SpriteNode star = new SpriteNode(l1); 
     172    auto star = new SpriteNode(l1); 
    238173    star.setMaterial("space/star.xml"); 
    239174    star.setSize(Vec3f(2500)); 
     
    242177    auto planet = new ModelNode(scene); 
    243178    planet.setModel("space/planet.ms3d"); 
    244     planet.setSize(Vec3f(600)); 
     179    planet.setSize(Vec3f(60)); 
    245180    planet.setAngularVelocity(Vec3f(0, -0.01, 0)); 
    246181     
     
    253188    } 
    254189    scene.onUpdate(&update); 
    255      
    256     bg.recalculate(); 
    257190     
    258191    // Rendering / Input Loop 
     
    261194    Timer delta = new Timer(); 
    262195    Log.write("Starting rendering loop."); 
    263     std.gc.fullCollect(); 
    264     while(1){ 
     196    //std.gc.fullCollect(); 
     197     
     198     
     199    Timer total = new Timer(); 
     200    total.setBounds(3, 5.0); 
     201     
     202    while(1) 
     203    { 
     204        writefln(total); 
     205         
    265206        float dtime = delta.get(); 
    266207        delta.reset(); 
     
    268209        Input.processInput(); 
    269210        camera.toTexture(); 
    270         bg.render(); 
    271          
     211        view.render(); 
    272212         
    273213        // Print framerate 
    274214        fps++; 
    275215        if (frame.get()>=0.25f) 
    276         {   char[] caption = formatString("Yage Test (%.2f fps) (%d objects, %d polygons, %d vertices rendered)\0", 
     216        {   char[] caption = formatString("(%.2f fps) (%d objects, %d polygons, %d vertices)", 
    277217                fps/frame.get(), camera.getNodeCount(), camera.getPolyCount(), camera.getVertexCount()); 
    278             SDL_WM_SetCaption(caption.ptr, null); 
    279             //delete caption; 
     218            SDL_WM_SetCaption((caption ~"\0").ptr, null); 
     219            window1.text = caption; 
    280220            frame.reset(); 
    281221            fps = 0; 
  • trunk/src/demo2/ship.d

    r71 r81  
    6262 
    6363        // Accelerate forward 
    64         if (Input.keydown[SDLK_UP] || Input.keydown[SDLK_w]) 
     64        if (Input.keyDown[SDLK_UP] || Input.keyDown[SDLK_w]) 
    6565        { 
    6666            accelerate(Vec3f(0, 0, -speed).rotate(pitch.getTransform()).rotate(getTransform())); 
     
    6969 
    7070        // Accelerate left, right, and backward 
    71         if (Input.keydown[SDLK_LEFT] || Input.keydown[SDLK_a]) 
     71        if (Input.keyDown[SDLK_LEFT] || Input.keyDown[SDLK_a]) 
    7272            accelerate(Vec3f(-speed/6, 0, 0).rotate(pitch.getTransform()).rotate(getTransform())); 
    73         if (Input.keydown[SDLK_RIGHT] || Input.keydown[SDLK_d]) 
     73        if (Input.keyDown[SDLK_RIGHT] || Input.keyDown[SDLK_d]) 
    7474            accelerate(Vec3f(speed/6, 0, 0).rotate(pitch.getTransform()).rotate(getTransform())); 
    75         if (Input.keydown[SDLK_DOWN] || Input.keydown[SDLK_s]) 
     75        if (Input.keyDown[SDLK_DOWN] || Input.keyDown[SDLK_s]) 
    7676            accelerate(Vec3f(0, 0, speed/3).rotate(pitch.getTransform()).rotate(getTransform())); 
    7777 
  • trunk/src/yage/candy.ddoc

    r35 r81  
    8686    </table> 
    8787    <div id="docfooter"> 
    88         Yage and all source files &copy; 2005-2007 Eric Poggel<br> 
     88        Yage and all source files &copy; 2005-2008 Eric Poggel<br> 
    8989        Documentation generated with <a href="http://www.dsource.org/projects/helix/wiki/CandyDoc"> 
    9090        <img src="img/candydoc.gif" alt="CandyDoc" style="vertical-align:middle; top:-1px"></a> 
  • trunk/src/yage/core/array.d

    r73 r81  
    6969 
    7070 
    71 /// Return the element with the maximum value of an array. 
     71/// Return the element with the minimum or maximum value of an array. 
    7272T amax(T)(T[] array, ) 
    7373{   T m = array[0]; 
     
    7777    return m; 
    7878} 
    79 ///  
     79/// ditto 
    8080T amax(T, K)(T[] array, K delegate(T elem) getKey) 
    8181{   T m = array[0]; 
     
    8686    return m; 
    8787} 
    88 T amax(T, K)(T[T] array, K delegate(T elem) getKey) 
    89 {   T m; 
    90     K mk; 
    91     foreach (T a; array) 
    92     {   m = a; 
    93         mk = getKey(a); 
    94         break;       
    95     } 
    96     foreach (T a; array) 
    97     {   if (getKey(a)>mk) 
    98             m=a;     
    99     } 
    100     return m; 
    101 
    102  
    103 /// Return the minimum value of an array. 
     88/// ditto 
    10489T amin(T)(T[] array) 
    10590{   T m = array[0]; 
     
    10994    return m; 
    11095} 
     96/// ditto 
     97T amin(T, K)(T[] array, K delegate(T elem) getKey) 
     98{   T m = array[0]; 
     99    K mk = getKey(array[0]); 
     100    foreach (T a; array) 
     101        if (getKey(a)<mk) 
     102            m=a;     
     103    return m; 
     104} 
     105 
    111106 
    112107/** 
  • trunk/src/yage/core/color.d

    r68 r81  
    1919 * so Color(0x6633ff00).hex == "00FF3366" 
    2020 * All Colors default to transparent black. 
    21  * TODO: Convert to using four floats for better arithmetic 
     21 * TODO: Convert to using four floats for better arithmetic? 
    2222 *  
    2323 * Example: 
     
    3131struct Color 
    3232{ 
    33     private static real frac = 1.0f/255; 
     33    private const real frac = 1.0f/255; 
    3434     
    3535    union 
     
    136136            case "yellow":  return Color(0xFF00FFFF); 
    137137            default: break; 
    138         }        
    139      
     138        } 
     139         
     140        // Allow hex colors to start with hash. 
     141        if (string[0] == '#') 
     142            string = string[1..length]; 
     143             
    140144        // Append alpha to 6-digit hex string. 
    141145        if (string.length == 6) 
    142             string ~= "FF";         
    143          
    144         // Convert string one char at a a time. 
     146            string ~= "FF"; // creates garbage! 
     147         
     148        // Convert string one char at a time. 
    145149        Color result; 
    146150        int digit; 
     
    210214    unittest 
    211215    {   assert(Color.sizeof == 4); 
    212          
     216            
    213217        // Test initializers 
    214218        assert(Color([0, 102, 51, 255]).hex == "006633FF"); 
  • trunk/src/yage/core/matrix.d

    r51 r81  
    277277    } 
    278278 
    279     /// Perform Matrix multiplication in reverse (since Matrix multiplication is not cummulative.) 
     279    /// Perform Matrix multiplication in reverse (since Matrix multiplication is not cummulative.) Is this backwards? 
    280280    Matrix postMultiply(Matrix b) 
    281281    {   return *this*b; 
    282282    } 
    283283 
     284    /// 
     285    Vec3f position() 
     286    {   return Vec3f(v[12..15]);         
     287    } 
     288     
    284289    /// 
    285290    void *ptr() 
     
    425430    } 
    426431 
    427     /// Set the rotation values of this Matrix from another Matrix 
     432    /// 
     433    void setPosition(Vec3f position) 
     434    {   v[12..15] = position.v[0..3]; 
     435    } 
     436     
     437    /// Set the rotation values of this Matrix from another Matrix or Quatrn. 
    428438    void setRotation(Matrix rot) 
    429439    {   v[0..3]  = rot.v[0..3]; 
    430440        v[4..7]  = rot.v[4..7]; 
    431441        v[8..11] = rot.v[8..11]; 
     442    } 
     443    /// ditto 
     444    void setRotation(Quatrn rot) 
     445    {   v[0] = 1-2*(rot.y*rot.y + rot.z*rot.z); 
     446        v[1] =   2*(rot.x*rot.y + rot.z*rot.w); 
     447        v[2] =   2*(rot.x*rot.z - rot.y*rot.w); 
     448        //v[3] =   0; 
     449        v[4] =   2*(rot.x*rot.y - rot.z*rot.w); 
     450        v[5] = 1-2*(rot.x*rot.x + rot.z*rot.z); 
     451        v[6] =   2*(rot.y*rot.z + rot.x*rot.w); 
     452        //v[7] =   0; 
     453        v[8] =   2*(rot.x*rot.z + rot.y*rot.w); 
     454        v[9] =   2*(rot.y*rot.z - rot.x*rot.w); 
     455        v[10] = 1-2*(rot.x*rot.x + rot.y*rot.y); 
    432456    } 
    433457 
     
    467491        return res; 
    468492    } 
    469  
    470493 
    471494    /** Convert the rotation part of the Matrix to Euler angles. 
  • trunk/src/yage/core/quatrn.d

    r51 r81  
    260260    } 
    261261 
    262     /// Set the rotation using a Vec3f of Euler angles. 
     262    /// Set the rotation using a Vec3f of Euler angles. TODO: replace with code below. 
    263263    void setEuler(Vec3f euler) 
    264264    {   *this = 
     
    267267          * Quatrn(0, 0, sin(euler.z*0.5), cos(euler.z*0.5)); // z 
    268268    } 
     269    /* 
     270    inline void quatrn::fromvector(vector rot) 
     271    { 
     272        float hlf; 
     273        double sr, sp, sy, cr, cp, cy; 
     274 
     275        hlf = rot[2]*0.5f; 
     276        sy = sin(hlf); 
     277        cy = cos(hlf); 
     278        hlf = rot[1]*0.5f; 
     279        sp = sin(hlf); 
     280        cp = cos(hlf); 
     281        hlf = rot[0]*0.5f; 
     282        sr = sin(hlf); 
     283        cr = cos(hlf); 
     284 
     285        double crcp = cr*cp; 
     286        double srsp = sr*sp; 
     287        double srcp = sr*cp; 
     288 
     289        v[0] = float(srcp*cy- cr*sp*sy); 
     290        v[1] = float(cr*sp*cy+ srcp*sy); 
     291        v[2] = float(crcp*sy - srsp*cy); 
     292        v[3] = float(crcp*cy + srsp*sy); 
     293    }*/ 
    269294 
    270295    /** 
  • trunk/src/yage/core/timer.d

    r32 r81  
    77module yage.core.timer; 
    88 
     9import std.c.math; 
    910import std.perf; 
    1011import std.string; 
     
    2930{ 
    3031    protected bool      paused  = false; 
    31     //protected real  min     = 0; 
    32     //protected real  max     = real.infinity; 
    33     //protected real  speed   = 1.0; 
     32    protected double  min     = 0; 
     33    protected double  max     = real.infinity; 
     34    //protected double    speed   = 1.0; 
    3435    protected ulong     us      = 0;        // microsecond counter 
    3536    protected PerformanceCounter hpc; 
     
    5556            {   hpc.stop(); 
    5657                us +=  hpc.microseconds(); 
    57                 real result = us*0.000001; 
    5858                hpc.start(); 
    5959            } 
    60             return us*0.000001; 
    61     }   } 
     60        } 
     61        double relative = us*0.000001; 
     62         
     63        real range = max-min; 
     64        if (relative > max) 
     65            relative = min + fmod(relative-min, range); 
     66        if (relative < min) 
     67            relative = max + fmod(relative-max, range); 
     68 
     69        return relative; 
     70    }    
    6271 
    6372    /// Is the Timer paused? 
     
    105114    } 
    106115 
     116     
     117    /** 
     118     * Set the timers rollunder and rollover values. 
     119     * Provide no arguments to reset them to the defaults of 0 and infinity. 
     120     * Params: 
     121     *     min = The timer will never be less than this value. 
     122     *     max = The timer will never be greater than this value and will rollover back to min after crossing it.*/ 
     123    void setBounds(double min=0, double max=double.infinity) 
     124    {   this.min = min; 
     125        this.max = max;      
     126    } 
     127     
     128     
    107129    char[] toString() 
    108130    {   return .toString(get()); 
  • trunk/src/yage/core/tree.d

    r73 r81  
    1717 * auto n = new Node(); 
    1818 * n.addChild(new Node()); 
    19  * -------------------------------- */ 
     19 * --------------------------------  
     20 */ 
    2021class Tree(T) 
    2122{    
    22     T parent; 
    23     T[] children; 
    24     int index=-1; 
    25      
    26     /// Ensure that child is removed from its parent. 
    27 //  ~this() 
    28 //  {   remove();        
    29 //  } 
    30      
     23    protected T parent;         // reference to parent 
     24    protected T[] children;     // array of this element's children. 
     25    protected int index=-1;     // index of this element in its parent's array, -1 if no parent. 
     26 
    3127    /** 
    3228     * Add a child element. 
    3329     * Automatically detaches it from any other element's children. 
    34      * Returns: A self reference.*/ 
     30     * Params: 
     31     *     child =  
     32     * Returns: A reference to the child. 
     33     */ 
    3534    T addChild(T child) 
    3635    in { 
     
    3938    }body 
    4039    {   child.setParent(cast(T)this); 
    41         return cast(T)this
     40        return child
    4241    } 
    4342     
     
    5352    {   return parent; 
    5453    } 
    55     T setParent(T _parent) /// Ditto 
     54    T setParent(T _parent) /// ditto 
    5655    in { assert(_parent !is null); 
    5756    }body 
     
    6665    } 
    6766     
    68     /// Is elem a child of this element? 
     67    /** 
     68     * Is elem a child of this element? 
     69     * This function will also return false if elem is null. */  
    6970    bool isChild(T elem) 
    70     {   if (elem.index < 0 || elem.index >= children.length) 
     71    {   if (!elem || elem.index < 0 || elem.index >= children.length) 
    7172            return false; 
    7273        return cast(bool)(children[elem.index] == elem); 
    7374    } 
    7475     
    75     /// Remove this element from its parent 
     76    /// Remove this element from its parent. 
    7677    void remove() 
    7778    {   // this needs to happen because some children (like lights) may need to do more in their remove() function. 
  • trunk/src/yage/core/vector.d

    r67 r81  
    4141    } 
    4242 
    43     invariant 
    44     {   foreach (T t; v) 
    45             assert(t != float.nan); 
    46     } 
    47  
    4843    /// Create a zero vector 
    4944    static VST opCall() 
     
    7267        VST res; 
    7368        res.v[0..S] = s[0..S]; 
    74         return res; 
    75     } 
    76  
    77     /// 
    78     VST add(VST s) 
    79     {   VST res; 
    80         for (int i=0; i<v.length; i++) 
    81             res.v[i] = v[i]+s.v[i]; 
    8269        return res; 
    8370    } 
     
    10895    } 
    10996 
     97    /// Is this Vector inside a box/cube/etc. defined by topLeft and bottomRight 
     98    bool inside(VST topLeft, VST bottomRight, bool inclusive=true) 
     99    {   if (inclusive) 
     100        {   for (int i=0; i<v.length; i++) 
     101                if (v[i] <= topLeft[i] || v[i] >= bottomRight[i]) 
     102                    return false; 
     103        } else 
     104        {   for (int i=0; i<v.length; i++) 
     105                if (v[i] < topLeft[i] || v[i] > bottomRight[i]) 
     106                    return false; 
     107        }                    
     108        return true; 
     109    } 
     110     
    110111    /// Return the _length of the vector (the magnitude). 
    111112    float length() 
     
    121122    } 
    122123 
     124 
     125    /// Allow for linear additions, subtractions, multiplcations, and divisions among Vectors of the same size and type. 
     126    VST opAdd(VST s) 
     127    {   VST res; 
     128        for (int i=0; i<v.length; i++) 
     129            res.v[i] = v[i]+s.v[i]; 
     130        return res; 
     131    } 
     132    /// ditto 
     133    void opAddAssign(VST s) 
     134    {   for (int i=0; i<v.length; i++) 
     135            v[i] += s.v[i]; 
     136    }    
     137    /// ditto 
     138    VST opSub(VST s) 
     139    {   VST res; 
     140        for (int i=0; i<v.length; i++) 
     141            res.v[i] = v[i]-s.v[i]; 
     142        return res; 
     143    } 
     144    /// ditto 
     145    void opSubAssign(VST s) 
     146    {   for (int i=0; i<v.length; i++) 
     147            v[i] -= s.v[i]; 
     148    } 
     149     
     150    /// Allow for additions, subtractions, multiplcations, and divisions where a scalar is applied to each vector component. 
     151    void opMulAssign(float s) 
     152    {   for (int i=0; i<v.length; i++) 
     153            v[i] += s; 
     154    } 
     155    /// ditto 
     156    void opDivAssign(float s) 
     157    {   for (int i=0; i<v.length; i++) 
     158            v[i] /= s; 
     159    } 
     160     
     161    /// Allow casting to float where appropriate 
     162    static if (is(T : float))   // if T can be implicitly cast to float 
     163    {   Vec!(S, float) opCast() 
     164        {   Vec!(S, float) result; 
     165            for (int i=0; i<v.length; i++) 
     166                result.v[i] = v[i]; 
     167            return result; 
     168    }   } 
     169     
    123170    /// Get the element at i 
    124     float opIndex(ubyte i) 
     171    float opIndex(size_t i) 
    125172    {   return v[i]; 
    126173    } 
    127174 
    128175    /// Assign value to the element at i 
    129     float opIndexAssign(T value, ubyte i) 
     176    float opIndexAssign(T value, size_t i) 
    130177    {   return v[i] = value; 
    131178    } 
     
    200247        {   float x, y, z; 
    201248    }   } 
    202  
    203     invariant 
    204     {   foreach (float t; v) 
    205             assert(t != float.nan); 
    206     } 
    207249 
    208250    /** Test some of the more common and more complex functions. */ 
     
    334376    {   return Vec3f(1/x, 1/y, 1/z); 
    335377    } 
     378     
     379    /** 
     380     * Unlike a transformation, we first apply the translation and then the rotation. */ 
     381    Vec3f inverseTransform(Matrix m) 
     382    {   Vec3f copy = Vec3f(x-m[12], y-m[13], z-m[14]); // apply translation in reverse 
     383        return Vec3f( 
     384            copy.x*m[0] + copy.y*m[1] + copy.z*m[2], 
     385            copy.x*m[4] + copy.y*m[5] + copy.z*m[6], 
     386            copy.x*m[8] + copy.y*m[9] + copy.z*m[10] 
     387        ); 
     388    } 
     389     
     390    /// 
     391    Vec3f inverseRotate(Matrix m) 
     392    {   Vec3f result; 
     393        result.v[0] = v[0]*m[0] + v[1]*m[1] + v[2]*m[2]; 
     394        result.v[1] = v[0]*m[4] + v[1]*m[5] + v[2]*m[6]; 
     395        result.v[2] = v[0]*m[8] + v[1]*m[9] + v[2]*m[10]; 
     396        return result; 
     397    } 
     398 
     399    /// 
     400    Vec3f inverseTranslate(Matrix m) 
     401    {   return Vec3f(x-m[12], y-m[13], z-m[14]); 
     402    } 
    336403 
    337404    /// Return the length of the vector (the magnitude). 
     
    467534        return res; 
    468535    } 
    469  
     536     
    470537    /// Return a copy of this vector with each component scaled by s. 
    471538    Vec3f scale(float s) 
     
    517584 
    518585    /** 
    519      * Interpret the values of this vector as a rotation axis and convert to 
    520      * a Quatrn.*/ 
     586     * Interpret the values of this vector as a rotation axis and convert to a Quatrn.*/ 
    521587    Quatrn toQuatrn() 
    522588    {   Quatrn res; 
     
    535601 
    536602    /** 
    537      * Interpret the values of this vector as a rotation axis and convert to 
    538      * a rotation Matrix.*/ 
     603     * Interpret the values of this vector as a rotation axis and convert to a rotation Matrix.*/ 
    539604    Matrix toMatrix() 
    540605    {   Matrix res; 
     
    561626    {   return formatString("<%.4f %.4f %.4f>", x, y, z); 
    562627    } 
     628 
     629    /// 
     630    void translate(Matrix m) 
     631    {   x += m.v[12]; 
     632        y += m.v[13]; 
     633        z += m.v[14]; 
     634    } 
     635     
     636    /// Return a copy of this vector transformed by Matrix m. 
     637    Vec3f transform(Matrix m) 
     638    {   return Vec3f( 
     639            x*m.v[0] + y*m.v[4] + z*m.v[8] + m.v[12], 
     640            x*m.v[1] + y*m.v[5] + z*m.v[9] + m.v[13], 
     641            x*m.v[2] + y*m.v[6] + z*m.v[10]+ m.v[14] 
     642        ); 
     643    } 
    563644} 
  • trunk/src/yage/gui/style.d

    r67 r81  
    44module yage.gui.style; 
    55 
     6import std.regexp; 
     7import std.string; 
     8import std.stdio; 
     9import std.conv; 
    610import yage.core.color; 
    711import yage.core.vector; 
     12import yage.resource.font; 
     13import yage.resource.resource; 
    814import yage.resource.material; 
     15import yage.resource.texture; 
    916 
    1017/** 
    1118 * Specifies the style of a Surface. 
    12  * Inspired by the CSS specification (http://www.w3schools.com/css/css_reference.asp)
    13  * Defined here to keep things well separated.  
    14  * Styles that have a top, right, bottom, left (like margin, border) are stored in arrays of length 4.*/ 
     19 * Inspired by the <a href="http://www.w3schools.com/css/css_reference.asp">CSS specification</a>
     20 *  
     21 * This struct is not fully documented. */ 
    1522struct Style 
    16 
    17     enum Unit {PX, PERCENT}; 
    18  
    19     Material backgroundMaterial; 
    20     Color   backgroundColor; 
    21     float[4] backgroundCoordinates;  // Texture coordinates for top, right, bottom, left edges 
    22  
    23     float[4] borderWidth; 
    24     Unit[4]  borderWidthUnits; 
    25     Color[4] borderColor; 
    26     Material decoration;        // Overrides radius and color if set 
    27     byte[5]  decorationRepeat;  // top, right, bottom, left, center 
    28  
    29     Material cursor; 
    30     bool visible = false;; 
    31     byte position; 
    32  
    33     //Font  fontFamily; 
    34     float fontSize; 
    35     byte  fontSizeUnits; 
    36     float fontWeight; 
    37  
    38     float[4] padding; 
    39     Unit[4] paddingUnits; 
    40  
    41     float[4] dimension;         // top, right, bottom, left 
    42&n