Ming: creating Flash movies 2

Ming can create and manipulate all elements of a Flash movie, including shapes, morphs, and textfields. The simplest example is a shape. We will create a simple shape and show some transformations. This introduces two basic objects: a SWFShape, which represents the original shape, and a SWFDisplayItem. This is used to transform the object after it has been added to the movie. The following code can simply be added to a main function. Don't forget SWFMovie_save() at the end!

/* Declare our variables */
SWFMovie movie;
SWFShape shape;
SWFDisplayItem it;
int red, green, blue, alpha, thickness, x, y, width, height;

Now we must initialize Ming and set up our movie as in part 1:


movie = newSWFMovieWithVersion(7);
SWFMovie_setDimension(movie, 300, 200);

/* We want a slow frame rate to be able to see the transformations clearly */
SWFMovie_setRate (movie, 1.0);

Our first new task is to create the shape itself, set its line style, and draw it.

shape = newSWFShape();

thickness = 5;
red = 0x00;
green = 0x66;
blue = 0x66;
alpha = 0xff;
SWFShape_setLine(shape, thickness, red, green, blue, alpha);

x = 20;
y = 30;
width = 20;
height = 20;
SWFShape_movePenTo(shape, x, y);
SWFShape_drawLineTo(shape, x, y + height);
SWFShape_drawLineTo(shape, x + width, y + height);
SWFShape_drawLineTo(shape, x + width, y);
SWFShape_drawLineTo(shape, x, y);

We have now created a square with a purple-coloured, 5-pixel thick line. All that remains to do is add it to the movie. But if we want to change the shape later, we have to track it. SWFMovie_add() returns a SWFDisplayItem, which we can use to apply transformations to the object.

it = SWFMovie_add(movie, shape);
/* End of first frame */

If we stop here, making sure to output the movie we've created with SWFMovie_save(movie, "mingshape.swf"), we will see just a static, purple square outline. But to make the SWF much more exciting, we will apply some transformations.

To move the object, use SWFDisplayItem_move or SWFDisplay_moveTo. Each physical transformation function comes in a relative version, where the transformation is applied to the present state, and an absolute version (ending in 'To'), where the end result is specified. This code moves the square to the pixel co-ordinates (40, 50):

SWFDisplayItem_moveTo(it, 40, 50);

Scale the object by 150% horizontally and 75% vertically:

SWFDisplayItem_scale(it, 1.5, 0.75);

Add colours to the colour transform:

red = 0x45;
blue = 0x33;
green = 0x80;
alpha = 0x88;

SWFDisplayItem_addColor(it, red, blue, green, alpha);

Skew in the x dimension:

SWFDisplayItem_skewX(it, 1.4);

Multiply colours:

SWFDisplayItem_multColor(it, 1, 2, 3, 1);

Skew the object in the y dimension:

SWFDisplayItem_skewYTo(it, 0.8);

See mingshape.swf for the complete code and resulting SWF.