0
\$\begingroup\$

Ok, I've got a weird one here. I'm trying to make a basic tile engine using a windows form, but some of my code is just...not happening. I'll post the chunks in question.

private void MapEditor_Load(object sender, EventArgs e)
    {
        LoadImageList();
        FixScrollBarScales();
        cboCodeValues.Items.Clear();
        cboCodeValues.Items.Add("ENEMY");
        cboCodeValues.Items.Add("CHEST");
        cboCodeValues.Items.Add("NPC");

        for (int x = 0; x < 100; x++)
            cboMapNumber.Items.Add(x.ToString().PadLeft(3, '0'));

        cboMapNumber.SelectedIndex = 0;
        TileMap.EditorMode = true;
        backgroundToolStripMenuItem.Checked = true;
    }

This should be called when the form loads, right? The code dives into LoadImageList(), which contains:

private void LoadImageList()
    {
        string filepath = Application.StartupPath + @"\Content\Textures\IndoorTileSet.png";
        Bitmap tileSheet = new Bitmap(filepath);
        int tilecount = 0;
        for(int y = 0; y < tileSheet.Height / TileMap.TileHeight; y++)
        {
            for(int x = 0; x < tileSheet.Width / TileMap.TileWidth; x++)
            {
                Bitmap newBitmap = tileSheet.Clone(new System.Drawing.Rectangle(x * TileMap.TileWidth, y * TileMap.TileHeight, 
                    TileMap.TileWidth, TileMap.TileHeight), System.Drawing.Imaging.PixelFormat.DontCare);
                imgListTiles.Images.Add(newBitmap);
                string itemName = "";
                if(tilecount == 0)
                    itemName = "Empty";
                if(tilecount == 1)
                    itemName = "Floor";
                listTiles.Items.Add(new ListViewItem(itemName, tilecount++));
            }
        }
    }

The bitmap loads correctly, but then the entire MapEditor_Load method just stops working. tileCount seems to be a local variable in the debugger, and its value is 0, but the debugger never executes the breakpoint on the line which it is assigned. I have absolutely no idea why it would do this, and it's driving me nuts. Any help? Oh, I put the bitmap load in a try/catch block just to see if it was handling an exception in a weird way, but I had no luck. It's not throwing an exception. I began having this problem immediately after replacing my IndoorTileSet with an updated version.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ This feels like it might be a corrupted build; try cleaning and rebuilding the solution in Visual Studio and see if the problem still occurs. \$\endgroup\$ Sep 23, 2011 at 1:46
  • \$\begingroup\$ I know this is probably game related, but I suspect you'd be better off posting this in Stack Overflow, that's where more of the WinForms experts would reside. \$\endgroup\$ Sep 23, 2011 at 3:25
  • \$\begingroup\$ Rebuild didn't do anything differently. Is there some way that making a change to the texture could have messed with something? I don't know enough about the resource pipeline to know exactly how it works, but it seems suspicious that that is when I started having problems. \$\endgroup\$
    – TimBKJ
    Sep 23, 2011 at 3:39
  • \$\begingroup\$ I'll try posting it in Stack Overflow, and see if they've got any ideas. \$\endgroup\$
    – TimBKJ
    Sep 23, 2011 at 3:46
  • \$\begingroup\$ You're probably getting an exception. Try adding some try/catch/MessageBox. \$\endgroup\$ Sep 23, 2011 at 8:20

2 Answers 2

1
\$\begingroup\$

Are you running 64-bit Windows? When you say "stop working" do you mean the program just continues, or does the debugger terminate itself and your process? If you answered "yes," this is what may be biting you:

In 64-bit Windows, the kernel will eat any exceptions that reach a managed/native transition. This often presents itself in managed applications and a failure for exceptions to propagate out of delegates for Load events on forms and such (but of course, since an exception was raised, not all code in the delegate body was executed). You can find more on this issue here.

Your tileset PNG probably can't be loaded by the bitmap loader somehow. One way to see if this is the case may be to move the code out of the load event execution path (just shove it in your main function for a moment) and try to run it, possibly with "Break on Throw" enabled for all exceptions.

\$\endgroup\$
1
  • \$\begingroup\$ This sounds promising, thanks for the info. I'll check it out when I get a chance, and let you know if this was the problem. \$\endgroup\$
    – TimBKJ
    Sep 23, 2011 at 17:13
0
\$\begingroup\$

MapEditor_Load would only get called if you have an event wired up to call that function on the Load event. Rather than implementing it as an event, I'd suggest using an override.

  // Notice your constructor...
  public MapEditor()
  {
     // InitializeComponent contains this line to hook up the Load event
     // this.Load += new System.EventHandler(this.MapEditor_Load);
     // (or may not contain it if your project is broken.)
     InitializeComponent();
  }

  // The load event (when hooked up) references this function,
  // as you can see in the comment above
  private void MapEditor_Load(object sender, EventArgs e)
  {
     // Your code here
  }

  // This is preferred because it avoids the complexity of events
  protected override void OnLoad(EventArgs e)
  {
     base.OnLoad(e);
     // Your code here
  }

Here you can see on the UI where the load event is hooked up: Event Pane showing function that handles Load event

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .