Juan Carlos Ruiz Pacheco

I need to Load into the content manager my own graphics format file, i already have the libraries to load this file and extracting it into a Bitmap Array , but i don't have any idea about how i can allow the content manager to load this info.

I'm making a 2d videogame, my own graphics file format have a lot of extra information for each frame that it have, because of that i need to read this file extract the bitmap info and the extra info, off course i need that my Texture2D objects obtain the images from the bitmaps that i extract from my own file.

something like that:

**Solution Explorer

--Content

----Textures

-------MyFile.JKI

Where MyFile.JKI is a file with my own graphic image format, i have the libraries to extract from it a bitmap array and extra info...

what can i do to load the images (Bitmaps) in this way

Texture2D t = content.Load<Texture2D>("MyFile.InternalImage1)";

Texture2D t = content.Load<Texture2D>("MyFile.InternalImage2)";

and what can i do to load extrainfo (Some type)  in this way

SomeType st = content.Load<SomeType>("MyFile.InternalType1)";

Could somebody helpme what are the steps that i need to follow to acomplish this objective

Have Somebody an example that can helpme

 

thanks

 PD: I've been looking XNA documentation about Custom Importer and somethin like extending Custom Importer but iĄŻm lossing with that, please helpme.




Re: XNA Framework I need to Load my own graphics format file --> Custom Importer

Shawn Hargreaves - MSFT

You need to write an importer for your custom image type, something like this:

[ContentImporter]
class MyImporter : ContentImporter<Texture2DContent>
{
public override Texture2DContent Import(string filename, ContentImporterContext context)
{
...
}
}

Build this into a standalone library assembly, add that to the list of pipeline assemblies in the XNA tab of the properties dialog for your main game project, and then you'll be able to select this new importer for your custom image files when you add them to the project.

To make this work more automatically, there are hints you can set in the [ContentImporter] attribute to tell C# Express what file extension this importer supports, and what processor should be used with it. If you set those, it will automatically pick the right importer when you add files matching your extension, but the above code should be enough to get you started.





Re: XNA Framework I need to Load my own graphics format file --> Custom Importer

JuanK_solocodigo

Thanks Shawn, < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I already knew that,

I did try make the most simple usable 'Bone' just like this:

 

[ContentImporterAttribute(".jki")]

public class JKITextureImporter : ContentImporter<Texture2DContent>

{

   public override Texture2DContent Import(String filename, ContentImporterContext context)

   {

      //This class decode my file and have some functionalities

      JKIDecoder jkid = new JKIDecoder();

      //Here i load the file

      ImageJKI myImage = jkid.DecodeFile(filename);

      //For Example One of myImage functionalities is convert my format to System.Draw.Bitmap class format

      Bitmap[] b = myImage.ToBitmapArray();

      //Now i need to asign one of theese bitmaps to the texture.. but... HOW

      Texture2DContent t = new Texture2DContent();

      t.Name = "Zue";

      return t;

   }

}

 

Well... i don't know how i should use the t (Texture2DContent) object to load one of my special format files.

How can i do to load my .jki Files

because i don't know where i can say to XNA in which way my Bitmaps should be assigned to a Texture2D object.






Re: XNA Framework I need to Load my own graphics format file --> Custom Importer

Shawn Hargreaves - MSFT

JuanK_solocodigo wrote:

Bitmap[] b = myImage.ToBitmapArray();

//Now i need to asign one of theese bitmaps to the texture.. but... HOW

Texture2DContent t = new Texture2DContent();



t.Mipmaps = b[0] should do the trick.





Re: XNA Framework I need to Load my own graphics format file --> Custom Importer

JuanK_solocodigo

Shawn, me again:

I tryed to do that, i create a fake text file and change the ext to jki.

[ContentImporterAttribute(".jki")]

public class JKITextureImporter : ContentImporter<Texture2DContent>

{

public override Texture2DContent Import(String filename, ContentImporterContext context)

{

//Now i need to asign one of theese bitmaps to the texture.. but... HOW

Texture2DContent t = new Texture2DContent();

myBMPC bmc = new myBMPC();

t.Name = "Zue";

t.Mipmaps = new MipmapChain(bmc);

return t;

}

class myBMPC : BitmapContent

{

private byte[] b;

public myBMPC()

{

b = new byte[1024];

}

public override byte[] GetPixelData()

{

return b;

}

public override void SetPixelData(byte[] sourceData)

{

b = null;

b = sourceData;

}

protected override bool TryCopyFrom(BitmapContent sourceBitmap, Microsoft.Xna.Framework.Rectangle sourceRegion, Microsoft.Xna.Framework.Rectangle destinationRegion)

{

return true;

}

protected override bool TryCopyTo(BitmapContent destinationBitmap, Microsoft.Xna.Framework.Rectangle sourceRegion, Microsoft.Xna.Framework.Rectangle destinationRegion)

{

return true;

}

public override bool TryGetFormat(out SurfaceFormat format)

{

format = SurfaceFormat.Rgba32;

return true;

}

}

}

But don't work , when i start to compile the IDE Reports:

Error 1 Invalid texture. Face 0 contains too many mipmaps. Content\Textures\imagen.jki

:'(






Re: XNA Framework I need to Load my own graphics format file --> Custom Importer

Shawn Hargreaves - MSFT

Your myBMPC class isn't setting the width and height when it is constructed.

This seems like an overly complicated way of doing things, though. Creating your own BitmapContent subclass is certainly possible, but quite a bit of work as the pipeline won't work properly until you fully implement all the methods like TryCopyFrom and TryCopyTo.

An easier way would just be to create a new PixelBitmapContent<Rgba32>, then use SetPixelData (or the higher level and more strongly typed SetPixel method) to write your data into it.





Re: XNA Framework I need to Load my own graphics format file --> Custom Importer

JuanK_solocodigo

Hi Shawn,

Thanks for your , now i can work importing my own graohic format from content pipeline, this is a first Draft ( well working ) of the class:

    [ContentImporter(".jki",DisplayName="Imagen De Multiples Frames JKI")]

    public class JKIImporter : ContentImporter<Texture2DContent>

    {

        public override Texture2DContent Import(string filename, ContentImporterContext context)

        {

            FileInfo f = new FileInfo(filename);

            string assetName = f.Name.Remove(f.Name.Length - 4, 4);

           

            DecodificadorJKI decodificador = new DecodificadorJKI();

            EncabezadoFijo encabezado;

            BaseColor[] coloresMascara = new BaseColor[0];

            PixelBitmapContent<Rgba32> pixelHelper;

            Texture2DContent[] textureContent;

            byte[] byteArray;

           

            ImagenJKI<Color32> imagen32 = (ImagenJKI<Color32>)decodificador.CargaImagenJKIDesdeArchivo(

                                                          filename, out encabezado, ref coloresMascara);

 

 

            textureContent = new Texture2DContent[imagen32.InformacionBase.NumCuadros];

 

            for (int i = 0; i < imagen32.InformacionBase.NumCuadros; i++)

            {

                pixelHelper = new PixelBitmapContent< Rgba32>(imagen32.cuadrosDeImagen[ i ].Ancho, imagen32.cuadrosDeImagen[ i ].Alto);

 

                byteArray = ImagenJKI<Color32>.CuadroImagenJKIAByteArray(imagen32, (Int16)i);

 

                pixelHelper.SetPixelData(byteArray);

                textureContent[ i ] = new Texture2DContent();

                textureContent[ i ].Mipmaps = new MipmapChain(pixelHelper);

                textureContent[ i ].Name = assetName + i.ToString();

            }

 

            return textureContent[0];

        }

    }

Luckly my  ImagenJKI<Color32> object is very easy to support and i've created the 'ToByteArray' method easily without work with System....Marshal from the bitmap :P

Now, as you can see, iĄŻm making a little trap to return only once Texture2DContent    but i need really is do something like that:

    [ContentImporter(".jki",DisplayName="Imagen De Multiples Frames JKI")]

    public class JKIImporter : ContentImporter<Texture2DContent[]>

    {

        public override Texture2DContent[] Import(string filename, ContentImporterContext context)

        {

               ...

               ...

              return textureContent;

        }

But for that i need, a least thats i think, to create or extend the Standard Processor ContentProccessor Functionality because not exist actually any Content processor that allow to procces Texture2DContent[]   . Could you give me a clue about how i can do that

Thanks Shawn,  your help is very VERY good... you show me the way that i need to follow without carryme in your hands ( i hope that the idea would be understandble, you know my languaje is spanish not english :P )






Re: XNA Framework I need to Load my own graphics format file --> Custom Importer

Shawn Hargreaves - MSFT

Arrays of textures, huh Cool! You doing some kind of animating sprite system by any chance

You have two options here depending on whether you need any special processing of your textures. If the format of the data you output from the importer is exactly how you want it in your game (right pixel format, already has mipmaps if you want those, etc) then you can just choose "no processing required" as your content processor. This will pass the array of Texture2DContent straight through, serialize them out to disk, and then at runtime you can do a Load<Texture2D[]> to read the whole thing back in.

If you do want to use one of the texture processors, for instance to create mipmaps, you need a new processor that will loop over the array and call a worker processor for each element. Something like:

[ContentProcessor]
class MyProcessor : ContentProcessor<Texture2DContent[], Texture2DContent[]>
{
     public override Texture2DContent[] Process(Texture2DContent[] input, ContentProcessorContext context)
     {
         for (int i = 0; i < input.Length; i++)
             input[ i ] = context.Convert<Texture2DContent[], Texture2DContent[]>(input[ i ], "SpriteTextureProcessor");

         return input;
     }
}

should do the trick.





Re: XNA Framework I need to Load my own graphics format file --> Custom Importer

JuanK_solocodigo

Arrays of textures, huh Cool! You doing some kind of animating sprite system by any chance

Yes, in fact is my own graphics format to hold multiple frame for an sprite, this support per frame maskcolor ( if need it) and other features like RLE compression, Index Based Compression, RLE Index Based Compression And Huffman Compression, offset for displacements for each frame( because not every frames need to have the same size), and allows multiple color Depths like 8,16,24,32 bits... different exhibition time per frame and some more thigs.

ok, with the last problem charging jki in Content pipeline i've detected a bug when use 32bit + alpha but now i'm working on this.

Now i'm in the job ( bored job i'm a oracle pl/sql developer :'( )

but in the night i would try fix the bug and create the content importer.

thanks again, in a few days i would ask in the forum about how load the extra info in my content importer.

Again thanks for your help,

PD: Me siento alegre por poder contar con tu ayuda. ( i write in spanish because 'alegre ' is something like happy in english but also sound like Allegro :P )