1 /*
2  *   Copyright (C) 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  *
17  */
18 
19 /*
20  * Zou Lunkai, zoulunkai@gmail.com
21  *
22  * Test for DoInitAction tag.
23  *
24  * Tags order(compiled with Ming0.4.beta4):
25  *   DefineMovieClip(mc1)
26  *   DoInitAction(mc1)
27  *   PlaceObject2(mc1)
28  *   DefineMovieClip(mc2)
29  *   DoInitAction(mc2);
30  *   PlaceObject2(mc2);
31  *
32  * Tests show that the actions order is like this:
33  *     mc1.init_actions
34  *     mc1.onClipInitialize
35  *     mc2.init_actions
36  *     mc2.onClipInitialize
37  *     mc1.onClipConstruct
38  *     mc2.onClipConstruct
39  *     _root.actions
40  *     mc1.onClipLoad
41  *     mc1.actions
42  *     mc2.onClipLoad
43  *     mc2.actions
44  */
45 
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <ming.h>
49 
50 #include "ming_utils.h"
51 
52 #define OUTPUT_VERSION 6
53 #define OUTPUT_FILENAME "InitActionTest.swf"
54 
55 
56 int
main(int argc,char ** argv)57 main(int argc, char** argv)
58 {
59   SWFMovie mo;
60   SWFMovieClip  mc1, mc2, dejagnuclip;
61   SWFShape  sh1, sh2;
62 
63   const char *srcdir=".";
64   if ( argc>1 )
65     srcdir=argv[1];
66   else
67   {
68       fprintf(stderr, "Usage: %s <mediadir>\n", argv[0]);
69       return 1;
70   }
71 
72   Ming_init();
73   mo = newSWFMovieWithVersion(OUTPUT_VERSION);
74   SWFMovie_setDimension(mo, 800, 600);
75   SWFMovie_setRate (mo, 12.0);
76 
77   dejagnuclip = get_dejagnu_clip((SWFBlock)get_default_font(srcdir), 10, 0, 0, 800, 600);
78   SWFMovie_add(mo, (SWFBlock)dejagnuclip);
79 
80   add_actions(mo, " _root.x = '0+'; ");
81   SWFMovie_nextFrame(mo); /* 1st frame */
82 
83 
84   mc1 = newSWFMovieClip();
85   sh1 = make_fill_square (300, 300, 60, 60, 255, 0, 0, 255, 0, 0);
86   SWFMovieClip_add(mc1, (SWFBlock)sh1);
87   /* add init actions */
88   add_clip_init_actions(mc1, " _root.note('mc1.init_actions'); "
89                              " _root.x += '1+'; "
90                              " y = 'var_of_root'; " );
91 
92   /* add actions */
93   add_clip_actions(mc1, " _root.note('mc1.actions');  _root.x += '9+'; ");
94   SWFMovieClip_nextFrame(mc1);//1st frame
95 
96   mc2 = newSWFMovieClip();
97   sh2 = make_fill_square (600, 600, 60, 60, 255, 0, 0, 255, 0, 0);
98   SWFMovieClip_add(mc2, (SWFBlock)sh2);
99   /* add init actions */
100   add_clip_init_actions(mc2, " _root.note('mc2.init_actions'); _root.x += '3+'; ");
101   /* add actions */
102   add_clip_actions(mc2, " _root.note('mc2.actions');  _root.x += '11+'; ");
103   SWFMovieClip_nextFrame(mc2);//1st frame
104 
105 
106   /* add mc1 to _root */
107   SWFDisplayItem it1;
108   it1 = SWFMovie_add(mo, (SWFBlock)mc1);
109   SWFDisplayItem_setDepth(it1, 10);
110   SWFDisplayItem_setName(it1, "mc1");
111 
112   SWFDisplayItem_addAction(it1,
113     newSWFAction(" _root.note('mc1.onClipInitialize'); _root.x += '2+'; "
114 	"_root.check_equals(this.__proto__, MovieClip.prototype);"
115 	),
116     SWFACTION_INIT);
117 
118   SWFDisplayItem_addAction(it1,
119     newSWFAction(" _root.note('mc1.onClipConstruct');  _root.x += '5+'; "),
120     SWFACTION_CONSTRUCT);
121 
122   SWFDisplayItem_addAction(it1,
123     newSWFAction(" _root.note('mc1.onClipLoad');  _root.x += '8+'; "),
124     SWFACTION_ONLOAD);
125 
126 
127   /* add mc2 to _root */
128   SWFDisplayItem it2;
129   it2 = SWFMovie_add(mo, (SWFBlock)mc2);
130   SWFDisplayItem_setDepth(it2, 11);
131   SWFDisplayItem_setName(it2, "mc2");
132 
133    SWFDisplayItem_addAction(it2,
134     newSWFAction(" _root.note('mc2.onClipInitialize'); _root.x += '4+'; "),
135     SWFACTION_INIT);
136 
137   SWFDisplayItem_addAction(it2,
138     newSWFAction(" _root.note('mc2.onClipConstruct');  _root.x += '6+'; "),
139     SWFACTION_CONSTRUCT);
140 
141   SWFDisplayItem_addAction(it2,
142     newSWFAction(" _root.note('mc2.onClipLoad');  _root.x += '10+'; "),
143     SWFACTION_ONLOAD);
144 
145 
146   /* add main timeline actions */
147   add_actions(mo, "_root.note('_root.actions');  _root.x += '7+'; ");
148   SWFMovie_nextFrame(mo); /* 2nd frame */
149 
150   /* The check below used to succeeds, and started failing when
151    * executing init actions "after" DLIST tags.
152    * Should be fixed if we postpone the call to ::construct
153    * to "after" init actions are executed, which would require
154    * some book keeping in sprite_instance class
155    */
156   check_equals(mo, "_root.x", "'0+1+2+3+4+5+6+7+8+9+10+11+'");
157 
158   check_equals(mo, "_root.y", "'var_of_root'");
159   add_actions(mo, " _root.totals(); stop(); ");
160   SWFMovie_nextFrame(mo); /* 3rd frame */
161 
162 
163   //Output movie
164   puts("Saving " OUTPUT_FILENAME );
165   SWFMovie_save(mo, OUTPUT_FILENAME);
166 
167   return 0;
168 }
169