/* once.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2002-2009 */

/*

Copyright (c) 2002-2009 Made to Order Software Corp.

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/


#include	<sswf/libsswf.h>

#define	TWIPS(x)	((long) ((x) * 20.0))
#define	TORAD(x)	((double) (x) * (M_PI / 180.0))


int main(int argc, char *argv[])
{
// Initialize the header
	sswf::TagHeader my_header;

	my_header.SetCompress();

	sswf::SRectangle rect;
	rect.Set(0, TWIPS(640), 0, TWIPS(480));
	my_header.SetFrame(rect);

	my_header.SetRate(15);

	//my_header.SetVersion(3); -- SetCompress() requires version 6 anyway

	sswf::TagProtect *protect = new sswf::TagProtect(&my_header);

// Add a box
	sswf::TagShape *square = new sswf::TagShape(&my_header);
	sswf::Style style(my_header);
	style.SetType(sswf::Style::STYLE_TYPE_SOLID);
	sswf::Color color;
	color.Set(0, 0, 0);		// solid black
	style.SetColor(0, color);
	square->AddStyle(style);
	square->AddMove(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(-0.5 * 60.0), TWIPS(-0.5 * 60.0));	// draw square
	square->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS( 1.0 * 60.0), TWIPS( 0.0 * 60.0));
	square->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS( 0.0 * 60.0), TWIPS( 1.0 * 60.0));
	square->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(-1.0 * 60.0), TWIPS( 0.0 * 60.0));
	square->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS( 0.0 * 60.0), TWIPS(-1.0 * 60.0));
	rect.Set(TWIPS(-0.6 * 60.0), TWIPS(0.6 * 60.0), TWIPS(-0.6 * 60.0), TWIPS(0.6 * 60.0));	// make bounds a bit bigger
	square->SetBounds(0, rect);

// Add a circle
	sswf::TagShape *circle = new sswf::TagShape(&my_header);
	style.SetType(sswf::Style::STYLE_TYPE_SOLID);
	color.Set(0, 255, 0, 128);		// 50% transparent green
	style.SetColor(0, color);
	circle->AddStyle(style);
	circle->AddMove(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(0), TWIPS(-28.35));	// draw circle
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(  8.3), TWIPS(   8.3), TWIPS(  11.8), TWIPS(     0));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(    0), TWIPS( 11.75), TWIPS(  8.35), TWIPS(   8.3));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS( -8.3), TWIPS(  8.35), TWIPS(  0.05), TWIPS( 11.75));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(-11.8), TWIPS(  0.05), TWIPS(  -8.3), TWIPS(   8.3));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(-8.35), TWIPS(  -8.3), TWIPS(-11.75), TWIPS(     0));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(-0.05), TWIPS(-11.75), TWIPS(  -8.3), TWIPS(  -8.3));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(  8.3), TWIPS( -8.35), TWIPS(     0), TWIPS(-11.75));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(11.75), TWIPS(     0), TWIPS(   8.3), TWIPS( -8.35));
	rect.Set(TWIPS(-1.0) * 60, TWIPS(1.0) * 60, TWIPS(-1.0) * 60, TWIPS(1.0) * 60);	// make bounds a bit bigger
	circle->SetBounds(0, rect);

// Place the two shapes
	sswf::TagPlace *place = new sswf::TagPlace(&my_header);
	place->SetDepth(1);
	place->SetObjectID(square->Identification());
	sswf::Matrix matrix;
	matrix.SetScale(1.0);
	matrix.SetRotate(0);
	matrix.SetTranslate(TWIPS(640 / 2), TWIPS(480 / 2));
	place->SetMatrix(matrix);
	new sswf::TagShowFrame(&my_header);

	place = new sswf::TagPlace(&my_header);
	place->SetDepth(2);
	place->SetObjectID(circle->Identification());
	matrix.SetScale(1.0);
	matrix.SetRotate(0);
	matrix.SetTranslate(TWIPS(640 / 2), TWIPS(480 / 2 - 70));
	place->SetMatrix(matrix);
	new sswf::TagShowFrame(&my_header);

// Rotate and move the shape
	for(int i = 0; i < 360; i += 5) {
		sswf::TagPlace *place = new sswf::TagPlace(&my_header);
		place->SetDepth(2);
		sswf::Matrix matrix;
		matrix.SetScale(1.0);
		matrix.SetRotate(0);
		double a = TORAD(i + 180.0);
		matrix.SetTranslate(TWIPS(640.0 / 2.0 + sin(a) * 70.0), TWIPS(480.0 / 2.0 + cos(a) * 70.0));
		place->SetMatrix(matrix);

		new sswf::TagShowFrame(&my_header);
	}

// Now we stop the animation
	sswf::TagDoAction *do_action = new sswf::TagDoAction(&my_header);
	sswf::Action *action = new sswf::Action(do_action, sswf::Action::ACTION_STOP);
	do_action->SetAction(*action);
	delete action;

	new sswf::TagShowFrame(&my_header);

// end the movie
	new sswf::TagEnd(&my_header);

// Save the result in a file
	sswf::Data result;
	size_t size;
	void *buffer;

	my_header.Save(result);
	result.Read(buffer, size);

	FILE *f = fopen("once.swf", "wb");
	if(f != 0) {
		fwrite(buffer, size, 1, f);
		fclose(f);
	}

	return 0;
}

