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 :)

2012-07-20

Joel on software, hammer factory factory factory

My highlight from his amazing article "Why I hate frameworks!"


When we stepped back and looked at the global tool infrastructure, we determined that people were frustrated with having to manage and operate a hammer factory factory, as well as the hammer factory that it produced. That kind of overhead can get pretty cumbersome when you deal with the likely scenario of also operating a tape measure factory factory, a saw factory factory, and a level factory factory, not to mention a lumber manufacturing conglomerate holding company. When we really looked at the situation, we determined that that's just too complex for someone who really just wants to build a spice rack


Read the full article at:
http://discuss.joelonsoftware.com/default.asp?joel.3.219431.12

2012-07-19

Unix Commands in Windows

http://www.lucidtips.com/2008/06/07/unix-utils-on-windows/
http://www.weihenstephan.de/~syring/win32/UnxUtils.zip

Following up on my previous blog post about using windows commands to do stuff like "find content in file":  type apache_error.log | findstr "time:". 

I found a way better alternative by utilizing something called "UnxUtils". UnxUtils makes it is possible to get the most important unix commands into a windows environment and accessible through command prompt. Such as grep ...
  1. Download UnxUtils
  2. Extract into folder (c:\tools\unxutils)
  3. update system path to contain scripts [scripts are located in the wbin-folder] ( \unxutils \usr\local\wbin
  4. Launch cmd
  5. run some unix command like cat/grep/more/less and see that it works
Now, you can run the powerful grep command with regular expressions as well as printing the end of file with less etc.

JS tools, framework, IDE

For future versions of me:

I've just started fooling around with creating a client written in JS/HTML. These are the tools I've decided to look into and installed on one computer and might need to access at other places.

  • WebStorm
  • Komodo Edit
  • Dojo
  • Maqetta
  • JSONP vs Client Side PHP-proxy
WebStorm is a CodeIDE created by JetBrains, seems very mature and awesome but costs a shit load of money. 30 days of evaluation though.'
Komodo Edit free to use code editor that I've used for PHP dev earlier. It is okay, but not awesome
Dojo js framework, I have tried understanding what it is, but I have no idea. Googling "What is Dojo framework?" "what does it do?" only results in a lot of "it's the most awesome, unbeatable js framework. It's lightweight and super fast and works for enterprise solutions and smaller applications". It's awesome nontheless... I guess I have to download it and see what it does.
Maqetta system built on dojo, used for doing "GUI mockups" and then delivering these as "ready to use" js/html/css-code.
JSONP vs Client Side PHP-proxy evaluate how to do communication from one domain to another since that is not supported in js.