1
\$\begingroup\$

Though it works I wonder if there might be a better way to do this. I would like to reuse this loading and establishing of fields of another class(es) (the static TileMapper class mentioned at the end) in a C#/.net/XNA environment.

I noticed that when moving through the OpenFileDialog to find files it felt sluggish. Is that because I had to set [STAThreadAttribute] just to get it to open?

private void loadMapToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFile = new OpenFileDialog();
        openFile.Title = "Select a File";
        openFile.InitialDirectory = Application.StartupPath + @"\Content\Map\";
        openFile.Filter = "MAP Files (.MAP)|*.MAP";
        openFile.FilterIndex = 1;
        openFile.Multiselect = false;

        if (openFile.ShowDialog() != DialogResult.Cancel)
        {
            try
            {
                // Example file - _W25_H25_SET_GL_test.Map
                TileMapper.LoadMap(new FileStream(openFile.FileName, FileMode.Open));
                // remove directory information
                string[] _mapname = openFile.FileName.Split('\\');
                string tempmap = _mapname.Last();
                // remove .map
                string[] removemap = tempmap.Split('.'); 
                MapName = removemap.First();
                //print file name
                txtMapName.Text = MapName;
                //get info stored in file name
                string[] nameParts = MapName.Split('_');
                string[] widthPart = nameParts[1].Split('W');
                string[] heightPart = nameParts[2].Split('H');

                TileMapper.MapWidth = int.Parse(widthPart[1]);
                TileMapper.MapHeight = int.Parse(heightPart[1]);
            }
            catch
            {
                System.Diagnostics.Debug.Print("Unable to load map file");
            }
        }
        else return;
     }
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I noticed that when moving through the OpenFileDialog to find files it felt sluggish. Is that because I had to set [STAThreadAttribute] just to get it to open?

That is a StackOverflow or GameDev.StackExchange kind of question.

Refactoring the code like this could be a start:

private void loadMapToolStripMenuItem_Click(object sender, EventArgs e)
{
    var openFile = new OpenFileDialog
                       {
                           Title = "Select a File",
                           InitialDirectory = Application.StartupPath + @"\Content\Map\",
                           Filter = "MAP Files (.MAP)|*.MAP",
                           FilterIndex = 1,
                           Multiselect = false
                       };

    if (openFile.ShowDialog() != DialogResult.Cancel)
    {
        try
        {
            string otherMapName;

            LoadMap(TileMapper, openFile.FileName, out otherMapName);

            txtMapName.Text = otherMapName;
        }
        catch
        {
            //TODO: display an error message box?
            System.Diagnostics.Debug.Print("Unable to load map file");
        }
    }
}

//TODO: why not move this method inside TileMapper?
private void LoadMap(TileMapper tileMapper, string fileName, out string mapName)
{
    mapName = null;

    // Example file - _W25_H25_SET_GL_test.Map
    tileMapper.LoadMap(new FileStream(fileName, FileMode.Open));

    mapName = Path.GetFileNameWithoutExtension(fileName);

    //get info stored in file name
    string[] nameParts = mapName.Split('_');
    string[] widthPart = nameParts[1].Split('W');
    string[] heightPart = nameParts[2].Split('H');

    tileMapper.MapWidth = int.Parse(widthPart[1]);
    tileMapper.MapHeight = int.Parse(heightPart[1]);
}
\$\endgroup\$
3
  • \$\begingroup\$ TileMapper is a static class which does contain the LoadMap method hence the call TileMapper.LoadMap which contains a deserializer etc. Or is that something I was doing wrong? I guess what I felt was the worst in the code was the use of 5 string arrays to extract specifics defined by the filename itself \$\endgroup\$
    – Dialock
    Commented Apr 30, 2012 at 18:29
  • \$\begingroup\$ 1. Your method of storing width and height in file name looks weird - why not serialize them into the file content? 2. Try to avoid using static classes with state. You can create instances of common classes in Game class and pass them to all users. Read about SOLID principles. \$\endgroup\$
    – Den
    Commented Apr 30, 2012 at 20:45
  • \$\begingroup\$ 1. True, the grand idea was that the filenames would be easier for me when connecting maps as the name contains its size as well as what tile set it was based on. \$\endgroup\$
    – Dialock
    Commented May 1, 2012 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.