I have a Class (base) that takes a TYPE in the constructor and loads the appropriate image into memory - this class is used to fill the level map of my game - so I create like 50 of the objects, 20 walls, 10 bricks, , 20 floors, etc... and each time I load the image into memory by doing something as shown below:

public class base
{
   private Bitmap image = null;
   ...
   if (Type == Wall)
     image = ImgWall;
   ....
 
   public Image ImgWall
   {
       get 
       { 
           return Image.FromFile("Images\\wall.bmp");
       }
   }
}
public class main_part
{
   ...
   // Put Walls on all sides
   for (all i's)
      level[i, 0] = new base(Type.Wall);
   for (all j's)
      level[0,j] = new base(Type.Wall);
}

To "share" the same Monster Image? might lower the load - but how could I "share" something like that? (making it static? I tried and it didn't change my stats .. from what I can see on my PC).
As you can see, I am filling the array (level) with objects of type base (the example shows only Wall, but there is also floor, and a few others) ... using this code each object creates and holds it's own instance of image
Isn't there a way to share all the loaded WALL, FLOOR, etc... images instead of loading them into each object? for example for all the Objects of Type = Wall only one instance of the IMAGE would be needed. So instead of having 50 floors I only load ONE, and instead of 20 walls I only load ONE, etc...
I thought STATIC would do it but it doesn't seem to be working for me (just tried making the functions themselves static)...
Currently the Mem Usage and VM Size on my PC are ~24megs/~13megs (which doesn't seem obsessive), but is too much for some of my friends PCs which means it is currently unacceptable - this is a very low key GDI+ game that should run fairly well...
What indication will I have that it is better? (shouldn't I be looking at reducing the Mem Usage and VM Size? Or is there a better indication?)
Any ideas, hints, and help would be greatly appreciated, thanks

I think you mean that...

class CImage
{
static Image myImage;
static CImage m_cimage = null;
Private CImage()
{
}
public static CImage GetTheOnlyInstance()
{
if(m_cimage != null)
return m_cimage
else
return new CImage();
}
}

Hi,

Nice singleton pattern by RamyMahrous but GetTheOnlyInstance()
could directly return the Image myImage and from .Net 2.0 instead of defining a private contructor to make a pure static class you can define the class itself as static.

Loren Soth

This article has been dead for over six months. Start a new discussion instead.