2012-08-31

ActionScript Ascii Art

Yesterday I found this pretty awesome tool that lets you convert any image to ascii art. It's basically done through traversing each pixel in the picture and converting it into a grayscale value and then replacing that grayscale value with an appropriate ascii-character, which one depends on how much black that pixel is.

Most important code part:

rgbVal = _data.getPixel(x, y);
redVal = (rgbVal & 0xFF0000) >> 16;
greenVal = (rgbVal & 0x00FF00) >> 8;
blueVal = rgbVal & 0x0000FF;

/*
* Calculate the gray value of the pixel.
* The formula for grayscale conversion: (Y = gray): Y = 0.3*R + 0.59*G + 0.11*B
*/
grayVal = Math.floor(0.3 * redVal + 0.59 * greenVal + 0.11 * blueVal);

It then continues with checking that value against a white and black treshold and then checking up the final gray-value against a "palette" containing:

var palette:String = "@#$%&8BMW*mwqpdbkhaoQ0OZXYUJCLtfjzxnuvcr[]{}1()|/?Il!i><+_~-;,. ";

index = Math.floor(grayVal / 4);
result += palette.charAt(index);

End result:

Say hello to my collegue, transformed into Ascii





2012-08-30

Panning and scrolling movieclips

So, yesterday I started reading AS3 Developer's Guide. After reading through close to 200 pages I found something of interest that I think is worth sharing/saving for a later time.

It is related to clipping a movieclip and then scroll it without using the expensive masking-technique and something that I wish I had known earlier.

So how does it work?

Imagine that you have a large movieclip that contains info that would cover say 3-4 screen sizes. Personally I had this exact issue with an info screen inside a game that contained mixed texts and images. So what you should know is that each display object has an attribute called scrollRect that states which part of the image that is rendered. I think that this is similar to the "sourceRect"-parameter that can be found in DirectX and thus my guess is that only parts of the movieclip is sent to the GPU for rendering which would greatly increase performance in some cases.

Example code:

ORIGINAL_HEIGHT = _infoScreen.height; //store the original size in a variable
_infoScreen.cacheAsBitmap = true;
_infoScreen.scrollRect = new Rectangle(0,0,_infoScreen.width, 300); //visible part of the movieclip


_up.addEventListener(MouseEvent.CLICK, function(e:Event):void {
    var r:Rectangle = _infoScreen.scrollRect;
    r.y -= 20;
    _infoScreen.scrollRect = r;
});


_down.addEventListener(MouseEvent.CLICK, function(e:Event):void {
    var r:Rectangle = _infoScreen.scrollRect;
    r.y += 20;
    _infoScreen.scrollRect = r;
});

Ugly picture as example:

As you can see only the specified scrollRect is shown and in the picture I have scrolled down a couple of times.

Good thing to know is that you will only experience a performance gain if you cache your movieclip as bitmap.

If you are interested in reading in more detail about it, you can do so inside the developers guide. When this was written that could be found in chapter 10 on page 178.

2012-08-21

My first podcast experience


So, yesterday I decided to see if there were any SE-podcasts available. Each day I spend 3 hours travelling to and from work so it would be perfect to spend that time doing something fun and fun for me means learning new stuff. Thus I googled around for a while and decided to start my venture with se-radio.net.

I found a podcast named E187 : Grant Ingersoll on the Solr Search Engine and picked that randomly. Turned out to be super interesting even though it's not inside my usual field. Basically it relates to "taming text" and making documents or any kind of text searchable [think wikipedia, amazon with it's products]. This is done using Lucene and Solr, a different kind of search engine that can be used in big or small applications as long as you have some kind of text in some kind of document-format that needs to be searchable.

It is open source and widely used.

Episode 187 - Grant Ingersoll on the Solr Search Engine

This was an awesome thing and I'll make sure to download a couple of more podcasts and load them to my phone so I have something to do at the train.

2012-08-07

Adding NameSpaces to FlashDevelop

So, in code I have the following logic inside my Debug.AS-file


if(CONFIG::DEBUG){
import com.demonsters.debugger.MonsterDebugger;
import messer_entertainment.utils.DebugSender;
}

Thus, when compiling this through ANT, I just add the line -define=CONFIG::DEBUG,${debug_mode} and this sets this namespace-variable to true. 

Today I was forced to have this namespace-variable set when compiling/running through the FD-IDE, I found the intersting part inside project/properties/compiler options/Compiler Constants and in there I added the string-set "CONFIG::DEBUG,true"

if only I had chosen namespace-variable CONFIG::debug instead, this would have been automatically fixed by FD whenever you do a debug-build. Ah well :)