Use vector graphics in flashpunk?


(Nirvan) #1

Hi,

I wondering how to use vector graphics in flashpunk. I know that there must be only vector grahpics when we decide to this. So we can load for example .svg files and use this? Game will be faster with this graphics if colors will by cartoony?

Thanks.


(Jacob Albano) #2

Adobe’s bitmap rendering is faster than vector rendering. It would actually lower your performance to use them.


(Nirvan) #3

Ok thanks :slight_smile: So I will stay with rasters.


(Christophe Bessis) #4

FP is bitmap based but you can use vector graphic and rasterize them

This can really usefull when you have to draw curves or handle multiple resolution and so on.

here a basic example.

package  
{
	import flash.display.BitmapData;
	import flash.display.Shape;
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Image;
	
	/**
	 * ...
	 * @author YopSolo
	 */
	public class MyEntity extends Entity 
	{
		
		public function MyEntity() 
		{
			// creating a vector shape using build-in method
			var sh:Shape = new Shape();
			var radius:int = 25;
			sh.graphics.beginFill( 0xFF0099 );
			sh.graphics.drawCircle(radius, radius, radius );
			sh.graphics.endFill();
			
			// rasterizing the vector graphics
			var dat:BitmapData = new BitmapData(radius * 2 , radius * 2, true, 0x0);
			dat.draw( sh );
			
			graphic = new Image( dat );
			
			graphic.x = FP.halfWidth - radius;
			graphic.y = FP.halfHeight - radius;
		}
		
        override public function update():void
        {
            trace("MyEntity updates.");
        }		
		
	}

}

The rendering performance are the same (cause it’s now a bitmap) but there is a cost when you use the draw() method. So you have to create your assets before using them (during the preload of your game/level for example)