How to create a dynamic array of direct draw surfaces [modified]
-
SCENARIO I have been learing to use C# and DirectX via small programs using the Express edition of c# 2005. To take what I have learn't further I have decided to develop a small engine, it is nothing fancy and so far it is only Direct Draw, just developing my skills. I have developed the engine as a class library that has methods such as Graphics2D=sets up direct draw and creates 2 surfaces for double buffering LoadImage=loads a file into an offscreen surface array and returns the index LoadAnimImage=same but creates a frame array and color key for transparency DrawImage(Index,etc)=Draws the indicated image from the surface array. there is also an overloaded DrawImage which has a frame parameter added for drawing animations. I also have a testing project that references the dll and uses its methods to show a scrolling background and an animated walking character with music. MY PROBLEM OK, all is working well, except that my surface array is static in the number of elements it has. Here is the code that relates to my question
private Surface[] ImgList = new Surface[2];//create an array of 2 surfaces
public int LoadImage(string FileName)
{
sDesc.Clear();
sDesc.SurfaceCaps.OffScreenPlain = true;
ImgList[NumImages] = new Surface(FileName, sDesc, dddevice);//Add a surface to the first element
NumImages+= 1;//move to the next empty ellement of the array
return NumImages;
}public void DrawImage(int Image,int x,int y)
{
//Draw the surface in the array indexed by the Image parameter
ssurf.DrawFast(0, 0, ImgList[Image-1],ImageRect,DrawFastFlags.DoNotWait);
}As I said, the above works fine, but I now want to make the surface array dynamic using the ArrayList from the system.collection namespace and here is my attempt
ArrayList al = new ArrayList();//Create a dynamic array
public int LoadImage(string FileName)
{
sDesc.Clear();
sDesc.SurfaceCaps.OffScreenPlain = true;
al.Add(new Surface(FileName, sDesc, dddevice));Add a surface to the array
NumImages+= 1;
return NumImages;
}public void DrawImage(int Image, int x, int y)
{
//Don't really understand what I am doing with al[Image-1] here
//but the idea was to draw the surface from the array indexed by image
ssurf.DrawFast(0,0,al[Image-1],ImageRect,DrawFastFlags.DoNotWait);}
When I build that I get cannot convert from system.array to microsoft.directx.directdraw.surface error, among others which are related to the above e
-
SCENARIO I have been learing to use C# and DirectX via small programs using the Express edition of c# 2005. To take what I have learn't further I have decided to develop a small engine, it is nothing fancy and so far it is only Direct Draw, just developing my skills. I have developed the engine as a class library that has methods such as Graphics2D=sets up direct draw and creates 2 surfaces for double buffering LoadImage=loads a file into an offscreen surface array and returns the index LoadAnimImage=same but creates a frame array and color key for transparency DrawImage(Index,etc)=Draws the indicated image from the surface array. there is also an overloaded DrawImage which has a frame parameter added for drawing animations. I also have a testing project that references the dll and uses its methods to show a scrolling background and an animated walking character with music. MY PROBLEM OK, all is working well, except that my surface array is static in the number of elements it has. Here is the code that relates to my question
private Surface[] ImgList = new Surface[2];//create an array of 2 surfaces
public int LoadImage(string FileName)
{
sDesc.Clear();
sDesc.SurfaceCaps.OffScreenPlain = true;
ImgList[NumImages] = new Surface(FileName, sDesc, dddevice);//Add a surface to the first element
NumImages+= 1;//move to the next empty ellement of the array
return NumImages;
}public void DrawImage(int Image,int x,int y)
{
//Draw the surface in the array indexed by the Image parameter
ssurf.DrawFast(0, 0, ImgList[Image-1],ImageRect,DrawFastFlags.DoNotWait);
}As I said, the above works fine, but I now want to make the surface array dynamic using the ArrayList from the system.collection namespace and here is my attempt
ArrayList al = new ArrayList();//Create a dynamic array
public int LoadImage(string FileName)
{
sDesc.Clear();
sDesc.SurfaceCaps.OffScreenPlain = true;
al.Add(new Surface(FileName, sDesc, dddevice));Add a surface to the array
NumImages+= 1;
return NumImages;
}public void DrawImage(int Image, int x, int y)
{
//Don't really understand what I am doing with al[Image-1] here
//but the idea was to draw the surface from the array indexed by image
ssurf.DrawFast(0,0,al[Image-1],ImageRect,DrawFastFlags.DoNotWait);}
When I build that I get cannot convert from system.array to microsoft.directx.directdraw.surface error, among others which are related to the above e
In case anyone is interested in a solution I have figured out a workaround by modifying my static array solution. I changed the array definition and the LoadImage method to use a new method called GrowImageArray.
This new method-
Creates a new array to the new dimension
-
Copies all the surfaces from the old array into the new
-
Adds the new surface to the new array
-
Returns the new array to the LoadImage Method
I realize that this is probably not the best way to achieve my goal, but the 'best way' is not important to me at this stage because this is an experience gaining exercise
private Surface[] ImgList = null;//create an emty array
...public int LoadImage(string FileName)
{
sDesc.Clear();
sDesc.SurfaceCaps.OffScreenPlain = true;
NumImages+= 1;
ImgList= GrowImageArray(FileName,ImgList, NumImages);
return NumImages;
}private Surface[] GrowImageArray(string FileName,Surface[] pOldArray, Int32 pNewSize)
{
Int32 counter = 0;
Surface[] newArray = new Surface[pNewSize];// Create the new array to the new dimension// //Copy the surfaces from the old array to the new array if (pNewSize > 1) { foreach (Surface item in pOldArray) { newArray\[counter\] = item; counter += 1; } } newArray\[counter\] = new Surface(FileName, sdesc, dddevice);//Add the new image to the last element return newArray;
}
public void DrawImage(int Image,int x,int y)
{
//Draw the surface in the array indexed by the Image parameter
ssurf.DrawFast(0, 0, ImgList[Image-1],ImageRect,DrawFastFlags.DoNotWait);
}PS I am still interested to find out how to achieve this with ArrayList?
-
-
In case anyone is interested in a solution I have figured out a workaround by modifying my static array solution. I changed the array definition and the LoadImage method to use a new method called GrowImageArray.
This new method-
Creates a new array to the new dimension
-
Copies all the surfaces from the old array into the new
-
Adds the new surface to the new array
-
Returns the new array to the LoadImage Method
I realize that this is probably not the best way to achieve my goal, but the 'best way' is not important to me at this stage because this is an experience gaining exercise
private Surface[] ImgList = null;//create an emty array
...public int LoadImage(string FileName)
{
sDesc.Clear();
sDesc.SurfaceCaps.OffScreenPlain = true;
NumImages+= 1;
ImgList= GrowImageArray(FileName,ImgList, NumImages);
return NumImages;
}private Surface[] GrowImageArray(string FileName,Surface[] pOldArray, Int32 pNewSize)
{
Int32 counter = 0;
Surface[] newArray = new Surface[pNewSize];// Create the new array to the new dimension// //Copy the surfaces from the old array to the new array if (pNewSize > 1) { foreach (Surface item in pOldArray) { newArray\[counter\] = item; counter += 1; } } newArray\[counter\] = new Surface(FileName, sdesc, dddevice);//Add the new image to the last element return newArray;
}
public void DrawImage(int Image,int x,int y)
{
//Draw the surface in the array indexed by the Image parameter
ssurf.DrawFast(0, 0, ImgList[Image-1],ImageRect,DrawFastFlags.DoNotWait);
}PS I am still interested to find out how to achieve this with ArrayList?
What you are doing there is basically what a vector does. The C++ standard library has a vector class which is an array that grows. It uses a template to take a type, that is, you declare it like this:
vector v = new vector();
where int is the type. Now i don't know any C# but i had a look and found that indeed you can add any type to your ArrayList and that this is the C# equivalent to a vector. I think you function is quite good and if it works then that's even better :) ----------------------------------
Customer in computer shop: "Can you copy the Internet onto this disk for me?"
-
-
SCENARIO I have been learing to use C# and DirectX via small programs using the Express edition of c# 2005. To take what I have learn't further I have decided to develop a small engine, it is nothing fancy and so far it is only Direct Draw, just developing my skills. I have developed the engine as a class library that has methods such as Graphics2D=sets up direct draw and creates 2 surfaces for double buffering LoadImage=loads a file into an offscreen surface array and returns the index LoadAnimImage=same but creates a frame array and color key for transparency DrawImage(Index,etc)=Draws the indicated image from the surface array. there is also an overloaded DrawImage which has a frame parameter added for drawing animations. I also have a testing project that references the dll and uses its methods to show a scrolling background and an animated walking character with music. MY PROBLEM OK, all is working well, except that my surface array is static in the number of elements it has. Here is the code that relates to my question
private Surface[] ImgList = new Surface[2];//create an array of 2 surfaces
public int LoadImage(string FileName)
{
sDesc.Clear();
sDesc.SurfaceCaps.OffScreenPlain = true;
ImgList[NumImages] = new Surface(FileName, sDesc, dddevice);//Add a surface to the first element
NumImages+= 1;//move to the next empty ellement of the array
return NumImages;
}public void DrawImage(int Image,int x,int y)
{
//Draw the surface in the array indexed by the Image parameter
ssurf.DrawFast(0, 0, ImgList[Image-1],ImageRect,DrawFastFlags.DoNotWait);
}As I said, the above works fine, but I now want to make the surface array dynamic using the ArrayList from the system.collection namespace and here is my attempt
ArrayList al = new ArrayList();//Create a dynamic array
public int LoadImage(string FileName)
{
sDesc.Clear();
sDesc.SurfaceCaps.OffScreenPlain = true;
al.Add(new Surface(FileName, sDesc, dddevice));Add a surface to the array
NumImages+= 1;
return NumImages;
}public void DrawImage(int Image, int x, int y)
{
//Don't really understand what I am doing with al[Image-1] here
//but the idea was to draw the surface from the array indexed by image
ssurf.DrawFast(0,0,al[Image-1],ImageRect,DrawFastFlags.DoNotWait);}
When I build that I get cannot convert from system.array to microsoft.directx.directdraw.surface error, among others which are related to the above e
Thanks to VizCoder providing confirmation that ArrayList's are indeed any type I looked at my orriginal attempt again. All I needed to do was change the DrawImage method to..
public void DrawImage(int Image, int x, int y)
{
ssurf.DrawFast(0,0,(Surface)al[Image-1],ImageRect,DrawFastFlags.DoNotWait);
}Because the ArrayList object can be of any type I had to cast it as type surface when using it
(Surface)al[Image-1]