Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. How to create a dynamic array of direct draw surfaces [modified]

How to create a dynamic array of direct draw surfaces [modified]

Scheduled Pinned Locked Moved Graphics
graphicshelpcsharpdatabasegame-dev
4 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GTaylor55
    wrote on last edited by
    #1

    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

    G 2 Replies Last reply
    0
    • G GTaylor55

      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

      G Offline
      G Offline
      GTaylor55
      wrote on last edited by
      #2

      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?

      X 1 Reply Last reply
      0
      • G GTaylor55

        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?

        X Offline
        X Offline
        XTAL256
        wrote on last edited by
        #3

        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?"

        1 Reply Last reply
        0
        • G GTaylor55

          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

          G Offline
          G Offline
          GTaylor55
          wrote on last edited by
          #4

          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]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups