2017-06-22 3 views
1

Ich habe ein anderes Problem mit XNA. Ich habe eine Texture2D mit vielen Kacheln. Dann habe ich eine Liste von Rechtecken, die die Grenzen von jeder 50x50 Pixel Kachel sein sollen.XNA speichert eine einzelne Kachel als PNG

Jetzt möchte ich in der Lage sein, jede Kachel als kleine PNG-Datei zu speichern. Hier ist mein Code:

//declare the rectangles and spritesets 
Texture2D tileSheet; 
List<Rectangle> tileSet; 

//load every tile, also works great 
tileSheet = Content.Load<Texture2D>(@"Tiles/Object"); 
int noOfTilesX = (int)tileSheet.Width/50; 
int noOfTilesY = (int)tileSheet.Height/50; 
tileSet = new List<Rectangle>(noOfTilesX * noOfTilesY); 
for (int j = 0; j < noOfTilesY; j++) 
{ 
    for (int i = 0; i < noOfTilesX; i++) 
    { 
     bounds = new Rectangle(i * 50, j * 50, 50, 50); 
     tileSet.Add(bounds); 
    } 
} 

//save as pngs if they do not exist 
if (!File.Exists(@"C:\Test.png")) 
{ 
    Stream stream = File.Create(@"C:\Test.png"); 
    //works, but it is only the complete file, i'd like to save a single tile 
    tileSheet.SaveAsPng(stream, tileSheet.Width, tileSheet.Height); 
} 

Das Problem ist: Ich kann nicht eine einzelne Kachel retten kann, ist es immer spart meine ganze Texture2D und dehnt sie auf die Auflösung i in den Parametern übergeben. Suchen im Internet hat mir nichts gesagt, es hat mir nur gesagt, wie man den Stream für eine komplette Textur2D verwenden kann.

Antwort

0

Ich antwortete nur eine Frage, wo jemand eine neue Textur, indem sie einen Teil einer größeren Textur, ähnlich wie Ihr Kachel-Problem gesucht.

können Sie diese Erweiterung Methode verwenden und dann das Ergebnis speichern, wie Sie normalerweise tun würde:

public static class TextureExtension 
{ 
    /// <summary> 
    /// Creates a new texture from an area of the texture. 
    /// </summary> 
    /// <param name="graphics">The current GraphicsDevice</param> 
    /// <param name="rect">The dimension you want to have</param> 
    /// <returns>The partial Texture.</returns> 
    public static Texture2D CreateTexture(this Texture2D src, GraphicsDevice graphics, Rectangle rect) 
    { 
     Texture2D tex = new Texture2D(graphics, rect.Width, rect.Height); 
     int count = rect.Width * rect.Height; 
     Color[] data = new Color[count]; 
     src.GetData(0, rect, data, 0, count); 
     tex.SetData(data); 
     return tex; 
    } 
} 

Empfohlene Nutzung:

using (FileStream stream = File.OpenWrite("path")) 
{ 
    tileTexture.CreateTexture(GraphicsDevice, new Rectangle(50, 50, 100, 100)).SaveAsJpeg(stream, 100, 100); 
} 
0

Versuchen Sie etwas wie das. Ich habe es nicht getestet, also könnte es einige Fehler geben, aber Sie müssen nur eine separate Textur für jede Kachel erstellen und dann jeden von ihnen speichern.

//declare the rectangles and spritesets 
Texture2D tileSheet; 
List<Rectangle> tileSet; 

//load every tile, also works great 
tileSheet = Content.Load<Texture2D>(@"Tiles/Object"); 
int noOfTilesX = (int)tileSheet.Width/50; 
int noOfTilesY = (int)tileSheet.Height/50; 
tileSet = new List<Rectangle>(noOfTilesX * noOfTilesY); 

// Gets the color data of the tile sheet. 
Color[] tileSheetPixels = new Color[tileSheet.Width * tileSheet.Height]; 
tileSheetPixels.GetData<Color>(tileSheetPixels); 

for (int j = 0; j < noOfTilesY; j++) 
{ 
    for (int i = 0; i < noOfTilesX; i++) 
    { 
     bounds = new Rectangle(i * 50, j * 50, 50, 50); 
     tileSet.Add(bounds); 

     // Creates a new texture for a single tile. 
     Texture2D singleTile = new Texture2D(graphics, 50, 50); 
     Color[] pixels = new Color[50 * 50]; 

     // Gets the pixels that correspond to the single tile and saves them in another 
     // color array. 
     for (int k = 0; k < pixels.Length; k++) 
     { 
      pixels[k] = new Color(); 

      int x = bounds.X; 
      x += (k % 50); 
      int y = bounds.Y; 
      y += (k/50); 

      pixels[k] = tileSheetPixels[y * 50 + x]; 
     } 

     // Sets the color data of the single tile texture to the color array 
     // created above. 
     singleTile.SetData<Color>(pixels); 

     //save as pngs if they do not exist 
     if (!File.Exists(@"C:\tile_" + i + "_" + j + ".png")) 
     { 
      Stream stream = File.Create(@"C:\tile_" + i + "_" + j + ".png"); 

      singleTile.SaveAsPng(stream, 50, 50); 
     } 
    } 
} 
Verwandte Themen