Skip to content
iccir edited this page Jan 12, 2012 · 1 revision

Overview

Introduction

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:

SWF Basics

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:

  1. 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.
  2. 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!"
  1. Everything else

Most of a movie comprises #1 and #2.

Class Overview

Class Overview

From the bottom-up

-[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:
Disappearing Bunny

It would be represented in the SWF file with the following tags:

  1. DefineShape defines the vector instructions to draw the bunny
  2. PlaceObject adds the bunny to the display list at depth 5
  3. ShowFrame makes a frame of animation (frame 1)
  4. PlaceObject moves the object at depth 5
  5. ShowFrame makes a frame of animation (frame 2)
  6. DefineShape defines the magical poof
  7. DefineFont defines font information for "Poof!"
  8. DefineText defines the the text field for "Poof!"
  9. RemoveObject removes the object at depth 5
  10. PlaceObject places the magical poof at depth 5
  11. PlaceObject places the text field at depth 6
  12. ShowFrame renders the final frame (frame 3)

At parse time, our SwiffMovie would do the following:

  1. Encounter DefineShape. Create a SwiffShapeDefinition instance and add it to m_definitions
  2. Encounter PlaceObject. Create a SwiffPlacedObject instance at m_placedObjects[5]
  3. Encounter ShowFrame. Create a SwiffFrame object with -placedObjects containing Step 2's SwiffPlacedObject
  4. Encounter placeObject. Create a copy of the SwiffPlacedObject at m_placedObjects[5], modify it, store it back into m_placedObjects[5]
  5. Encounter ShowFrame. Create a SwiffFrame object with -placedObjects containing Step 4's SwiffPlacedObject
  6. Encounter DefineShape. Create a SwiffShapeDefinition instance and add it to m_definitions
  7. Encounter DefineFont. Create a SwiffFontDefinition instance and add it to m_definitions
  8. Encounter DefineText. Create a SwiffStaticTextDefinition instance and add it to m_definitions
  9. Encounter RemoveObject. Clear out m_placedObjects[5].
  10. Encounter PlaceObject. Create a SwiffPlacedObject instance at m_placedObjects[5] that references #6's definition
  11. Encounter PlaceObject. Create a SwiffPlacedObject instance at m_placedObjects[6] that references #8's text field
  12. Encounter ShowFrame. Create a SwiffFrame object with -placedObjects containing the two SwiffPlacedObject instances
Clone this wiki locally