C# - What's the best method of storing images?

EFileTahi-A

Well-known member
Joined
Aug 8, 2004
Messages
539
Location
Portugal / Barreiro
At the moment I have an Texture[] array to hold all the images to be used in the game. However, this kinda limits things. For starters, I cannot easly identify the images using numbers 1, 2, 3, 4, etc...

I could load all images in Hastable, no prob on that, unfortunately I have to covert them to a bitmap then load Bitmap into the texture. The prob is, whever I use "Bitmap" inside my DirectX render proceedure it becomes like 4000% slower...

Also, my Texture[] array is a static object so that it can be accessible from anywhere, the problem is, when I reload the graphics it will not release the old graphics from memory leading to the so called memory-leak even if I say to the Texture[] array to be NULL before reloading the new graphics. And, since it is static I cannot dispose it or it will not be accessible no more...

Can I create some sort of instance of the Texture[] array object so that I can dispose it and re-use it?
Suggestions?
 
Is there a reason why you need to convert them to bitmaps to use with a HashTable rather than the format you are using with the array based implementation? If you are getting errors - what errors are you getting?
Rather than a static array you could wrap it in a singleton and provide public methods to generate / retreive the array. Also what is preventing you using a static array? You should still be able to set it to null / assign new values to it regardless of it being static.
 
PlausiblyDamp said:
Is there a reason why you need to convert them to bitmaps to use with a HashTable rather than the format you are using with the array based implementation? If you are getting errors - what errors are you getting?

This is the thing: I load all the available graphics files (BMPs) from several directories into an HashTable array as Bitmap objects. The graphics within this hashtable "should" be applyed then to the buttons background (see picture) background allowing the user so select and use the corresponding picture to build the map. This is done and working fine.

4b48086fb07ffc30278f43fcc2096de7.jpg


"Ah, this will be easy! Now all I have to do is to load the "Texture.FromBitmap()" with the correct picture from the Hashrable to render the games map when using Direct3D to draw the sprite". Wrong, doing this would be TOO much slow because I need to copy the bitmap into the Texture and only then use the Sprite.Draw() Method to draw the texture. I mean this becomes like THOUSANDS of times slower!

How does the games engine work:
I have an 3D string[,,] array (sArrMap[500,500,3]) holding the names of all tiles from all graphical layers layers
I make a loop from the correct x,y coords to render the specific part of the map (since my program supports map sizes like 500x500 tiles)

(pseucoding):
Code:
for (int z = 2; z < 3; z++) // renders the layer number
{
	for (int i = currentYPostion; i < totalYPostion; i++) //this renders only the showing tiles in Y pos
	{
		for (int n = currentXPostion; n < totalXPostion; i++) //this renders only the showing tiles in X pos
		{
			texture = (bitmap)Hashtable(sArrMap[n,i,z].string());
			Sprite.Draw(texture);
		}
	}
}

This is the slow code using loading Bitmaps from the hashtable.
This version allows an ABISMAL result of something like (200x1)x1 tiles. That is 200 tiles per 1 layers with about 1 fps, meaning that in one second this loop renders something like 200 32*32 tiles in one second. In one second... Jesus!

Now the fast and perfect version using an array of Textures[] (a DirectX object) which is loaded at the begining the same way the hashtable is loaded as I described above.

(pseucoding):
Code:
for (int z = 2; z < 3; z++) // renders the layer number
{
	for (int i = currentYPostion; i < totalYPostion; i++) //this renders only the showing tiles in Y pos
	{
		for (int n = currentXPostion; n < totalXPostion; i++) //this renders only the showing tiles in X pos
		{
			Sprite.Draw(Convert.ToInt16(Texture[sArrMap[n,i,z]));
		}
	}
}

This version allows something like (400x3)x20 tiles. That is 400 tiles per 3 layers with about 30 fps, meaning that in one second this loop renders something like 36.000 32*32 tiles in one second (if not much more).

Resuming, the only reason that I need Bitmaps is to config the Graphics buttons backgroud with all the avaialble graphics scanned in the target Dir(s).

Rather than a static array you could wrap it in a singleton and provide public methods to generate / retreive the array. Also what is preventing you using a static array? You should still be able to set it to null / assign new values to it regardless of it being static.

I can use the static Texture[] Array all right, the problem is even if I turn it to NULL it will not release the its resources!

(pseucoding):
Code:
private void buttonclick_ReloadGraphics()
{
	Texture[] = null;
	Loop
	{
		Scan all dirs in graphics dir and load any file wnding with .BMP
		if {BMP found}
		{
                         i++;
			Texture[i] = BMP_found; 
		}
	}
}

So basicaly, whenever this function would be executed it should release its previous images, and load the new ones. Unfortunatly the resources wont be realeased and an Extra MBs of memory will be eaten with the new images loading...

I do not understand this coment of yours:
"Rather than a static array you could wrap it in a singleton and provide public methods to generate / retreive the array."

Anyway, What I seek is a method of which I can hold images in memory so that they can be used in the graphics buttons background, textures and being able to be reset releasing its associated resources.
 
Last edited by a moderator:
I found a way of releasing the Texture[]s object resources.

(Pseucoding)
Code:
for (i = 0; i < X; i++)
{
    Texture[i].Dispose();
}
GarbageCollect();

Ok, now I only need to know the way of loading all my BMPs/PNGs images as texture object using keynames instead of numbers.
 
Back
Top