February 8th, 2007 by Sharky

kick it on GameDevKicks.com 

Ultrahead has blogged a wee post seeding some discussion on acceptable texture sizes. Already some good info there in the comments, so definitely one to watch.

“Air Legends” progress…

Lately I’ve been looking at making a proper Installer for “Air Legends”. C# express does not seem to have Installer projects, and I am not a fan of Visual Studio 2005’s installer projects. So I’m currently looking at using Microsoft’s Windows Installer XML (aka. “WiX”).

Shawn Hargreaves helped me solve the problem with my “not ready for public consumption” screenshot feature in “Air Legends”. As it turns out, it actually renders fine on some hardware, but not others. The solution at the moment is not to save in an image format that supports transparency. BMP format works fine. It’ll be fixed in the next release…sometime…when it feels right.

Screenshot code snippet…

Meanwhile here is my screenshot saving code… Feel free to use it. It is based on the XBOX 360 screenshot code Shawn Hargreaves blogged about recently. I tailored it for Windows since I didn’t need all the XBOX bits right now.

Not much to say about it really. It exists inside my Game class, and does its best to come up with a unique filename (based on time and date). The string _screenshotPath would be defined earlier and perhaps populated from configuration. If not, it will default itself to a “Screenshots” subdirectory off the game’s executable location.

 

public void TakeScreenshot()
{
        IGraphicsDeviceService gs = (IGraphicsDeviceService)this.Services.GetService(typeof(IGraphicsDeviceService));
        GraphicsDevice device = gs.GraphicsDevice;
        int w = device.PresentationParameters.BackBufferWidth;
        int h = device.PresentationParameters.BackBufferHeight;
        using (Texture2D screenshot = new Texture2D(device, w, h, 1,
                                                    ResourceUsage.ResolveTarget,
                                                    SurfaceFormat.Color,
                                                    ResourceManagementMode.Manual))
        {
            device.ResolveBackBuffer(screenshot);
            string fileName = "";
            if (_screenshotPath == null)
            {
                _screenshotPath = Application.StartupPath;
                if (!System.IO.Directory.Exists(_screenshotPath + "\\screenshots"))
                    System.IO.Directory.CreateDirectory(_screenshotPath + "\\screenshots");
                _screenshotPath += "\\screenshots";
            }
            else
            {
                //make sure path is valid
                if (!System.IO.Directory.Exists(_screenshotPath))
                {
                    try
                    {
                        System.IO.Directory.CreateDirectory(_screenshotPath);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
            string timestampName;
            timestampName = DateTime.Now.ToString("yyyyMMdd_HHmmss");
            string[] files;
            files = Directory.GetFiles(_screenshotPath, string.Format("{0}.*", timestampName));
            if (files.Length > 0)
            {
                string tryName = timestampName;
                for (int seq = 0; files.Length > 0; seq++)
                {
                    tryName = String.Format("{0}_{1}", timestampName, seq);
                    files = Directory.GetFiles(_screenshotPath, string.Format("{0}.*", tryName));
                }
                timestampName = tryName;
            }
            fileName = String.Format("{0}\\{1}.bmp", _screenshotPath, timestampName);
            screenshot.Save(fileName, ImageFileFormat.Bmp);
        }
}

Average Rating: 4.9 out of 5 based on 210 user reviews.

3 Responses to “”

  1. XNAtutorial.com » Weekly Update - SpaceWar Competition Says:

    [...] Lawrence tweaked Shawn Hargreaves screenshot component, so now it works for Windows. Also, he updated Sharky’s Air Legends, so if it used to crash on you, try again. [...]

  2. Rick Says:

    I get errors when I try to compile “unrecognized escape sequence”, anyone else received this message?

  3. Sharky Says:

    I’m not sure Rick.

    Are you trying with XNA 1.0 or 2.0?

    I’m still converting my stuff to XNA 2.0, but I haven’t come across that one yet.

Leave a Reply