private void UnloadAllTextures()
 {
     GL.BindTexture(TextureTarget.Texture2D, 0); // make sure no textures are loaded before deleting
     foreach (System.Collections.Generic.KeyValuePair <string, int> tex in textures)
     {
         OpenTkTextureLoadFuncs.UnloadTexture(tex.Value);
     }
     textures.Clear();
 }
        private void LoadFontTextures()
        {
            for (int i = 0; i < font.PageTexPaths.Length; i++)
            {
                if (font.PageTexPaths[i] == null)
                {
                    continue;
                }

                string texkey  = font.PageTexPaths[i];
                string texpath = font.PageTexPaths[i];

                try
                {
                    if (font.CanLoad8Bit)
                    {
                        if (!textures.ContainsKey(texkey))
                        {
                            textures.Add(texkey, OpenTkTextureLoadFuncs.LoadTexture8BitGrayscale(texpath));
                        }
                        else
                        {
                            textures[texkey] = OpenTkTextureLoadFuncs.LoadTexture8BitGrayscale(texpath);
                        }
                    }
                    else if (!font.CommonInfo.Packed | !font.AreAllGylphsSingleChannel) // if not packed, should always load normally.
                                                                                        // if packed but some glyphs use multiple channels, should load a normal copy too
                    {
                        if (!textures.ContainsKey(texkey))
                        {
                            textures.Add(texkey, OpenTkTextureLoadFuncs.LoadTexture(texpath));
                        }
                        else
                        {
                            textures[texkey] = OpenTkTextureLoadFuncs.LoadTexture(texpath);
                        }
                    }

                    if (font.CommonInfo.Packed)
                    {
                        int[] channelTextures = OpenTkTextureLoadFuncs.LoadTextureToSplitChannels(texpath);

                        if (channelTextures.Length < 4)
                        {
                            throw new Exception("not enough channels in image");
                        }

                        if (!textures.ContainsKey(texkey + "A"))
                        {
                            textures.Add(texkey + "A", channelTextures[0]);
                        }
                        else
                        {
                            textures[texkey + "A"] = channelTextures[0];
                        }

                        if (!textures.ContainsKey(texkey + "R"))
                        {
                            textures.Add(texkey + "R", channelTextures[1]);
                        }
                        else
                        {
                            textures[texkey + "R"] = channelTextures[1];
                        }

                        if (!textures.ContainsKey(texkey + "G"))
                        {
                            textures.Add(texkey + "G", channelTextures[2]);
                        }
                        else
                        {
                            textures[texkey + "G"] = channelTextures[2];
                        }

                        if (!textures.ContainsKey(texkey + "B"))
                        {
                            textures.Add(texkey + "B", channelTextures[3]);
                        }
                        else
                        {
                            textures[texkey + "B"] = channelTextures[3];
                        }
                    }
                }
                catch (Exception e)
                {
                    SkinnedMessageBox.Show(ErrorSkin, DialogResMgr.GetString("MissingTextureError") + "\n(" + texpath + ")", "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    throw e;
                }
            }
        }
        private void SetupTextures()
        {
            UnloadAllTextures();

            foreach (System.Collections.Generic.KeyValuePair <string, string> tex in skin.TexturePaths)
            {
                try
                {
                    if (!textures.ContainsKey(tex.Key))
                    {
                        textures.Add(tex.Key, OpenTkTextureLoadFuncs.LoadTexture(tex.Value));
                    }
                    //else
                    //{
                    //    OpenTkTextureLoadFuncs.UnloadTexture(textures[tex.Key]);
                    //    textures[tex.Key] = OpenTkTextureLoadFuncs.LoadTexture(tex.Value);
                    //}
                }
                catch
                {
                    SkinnedMessageBox.Show(skin, DialogResMgr.GetString("MissingTextureError") + "\n(" + tex.Value + ")", "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    Stop();
                }
            }


            // create an empty texture for using in otherwise unloaded chara icon textures
            // this avoids needing to check for their existence in the rendering loop
            int emptytex = OpenTkTextureLoadFuncs.LoadTransparentRGBATexture();

            for (int i = 0; i < charaicons.Length; i++)
            {
                string iconkey = "spr_Charaicon" + i.ToString();
                string icontex = charaicons[i].ImagePath;

                string typestr   = (charaicons[i].Type + 1).ToString();
                string raritystr = (charaicons[i].Rarity + 1).ToString();

                string frontkey = "sprCharafront" + typestr + raritystr;
                string fronttex = skin.RootDir + "/charaimg/icon" + typestr + "_rare" + raritystr + "_front.png";

                string backkey = "sprCharaback" + typestr + raritystr;
                string backtex = skin.RootDir + "/charaimg/icon" + typestr + "_rare" + raritystr + "_bg.png";


                if (icontex != null && icontex.Length > 0)
                {
                    try
                    {
                        if (!textures.ContainsKey(iconkey))
                        {
                            textures.Add(iconkey, OpenTkTextureLoadFuncs.LoadTexture(icontex));
                        }
                    }
                    catch
                    {
                        SkinnedMessageBox.Show(skin, DialogResMgr.GetString("MissingTextureError") + "\n(" + icontex + ")", "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                        Stop();
                    }
                }
                else
                {
                    // load an empty texture if no icon is specified
                    if (!textures.ContainsKey(iconkey))
                    {
                        textures.Add(iconkey, emptytex);
                    }
                }


                try
                {
                    if (!textures.ContainsKey(frontkey))
                    {
                        textures.Add(frontkey, OpenTkTextureLoadFuncs.LoadTexture(fronttex));
                    }
                }
                catch
                {
                    SkinnedMessageBox.Show(skin, DialogResMgr.GetString("MissingTextureError") + "\n(" + fronttex + ")", "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    Stop();
                }

                try
                {
                    if (!textures.ContainsKey(backkey))
                    {
                        textures.Add(backkey, OpenTkTextureLoadFuncs.LoadTexture(backtex));
                    }
                }
                catch
                {
                    SkinnedMessageBox.Show(skin, DialogResMgr.GetString("MissingTextureError") + "\n(" + backtex + ")", "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    Stop();
                }
            }


            // load empty textures for all unused front and back textures
            // not necessary, so commented (can be restored later if needed)
            //for (int i = 1; i < 4; i++)
            //{
            //    string typestr = i.ToString();

            //    for (int j = 1; j < 4; j++)
            //    {
            //        string raritystr = j.ToString();

            //        string frontkey = "sprCharafront" + typestr + raritystr;
            //        string backkey = "sprCharaback" + typestr + raritystr;

            //        if (!textures.ContainsKey(frontkey))
            //            textures.Add(frontkey, emptytex);

            //        if (!textures.ContainsKey(backkey))
            //            textures.Add(backkey, emptytex);
            //    }
            //}
        }