int main(int count, refRefChar strings) { bool compiling; // Will we compile the C target file? char path[maxPathLength]; // An absolute pathname. set seen; // Command line options seen so far. refStream source; // Read source files through this. bool who; // Did the user ask who we are? // STRING OPTION. Return the string value of OPTION. It's either the string on // the command line that follows OPTION, or else it's everything but the first // character of OPTION. refChar stringOption(refChar option) { if (option[1] == eosChar) { count -= 1; strings += 1; if (count > 0 && d(d(strings)) != eosChar) { return d(strings); } else { fail("Missing value for '-%c'.", d(option)); }} else { return option + 1; }} // INT OPTION. Like STRING OPTION, but here we return the INT value of OPTION, // which must be between MIN and MAX. int intOption(refChar option, int min, int max) { refChar end; int number; errno = 0; number = strtol(stringOption(option), r(end), 10); if (d(end) != eosChar || errno == outRange || number < min || number > max) { fail("Unexpected value for '-%c'.", d(option)); } else { return number; }}
int intOption(refChar option, int min, int max) { refChar end; int number; errno = 0; number = strtol(stringOption(option), r(end), 10); if (d(end) != eosChar || errno == outRange || number < min || number > max) { fail("Illegal value for '-%c'.", d(option)); } else { return number; }}
StreamingSounds(int argc, const char * argv[]) : UnitTest(argc, argv) { vector<string> files; string dirIn = stringOption(options, "dirIn"); string dirOut = stringOption(options, "dirOut"); string extension = stringOption(options, "ext"); string fileIn = ""; string fileOut = ""; findFiles(dirIn, files, extension); for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) { fileIn = *i; fileOut = fileIn.substr(0, fileIn.find_first_of('.')) + ".swf"; cout << fileIn << endl; #ifdef WIN32 fileOut = dirOut + "\\" + fileOut; #else fileOut = dirOut + "/" + fileOut; #endif try { FSMovie movie; FSSoundConstructor* soundGenerator = SoundConstructor(); if (soundGenerator->setSoundFromFile(fileIn.c_str()) != TransformUtil::OK) { throw FSException("Could not load sound file"); } float framesPerSecond = 12.0f; movie.setFrameSize(FSBounds(0, 0, 8000, 4000)); movie.setFrameRate(framesPerSecond); movie.add(new FSSetBackgroundColor(FSColorTable::lightblue())); /* * Calculate the time it takes to play the sound and the number of frames this * represents. */ float duration = ((float) soundGenerator->getSamplesPerChannel()) / ((float) soundGenerator->getSampleRate()); int numberOfFrames = (int) (duration * framesPerSecond); /* * Play the Streaming Sound. * * Calculate the number of decoded sound samples played for each frame and * the size, in bytes, of each block compressed sound data. */ int samplesPerBlock = soundGenerator->getSampleRate() / (int) framesPerSecond; int numberOfBlocks = soundGenerator->getSamplesPerChannel() / samplesPerBlock; /* * An FSSoundStreamHeader2 object defines the attributes of the streaming sound. */ movie.add(soundGenerator->streamHeader(samplesPerBlock)); /* * Add a streaming block for each frame so the sound is played as each frame * is displayed. */ for (int i=0; i<numberOfBlocks; i++) { movie.add(soundGenerator->streamBlock(i, samplesPerBlock)); movie.add(new FSShowFrame()); } movie.encodeToFile(fileOut.c_str()); delete soundGenerator; } catch (FSException e) { cerr << e.what(); } } }
StreamingSounds(map< string, string>& options) { string soundFile = stringOption(options, "file"); FSSoundConstructor* soundGenerator = SoundConstructor(); FSMovie movie; int status = TransformUtil::OK; if ((status = soundGenerator->setSoundFromFile(soundFile.c_str())) != TransformUtil::OK) { switch (status) { case TransformUtil::FileNotFound: cout << "Could not find sound file" << endl; break; case TransformUtil::ReadError: cout << "Could not read sound file" << endl; break; case TransformUtil::FormatError: cout << "Could not read sound file" << endl; break; } } if (status == TransformUtil::OK) { float framesPerSecond = 12.0f; /* * Calculate the time it takes to play the sound and the number of frames * this represents. */ float duration = ((float) soundGenerator->getSamplesPerChannel()) / ((float) soundGenerator->getSampleRate()); int numberOfFrames = (int) (duration * framesPerSecond); /* * Calculate the number of decoded sound samples played for each frame and * the size, in bytes, of each block compressed sound data. */ int samplesPerBlock = soundGenerator->getSampleRate() / (int) framesPerSecond; int numberOfBlocks = soundGenerator->getSamplesPerChannel() / samplesPerBlock; /* * Add all the objects together to create the movie. */ movie.setFrameSize(FSBounds(0, 0, 8000, 4000)); movie.setFrameRate(framesPerSecond); movie.add(new FSSetBackgroundColor(FSColorTable::lightblue())); /* * An FSSoundStreamHeader2 object defines the attributes of the streaming sound. */ movie.add(soundGenerator->streamHeader(samplesPerBlock)); /* * Add a streaming block for each frame so the sound is played as each frame * is displayed. */ for (int i=0; i<numberOfBlocks; i++) { movie.add(soundGenerator->streamBlock(i, samplesPerBlock)); movie.add(new FSShowFrame()); } saveMovie(movie, stringOption(options, "resultDir"), "StreamingSounds.swf"); } delete soundGenerator; }
FillStyles(map< string, string>& options) { FSMovie movie; int width = 3600; int height = 2700; string imageFile = stringOption(options, "image"); /* * A row of five rectangles will be displayed - one for each fill style. * The additional space is ued to provide padding between the rectangles * and to the edge of the screen. */ movie.setFrameSize(FSBounds(0, 0, (width+800)*5 , height+800)); movie.setFrameRate(1.0f); movie.add(new FSSetBackgroundColor(FSColorTable::lightblue())); /* * Define the rectangle that is filled with the different fill styles. */ FSBounds bounds(-width/2, -height/2, width/2, height/2); FSShape rectangle; rectangle.add(new FSShapeStyle(1, 1, 0, -width/2, -height/2)); rectangle.add(new FSLine(width, 0)); rectangle.add(new FSLine(0, height)); rectangle.add(new FSLine(-width, 0)); rectangle.add(new FSLine(0, -height)); /* * The following variables are reused for each fill style to simplify the code. * In Transform an object assumes ownership for any added to it, so the shape * and style arrays can be safely reused in the FSDefineShape constructor. A * shallow copy of the object is made. The FSDefineShape object assumes * ownership of the objects they contain and no explicit memory management * is required - the objects will be deleted when the shape is deleted. */ int identifier = 0; FSShape shape; FSVector<FSLineStyle*> lineStyles(1); FSVector<FSFillStyle*> fillStyles(1); /************************************************* * Create a rectangle filled with a solid colour. *************************************************/ shape = rectangle; identifier = movie.newIdentifier(); lineStyles[0] = new FSSolidLine(20, FSColorTable::black()); fillStyles[0] = new FSSolidFill(FSColorTable::red()); movie.add(new FSDefineShape(identifier, bounds, fillStyles, lineStyles, shape)); movie.add(new FSPlaceObject(identifier, 1, 2000, 1600)); /****************************************** * Create a rectangle tiled with an image. ******************************************/ shape = rectangle; identifier = movie.newIdentifier(); lineStyles[0] = new FSSolidLine(20, FSColorTable::black()); try { size_t size = 0; byte* bytes = dataFromFile(imageFile.c_str(), size); FSDefineJPEGImage* image = new FSDefineJPEGImage(movie.newIdentifier(), bytes, size); /* * Scale the loaded image to half its original size so it will tile four times inside the * rectangle. When an image is loaded its width and height default to twips rather than * pixels. An image 300 x 200 pixels will be displayed as 300 x 200 twips (15 x 10 pixels). * Scaling the image by 20 (20 twips = 1 pixel) would restore it to its original size. Here * we only scale the image to half its original size so variable sized images passed on the * command line when the example is run are more easily seen. * * The coordinate transform relocates the top left corner of the image to the top left * corner of the rectangle in which it is displayed. */ FSCoordTransform transform = FSCoordTransform(-width/2, -height/2, 10.0, 10.0); fillStyles[0] = new FSBitmapFill(FSFillStyle::TiledBitmap, image->getIdentifier(), transform); movie.add(image); movie.add(new FSJPEGEncodingTable()); // The image will still be displayed with an empty table movie.add(new FSDefineShape(identifier, bounds, fillStyles, lineStyles, shape)); movie.add(new FSPlaceObject(identifier, 2, 6000, 1600)); } catch (FSFileOpenException e) { cerr << e.what(); } catch (FSAccessException e) { cerr << e.what(); } /************************************************** * Create a rectangle filled with a clipped image. **************************************************/ shape = rectangle; identifier = movie.newIdentifier(); lineStyles[0] = new FSSolidLine(20, FSColorTable::black()); try { size_t size = 0; byte* bytes = dataFromFile(imageFile.c_str(), size); FSDefineJPEGImage* image = new FSDefineJPEGImage(movie.newIdentifier(), bytes, size); /* * Scale the loaded image to half its original size so variable sized images are * more easily seen. The coordinate transformation, taking into account the scaling * factor will display the image centered in the middle of the rectangle. */ FSCoordTransform transform = FSCoordTransform(-(image->getWidth()*10)/2, -(image->getHeight()*10)/2, 10.0, 10.0); fillStyles[0] = new FSBitmapFill(FSFillStyle::ClippedBitmap, image->getIdentifier(), transform); movie.add(image); movie.add(new FSJPEGEncodingTable()); // The image will still be displayed with an empty table movie.add(new FSDefineShape(identifier, bounds, fillStyles, lineStyles, shape)); movie.add(new FSPlaceObject(identifier, 3, 10000, 1600)); } catch (FSFileOpenException e) { cerr << e.what(); } catch (FSAccessException e) { cerr << e.what(); } /********************************************************* * Create a rectangle filled with linear gradient colour. *********************************************************/ shape = rectangle; identifier = movie.newIdentifier(); /* * The gradient square must be mapped to the rectangle being filled. The square * measures 32768 x 32768 twips. The coordinates range from -16384, -16384 at the * bottom left corner to (16384, 16384) at the top right corner. Mapping takes place * in three steps: * * 1. The coordinate system of the gradient square is translated to match the coordinate * system of the shape in which the gradient is displayed. In this example, both the * gradient square and the rectangle in which it will be displayed are both centred at * (0,0) - see the FSBounds object created above. For this example the full range of the * gradient will be displayed so no translation is required. The second example that * displays a radial gradient illustrates how the translation can be calculated to change * the portion of the gradient being displayed. * * 2. The gradient square is scaled so that the full range of the gradient will be * displayed inside the rectangle. If a smaller scaling factor is set then the * portion of the gradient being displayed will change. The second example shows * how the scaling factor affects the gradient. * * 3. The gradient can be rotated to change the direction of the colour change. If a * rotation is not applied then the gradient changes in the positive x axis - front left * to right on the Flash Player's screen. */ // To display the full gradient, calculate the ratio of the shape's width to the width // of the gradient square. The gradient will be rotated so the width is used rather than // calculating the length of the diagonal. float scale = bounds.getWidth() / 32768.0f; // Since the centre of the rectangle and the gradient square are both at (0,0) no // additional translation is required. int translateX = 0; int translateY = 0; // The order of composition is important. If the scale transform was the first argument // then the translation coordinates would also be scaled. // The scaling is performed in the x and y direction as the gradient is rotated in the // the following step. FSCoordTransform transform = FSCoordTransform(translateX, translateY, scale, scale); // Apply a rotation so the gradient changes across the diagonal of the rectangle. transform.rotate(45); /* * The array of FSGradient objects defines how the colour changes across the gradient * square. The ratio defines the proportion across the square: 0 is the left side and * 255 is the right side. */ FSVector<FSGradient> gradients(2); gradients[0] = FSGradient(0, FSColorTable::white()); gradients[1] = FSGradient(255, FSColorTable::black()); lineStyles[0] = new FSSolidLine(20, FSColorTable::black()); fillStyles[0] = new FSGradientFill(FSFillStyle::LinearGradient, transform, gradients); movie.add(new FSDefineShape(identifier, bounds, fillStyles, lineStyles, shape)); movie.add(new FSPlaceObject(identifier, 4, 14000, 1600)); /********************************************************* * Create a rectangle filled with radial gradient colour. *********************************************************/ shape = rectangle; identifier = movie.newIdentifier(); // Resize the gradients array so the gradient will contain 4 colours. gradients.resize(4); /* The gradient will be centred in the bottom left corner of the rectangle * since the full spectrum of the radial gradient will be displayed the * gradient square need only be scaled by half the amount compared to the * linear gradient above. */ float scaleX = (bounds.getWidth() / 32768.0f) * 2.0f; float scaleY = (bounds.getHeight() / 32768.0f) * 2.0f; /* Here the translation is calculated so the centre is expressed as a percentage * of the width of the rectangle. The general form of the calculation is: * * translateX = bounds.getMinX() + bounds.getWidth() * 0.0; * translateY = bounds.getMinY() + bounds.getHeight() * 0.25; */ translateX = bounds.getMinX(); translateY = bounds.getMinY(); transform = FSCoordTransform(translateX, translateY, scaleX, scaleY); gradients[0] = FSGradient(15, FSColorTable::red()); gradients[1] = FSGradient(63, FSColorTable::orange()); gradients[2] = FSGradient(127, FSColorTable::yellow()); gradients[3] = FSGradient(255, FSColorTable::green()); lineStyles[0] = new FSSolidLine(1, FSColorTable::black()); fillStyles[0] = new FSGradientFill(FSFillStyle::RadialGradient, transform, gradients); movie.add(new FSDefineShape(identifier, bounds, fillStyles, lineStyles, shape)); movie.add(new FSPlaceObject(identifier, 5, 18000, 1600)); movie.add(new FSShowFrame()); saveMovie(movie, stringOption(options, "resultDir"), "FillStyles.swf"); }
MovieBasics(map<string, string>& options) { FSMovie movie; /* * The movie bounds are specified in twips. The bounding rectangle is defined * by two points: the lower left corner and the upper right corner of the * rectangle that encloses the area. * * The bounding rectangle also defines the coordinate range for the x and y axes. * Here the coordinates for the x and y axes range from -4000 to +4000. A point with * the coordinates (0,0) lies in the center of the screen. * * If the coordinates of the corners were specified as (0,0) and (4000, 4000) the * size of the screen is still the same however the center of the screen now * lies at (2000,2000) */ int xLower = -4000; int yLower = -4000; int xUpper = 4000; int yUpper = 4000; movie.setFrameSize(FSBounds(xLower, yLower, xUpper, yUpper)); /* * Set the frame rate at which the movie will be played. */ float framesPerSecond = 1.0f; movie.setFrameRate(framesPerSecond); /* * Set the movie's background colour to light blue. The background colour * only be set once and should be the first object added to a movie. If no * background colour is specified then the Flash Player will set it to white. */ movie.add(new FSSetBackgroundColor(FSColorTable::lightblue())); /* * Define a shape to be displayed. Each object must be assigned a unique identifier * which is used to reference the object when adding, updating or removing it from * the display list. The movie object keeps track of the identifier to guarantee * each is unique. */ int identifier = movie.newIdentifier(); int width = 4000; int height = 4000; FSBounds bounds(-2000, -2000, 2000, 2000); FSVector<FSLineStyle*> lineStyles(1); FSVector<FSFillStyle*> fillStyles(1); lineStyles[0] = new FSSolidLine(20, FSColorTable::black()); fillStyles[0] = new FSSolidFill(FSColorTable::red()); /* * Create the outline of the shape. All drawing is performed using a series * of line an curve objects. The FSShapeStyle class is used to select the * current line and fill styles and to perform absolute moves within the * coordinate space of the shape (relative moves can be performed by drawing * line when no line style is selected - set the line style to 0). */ FSShape shape; shape.add(new FSShapeStyle(1, 1, 0, -width/2, -height/2)); shape.add(new FSLine(width, 0)); shape.add(new FSLine(0, height)); shape.add(new FSLine(-width, 0)); shape.add(new FSLine(0, -height)); FSDefineShape* rectangle = new FSDefineShape(identifier, bounds, fillStyles, lineStyles, shape); /* * Add the rectangle to the movie. All shapes and objects must be defined before * they can be placed on the display list and rendered on the Flash Player's * screen. */ movie.add(rectangle); /* * Place the shape on the display list. See the DisplayList example for the set of * objects that are used to add, update and remove objects from the display list. */ movie.add(new FSPlaceObject(identifier, 1, 0, 0)); /* * Show the contents of the display list. Frames are delimited by successive * FSShowFrame objects. */ movie.add(new FSShowFrame()); saveMovie(movie, stringOption(options, "resultDir"), "MovieBasics.swf"); }