Actionscript 3 – Singleton

The Singleton design pattern is popular method that is used quite ubiquitously in software engineering. Sometimes, it is appropriate to have only one instance of a class. For example, the window manager, sprite manager, and filesystems are all good examples of classes that would neatly follow the singleton paradigm (since you only need one of them in your application). Singletons are extremely useful when you have exactly one instance of an object that requires global access.

In C++ code, one would derive singleton classes from a singletons template. Doing this prevents the coder from polluting your global space with nasty variables. (Also, the compiler does not guarantee the order that globals or statics are instantiated.)

Although Actionscript does not have templating functionality like C++, you can still make your desired class a singleton by simply adding a few functions:

package com.example
{
	public class SpriteManager
	{
		public function SpriteManager()
		{;}
 
		// Singleton methods
		//
		private static var _instance:SpriteManager = null;
		public static function CreateInst():void { _instance = new SpriteManager(); }
		public static function Get():SpriteManager { return _instance; }
		public static function InstExists():Boolean { return _instance != null; }
		public static function DestroyInst():void { _instance = null; }
	}
 
}

As you can see, you use a private static var _instance to hold the single instance of the class. The CreateInst() function creates a the single instance of that particular class:

SpriteManager.CreateInst();

You access the class by calling Get(). For example, if I wanted to fetch the instance of the SpriteManager, I call:

var manager:SpriteManager = SpriteManager.Get();

And there you go, a simple Actionscript 3 singleton class for the win. And on a related matter, there is a Playboy bunny named Sasha Singleton.

Introducing: FunkBot3000

Earlier this week, I was noodling around in Graphics Gale (my favorite pixel-art program) of which I haphazardly gave birth to this pixelated robot (shown on right). Yearning to get back into Flash game proramming, I decided to quickly prototype a small Flash demo using Actionscript and the spankin’ new flixel 2.0 engine.

I’ve always been interested in rhythm games. But oftentimes, I feel that music takes a backseat to gameplay. And rightly so since most games focus on providing gameplay that merely uses music as support for building the ambiance or to convey emotion to the player.

However, once in a while, a game is developed which mend music and gameplay in innovative and fun ways that it makes you wonder about the vast possibilities of game design. Games such as DDR, Rockband, Parappa the Rappa and Rhythm Tengoku are all prime examples of games that were able to creatively integrate music and rhythm into its core gameplay to fully immerse the player in the music.

I am currently developing this Flash game on the side in hopes to learn more about using rhythm-based game mechanics. It’s called FunkBot3000, and it is game about funky robots which are fueled by the power of music. I am currently not sure on how much time I can devote to this project because of my busy academic schedule. But  I’ll try my best to bring it to some playable state.

Play FunkBot3000 ]

National Geographic Best Science Photography

National Geographic has recently announced their winners for The Best Science Photography of the year. Above is the picture taken by Sung Hoon Kang at the biology department of Harvard University. Not only did it win National Geographics’s contest, but was selected as the best photograph in the 2009 International Science and Engineering Visualization Challenge. Quite stunning. In fact, all the runner ups that were selected are quite awesome to look at as well.

[Link]

Dock Ellis & The LSD No-No

For those of you who do not know, Dock Phillip Ellis, Jr was a Major League Baseball player who pitched for the Pittsburgh Pirates. He is best remembered for the claim that he threw a no-hitter in 1970 while under the influence of LSD. In this video, Dock Ellis recalls what happened that day. Quite amusing.

Skull Shelf

A badass photo that I found online. Great composition. Would anyone happen to know who the artist of this photo is?

DirectX10 (D3D10) & DevIL Image Library

After extensively searching online for some documentation on how to use the DevIL Image Library with DirectX10 to no avail, I have brought it upon myself to write a brief post on how to get them two to coexist in love.

Although DirectX10 (Direct3D10) already has functionality that allows users to load images directly, it might not be enough. For example, DirectX is unable to load some image types such as TARGA (.tga) files. And if you are writing a OpenGL & DirectX10 dual-renderer, you might want unite both renderers under a common image library API. Plus, DevIL has a lot of great functionality, and you want to take full advantage of it. Below, I provided the code to load an image given the filename.

#include <IL/il.h>
#include <string>
#include <assert.h>
 
// Use this macro to verify if D3D10 function succeeded
#define HV(x) if(!SUCCEEDED(x)) MessageBox(NULL, #x, "HRESULT fail", MB_OK)
 
// Creates a shader resource view given a filepath & D3D10 device
//	@filePath - String of the file path of the image file
//	@pDevice - Pointer to the D3D10 device
ID3D10ShaderResourceView* MakeSRV( const std::string& filePath, ID3D10Device* pDevice )
{
	// Will be filled and returned
	ID3D10ShaderResourceView* pSRV = NULL;
 
	// Load image from DevIL
	ILuint idImage;
	ilGenImages( 1, &idImage );
	ilBindImage( idImage );
	ilLoadImage( filePath.c_str() );
	assert ( IL_NO_ERROR == ilGetError() );
 
	// Fetch dimensions of image
	int width = ilGetInteger( IL_IMAGE_WIDTH );
	int height = ilGetInteger( IL_IMAGE_HEIGHT );
 
	// Load the data
	ilConvertImage( IL_RGBA,IL_UNSIGNED_BYTE );
	unsigned char * pData = ilGetData();
 
	// Build the texture header descriptor
	D3D10_TEXTURE2D_DESC descTex;
	descTex.Width = width;
	descTex.Height = height;
	descTex.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	descTex.Usage = D3D10_USAGE_DEFAULT;
	descTex.BindFlags = D3D10_BIND_SHADER_RESOURCE | D3D10_BIND_RENDER_TARGET;
	descTex.CPUAccessFlags = 0;
	descTex.MipLevels = 1;
	descTex.ArraySize = 1;
	descTex.SampleDesc.Count = 1;
	descTex.SampleDesc.Quality = 0;
	descTex.MiscFlags = D3D10_RESOURCE_MISC_GENERATE_MIPS;
 
	// Resource data descriptor
	D3D10_SUBRESOURCE_DATA data ;
	memset( &data, 0, sizeof(D3D10_SUBRESOURCE_DATA));
	data.pSysMem = pData;
	data.SysMemPitch = 4 * width;
 
	// Create the 2d texture from data
	ID3D10Texture2D * pTexture = NULL;
	HV( pDevice->CreateTexture2D( &descTex, &data, &pTexture ));
 
	// Create resource view descriptor
	D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc;
	srvDesc.Format = descTex.Format;
	srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
	srvDesc.Texture2D.MostDetailedMip = 0;
	srvDesc.Texture2D.MipLevels = D3D10_RESOURCE_MISC_GENERATE_MIPS ;
 
	// Create the shader resource view
	HV( pDevice->CreateShaderResourceView( pTexture, &srvDesc, &pSRV ));
 
	// Delete from IL buffer after image loaded correctly
	ilDeleteImages( 1, &idImage );
	idImage = 0;
 
	return pSRV;
}

There you have it. Let me know if you got any woes getting it to work. Thanks to my bud Manny for helping with the code.

OpenGL Shader Lovin

Here’s a video I captured of a recent class project which emphasized replacing OpenGL’s fixed-function pipeline with custom vertex and fragment shaders. I used low-level assembly ARB language to implement this.

This box shows several shader techniques such as bump-maps, height map parallax, specular highlight and attenuation.


© Copyright 2010 Illogic Tree | "Modicus Remix" by Zidalgo | Log in