XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

Dynamic Loading

Last post 11/16/2009 10:33 AM by Shahin. 6 replies.
  • 5/24/2008 7:33 PM

    Dynamic Loading

    I was wondering how to load some textures dynamically, as I wont know them at compile time. How could this be acheived? From my (little) knowledge of the content pipeline I have to specify them at compile time. I want my porgam to iterate through a list of .bmp files to be displayed using rectangles.

    Thanks, Mike


  • 5/24/2008 10:24 PM In reply to

    Re: Dynamic Loading

    You can use the static FromFile method of the Texture2D class. I'd also recommend compressing those files in another format such as png to keep filesize in the kb range instead of mb range that bmps usually result in.
  • 5/26/2008 3:38 AM In reply to

    Re: Dynamic Loading

    Note that the dynamic loading functions only exist in the windows version of the XNA framework so will not work on the 360 (or the Zune??? not sure about the zune)
    Play Kissy Poo - a game for 4 year olds on Xbox and windows
    The ZBuffer
    News and information for XNA
      Follow The Zman on twitter, Email me
        Please read the forum FAQs - Bug/Feature reporting
          Don't forget to mark good answers and good playtest feedback when you see it!!!
  • 5/26/2008 11:22 PM In reply to

    Re: Dynamic Loading

    As long as the XBox 360 supports the Texture2D.SetData method and basic file I/O, you can load textures dynamically. You must read the .bmp file data, convert its pixel data to an ARGB format and store it in a managed byte array.  Then, you just pass the byte array to the Texture2D.SetData method and you're done.

    The following code copies data from a System.Drawing.Bitmap into a Texture2D, using the Texture2D.SetData method.  While the System.Drawing.Bitmap class is not supported on the XBox 360, this code simply demonstrates that as long as you can get a managed byte array containing your bitmap data in an ARGB format, you can copy the data into a texture with the SetData method.


    //Returns a Texture2D containing the same image as the bitmap parameter, resized if necessary.  Returns null if an error occurs. 
    public Texture2D CreateTextureFromBitmap( GraphicsDevice graphics_device, System.Drawing.Bitmap bitmap ) 
        Texture2D   texture         = null
        bool        dispose_bitmap  = false
        try 
        { 
            if (bitmap != null
            { 
                //Resize the bitmap if necessary, then capture its final size 
                if (graphics_device.GraphicsDeviceCapabilities.TextureCapabilities.RequiresPower2) 
                { 

                    System.Drawing.Size new_size; //New size will be next largest power of two, so bitmap will always be scaled up, never down

                    new_size        = new Size( (int)Math.Pow( 2.0, Math.Ceiling( Math.Log( (double)bitmap.Width ) / Math.Log( 2.0 ) ) ), (int)Math.Pow( 2.0, Math.Ceiling( Math.Log( (double)bitmap.Height ) / Math.Log( 2.0 ) ) ) );
                    bitmap          = new Bitmap( bitmap, new_size );
                    dispose_bitmap  = true
                } 
                System.Drawing.Size bitmap_size = bitmap.Size; 
                 
                //Create a texture with an appropriate format 
                texture = new Texture2D( graphics_device, bitmap_size.Width, bitmap_size.Height, 1, TextureUsage.None, SurfaceFormat.Color ); 
                 
                //Lock the bitmap data and copy it out to a byte array 
                System.Drawing.Imaging.BitmapData bmpdata = bitmap.LockBits( new System.Drawing.Rectangle( 0, 0, bitmap_size.Width, bitmap_size.Height ), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb ); 
                byte [ ] pixel_bytes = null;
                try
                {
                    pixel_bytes = new byte[bmpdata.Stride * bmpdata.Height];
                    Marshal.Copy( bmpdata.Scan0, pixel_bytes, 0, temp_bytes.Length );
                }catch{} //If error occurs allocating memory, bitmap will still be unlocked properly
                bitmap.UnlockBits( bmpdata ); 
     
                //Set the texture's data to the byte array containing the bitmap data that was just copied 
                if (pixel_bytes != null) texture.SetData<byte>( pixel_bytes ); 
            } 
        } 
        catch 
        { 
            //Error occured; existing texture must be considered invalid 
            if (texture != null
            { 
                texture.Dispose(); 
                texture = null
            } 
        } 
        finally 
        { 
            if (dispose_bitmap) 
                bitmap.Dispose(); 
        } 
        return texture; 
  • 5/26/2008 11:58 PM In reply to

    Re: Dynamic Loading

    Have you tried doing a Directory.GetFiles("Content/path to textures"), looping through the strings and doing Content.Load<Texture2D>(File.GetFilenameWithoutExtension(filename)); ? Granted this assumes they have been built by the pipeline by someone at some point.
    John Sedlak Xna/DirectX MVP
    XNA Articles, Tutorials and Videos | My Blog
  • 5/28/2008 6:00 PM In reply to

    Re: Dynamic Loading

    I just tried something like this. I built my file as normal, then copied the .xnb to my directory, and then removed it form the content sub project. Heres the code im using to load it:
    Content = new ContentManager(this.ScreenManager.Game.Services);
                Content.RootDirectory = header.Path;
                terrain = Content.Load<Model>(header.GameZoneID + "-terrain");

    This throws an asset not found error. Is there something im missing? Ive checked the path and it appears fine.

    Nevermind, probelm solved.

    Thanks anyway, Mike

     

  • 11/16/2009 10:33 AM In reply to

    Re: Dynamic Loading

    Would you mind telling us how you did it?
Page 1 of 1 (7 items) Previous Next