flashgameblog.at

28.11.2009
00:56

PixelBender for game developers

 

This week I found the time to take a look at PixelBender. And  of course my first thought was, how to use it in my next game.

In praxis PixelBender is used for two thing. On one hand it's used for what it's supposed to - visual effects. On the other hand it's used for calculation. In this post I am talking just about visual effects.

For my games in the past I used movieclip animations for visual effects. With visual effects I mean brain death effects. That means the effect is played and doesn't have any back coupling to the logic or the game environment. That's nice to use, because you trigger the effect and don't have to care about the rest. The movieclip removes itself from the stage with a short script in the last frame.

This mechanism had got the following characteristics:

- Effect has to be easy to start.
- The effect should be animated automatically.
- When the effect is finished everything should be cleaned up  automatically.

For PixelBender effects I wanted a mechanism doing exactly the same, but things are a bit more complicated.

- For flash games all recourses should be compiled into one swf. There are some issues to get the PixelBenders effect into the swf file.
- PixelBender effects are not designed to be animated.
- The filters array of the Displayobject object is not able to be manipulated.

Getting PixelBender effects into the swf file


Exporting the PixelBender kernel in the PixelBender toolkit created a .pbj file. This file is used by the flash player at runtime. There are three methods get the .pbj file into the swf file.

1. loading at runtime

I will not discuss this solution here, because it isn't practical for flash games. Flash games are mostly spread over a lot a sites and servers. You would need your own server to store all your effects, what would cause a lot of traffic and work to maintain.

2. [embed]

The embed tag allows you to embed the .pbj file into your swf file. There is just one drawback about it. The embed tag in not part of the AS3 specifications, it's part of the flex framework. So, if you use this tag the flex framework is involved.

3. PixelBender to Actionscript converter

I have found this tool, that converts your .pbj file into an actionscript class containing the .pbj file as a byte array. With this method the .pbj file is embedded into the swf file without involving the flex framework. But there is a drawback too. This method makes it necessary to convert the .pbj file newly every time you change it.

http://blog.brun.pl/2008/05/pixel-bender-pbj-file-to-actionscript-class-converter/


I decided to use the embed tag in my class, because from my point of view it's the best solution.

I won't explain the source of the classes here. They should be easily understood and where it's needed the source is documented.

The main class is the effectManager. You have to call it's main function in your main game loop. You may now ask why I didn't use timer objects for the animations. The solution is simple: I design all my programs to be driven by one force based on the ENTER_FRAME event. It's simply easier to handle.

In the download file you will find the logic for the effect management, an example PixelBender project and an example how it's used in the swf file.

 

download: pixelbender1.zip

 

  1. package {
  2.  
  3.   import classes.at.lueftenegger.effects.EffectsManager;
  4.   import classes.at.lueftenegger.effects.fx.ShockWave;
  5.   import flash.display.Sprite;
  6.   import flash.events.Event;
  7.   import flash.events.MouseEvent;
  8.  
  9.  
  10.   public class Main extends Sprite{
  11.    
  12.     // Embed is not part of the AS3 specification.
  13.     // It's part of the Flex framework.
  14.     // So this framework is involved here,
  15.     // but there is no other solution.
  16.     [Embed("./assets/shockwave.pbj", mimeType = "application/octet-stream")]
  17.     public static var fxShockWave:Class;
  18.    
  19.     private var _fm:EffectsManager;
  20.    
  21.     public function Main(){
  22.       _fm = new EffectsManager();
  23.      
  24.       addEventListener(MouseEvent.CLICK, doShockwave);
  25.       addEventListener(Event.ENTER_FRAME, main);
  26.      
  27.       // you have to write a Class implementing IEffect for every single effect
  28.       // you create in your game.
  29.       // This line gives the effect class a reference
  30.       // to the embeded pbj file
  31.       ShockWave.effect = fxShockWave;
  32.      
  33.     }
  34.    
  35.     private function doShockwave(e:MouseEvent):void {
  36.      
  37.       // this line of code triggers the effect. That's all you have to do.
  38.       _fm.AddEffect(new ShockWave(stage.mouseX, stage.mouseY), image);
  39.      
  40.     }
  41.    
  42.     private function main(e:Event):void {
  43.       // game main loop
  44.      
  45.       // ... your game runs here     
  46.      
  47.       // in your game main loop call the effect manager's
  48.       // main function to run the effects.
  49.       _fm.main();
  50.     }
  51.    
  52.   }
  53.  
  54. }
22.11.2009
23:36

AST for AS3 (AST2)

 

This time i would like to show you the AS3 version of my ASToolBox. The AS3 version works a bit different than the AS2 version. I have split the whole tool into two components. There is a standalone applikation for all output data and a static class for use in your Flash file.

 

download: ast2.zip


You can find the installation instructions in the readme file.


To use AST you have to give it a reference to the stage.

 

  1. ...
  2. import at.lueftenegger.ast2.AST;
  3. ...
  4. AST.stage = stage;
  5. ...
  6.  
  7.  
  8. // The output and timer function are the same as in the AS2 version:
  9.  
  10. AST.tracelocal("W");
  11. AST.trace("1");
  12. AST.clear();
  13. AST.trace("2");
  14. AST.traceStage();
  15. AST.var_dump(
  16. {
  17.   _string:"Hello world!",
  18.   _int:int(1),
  19.   _uint:uint(2),
  20.   _number:Number(3),
  21.   _bool:Boolean(true),
  22.   _array:[1,2,3],
  23.   _object:{_a:"hello", _b:"world" },
  24.   _nested:[  "a",
  25.   "b",
  26.   {  c:"c",
  27.     d:"d",
  28.     e[  0,
  29.         1,
  30.         2
  31.       ]
  32.   },
  33.   int(1),
  34.   uint(2),
  35.   Number(3)
  36.       ],
  37.   _xml: XML("<a>hello World</a>")
  38. }
  39. );
  40.  
  41. // timer functions
  42. var u:Number;
  43. AST.timer_clear();
  44.  
  45. var i:int = 0;
  46.  
  47. for(i = 0; i < 10; i++){
  48.   AST.timer_start();
  49.   for(var x:int = 0; x < 1000000; x++)
  50.     u = Math.sin(x)+Math.cos(i)+Math.random()/Math.cos(x);
  51.   AST.timer_stop();
  52. }
  53. AST.timer_report();
  54. AST.timer_avg();
  55.  
  56. //timer 2
  57. AST.timer_clear();
  58. for( i = 0; i < 3; i++){
  59.   AST.timer_start();
  60.   for( x = 0; x < 1000; x++)
  61.     u = Math.sin(x)+Math.cos(i)+Math.random()/Math.cos(x);
  62.   AST.timer_stop();
  63. }
  64. AST.timer_report();
  65. AST.timer_avg();
  66.  
  67. // trace stage
  68. // this function is new to AST2
  69. // It gives a tree structure of the displaylist.
  70.  
  71. AST.traceStage();
  72. for(i = 0; i<30; i++)
  73.   addChild( new MovieClip() );
  74.  
  75. AST.traceStage();
17.11.2009
21:49

AST - ASToolBox (AS2)

 

This time I want to share a tool for all AS2 coders out there.

I wrote it because I needed an easy to use trace tool for my projects written with flashdevelop. At this time trace in flashdevelop wasn't an easy step and I didn't want to read all the tutorials because they simply didn't solve the problems for my installation.

ASToolBox (short ast) is a static class which offers functions for tracing, var_dump and time measurement of code segments.

Installation:
Copy the class source code below in a .as file and safe it in the folder

C:\Dokumente und Einstellungen\[USERNAME]\Lokale Einstellungen\Anwendungsdaten\Adobe\Flash CS4\de\Configuration\Classes\FP8


trace data
To trace data ast offers three functions. Firstly there is trace. It's the most common function in flash, so I think I don't need to explain it.

Secondly there we have var_dump. It takes an Object and traces an analysed string of it. It's an flash equivalent of the PHP function.

And Finally println, it works very similar as the C equivalent, but it doesn't know data types. Everything is handled as a String. Actually it doesn't output anything. It just prepares and returns a string for output.

Additionally to these output functions there is clear, which simply clears the output window.

time measurement

Sometimes you want to measure the time that is taken for a peace of code to be executed. Ast gives you some functions to make this task very simple.

 

  1.  
  2. ast.trace("In europe children get their presents on 24. December in the evening.");
  3. ast.var_dump({a:1, b:"w", c:["hello", "world"], d:{a:1, b:2, c:3}});
  4. ast.trace( ast.println("In europe %0 get their presents on %1. December in the evening%2", ["children", 24, "."]) );
  5. ast.clear();
  6.  
  7.  
  8. var z:Number = 0;
  9. var finish:Number = 0;
  10.  
  11. for( var i:Number = 0; i < 5; i++){
  12.   finish = 1000+Math.random()*10000;
  13.   ast.timer_start()
  14.   for( var a:Number = 0; a < finish; a++)
  15.     z = --z +1;
  16.   ast.timer_stop();
  17. }
  18.  
  19. ast.timer_report();
  20. ast.timer_avg();
  21.  
  22. // if you don't call clear here the measurements
  23. // above are added to the report below.
  24. ast.timer_clear();
  25.  
  26. var z:Number = 0;
  27. var finish:Number = 0;
  28.  
  29. for( var i:Number = 0; i < 5; i++){
  30.   finish = 1000+Math.random()*10000;
  31.   var handle:Number = ast.timer_start()
  32.   for( var a:Number = 0; a < finish; a++)
  33.     z = --z +1;
  34.   ast.timer_stop(handle2);
  35. }
  36.  
  37. ast.timer_report();
  38. ast.timer_avg();
  39.  

 

Here you can download ast.as

 

15.11.2009
12:28

flash frame rate measurement(fps)

 

Frame rate measurement is an important topic in flash game development.

Goggle brings you a ton of scripts showing the current fame rate in a text field in a corner of your swf. But in your games you often have situations where the amount of calculations in/decreases very rapidly and you want to see at the first look how the frame rate if affected. For this a graph showing the frame rate is much better than the frame rate in a text field.

I would like to share a tool I wrote in 2006 and ported to AS3 last month.

 

download: http://www.lueftenegger.at/downloads/


Manual:

- green graph: shows the current frame rate.
- blue graph: shows the average frame rate of the last 50 frames.
- red graph: shows the average frame rate of the whole runtime.



To use the window, just drag the Symbol "fpsWindow" from the library on the stage.

To create it programmatically use the following code:

  1. // AS3:
  2. addChild(new fpsWindow());
  3.  
  4. // AS2:
  5. _root.attachMovieClip("fpsWindow","fpsWindow",getNextHighestDepth());
14.11.2009
13:48

flashgameblog.at in english

Last night I had an interesting chat with some developers. We talked about blogging and I came to the conclusion to continue this blog in english. The german posts will stay as they are.

 

Letzte Nacht hatte ich eine interessante Unterhaltung mit einigen Flashspieleentwicklern. Diese Unterhaltung führte mich zu der Entscheidung, diesen Blog in englisch weiterzuführen. Die vorhandenen deutschen Einträge bleiben ungeändert vorhanden.

< < November 2009 > >
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          

Blog rolls

Latest Comments

ISO Blender Camera Setup
24.04.2010 06:58
update
21.03.2010 01:15
if-Statement
16.11.2009 08:34
hmm...
16.11.2009 08:00

Archive

  • [-]2010(8)
    • [-]August(1)
    • [-]July(1)
    • [-]June(2)
    • [-]April(1)
    • [-]March(2)
    • [-]February(1)
  • [-]2009(13)
    • [-]December(4)
    • [-]November(8)
    • [-]October(1)

Copy and paste this link into your RSS news reader

RSS 0.91Posts
RSS 2.0Posts

Social Bookmarking

Bookmark bei: Mr. Wong Bookmark bei: Webnews Bookmark bei: Icio Bookmark bei: Oneview Bookmark bei: Linkarena Bookmark bei: Favoriten Bookmark bei: Seekxl Bookmark bei: Favit Bookmark bei: Social Bookmarking Tool Bookmark bei: Power Oldie Bookmark bei: Bookmarks.cc Bookmark bei: Newskick Bookmark bei: Newsider Bookmark bei: Linksilo Bookmark bei: Readster Bookmark bei: Folkd Bookmark bei: Yigg Bookmark bei: Digg Bookmark bei: Del.icio.us Bookmark bei: Reddit Bookmark bei: Simpy Bookmark bei: StumbleUpon Bookmark bei: Slashdot Bookmark bei: Netscape Bookmark bei: Furl Bookmark bei: Yahoo Bookmark bei: Spurl Bookmark bei: Google Bookmark bei: Blinklist Bookmark bei: Blogmarks Bookmark bei: Diigo Bookmark bei: Technorati Bookmark bei: Newsvine Bookmark bei: Blinkbits Bookmark bei: Ma.Gnolia Bookmark bei: Smarking Bookmark bei: Netvouz Information