-
Notifications
You must be signed in to change notification settings - Fork 43
Overview
Before diving into the SwiffCore source base, make sure that you have the SWF File Format Specification nearby. A basic understanding of the SWF architecture is good to have.
For the impatient:
A SWF file starts out with a small header, followed by a series of "tags". Each tag starts with the tag type, followed by the length in bytes of the tag (thus enabling parsers to advance through any unknown tag types).
SWF tags fall into three broad categories:
-
Definitions
Define shapes/movie clips/bitmaps/etc which can be instantiated and placed onto the stage multiple times. Each definition is referenced by a libraryID from 1-65535. -
Placements
Tags which say:
- "Hey, make an instance of library ID 36 and place it here."
- "Move that instance to this new location."
- "Fade that instance to 50% alpha"
- "Remove that instance!"
- Everything else
Most of a movie comprises #1 and #2.
-[SwiffMovie initWithData:]
creates a SwiffParser for the passed-in data. SwiffParser is a plain (non-CFTypeRef) C object responsible for parsing the SWF bitstream. After reading the header via SwiffParserReadHeader()
, the SwiffMovie advances through each tag (SwiffParserAdvanceToNextTag()
) in the SWF file and creates corresponding Objective-C instances.
When the SwiffMovie encounters a definition tag, it instantiates the corresponding Swiff...Definition and adds it to the m_definitions
ivar. The definition can later be obtained via -[SwiffMovie definitionWithLibraryID:]
.
At parse time, the SwiffMovie maintains an list of on-Stage instances (called the "display list", and represented by SwiffMovie.m_placedObjects
). Each time a ShowFrame
tag is encountered, a copy of this list is made and stuffed into a SwiffFrame object. Placement tags modify this list. The PlaceObject
tag adds a new instance to the list, or modifies an existing instance. The RemoveObject
tag removes an item from the list.
Consider the following animation of a disappearing bunny:
It would be represented in the SWF file with the following tags:
- DefineShape defines the vector instructions to draw the bunny
- PlaceObject adds the bunny to the display list at depth 5
- ShowFrame makes a frame of animation (frame 1)
- PlaceObject moves the object at depth 5
- ShowFrame makes a frame of animation (frame 2)
- DefineShape defines the magical poof
- DefineFont defines font information for "Poof!"
- DefineText defines the the text field for "Poof!"
- RemoveObject removes the object at depth 5
- PlaceObject places the magical poof at depth 5
- PlaceObject places the text field at depth 6
- ShowFrame renders the final frame (frame 3)
At parse time, our SwiffMovie would do the following:
- Encounter DefineShape. Create a SwiffShapeDefinition instance and add it to
m_definitions
- Encounter PlaceObject. Create a SwiffPlacedObject instance at
m_placedObjects[5]
- Encounter ShowFrame. Create a SwiffFrame object with
-placedObjects
containing Step 2's SwiffPlacedObject - Encounter placeObject. Create a copy of the SwiffPlacedObject at
m_placedObjects[5]
, modify it, store it back into m_placedObjects[5] - Encounter ShowFrame. Create a SwiffFrame object with
-placedObjects
containing Step 4's SwiffPlacedObject - Encounter DefineShape. Create a SwiffShapeDefinition instance and add it to
m_definitions
- Encounter DefineFont. Create a SwiffFontDefinition instance and add it to
m_definitions
- Encounter DefineText. Create a SwiffStaticTextDefinition instance and add it to
m_definitions
- Encounter RemoveObject. Clear out
m_placedObjects[5]
. - Encounter PlaceObject. Create a SwiffPlacedObject instance at
m_placedObjects[5]
that references #6's definition - Encounter PlaceObject. Create a SwiffPlacedObject instance at
m_placedObjects[6]
that references #8's text field - Encounter ShowFrame. Create a SwiffFrame object with
-placedObjects
containing the two SwiffPlacedObject instances