/* script.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);
	style.SetColor(1, color);
	circle->AddStyle(style);

	style.Reset();
	style.SetType(sswf::Style::STYLE_TYPE_LINE);
	color.Set(0, 255, 0);		// solid green
	style.SetLine(0, 1, color);
	color.Set(255, 0, 255);		// solid purple
	style.SetLine(1, 2, color);
	circle->AddStyle(style);

	// shape 0
	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));

	circle->AddMove(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(0), TWIPS(-28.35));	// draw circle
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(  8.3), TWIPS(   8.3), TWIPS(  11.8), TWIPS(     0));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(    0), TWIPS( 11.75), TWIPS(  8.35), TWIPS(   8.3));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS( -8.3), TWIPS(  8.35), TWIPS(  0.05), TWIPS( 11.75));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(-10.0), TWIPS(  0.05), TWIPS( -10.1), TWIPS(     0));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(-10.0), TWIPS(     0), TWIPS( -10.1), TWIPS(     0));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(-0.05), TWIPS(-11.75), TWIPS(  -8.3), TWIPS(  -8.3));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(  8.3), TWIPS( -8.35), TWIPS(     0), TWIPS(-11.75));
	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, 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);
	circle->SetBounds(1, rect);

// Create our sprite
	sswf::TagSprite *sprite = new sswf::TagSprite(&my_header);

// Place the two shapes in our sprite
	sswf::TagPlace *place = new sswf::TagPlace(sprite);
	place->SetDepth(1);
	place->SetObjectID(square->Identification());
	sswf::Matrix matrix;
	matrix.SetScale(1.0);
	matrix.SetRotate(0);
	matrix.SetTranslate(0, TWIPS(105));
	place->SetMatrix(matrix);

	place = new sswf::TagPlace(sprite);
	place->SetDepth(2);
	place->SetObjectID(circle->Identification());
	matrix.SetScale(1.0);
	matrix.SetRotate(0);
	matrix.SetTranslate(TWIPS(0), TWIPS(0));
	place->SetMatrix(matrix);

	for(int i = 1; i < 180; i += 5) {
		int p = TWIPS(60 - sin(TORAD(i)) * 120.0);
		sswf::TagPlace *place = new sswf::TagPlace(sprite);
		place->SetDepth(2);
		if(p > 30 * 20) {
			place->SetMorphPosition((p - 30 * 20) * 65535 / (30 * 20));
		}
		else {
			place->SetMorphPosition(0);
		}
		sswf::Matrix matrix;
		matrix.SetScale(1.0);
		matrix.SetRotate(0);
		matrix.SetTranslate(TWIPS(0), p);
		place->SetMatrix(matrix);

		new sswf::TagShowFrame(sprite);
	}

// Place our sprite in the main animation
	place = new sswf::TagPlace(&my_header);
	place->Replace();
	place->SetName("bouncing");
	place->SetDepth(1);
	place->SetObjectID(sprite->Identification());
	matrix.SetScale(1.0);
	matrix.SetRotate(0.0);
	matrix.SetTranslate(TWIPS(-60), TWIPS(480.0 / 2.0));
	place->SetMatrix(matrix);

	// Create a small script which resets a variable
	// (the horizontal position of our sprite)
	{
		sswf::TagDoAction *do_action = new sswf::TagDoAction(&my_header);
		sswf::ActionPushData push_data(&my_header);
		push_data.AddString("xpos");
		push_data.AddInteger(-60);
		do_action->SetAction(push_data);
		sswf::Action set_variable(&my_header, sswf::Action::ACTION_SET_VARIABLE);
		do_action->SetAction(set_variable);
	}

	new sswf::TagShowFrame(&my_header);

	// the next script does a "previous frame" each time and that's
	// what this extra frame is for!
	new sswf::TagShowFrame(&my_header);

// Move the sprite from left to right (hidden at the start and end)
	/*
	for(int i = -60; i < 640 + 60; i += 2) {
		sswf::TagPlace *place = new sswf::TagPlace(&my_header);
		place->SetDepth(1);
		sswf::Matrix matrix;
		matrix.SetScale(1.0);
		matrix.SetRotate(0);
		matrix.SetTranslate(TWIPS(i), TWIPS(480.0 / 2.0));
		place->SetMatrix(matrix);
		*/

		sswf::Action add(&my_header, sswf::Action::ACTION_ADD);
		sswf::Action get_variable(&my_header, sswf::Action::ACTION_GET_VARIABLE);
		sswf::Action less_than(&my_header, sswf::Action::ACTION_LESS_THAN);
		sswf::Action next_frame(&my_header, sswf::Action::ACTION_NEXT_FRAME);
		sswf::Action previous_frame(&my_header, sswf::Action::ACTION_PREVIOUS_FRAME);
		sswf::Action set_property(&my_header, sswf::Action::ACTION_SET_PROPERTY);
		sswf::Action set_variable(&my_header, sswf::Action::ACTION_SET_VARIABLE);
		//sswf::Action swap(&my_header, sswf::Action::ACTION_SWAP);

		sswf::TagDoAction *do_action = new sswf::TagDoAction(&my_header);
		sswf::ActionPushData push_data(&my_header);
		push_data.AddInteger(700);
		push_data.AddString("xpos");
		push_data.AddString("xpos");
		push_data.AddInteger(2);		// xpos += 2;
		push_data.AddString("xpos");
		push_data.AddString("bouncing");
		push_data.AddFloat(0.0f);		// Property "_x" (see src/sswf/sswf_save.c++ around line #92, or search for "direct_properties")
		push_data.AddString("xpos");
		do_action->SetAction(push_data);

		do_action->SetAction(get_variable);
		do_action->SetAction(set_property);

		do_action->SetAction(get_variable);
		do_action->SetAction(add);
		do_action->SetAction(set_variable);

		do_action->SetAction(get_variable);
		//do_action->SetAction(swap); -- often you need to swap these!
		do_action->SetAction(less_than);	// xpos > 700

		sswf::ActionBranch if_true(&my_header, sswf::Action::ACTION_BRANCH_IF_TRUE);
		if_true.SetLabel("next");
		do_action->SetAction(if_true);

		// if not true, repeat this action script
		do_action->SetAction(previous_frame);

		sswf::ActionBranch all_done(&my_header);
		all_done.SetLabel("previous");
		do_action->SetAction(all_done);

		sswf::ActionLabel label_next(&my_header);
		label_next.SetLabel("next");
		do_action->SetAction(label_next);

		do_action->SetAction(next_frame);

		sswf::ActionLabel label_previous(&my_header);
		label_previous.SetLabel("previous");
		do_action->SetAction(label_previous);

		sswf::Action play(&my_header, sswf::Action::ACTION_PLAY);
		do_action->SetAction(play);

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

	return 0;
}

