/* simple.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_c.h>
#include	<math.h>
#include	<stdio.h>

#define	TWIPS(x)	((long) ((x) * 20.0))


int main(int argc, char *argv[])
{
// in C you need all the variables at the start of a block!
	struct SSWF_TagHeader *my_header;
	struct SSWF_TagSetBackgroundColor *my_background_color;
	struct SSWF_TagBase *my_header_base;
	struct SSWF_TagProtect *protect;
	struct SSWF_TagShape *square;
	struct SSWF_Style *style;
	struct SSWF_Color *color;
	struct SSWF_TagPlace *place;
	struct SSWF_TagBaseID *square_base;
	struct SSWF_Matrix *matrix;
	struct SSWF_Data *result;
	struct SSWF_SRectangle *rect;
	struct SSWF_ErrorManager *error_manager;
	void *buffer;
	size_t size;

// Initialize the header
	my_header = sswf_tag_header_create();

	sswf_tag_header_set_compress(my_header, 1);

	rect = sswf_srectangle_create();
	sswf_srectangle_set(rect, 0, TWIPS(640), 0, TWIPS(480));
	sswf_tag_header_set_frame(my_header, rect);

	sswf_tag_header_set_rate(my_header, 15);

	//sswf_tag_header_set_version(my_header, 3); -- SetCompress() requires version 6 anyway

	my_header_base = sswf_tag_header_cast_tag_base(my_header);
	my_background_color = sswf_tag_set_background_color_create(my_header_base);
	color = sswf_color_create();
	sswf_color_set(color, 243, 243, 243, 255);		// solid light grey
	sswf_tag_set_background_color_set_color(my_background_color, color);
	protect = sswf_tag_protect_create(my_header_base);

// Add a square
	square = sswf_tag_shape_create(my_header_base);
	error_manager = sswf_tag_header_cast_error_manager(my_header);
	style = sswf_style_create(error_manager);
	sswf_style_set_type(style, STYLE_TYPE_LINE);
	sswf_color_set(color, 0, 255, 255, 255);		// solid Cyan
	sswf_style_set_line(style, 0, 2, color);
	sswf_tag_shape_add_style(square, style, 0);

	sswf_tag_shape_add_move(square, MORPH_MODE_SHAPE0, TWIPS(-0.5), TWIPS(-0.5));	// draw square
	sswf_tag_shape_add_edge1(square, MORPH_MODE_SHAPE0, TWIPS( 1.0), TWIPS( 0.0));
	sswf_tag_shape_add_edge1(square, MORPH_MODE_SHAPE0, TWIPS( 0.0), TWIPS( 1.0));
	sswf_tag_shape_add_edge1(square, MORPH_MODE_SHAPE0, TWIPS(-1.0), TWIPS( 0.0));
	sswf_tag_shape_add_edge1(square, MORPH_MODE_SHAPE0, TWIPS( 0.0), TWIPS(-1.0));
	//SSWF_SRectangle *rect; -- already defined, just reuse the same
	sswf_srectangle_set(rect, TWIPS(-0.6), TWIPS(0.6), TWIPS(-0.6), TWIPS(0.6));	// make bounds a bit bigger
	sswf_tag_shape_set_bounds(square, 0, rect, 0);


// Place the shape
	place = sswf_tag_place_create(my_header_base);
	sswf_tag_place_set_depth(place, 1);
	square_base = sswf_tag_shape_cast_tag_base_id(square);
	sswf_tag_place_set_object_id(place, sswf_tag_base_id_identification(square_base));
	matrix = sswf_matrix_create();
	sswf_matrix_set_scale2(matrix, 120.0);
	sswf_matrix_set_rotate(matrix, 45.0 / (M_PI * 2.0));
	sswf_matrix_set_translate(matrix, TWIPS(640.0 / 2.0),TWIPS(480.0 / 2.0));
	sswf_tag_place_set_matrix(place, matrix);

// these are for completeness of the sample but in this
// case these two tags are both automatically generate
// by the SSWF library otherwise
	sswf_tag_show_frame_create(my_header_base);
	sswf_tag_end_create(my_header_base);

// Save the result in a file
	result = sswf_data_create();
	sswf_tag_header_save(my_header, result);
	sswf_data_read(result, &buffer, &size);

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

	return 0;
}

