1 /* script.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2002-2009 */
2 
3 /*
4 
5 Copyright (c) 2002-2009 Made to Order Software Corp.
6 
7 Permission is hereby granted, free of charge, to any
8 person obtaining a copy of this software and
9 associated documentation files (the "Software"), to
10 deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify,
12 merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom
14 the Software is furnished to do so, subject to the
15 following conditions:
16 
17 The above copyright notice and this permission notice
18 shall be included in all copies or substantial
19 portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
22 ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
23 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
24 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
25 EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28 ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31 
32 */
33 
34 
35 #include	<sswf/libsswf.h>
36 
37 #define	TWIPS(x)	((long) ((x) * 20.0))
38 #define	TORAD(x)	((double) (x) * (M_PI / 180.0))
39 
40 
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43 // Initialize the header
44 	sswf::TagHeader my_header;
45 
46 	my_header.SetCompress();
47 
48 	sswf::SRectangle rect;
49 	rect.Set(0, TWIPS(640), 0, TWIPS(480));
50 	my_header.SetFrame(rect);
51 
52 	my_header.SetRate(15);
53 
54 	//my_header.SetVersion(3); -- SetCompress() requires version 6 anyway
55 
56 	sswf::TagProtect *protect = new sswf::TagProtect(&my_header);
57 
58 // Add a box
59 	sswf::TagShape *square = new sswf::TagShape(&my_header);
60 	sswf::Style style(my_header);
61 	style.SetType(sswf::Style::STYLE_TYPE_SOLID);
62 	sswf::Color color;
63 	color.Set(0, 0, 0);		// solid black
64 	style.SetColor(0, color);
65 	square->AddStyle(style);
66 	square->AddMove(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(-0.5 * 60.0), TWIPS(-0.5 * 60.0));	// draw square
67 	square->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS( 1.0 * 60.0), TWIPS( 0.0 * 60.0));
68 	square->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS( 0.0 * 60.0), TWIPS( 1.0 * 60.0));
69 	square->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(-1.0 * 60.0), TWIPS( 0.0 * 60.0));
70 	square->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS( 0.0 * 60.0), TWIPS(-1.0 * 60.0));
71 	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
72 	square->SetBounds(0, rect);
73 
74 // Add a circle
75 	sswf::TagShape *circle = new sswf::TagShape(&my_header);
76 	style.SetType(sswf::Style::STYLE_TYPE_SOLID);
77 	color.Set(0, 255, 0, 128);		// 50% transparent green
78 	style.SetColor(0, color);
79 	style.SetColor(1, color);
80 	circle->AddStyle(style);
81 
82 	style.Reset();
83 	style.SetType(sswf::Style::STYLE_TYPE_LINE);
84 	color.Set(0, 255, 0);		// solid green
85 	style.SetLine(0, 1, color);
86 	color.Set(255, 0, 255);		// solid purple
87 	style.SetLine(1, 2, color);
88 	circle->AddStyle(style);
89 
90 	// shape 0
91 	circle->AddMove(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(0), TWIPS(-28.35));	// draw circle
92 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(  8.3), TWIPS(   8.3), TWIPS(  11.8), TWIPS(     0));
93 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(    0), TWIPS( 11.75), TWIPS(  8.35), TWIPS(   8.3));
94 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS( -8.3), TWIPS(  8.35), TWIPS(  0.05), TWIPS( 11.75));
95 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(-11.8), TWIPS(  0.05), TWIPS(  -8.3), TWIPS(   8.3));
96 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(-8.35), TWIPS(  -8.3), TWIPS(-11.75), TWIPS(     0));
97 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(-0.05), TWIPS(-11.75), TWIPS(  -8.3), TWIPS(  -8.3));
98 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(  8.3), TWIPS( -8.35), TWIPS(     0), TWIPS(-11.75));
99 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE0, TWIPS(11.75), TWIPS(     0), TWIPS(   8.3), TWIPS( -8.35));
100 
101 	circle->AddMove(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(0), TWIPS(-28.35));	// draw circle
102 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(  8.3), TWIPS(   8.3), TWIPS(  11.8), TWIPS(     0));
103 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(    0), TWIPS( 11.75), TWIPS(  8.35), TWIPS(   8.3));
104 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS( -8.3), TWIPS(  8.35), TWIPS(  0.05), TWIPS( 11.75));
105 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(-10.0), TWIPS(  0.05), TWIPS( -10.1), TWIPS(     0));
106 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(-10.0), TWIPS(     0), TWIPS( -10.1), TWIPS(     0));
107 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(-0.05), TWIPS(-11.75), TWIPS(  -8.3), TWIPS(  -8.3));
108 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(  8.3), TWIPS( -8.35), TWIPS(     0), TWIPS(-11.75));
109 	circle->AddEdge(sswf::TagShape::MORPH_MODE_SHAPE1, TWIPS(11.75), TWIPS(     0), TWIPS(   8.3), TWIPS( -8.35));
110 
111 	rect.Set(TWIPS(-1.0) * 60, TWIPS(1.0) * 60, TWIPS(-1.0) * 60, TWIPS(1.0) * 60);	// make bounds a bit bigger
112 	circle->SetBounds(0, rect);
113 	circle->SetBounds(1, rect);
114 
115 // Create our sprite
116 	sswf::TagSprite *sprite = new sswf::TagSprite(&my_header);
117 
118 // Place the two shapes in our sprite
119 	sswf::TagPlace *place = new sswf::TagPlace(sprite);
120 	place->SetDepth(1);
121 	place->SetObjectID(square->Identification());
122 	sswf::Matrix matrix;
123 	matrix.SetScale(1.0);
124 	matrix.SetRotate(0);
125 	matrix.SetTranslate(0, TWIPS(105));
126 	place->SetMatrix(matrix);
127 
128 	place = new sswf::TagPlace(sprite);
129 	place->SetDepth(2);
130 	place->SetObjectID(circle->Identification());
131 	matrix.SetScale(1.0);
132 	matrix.SetRotate(0);
133 	matrix.SetTranslate(TWIPS(0), TWIPS(0));
134 	place->SetMatrix(matrix);
135 
136 	for(int i = 1; i < 180; i += 5) {
137 		int p = TWIPS(60 - sin(TORAD(i)) * 120.0);
138 		sswf::TagPlace *place = new sswf::TagPlace(sprite);
139 		place->SetDepth(2);
140 		if(p > 30 * 20) {
141 			place->SetMorphPosition((p - 30 * 20) * 65535 / (30 * 20));
142 		}
143 		else {
144 			place->SetMorphPosition(0);
145 		}
146 		sswf::Matrix matrix;
147 		matrix.SetScale(1.0);
148 		matrix.SetRotate(0);
149 		matrix.SetTranslate(TWIPS(0), p);
150 		place->SetMatrix(matrix);
151 
152 		new sswf::TagShowFrame(sprite);
153 	}
154 
155 // Place our sprite in the main animation
156 	place = new sswf::TagPlace(&my_header);
157 	place->Replace();
158 	place->SetName("bouncing");
159 	place->SetDepth(1);
160 	place->SetObjectID(sprite->Identification());
161 	matrix.SetScale(1.0);
162 	matrix.SetRotate(0.0);
163 	matrix.SetTranslate(TWIPS(-60), TWIPS(480.0 / 2.0));
164 	place->SetMatrix(matrix);
165 
166 	// Create a small script which resets a variable
167 	// (the horizontal position of our sprite)
168 	{
169 		sswf::TagDoAction *do_action = new sswf::TagDoAction(&my_header);
170 		sswf::ActionPushData push_data(&my_header);
171 		push_data.AddString("xpos");
172 		push_data.AddInteger(-60);
173 		do_action->SetAction(push_data);
174 		sswf::Action set_variable(&my_header, sswf::Action::ACTION_SET_VARIABLE);
175 		do_action->SetAction(set_variable);
176 	}
177 
178 	new sswf::TagShowFrame(&my_header);
179 
180 	// the next script does a "previous frame" each time and that's
181 	// what this extra frame is for!
182 	new sswf::TagShowFrame(&my_header);
183 
184 // Move the sprite from left to right (hidden at the start and end)
185 	/*
186 	for(int i = -60; i < 640 + 60; i += 2) {
187 		sswf::TagPlace *place = new sswf::TagPlace(&my_header);
188 		place->SetDepth(1);
189 		sswf::Matrix matrix;
190 		matrix.SetScale(1.0);
191 		matrix.SetRotate(0);
192 		matrix.SetTranslate(TWIPS(i), TWIPS(480.0 / 2.0));
193 		place->SetMatrix(matrix);
194 		*/
195 
196 		sswf::Action add(&my_header, sswf::Action::ACTION_ADD);
197 		sswf::Action get_variable(&my_header, sswf::Action::ACTION_GET_VARIABLE);
198 		sswf::Action less_than(&my_header, sswf::Action::ACTION_LESS_THAN);
199 		sswf::Action next_frame(&my_header, sswf::Action::ACTION_NEXT_FRAME);
200 		sswf::Action previous_frame(&my_header, sswf::Action::ACTION_PREVIOUS_FRAME);
201 		sswf::Action set_property(&my_header, sswf::Action::ACTION_SET_PROPERTY);
202 		sswf::Action set_variable(&my_header, sswf::Action::ACTION_SET_VARIABLE);
203 		//sswf::Action swap(&my_header, sswf::Action::ACTION_SWAP);
204 
205 		sswf::TagDoAction *do_action = new sswf::TagDoAction(&my_header);
206 		sswf::ActionPushData push_data(&my_header);
207 		push_data.AddInteger(700);
208 		push_data.AddString("xpos");
209 		push_data.AddString("xpos");
210 		push_data.AddInteger(2);		// xpos += 2;
211 		push_data.AddString("xpos");
212 		push_data.AddString("bouncing");
213 		push_data.AddFloat(0.0f);		// Property "_x" (see src/sswf/sswf_save.c++ around line #92, or search for "direct_properties")
214 		push_data.AddString("xpos");
215 		do_action->SetAction(push_data);
216 
217 		do_action->SetAction(get_variable);
218 		do_action->SetAction(set_property);
219 
220 		do_action->SetAction(get_variable);
221 		do_action->SetAction(add);
222 		do_action->SetAction(set_variable);
223 
224 		do_action->SetAction(get_variable);
225 		//do_action->SetAction(swap); -- often you need to swap these!
226 		do_action->SetAction(less_than);	// xpos > 700
227 
228 		sswf::ActionBranch if_true(&my_header, sswf::Action::ACTION_BRANCH_IF_TRUE);
229 		if_true.SetLabel("next");
230 		do_action->SetAction(if_true);
231 
232 		// if not true, repeat this action script
233 		do_action->SetAction(previous_frame);
234 
235 		sswf::ActionBranch all_done(&my_header);
236 		all_done.SetLabel("previous");
237 		do_action->SetAction(all_done);
238 
239 		sswf::ActionLabel label_next(&my_header);
240 		label_next.SetLabel("next");
241 		do_action->SetAction(label_next);
242 
243 		do_action->SetAction(next_frame);
244 
245 		sswf::ActionLabel label_previous(&my_header);
246 		label_previous.SetLabel("previous");
247 		do_action->SetAction(label_previous);
248 
249 		sswf::Action play(&my_header, sswf::Action::ACTION_PLAY);
250 		do_action->SetAction(play);
251 
252 		new sswf::TagShowFrame(&my_header);
253 	/*
254 	}
255 	*/
256 
257 // end the movie
258 	new sswf::TagEnd(&my_header);
259 
260 // Save the result in a file
261 	sswf::Data result;
262 	size_t size;
263 	void *buffer;
264 
265 	my_header.Save(result);
266 	result.Read(buffer, size);
267 
268 	FILE *f = fopen("script.swf", "wb");
269 	if(f != 0) {
270 		fwrite(buffer, size, 1, f);
271 		fclose(f);
272 	}
273 
274 	return 0;
275 }
276 
277