sample #12
putting all together
  1. <?php
  2. include("sample_utils.php"); // provides some utils and using mk
  3.  
  4. $swf = new MKswf("test_nuke.swf"); // start a fresh swf
  5. $swf->set_swfDir(SERVER_MKTMP); // change default working dir
  6.  
  7. $img01 = "tile01.png"; // list of image to load
  8. $img02 = "tile02.png"; //
  9. $img03 = "tile03.png"; //
  10. $img04 = "cave01.png"; //
  11. $img05 = "nuke01.png"; //
  12.  
  13. $bm = new Bitmap_item(); // mk bitmap item class
  14.  
  15. $bmTag = $bm->create_from_file($img01); // create swf tag from file
  16. $img01Width = $bm->get_bitmapWidth(); //
  17. $img01Height = $bm->get_bitmapHeight(); // width, height of image
  18. $swf->add_tagData($bmTag); // populate swf with imge data
  19.  
  20. $bm->set_bitmapID(2); // from second image and on we need to set ID
  21. $bmTag = $bm->create_from_file($img02); // create swf tag from file
  22. $img02Width = $bm->get_bitmapWidth(); //
  23. $img02Height = $bm->get_bitmapHeight(); // width, height of image
  24. $swf->add_tagData($bmTag); // populate swf with imge data
  25.  
  26. $bm->set_bitmapID(3);
  27. $bmTag = $bm->create_from_file($img03); // create swf tag from file
  28. $img03Width = $bm->get_bitmapWidth(); //
  29. $img03Height = $bm->get_bitmapHeight(); // width, height of image
  30. $swf->add_tagData($bmTag); // populate swf with imge data
  31.  
  32. $bm->set_bitmapID(4);
  33. $bmTag = $bm->create_from_file($img04); // create swf tag from file
  34. $img04Width = $bm->get_bitmapWidth(); //
  35. $img04Height = $bm->get_bitmapHeight(); // width, height of image
  36. $swf->add_tagData($bmTag); // populate swf with imge data
  37.  
  38. $bm->set_bitmapID(5);
  39. $bmTag = $bm->create_from_file($img05); // create swf tag from file
  40. $img05Width = $bm->get_bitmapWidth(); //
  41. $img05Height = $bm->get_bitmapHeight(); // width, height of image
  42. $swf->add_tagData($bmTag); // populate swf with imge data
  43.  
  44. $sc = new MKshape_container("", 6); // the shape that will wrap bitmaps
  45.  
  46. // add bitmap fills
  47. // for a single bounded image use CLIPPED_BITMAP_FILL
  48. // for tiled images use REPEATING_BITMAP_FILL
  49. // the last parameter [matrixBitmap] will place the bitmap in the right position
  50. $sc->add_bitmap_fill(1, REPEATING_BITMAP_FILL);
  51. $sc->add_rect(0, 0, 550, 290); // image bounding box
  52.  
  53. $mad = array(); // this ...
  54. $mad["x"] = 0; // ... array is used ...
  55. $mad["y"] = 290; // ... to place bitmap
  56. $mad["width"] = 640; // ... properly
  57. $mad["height"] = $img02Height; //
  58. $sc->add_bitmap_fill(2, REPEATING_BITMAP_FILL, $mad);
  59. $sc->add_rect(0, 290, 550, $img02Height); // image bounding box
  60.  
  61. $mad["x"] = 0; // ... array is used ...
  62. $mad["y"] = 290 + $img02Height; // ... to place bitmap
  63. $mad["width"] = 640; // ... correctly
  64. $mad["height"] = $img03Height; //
  65. $sc->add_bitmap_fill(3, REPEATING_BITMAP_FILL, $mad);
  66. $sc->add_rect(0, 290 + $img02Height, 550, $img03Height); // image bounding box
  67.  
  68. $sc->increase_shapeDepth();
  69. $mad["x"] = 150; // ... array is used ...
  70. $mad["y"] = 156; // ... to place bitmap
  71. $mad["width"] = $img04Width; // ... correctly
  72. $mad["height"] = $img04Height; //
  73. $sc->add_bitmap_fill(4, CLIPPED_BITMAP_FILL, $mad);
  74. $sc->add_rect(150, 156, $img04Width, $img04Height); // image bounding box
  75.  
  76. $sc->increase_shapeDepth();
  77. $mad["x"] = 230; // ... array is used ...
  78. $mad["y"] = 208; // ... to place bitmap
  79. $mad["width"] = $img05Width; // ... correctly
  80. $mad["height"] = $img05Height; //
  81. $sc->add_bitmap_fill(5, CLIPPED_BITMAP_FILL, $mad);
  82. $sc->add_rect(230, 208, $img05Width, $img05Height); // image bounding box
  83.  
  84. $shapeTD = $sc->update_tagData(); // get shape tag
  85. $shapePB = $sc->update_place_tagData();
  86.  
  87. $svg01 = new MKsimple_svg_reader("nuke_txt.svg"); // get vector lettering data
  88. $svg01->set_itemID(7);
  89. $svgTD = $svg01->update_TagData();
  90. $svgPB = $svg01->update_place_tagData();
  91.  
  92. $swf->add_tagData($shapeTD); // populate with shape
  93. $swf->add_tagData($svgTD); // populate with shape
  94. $swf->add_tagData($shapePB); // place on stage
  95. $swf->add_tagData($svgPB); // place on stage
  96. $swf->set_isCompressed("yes"); // gain some extra byte
  97. $swf->swf_output(); // save to disk
  98.  
  99. $swfHeader = $swf->fetch_assoc(); // prepare to publish
  100.  
  101. $str = render_swf_box($swfHeader, $imgDir, false);
  102. $str .= "<br/><strong>test_nuke.swf created in " .SERVER_MKTMP ."</strong>";
  103. echo show_content($str, basename(__FILE__));
  104. ?>
This sample creates a game background with an element (the cave) and one hero (duke nuke), using png with alpha channel, finally adds two letterings. Code is a little silly way to keep simple to understand. I used direct call but of course you are free to implements things the ways as you want. Note that the output is less than 19k despite to using 5 bitmaps with alpha data.

mikrokosmos