-1

我正在尝试在一个纹理上绘制许多纹理来为 RTS 游戏创建地图,虽然我可以在屏幕上绘制单个纹理,但将它们全部绘制到渲染目标似乎没有效果(调试时窗口仍然是 AliceBlue ) . 我试图确定是否有任何东西被绘制到渲染目标,所以我试图将它作为 Jpeg 保存到文件中,然后从我的桌面查看该 Jpeg。如何从 MemoryStream 访问该 Jpeg?

受保护的覆盖 void LoadContent() {

         spriteBatch = new SpriteBatch(GraphicsDevice);
         gridImage = new RenderTarget2D(GraphicsDevice, 1000, 1000);
         GraphicsDevice.SetRenderTarget(gridImage);
         GraphicsDevice.Clear(Color.AliceBlue);
         spriteBatch.Begin();


         foreach (tile t in grid.tiles)
         {
             Texture2D dirt = Content.Load<Texture2D>(t.texture);
             spriteBatch.Draw(dirt, t.getVector2(), Color.White);
         }
         test = Content.Load<Texture2D>("dirt");


             GraphicsDevice.SetRenderTarget(null);
             MemoryStream memoryStream = new MemoryStream();

            gridImage.SaveAsJpeg(memoryStream, gridImage.Width, gridImage.Height); //Or SaveAsPng( memoryStream, texture.Width, texture.Height )



         //    rt.Dispose();
             spriteBatch.End();
    }
4

1 回答 1

3

我做了一个简单的截图方法,

  void Screenie()
            {
                int width = GraphicsDevice.PresentationParameters.BackBufferWidth;
                int height = GraphicsDevice.PresentationParameters.BackBufferHeight;

                //Force a frame to be drawn (otherwise back buffer is empty) 
                Draw(new GameTime());

                //Pull the picture from the buffer 
                int[] backBuffer = new int[width * height];
                GraphicsDevice.GetBackBufferData(backBuffer);

                //Copy to texture
                Texture2D texture = new Texture2D(GraphicsDevice, width, height, false, GraphicsDevice.PresentationParameters.BackBufferFormat);
                texture.SetData(backBuffer);
                //Get a date for file name
                DateTime date = DateTime.Now; //Get the date for the file name
                Stream stream = File.Create(SCREENSHOT FOLDER + date.ToString("MM-dd-yy H;mm;ss") + ".png"); 

                //Save as PNG
                texture.SaveAsPng(stream, width, height);
                stream.Dispose();
                texture.Dispose();
            }

另外,您是否正在加载 Texture2D dirt = Content.Load<Texture2D>(t.texture);每一帧?它看起来像......不要那样做!这将导致加载数百个图块的巨大延迟,每秒数百次!而是制作一个全局纹理Texture2D DirtDexture并在您的LoadContent()方法中执行DirtTexture = Content.Load<Texture2D>(t.texture); 现在绘制时您可以执行spriteBatch.Draw(DirtTexture,...

spriteBatch = new SpriteBatch(GraphicsDevice);和 _ gridImage = new RenderTarget2D(GraphicsDevice, 1000, 1000);

您不需要每帧都创建新的 RenderTarget 和 Spritebatch!只需在Initialize()方法中执行!

另请参阅RenderTarget2DXNA RenderTarget 示例有关使用渲染目标的更多信息

编辑:我意识到这一切都在 LoadContent 中,我没有看到因为格式被搞砸了,记得在你的 Draw Method 中添加你的 Foreach(Tile 等)

于 2013-01-02T00:43:56.890 回答