/* rotate.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))


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 square
	sswf::TagShape *square = new sswf::TagShape(&my_header);
	sswf::Style style(my_header);
	style.SetType(sswf::Style::STYLE_TYPE_LINE);
	sswf::Color color;
	color.Set(255, 255, 0);		// solid Yellow
	style.SetLine(0, 2, color);
	square->AddStyle(style);
	square->AddMove(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(-0.5), TWIPS(-0.5));	// draw square
	square->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS( 1.0), TWIPS( 0.0));
	square->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS( 0.0), TWIPS( 1.0));
	square->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(-1.0), TWIPS( 0.0));
	square->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS( 0.0), TWIPS(-1.0));
	//sswf::SRectangle rect; -- already defined, just reuse the same
	rect.Set(TWIPS(-0.6), TWIPS(0.6), TWIPS(-0.6), TWIPS(0.6));	// make bounds a bit bigger
	square->SetBounds(0, rect);

// Place the shape
	sswf::TagPlace *place = new sswf::TagPlace(&my_header);
	place->SetDepth(1);
	place->SetObjectID(square->Identification());
	sswf::Matrix matrix;
	matrix.SetScale(120.0);
	matrix.SetRotate(45.0 / (M_PI * 2.0));
	matrix.SetTranslate(TWIPS(640.0 / 2.0),TWIPS(480.0 / 2.0));
	place->SetMatrix(matrix);
	new sswf::TagShowFrame(&my_header);

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

		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("rotate.swf", "wb");
	if(f != 0) {
		fwrite(buffer, size, 1, f);
		fclose(f);
	}

	return 0;
}

