Sign in to follow this  
flashinpan

Help me understand what is happening

Recommended Posts

I have this game I am writing in C#. I had a gentleman helping me a long time ago. He had a better understanding of graphics than I did. He wrote the following code...and I know it works and have tried my best to understand it...but I am not quite sure what is happening: This line especially has me scratching my head even after looking at the help topics, some of which I pasted as comments: //More info on PixelFormat.Format32bppArgb //The pixel format defines the number of bits of memory associated //with one pixel of data. The format also defines the order of the //color components within a single pixel of data. Bitmap retval = new Bitmap(dump.Size.Width,dump.Size.Height,PixelFormat.Format32bppArgb); HERE IS THE METHOD CALL //Note to me: Look up GetManifestResourceStream for more detail //Here we are assigning the internal game board bitmap (resource file) to a stream object. Stream s = this.GetType().Assembly.GetManifestResourceStream(resBoardName); then: _backBuffer = ConvertStreamTo32bbp(s); HERE IS THE METHOD SOURCE
public static Bitmap ConvertStreamTo32bbp(Stream s)
		{
			//Temporary...just so we can get the stream into a Bitmap object, that is all
			Bitmap dump = new Bitmap(s, true );

			//More info on PixelFormat.Format32bppArgb
			//The pixel format defines the number of bits of memory associated 
			//with one pixel of data. The format also defines the order of the 
			//color components within a single pixel of data.
			Bitmap retval = new Bitmap(dump.Size.Width,dump.Size.Height,PixelFormat.Format32bppArgb);

			//Our temporary Graphics object
			Graphics g = Graphics.FromImage(retval);

			//Draw the dump Bitmap object on the retval Bitmap object, starting in the upper left corner 0,0
			g.DrawImage(dump,0,0);

			//The graphic object has done it's job, so we can Dispose of it
			g.Dispose();

			//Dispose of the dump Bitmap object, it has served its purpose
			dump.Dispose();

			//Return the Bitmap object (our 10 x 10 game board is stored in retval now)
			return retval;
		}

For your reference, here is the entire game source. Just click on the GAME SOURCE DOWNLOAD link (you may have to right-click and "Save Target As..." on the link).

Share this post


Link to post
Share on other sites
It sucks a bitmap from a stream and converts it to 32bpp bitmap (from whatever format it is in the stream).
A stream is a generalized file, it could be stored in a file, part of a file, a database, come over a network, etc... an input stream is a sequence of bytes you can read.

Normal bitmaps are stored brg, and part of this conversion is to make it argb which is friendly to graphics cards. The a stands for 'alpha' which can be used for transparency (though it's often ignored).

Share this post


Link to post
Share on other sites
Quote:
Original post by Magmai Kai Holmlor
It sucks a bitmap from a stream and converts it to 32bpp bitmap (from whatever format it is in the stream).
A stream is a generalized file, it could be stored in a file, part of a file, a database, come over a network, etc... an input stream is a sequence of bytes you can read.

Normal bitmaps are stored brg, and part of this conversion is to make it argb which is friendly to graphics cards. The a stands for 'alpha' which can be used for transparency (though it's often ignored).



Interesting that you would mention transparency. I think this may have something to do with the game.

I do know that the game objects that are drawn on the game board (later on) can be dragged and dropped and remain "on top" of the board.

So this function is being used to convert a windows resource file (bitmap) into a bitmap in memory that is graphics card friendly?


Share this post


Link to post
Share on other sites
Basically.

There exists in the system a Bitmap class, a Graphics class, and a Stream class. The Stream represents whatever the resource file is. The Bitmap class provides functionality (via the class constructors) to (a) create a Bitmap from a Stream, using the data in the stream "as is" (i.e. whatever pixel format that data specifies); (b) create a Bitmap with a specified pixel format and size. Bitmap objects also provide the ability to create a Graphics object that represents the Bitmap. The Graphics objects provide the ability to draw themselves onto a target Bitmap.

The function strings all of this together, by loading the image "normally" first, then creating a blank "canvas" bitmap of the same size, with the desired pixel format. Then, the first Bitmap is converted to a Graphics object, which in turn gets drawn onto the "canvas" - which has the net effect of translating the pixel data.

The rest of it is just memory management.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this