Changeset 91

Show
Ignore:
Timestamp:
10/05/08 21:04:58 (1 month ago)
Author:
JoeCoder
Message:

Greatly simplified yage.scene.node.Node.calcTransform by using recursion.

Files:

Legend:

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

    r90 r91  
    11/** 
    2  * Copyright:  none 
     2 * Copyright:  Public Domain 
    33 * Authors:    Eric Poggel 
    4  * License:    Public Domain 
     4 * Warranty: none 
    55 * 
    66 * This module is not technically part of the engine, but merely uses it. 
  • trunk/src/demo1/main.d

    r90 r91  
    11/** 
    2  * Copyright:  none 
     2 * Copyright:  Public Domain 
    33 * Authors:    Eric Poggel 
    4  * License:    Public Domain 
     4 * Warranty: none 
    55 * 
    66 * This module is not technically part of the engine, but merely uses it. 
     7 * This is a demo to show off some of the cool features of Yage. 
    78 */ 
    89 
     
    2728    //Device.init(1024, 768, 32, true); 
    2829    //Device.init(1440, 900, 32, true); 
     30     
     31    /* 
     32    // test 
     33    auto t = new MovableNode(); 
     34    t.setPosition(Vec3f(0, 1, 0)); 
     35    auto r = t.addChild(new MovableNode()); 
     36    r.setPosition(Vec3f(0, 2, 0)); 
     37    writefln(r.getAbsolutePosition()); 
     38    return 1; 
     39    */ 
    2940 
    3041    // Paths 
     
    91102    window.onMouseOver = &onMouseOver; 
    92103    window.onMouseOut = &onMouseOut; 
    93  
     104     
    94105    // Events for main surface. 
    95106    view.onKeyDown = delegate void (Surface self, int key, int modifier){ 
  • trunk/src/demo1/ship.d

    r90 r91  
    11/** 
    2  * Copyright:  none 
     2 * Copyright:  Public Domain 
    33 * Authors:    Eric Poggel 
    4  * License:    Public Domain 
     4 * Warranty: none 
    55 * 
    66 * This module is not technically part of the engine, but merely uses it. 
  • trunk/src/demo2/main.d

    r90 r91  
    11/** 
    2  * Copyright:  none 
     2 * Copyright:  Public Domain 
    33 * Authors:    Eric Poggel 
    4  * License:    Public Domain 
     4 * Warranty:   none 
    55 * 
    66 * This module is not technically part of the engine, but merely uses it. 
  • trunk/src/yage/core/tree.d

    r90 r91  
    1616 * class Node : Tree!(Node) {} 
    1717 * auto n = new Node(); 
    18  * n.addChild(new Node()); 
     18 * auto n2 = n.addChild(new Node()); 
    1919 * --------------------------------  
    2020 */ 
     
    2929     * Automatically detaches it from any other element's children. 
    3030     * Params: 
    31      *     child =  
    32      * Returns: A reference to the child. 
    33      */ 
     31     *     child = Node to add as a child of this element. 
     32     * Returns: A reference to the child. */ 
    3433    S addChild(S : T)(S child) 
    3534    in { 
     
    7473    } 
    7574     
     75    S removeChild(S : T)(S child) 
     76    {   if (child.index > 0) 
     77        {   //yage.core.all.remove(parent.children, index, false); 
     78            children.remove(index, false); 
     79            if (index < parent.children.length) 
     80                parent.children[index].index = index; 
     81            child.index = -1; // so remove can't be called twice. 
     82            child.parent = null;             
     83        } 
     84        return child; 
     85    } 
     86     
    7687    /// Remove this element from its parent. 
    7788    /// TODO: replace with removeChild 
  • trunk/src/yage/scene/movable.d

    r90 r91  
    8282            return cache[scene.transform_read].transform_abs; 
    8383        if (transform_dirty) 
    84             calcTransform(); 
     84        {   calcTransform(); 
     85        } 
    8586        return transform_abs; 
    8687    } 
  • trunk/src/yage/scene/node.d

    r90 r91  
    9999         
    100100        merge(child); 
     101        auto old_parent = child.getParent(); 
    101102        super.addChild(child); 
    102103        setTransformDirty(); // seems like this should be child.setTransformDirty(), but that breaks things. 
     104        child.parentChange(old_parent); 
    103105        return child; 
    104106    } 
     
    166168     
    167169 
    168  
    169  
    170170    /// Get the Scene at the top of the tree that this node belongs to, or null if this is part of a sceneless node tree. 
    171171    Scene getScene() 
     
    266266    protected void calcTransform() 
    267267    { 
    268         // Errors occur here 
    269         // could this function be called by two different threads on the same Node? 
    270         // and then the path gets messed up? 
    271         Node path[16384] = void; // surely no one will have a scene graph deeper than this! 
    272         Node node = this; 
    273  
    274         // build a path up to the first node that does not have transform_dirty set. 
    275         int i=0; 
    276         synchronized(this)  // section references the node's parent, which could be changed by another thread. 
    277         {   do 
    278             {   path[i] = node; 
    279                 i++; 
    280                 // If parent isn't a Scene 
    281                 if (cast(Node)node.parent) 
    282                     node = cast(Node)node.parent; 
    283                 else break; 
    284             }while (node.parent !is null && node.transform_dirty) 
    285      
    286             // Follow back down that path calculating absolute matrices. 
    287             foreach_reverse(Node n; path[0..i]) 
    288             {   // If parent isn't a Scene 
    289                 if (cast(Node)n.parent) 
    290                     n.transform_abs = n.transform * (cast(Node)n.parent).transform_abs; 
    291                 else // since scene's don't have a transform matrix 
    292                     n.transform_abs = n.transform; 
    293                 n.transform_dirty = false; 
    294             } 
    295         } 
     268        if (transform_dirty) 
     269        {   synchronized(this)           
     270            {   if (parent) 
     271                {   synchronized(this.parent) 
     272                    {   parent.calcTransform(); 
     273                        transform_abs = transform * parent.transform_abs; 
     274                        transform_dirty = false; 
     275                }   } 
     276                else 
     277                    transform_abs = transform; 
     278        }   } 
     279    } 
     280 
     281    /* 
     282     * Called after a node's parent changes. */ 
     283    protected void parentChange(Node old_parent) 
     284    { 
     285         
    296286    } 
    297287}