All pastes #186250 Raw Edit

nickspacek

public text v1 · immutable
#186250 ·published 2006-09-29 18:49 UTC
rendered paste body
private void LoadScn(string fileName, SpriteArray spriteArray, DynamicSpriteArray dynamicSpriteArray,
    SpriteGridArray spriteGridArray, AttachableList<SpriteFrame> spriteFrames, SpriteLayer spriteLayer)
{
    // the first step is to create the SpriteEditorScene and load (deserialize it)
    SpriteEditorScene spriteEditorScene = new SpriteEditorScene();
    spriteEditorScene.LoadScene(fileName);

    // Remember, Sprites in the Scene can have attachments.  These attachments should
    // obviously be reestablished when the .scn loads.  To do this, we have to have a 
    // SpriteArray storing all elements which can be attached to.  Two things to remember:
    // 1.  The SpriteArray will have Sprites and DynamicSprites as they can be attached 
    //      to eachother.
    // 2.  Sprites can (and should) be added one-way since the list will only 
    //      be used in this method.  Not only is this slightly faster, but there's no
    //      need to explicitly clear the SpriteArra at the end of the method if it's one way.
    SpriteArray oneWaySpriteArray = new SpriteArray();

    // great, everything's loaded into ram.  Next step is to take the individual components
    // of the SpriteEditorScene and create actual FRB objects out of them.

    // first, we'll deal with the regular Sprites.  All Sprite information is stored
    // in the SpriteSaveArray field called ssa.  It is possible to manually set each
    // individual Sprite property from its corresponding SpriteSave reference, but 
    // not only is that a lot of code, but it also requires your code to update whenever
    // the .scn format changes.  It's much easier to simply call the Sprite's SetFromSave
    // method.

    // I'll loop through the ssa SpriteSaveArray and create the Sprite from the SpriteSave
    foreach(SpriteSave spriteSave in spriteEditorScene.ssa) {
        Sprite newSprite = new Sprite();
        newSprite.SetFromSave(spriteSave);

        // since the SetFromSave is a public method, any objects which inherit from the
        // Sprite class will also be able to call this method.  That means that regardless
        // of the type, this method will work.  Just replace the Sprite type with your
        // object type.

        // add the Sprite to the SpriteManager and perhaps to other SpriteArrays
        if(spriteLayer != null)
            sprMan.AddSprite(newSprite, spriteLayer);
        else
            sprMan.AddSprite(newSprite);

        spriteArray.Add(newSprite); // this is two way
        oneWaySpriteArray.AddOneWay(newSprite); // this is one way

        // the Sprite is almost ready to go.  One thing that remains is to validate the
        // ordered/unordered property of the Sprite.  That is, the Sprite knows if it
        // should be ordered or unordered, so calling ValidateOrdered will make sure that
        // it is categorized correctly in the SpriteManager.
        // Setting the texture and attachments will be done outside of the 
        // loops once we have all of our Sprites and Dynamic Sprites created.
        sprMan.ValidateOrdered(newSprite);
    }

    // yeah, it's redundant, but do it all again for DynamicSprites
    foreach(DynamicSpriteSave dynamicSpriteSave in spriteEditorScene.dynamicSpriteSaveList) {
        DynamicSprite newDynamicSprite = new DynamicSprite();
        newDynamicSprite.SetFromSave(dynamicSpriteSave);

        // add the DynamicSprite to the SpriteManager and perhaps to other SpriteArrays
        if(spriteLayer != null)
            sprMan.AddDynamicSprite(newDynamicSprite, spriteLayer);
        else
            sprMan.AddDynamicSprite(newDynamicSprite);

        dynamicSpriteArray.Add(newDynamicSprite); // this is two way
        oneWaySpriteArray.AddOneWay(newDynamicSprite); // this is one way

        // no need to validate ordered
    }
    // validate the textures
    // this call is what usually takes the most time when loading a scene.  .scn files
    // are usually very small (a few kbytes), whereas all textures can total a few
    // mbytes.  
    sprMan.ValidateSpriteTextureAndAnimations(oneWaySpriteArray);

    // may consider checking the static SpriteManager.texturesNotFound field if you plan
    // on handling textures that weren't found when validating.  If checking this, 
    // you may also want to check it after creating the MSGs like SpriteGrids and SpriteFrames

    // now that all Sprites are loaded, we can validate Dependencies (attachments)
    sprMan.ValidateDependencies(spriteEditorScene.ssa, oneWaySpriteArray, oneWaySpriteArray);
    sprMan.ValidateDependencies(spriteEditorScene.dynamicSpriteSaveList, oneWaySpriteArray, oneWaySpriteArray);

    // our Sprites are good to go.  Next, SpriteGrids
    // the spriteGridSaveArray could be null, so check against that
    if(spriteEditorScene.spriteGridSaveArray != null) {
        spriteGridArray.SetFromSave(spriteEditorScene.spriteGridSaveArray, sprMan, camera, random);

        // just like Sprites, SpriteGrids must have their textures validated.
        // Also, they must be populated and the paint must be refreshed.
        foreach(SpriteGrid spriteGrid in spriteGridArray) {
            // If using a version of FRB earlier than August 2007, the
            // following call is needed.  Otherwise, it is done automatically in the
            // SetFromSave call above.
            spriteGrid.ValidateTexturesAndAnimations();

            // We'll just say the population should occur at the center of the screen
            spriteGrid.PopulateGrid(camera.x, camera.y, camera.z);
            spriteGrid.RefreshPaint();
        }
    }
    // ahh, now that part was easy.  Finally, we create the SpriteFrames.  Like the SpriteGrids,
    // they're pretty easy to deal with.

    // first, make sure the spriteFrameSaveList is not null
    if(spriteEditorScene.spriteFrameSaveList != null) {
        // let's create the SpriteFrames
        foreach(SpriteFrameSave spriteFrameSave in spriteEditorScene.spriteFrameSaveList) {
            SpriteFrame newSpriteFrame;
            if(spriteLayer != null)
                newSpriteFrame = new SpriteFrame(sprMan, spriteFrameSave, spriteLayer);
            else
                newSpriteFrame = new SpriteFrame(sprMan, spriteFrameSave);

            // SpriteFrames automatically validate their textures when loading from
            // a SpriteFrameSave - how nice of them!

            spriteFrames.Add(newSpriteFrame); // two way
        }
    }
}