// Another overload, does the same as above, but also takes the Sprite object we're loading
 // into as an argument, so that it can set the height and width of the sprite.
 public int loadImage(string filename, Sprite spr, bool tile = false)
 {
     try
     {
         Bitmap file = new Bitmap(filename);
         spr.Width = file.Width;
         spr.Height = file.Height;
         return loadImage(file, tile);
     }
     catch (FileNotFoundException e)
     {
         return -1;
     }
 }
        private void updateRenderList()
        {
            // Load gamesObjects into room, taking their z-depth to slot them into the correct slot in our dictionary
            // Also set their starting positions
            foreach (Vector3 vec in currentRoom.Objects.Keys)
            {
                // Note to self: ERROR here, must check if key is there first.

                Sprite spr = new Sprite(); // currentRoom.Objects[vec].sprite;
                string objName = currentRoom.Objects[vec].getName();
                objName = objName.Remove(objName.IndexOf('.'));

                // See if a sprite has been loaded for this object yet
                if (!textures.ContainsKey(objName))
                {
                    // Nope, so generate a sprite for it
                    spr = loadSprite(objName, objectSprites[objName]);
                    currentRoom.Objects[vec].sprite = spr;
                }
                else if (currentRoom.Objects[vec].sprite == null)
                {
                    // Resuse an old sprite
                    spr.TextureID = textures[objName];
                    spr.Width = (int) texSizes[objName].X;
                    spr.Height = (int)texSizes[objName].Y;
                    currentRoom.Objects[vec].sprite = spr;
                }

                // Check if our dictionary has an entry for the current draw depth yet
                if (!objects.ContainsKey((int)vec.Z))
                {
                    // It doesn't, so create a new list for depth Z
                    objects.Add((int)vec.Z, new List<Sprite>());

                    // Add this object to the new list
                    objects[(int)vec.Z].Add(currentRoom.Objects[vec].sprite);
                    currentRoom.Objects[vec].sprite.loaded = true; // prevent double-loading
                }
                else
                {
                    // Add this object to the correct list if not there already
                    if (currentRoom.Objects[vec].sprite.loaded == false)
                    {
                        // Prevent double-loading
                        currentRoom.Objects[vec].sprite.loaded = true;
                        objects[(int)vec.Z].Add(currentRoom.Objects[vec].sprite);
                    }
                }
            }
        }
        private void glRoomView_MouseDown(object sender, MouseEventArgs e)
        {
            // First, get the coordinate of the click
            Point clickPt = glRoomView.PointToClient(MousePosition);
            clickPt.Y = glRoomView.Height - clickPt.Y; // Convert ref from upper left corner to lower left

            // Clear previous selection
            selectedSpr = null;
            selectedObj = null;

            // Check if the point is inside
            foreach (GameObject obj in currentRoom.Objects.Values)
            {
                if (obj.IsInside(clickPt))
                {
                    // Select the object for editing
                    selectedSpr = obj.sprite;
                    selectedObj = obj;

                    // Store the original position of the object before moving it
                    // so that the dictionaries can be accessed and updated
                    originalPos = new Vector3(selectedObj.getMinX(), selectedObj.getMinY(), selectedObj.depth);

                    // Display selected object's data in Room Viewer
                    txtXPos.Text = obj.getMinX().ToString();
                    txtYPos.Text = obj.getMinY().ToString();

                    mouseDown = true;

                    // Redraw so user can see selected object
                    glRoomView.Invalidate();
                    glRoomView.Update();

                    // break out to avoid conflicts
                    break;
                }
            }
        }
        // Function to load a texture for a given gameobject
        private Sprite loadSprite(string obj, string sprPath)
        {
            // Create a helper object so we can access the sprite loading functions
            SpriteLoader loader = new SpriteLoader();

            // Create a new sprite object
            Sprite spr = new Sprite();

            // Using the passed object's name as a key
            textures.Add(obj, loader.loadImage(sprPath, spr));
            spr.TextureID = textures[obj];
            texSizes.Add(obj, new Vector2(spr.Width, spr.Height));

            return spr;
        }