home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / flash078.zip / flashsource-r0_7_8.zip / main.cpp < prev    next >
C/C++ Source or Header  |  2001-08-03  |  13KB  |  491 lines

  1. #include <fstream>
  2. #include <sstream>
  3. #include <vector>
  4.  
  5. #include "FButton.h"
  6. #include "FBase.h"
  7. #include "FShape.h"
  8. #include "FDisplay.h"
  9. #include "FControl.h"
  10. #include "FSprite.h"
  11. #include "FSound.h"
  12. #include "FlashMP3Encoder.h"
  13. #include "FFont.h"
  14. #if defined(__WIN32__)
  15. #include "FlashFontObj.h"
  16. #endif
  17. #include "FMorph.h"
  18.  
  19. #include "FTarga.h"
  20. #include "FBitmap.h"
  21.  
  22. #include "FImport.h"
  23. #include "magick++/Magick++.h"
  24.  
  25. using namespace Magick; 
  26.  
  27. UWORD ReadImage(char *fname, std::ostringstream &f)
  28. {
  29.     UWORD return_val = 0;
  30.     try
  31.     {
  32.         Image image(fname);
  33.         int width = image.baseColumns();
  34.         int height = image.baseRows();
  35.         
  36.         //Encode Image with 24 bit color
  37.         int format = 5;
  38.         
  39.         unsigned char *imagedata = (unsigned char *)malloc(height*width*3);
  40.         unsigned char *tgadata = (unsigned char *)malloc(height*width*4);
  41.  
  42.         image.write(0,0,width,height,"RGB",CharPixel,imagedata);    
  43.         {
  44.             for(UWORD y = 0; y < height; y++)
  45.             {
  46.                 for(UWORD x = 0; x < width; x++)
  47.                 {
  48.                     UDWORD base = (y*width+x)*4;
  49.  
  50.                     UDWORD base2 = (y*width+x)*3;
  51.                 
  52.                     unsigned char r = imagedata[base2];
  53.                     unsigned char g = imagedata[base2+1];
  54.                     unsigned char b = imagedata[base2+2];
  55.                     
  56.                     ((unsigned char*)tgadata)[base+0]   =  0xff;
  57.                     ((unsigned char*)tgadata)[base+1]   =  r;
  58.                     ((unsigned char*)tgadata)[base+2]   =  g;
  59.                     ((unsigned char*)tgadata)[base+3]   =  b;
  60.                     
  61.                 }
  62.             }
  63.         }
  64.         FlashZLibBitmapData d((unsigned char *)tgadata,(width*height*4));
  65.         FlashTagDefineBitsLossless2 db(format,width, height, d);
  66.         f << db;
  67.  
  68.         free (imagedata);
  69.         free (tgadata);
  70.  
  71.         FlashMatrix m;
  72.         m.SetScale(20,20);
  73.         
  74.         FlashShapeWithStyle s;
  75.         FlashFillStyleArray ffa;
  76.         FlashFillStyleBitmap sf(db.GetID(),m); 
  77.  
  78.         ffa.AddFillStyle(&sf);
  79.         s.SetFillStyleArray(ffa);
  80.             
  81.         FlashShapeRecordChange c(0,0);
  82.         c.ChangeFillStyle1(1);
  83.         s.AddRecord(c);
  84.         s.AddRecord(FlashShapeRecordStraight(width*20, 0));
  85.         s.AddRecord(FlashShapeRecordStraight(0, height*20));
  86.         s.AddRecord(FlashShapeRecordStraight(-width*20, 0));
  87.         s.AddRecord(FlashShapeRecordStraight(0, -height*20)); 
  88.             
  89.         
  90.         FlashTagDefineShape3 ftds(s);
  91.         
  92.         f << ftds;
  93.         return_val =  ftds.GetID();
  94.     }
  95.     catch(Exception &error_)
  96.     {
  97.         std::cout << "Caught exception: " << error_.what() << "\n"; 
  98.     }
  99.     return return_val;
  100. }
  101.  
  102. void SetBackground(std::ostringstream &f)
  103. {
  104.     f << FlashTagPlaceObject2 (1,ReadImage("c:\\windows\\desktop\\background.tga",f));
  105. }
  106. #if defined(__WIN32__)
  107. void WriteText(std::ostringstream &f)
  108. {
  109.     FlashFontFactory fff;
  110.     fff.WriteText(f,"Arial Black","Virtuascape", 530*20, 505*20, FlashRGB(0x34,0x34,0x34),18,100,220);
  111.     fff.WriteText(f,"Arial Black","Virtuascape", 650*20, 520*20, FlashRGB(0xff,0xff,0xff),10,101,100);
  112.     
  113. }
  114. #endif
  115. void Write3DBox(std::ostringstream &f, int x1, int y1, int x2, int y2, FlashRGB &c1, UWORD depth)
  116. {
  117.     FlashShapeWithStyle shape;
  118.     
  119.     FlashRGB c2((unsigned short)(1.50 * c1.GetR()), (unsigned short)(1.50 * c1.GetG()), (unsigned short)(1.50 * c1.GetB()));
  120.     FlashRGB c3((unsigned short)(0.50 * c1.GetR()), (unsigned short)(0.50 * c1.GetG()), (unsigned short)(0.50 * c1.GetB()));
  121.     
  122.     FlashFillStyleArray fsa;
  123.     FlashFillStyleSolid s1(c1);
  124.     fsa.AddFillStyle(&s1);
  125.         
  126.     FlashLineStyleArray fla;
  127.     FlashLineStyle ls1(20,c2);
  128.     FlashLineStyle ls2(20,c3);
  129.     
  130.     fla.AddLineStyle(&ls1);
  131.     fla.AddLineStyle(&ls2);
  132.  
  133.     shape.SetFillStyleArray(fsa);
  134.     shape.SetLineStyleArray(fla);
  135.  
  136.     FlashShapeRecordChange line0(x1,y1);
  137.     line0.ChangeFillStyle1(1);
  138.     line0.ChangeLineStyle(1);
  139.  
  140.     FlashShapeRecordChange line1;
  141.     line1.ChangeLineStyle(1);
  142.  
  143.     FlashShapeRecordChange line2;
  144.     line2.ChangeLineStyle(2);
  145.  
  146.     shape.AddRecord(line0);
  147.     shape.AddRecord(FlashShapeRecordStraight(x2-x1,0));
  148.     shape.AddRecord(line2);
  149.     shape.AddRecord(FlashShapeRecordStraight(0,y2-y1));
  150.     shape.AddRecord(FlashShapeRecordStraight(-(x2-x1),0));
  151.     shape.AddRecord(line1);
  152.     shape.AddRecord(FlashShapeRecordStraight(0,-(y2-y1)));
  153.  
  154.     FlashTagDefineShape3 tshape(shape);
  155.     f << tshape;
  156.     f << FlashTagPlaceObject2(depth, tshape.GetID());
  157.     
  158. }
  159. void WriteSprite(std::ostringstream &f)
  160. {
  161.     UWORD id = ReadImage("c:\\windows\\desktop\\v.tga",f);
  162.     FlashTagSprite ftds;
  163.     FlashMatrix m;
  164.     m.SetTranslate(300*20,230*20);
  165.     FlashColorTransform cfx;
  166.     
  167.     FlashTagPlaceObject2 f1(2,id,m);
  168.  
  169.     int r;
  170.     
  171.     r = 128+rand()%128;
  172.     cfx.SetAddRGB(FlashRGB(0,0,0,-r));
  173.     FlashTagPlaceObject2 f2(2,id,m,cfx);
  174.     f2.SetMove(true);
  175.     r = 128+rand()%128;
  176.     cfx.SetAddRGB(FlashRGB(0,0,0,-r));
  177.     FlashTagPlaceObject2 f3(2,id,m,cfx);
  178.     f3.SetMove(true);
  179.     r = 128+rand()%128;
  180.     cfx.SetAddRGB(FlashRGB(0,0,0,-r));
  181.     FlashTagPlaceObject2 f4(2,id,m,cfx);
  182.     f4.SetMove(true);
  183.     r = 128+rand()%128;
  184.     cfx.SetAddRGB(FlashRGB(0,0,0,-r));
  185.     FlashTagPlaceObject2 f5(2,id,m,cfx);
  186.     f5.SetMove(true);
  187.     r = 128+rand()%128;
  188.     cfx.SetAddRGB(FlashRGB(0,0,0,-r));
  189.     FlashTagPlaceObject2 f6(2,id,m,cfx);
  190.     f6.SetMove(true);
  191.     r = 128+rand()%128;
  192.     cfx.SetAddRGB(FlashRGB(0,0,0,-r));
  193.     FlashTagPlaceObject2 f7(2,id,m,cfx);
  194.     f7.SetMove(true);
  195.     r = 128+rand()%128;
  196.     cfx.SetAddRGB(FlashRGB(0,0,0,-r));
  197.     FlashTagPlaceObject2 f8(2,id,m,cfx);
  198.     f8.SetMove(true);
  199.  
  200.     FlashTagShowFrame s;
  201.  
  202.     ftds.Add(&f1);
  203.     ftds.Add(&s);
  204.     ftds.Add(&f2);
  205.     ftds.Add(&s);
  206.     ftds.Add(&f3);
  207.     ftds.Add(&s);
  208.     ftds.Add(&f4);
  209.     ftds.Add(&s);
  210.     ftds.Add(&f5);
  211.     ftds.Add(&s);
  212.     ftds.Add(&f6);
  213.     ftds.Add(&s);
  214.     ftds.Add(&f7);
  215.     ftds.Add(&s);
  216.     ftds.Add(&f8);
  217.     ftds.Add(&s);
  218.  
  219.     f << ftds;
  220.     f << FlashTagPlaceObject2(2,ftds.GetID());
  221.  
  222. }
  223. void TestRotatingText(void)
  224. {    
  225. #if defined(__WIN32__)
  226.     std::ofstream fileout("testing2.swf",std::ios::binary);
  227.     std::ostringstream f(std::ios::binary);
  228.  
  229.     f << FlashTagBackgroundColor(0xff,0xee,0xff);
  230.     FlashRect bounds;
  231.     UWORD depth = 0;
  232.  
  233.     FlashFontFactory fff;
  234.     fff.WriteText(f,"Arial","Hello,SWFSource", 25*20, 10*20, FlashRGB(0xff,0x00,0x00),10,depth,bounds);
  235.     depth = 1;
  236.     fff.WriteText(f,"Arial","Support Double-byte Char Set", 25*20, 50*20, FlashRGB(0x00,0x00,0x50),10,depth,bounds);
  237.     depth = 1;
  238.     fff.WriteText(f,"Arial","CJK(China,Japan,Korea)", 25*20, 75*20, FlashRGB(0x00,0x50,0x00),10,depth,bounds);
  239.     depth = 2;
  240.     //Test DBCS 
  241.     //WriteText(f,"Arial","±Θ╧╨╟╤▒╣¬ó½▓", 20*20, 100*20, FlashRGB(0x00,0x00,0x00),20,depth,0,bounds);
  242.     fff.WriteText(f,"Arial","Test ChnJpnKor Characters", 20*20, 100*20, FlashRGB(0x00,0x00,0x00),20,depth,bounds);
  243.     depth = 3;
  244.     UWORD id = fff.WriteText(f,"Arial","SWFSource SDK", 25*20, 75*20, FlashRGB(0x00,0x00,0xff),34,depth,bounds, 0, false, true, false);
  245.  
  246.     f << FlashTagShowFrame();
  247.  
  248.     int nFrames = 25;
  249.     for (int ii = 1 ; ii < nFrames;ii++) {
  250.         f << FlashTagRemoveObject2(depth);//then vv.RemoveObject(txt);
  251.         FlashMatrix m2;
  252.         m2 = CreateMatrix(bounds,(nFrames-ii),(nFrames-ii),(nFrames+1-ii)*128,0,0,true,true);
  253.         f << FlashTagPlaceObject2(depth,id,m2);
  254.         f << FlashTagShowFrame();
  255.     }
  256.  
  257.     f << FlashTagEnd();
  258.     
  259.     //fileout << FlashHeader(5,f.str().size(),800*20,600*20,8.0,1);
  260.     fileout << FlashHeader(5,f.str().size(),400*20,200*20,8.0,1);
  261.     fileout << f.str();
  262. #endif
  263. }
  264. void main2(void)
  265. {
  266.     TestRotatingText();
  267.  
  268.     std::ofstream fileout("testing.swf",std::ios::binary);
  269.     std::ostringstream f(std::ios::binary);
  270.     
  271.     f << FlashTagBackgroundColor(0xff,0xff,0xff);
  272.     SetBackground(f);
  273.     //Write3DBox(f,10,10,799*20,20*20,FlashRGB(0xff,0xaa,0x00), 5);
  274.     //Write3DBox(f,10*20,30*20,250*20,500*20,FlashRGB(0xaa,0xaa,0xaa), 4);
  275.     //Write3DBox(f,12*20,32*20,248*20,42*20, FlashRGB(0xff,0xaa,0x00), 5);
  276.  
  277.     //WriteText(f);
  278.     //WriteSprite(f);
  279.  
  280.     //FlashActionGetURL url("http://www.google.com","");
  281.     //FlashActionStop s;
  282.  
  283.     //FlashTagDoAction ftd;
  284.     //ftd.AddAction(&s);
  285.     //ftd.AddAction(&url);
  286.  
  287.     //f << ftd;
  288.     //f << FlashTagShowFrame();
  289.     
  290.     // Shape Morph Example
  291.     
  292.     /*FlashShape s;
  293.     FlashShapeRecordChange changerec;
  294.     changerec.ChangeFillStyle0(1);
  295.     
  296.     s.AddRecord(changerec);
  297.     s.AddRecord(FlashShapeRecordChange(100,100));
  298.     s.AddRecord(FlashShapeRecordStraight(100,0));
  299.     s.AddRecord(FlashShapeRecordStraight(0,100));
  300.     s.AddRecord(FlashShapeRecordStraight(-100,0));
  301.     s.AddRecord(FlashShapeRecordStraight(0,-100));
  302.     
  303.     FlashShape s2;
  304.     s2.AddRecord(FlashShapeRecordChange(100,100));
  305.     s2.AddRecord(FlashShapeRecordStraight(200,0));
  306.     s2.AddRecord(FlashShapeRecordStraight(0,200));
  307.     s2.AddRecord(FlashShapeRecordStraight(-200,0));
  308.     s2.AddRecord(FlashShapeRecordStraight(0,-200));
  309.  
  310.     FlashMorphFillStyleSolid fss(FlashRGB(0xff,0xff,0xff), FlashRGB(0x00,0x00,0x00));
  311.     
  312.     FlashMorphFillStyles fmfs;
  313.     fmfs.AddFillStyle(&fss);
  314.     
  315.     FlashTagDefineMorphShape ms(s,s2);
  316.     ms.SetMorphFillStyles(fmfs);
  317.  
  318.     f << ms;
  319.     FlashTagPlaceObject2 po(1, ms.GetID());
  320.     f << po;
  321.     
  322.     FlashTagPlaceObject2 po2(1, ms.GetID());
  323.     po2.SetMove(true);*/
  324.     
  325.     /*for(UWORD ratio = 0; ratio < 0xffff; ratio+=0xff)
  326.     {
  327.         po2.SetRatio(ratio);
  328.         f << po2;
  329.         f << FlashTagShowFrame();
  330.     }*/
  331.     /*    UWORD ratio = i2*(0xffff/100);
  332.         
  333.         po2.SetRatio(ratio);
  334.         f << po2;
  335.     */    
  336.     // SAMPLE FONT OUTPUT...see FlashFontObj.cpp
  337.     
  338.     /*FlashFontFactory fff;
  339.     fff.WriteText(f,"Arial Bold","SWF Source", 100*20, 135*20, FlashRGB(0xff,0xff,0xff),8,1,105);
  340.     fff.WriteText(f,"Impact","WWW", 100*20, 150*20, FlashRGB(0xff,0xcc,0x00),34,0);*/
  341.     
  342.     // SAMPLE MP3 OUTPUT
  343.     
  344.     FlashMP3Encoder MP3("c:\\windows\\desktop\\test.mp3",15);
  345.     MP3.WriteDefineTag(f);
  346.     //MP3.WriteHeader(f);
  347.     //for(int i2=0; i2 < 45; i2++)
  348.     //{
  349.     //    MP3.WriteStream(f);
  350.         //f << FlashTagShowFrame();
  351.     //}
  352.  
  353.     //UWORD MP3_id = MP3.WriteDefineTag(f);
  354.     //f << FlashTagStartSound(MP3_id, FlashSoundInfo(0x01 << 4));
  355.     
  356.     // SAMPLE IMAGE OUTPUT
  357.     
  358.     
  359.     f << FlashTagShowFrame();
  360.     
  361.     FlashTagDoAction theButton;
  362.     
  363.     theButton.AddAction(new FlashActionPush(0,"Jump3",strlen("Jump3")+1));
  364.     theButton.AddAction(new FlashActionGotoFrame2(true));
  365.  
  366.     f << theButton;
  367.  
  368.     FlashActionWaitForFrame2 fw(1);
  369.     FlashTagDoAction ftd;
  370.     ftd.AddAction(&fw); 
  371.     f << ftd;
  372.  
  373.     f << FlashTagEnd();
  374.     
  375.     fileout << FlashHeader(5,f.str().size(),800*20,600*20,15.8,1);
  376.     fileout << f.str();
  377.  
  378. }
  379.  
  380.  
  381. void main3(int argc, char **argv)
  382. {
  383. //    TEST FLASH IMPORT
  384. #if defined(__WIN32__)
  385.     std::ifstream filein("c:\\test.swf",std::ios::binary);
  386. #else
  387.     std::ifstream filein(argv[1],std::ios::binary);
  388. #endif
  389.     if(filein.fail()) std::cout << "ERROR: Could not open input file";
  390.     {
  391.         FlashImporter fi;
  392.         FlashMyImporter i;
  393.         FlashHeader fh = fi.ImportHeader(filein,i);
  394.         while(fi.ImportTag(filein,i));
  395.     }
  396. }
  397.  
  398. int main4()
  399. {
  400.     
  401.     FlashMorphFillStyles fills;
  402.     
  403.     double xScale = 10000 / 0x00008000L;
  404.     double yScale = 10000 / 0x00008000L;
  405.  
  406.     int xCenter = 10000 / 2;
  407.     int yCenter = 10000 / 2;
  408.     
  409.  
  410.     FlashMatrix matrix1(true, FlashFixed(xScale), FlashFixed(yScale),
  411.                         false, FlashFixed(0), FlashFixed(0),    // no rotate
  412.                         xCenter, yCenter);
  413.             
  414.     FlashMatrix matrix2(false, FlashFixed(xScale), FlashFixed(yScale),
  415.                         false, FlashFixed(0), FlashFixed(0),    // no rotate
  416.                         0, 0);
  417.  
  418.     
  419.     FlashMorphGradient gradient;
  420.  
  421.     FlashMorphGradientRecord record1(0, FlashRGB(255,0,0), 0, FlashRGB(255,255,255));
  422.     gradient.AddRecord(record1);
  423.  
  424.     FlashMorphGradientRecord record2(255, FlashRGB(0,0,0), 255, FlashRGB(255,255,255));
  425.     gradient.AddRecord(record2);
  426.             
  427.     // build fill style
  428.     
  429.     FlashMorphFillStyleLinear *fill= new FlashMorphFillStyleLinear(matrix1, matrix2, gradient);
  430.         
  431.     fills.AddFillStyle(fill);
  432.  
  433.     std::ofstream fileout("testing.swf",std::ios::binary);
  434.     std::ostringstream f(std::ios::binary);
  435.     
  436.     f << FlashTagBackgroundColor(0x88,0x88,0x88);
  437.     
  438.     FlashShape s;
  439.     FlashShapeRecordChange changerec;
  440.     changerec.ChangeFillStyle0(1);
  441.     
  442.     s.AddRecord(changerec);
  443.     s.AddRecord(FlashShapeRecordChange(0,0));
  444.     s.AddRecord(FlashShapeRecordStraight(10000,0));
  445.     s.AddRecord(FlashShapeRecordStraight(0,10000));
  446.     s.AddRecord(FlashShapeRecordStraight(-10000,0));
  447.     s.AddRecord(FlashShapeRecordStraight(0,-10000));
  448.     
  449.     FlashShape s2;
  450.     s2.AddRecord(FlashShapeRecordChange(1000,1000));
  451.     s2.AddRecord(FlashShapeRecordStraight(2000,0));
  452.     s2.AddRecord(FlashShapeRecordStraight(0,2000));
  453.     s2.AddRecord(FlashShapeRecordStraight(-2000,0));
  454.     s2.AddRecord(FlashShapeRecordStraight(0,-2000));
  455.  
  456.         FlashRGB C1(0xff,0xff,0xff);
  457.         FlashRGB C2(0xff,0xff,0xff);
  458.  
  459.         FlashMorphFillStyleSolid fss(C1, C2);
  460.     
  461.     FlashMorphFillStyles &fmfs = fills;
  462.     //fmfs.AddFillStyle(&fss);
  463.     
  464.     FlashTagDefineMorphShape ms(s,s2);
  465.     ms.SetMorphFillStyles(fmfs);
  466.  
  467.     f << ms;
  468.     FlashTagPlaceObject2 po(1, ms.GetID());
  469.     f << po;
  470.     
  471.     FlashTagPlaceObject2 po2(1, ms.GetID());
  472.     po2.SetMove(true);
  473.     
  474.     for(UWORD ratio = 0; ratio < 0xffff; ratio+=0xff)
  475.     {
  476.         po2.SetRatio(ratio);
  477.         f << po2;
  478.         f << FlashTagShowFrame();
  479.     }
  480.     
  481.  
  482.  
  483.     fileout << FlashHeader(5,f.str().size(),800*20,600*20,15.8,1);
  484.     fileout << f.str();
  485.  
  486.     delete fill;
  487.     
  488.     return 0;
  489. }
  490.  
  491.