The main game code
Наследование: TomShane.Neoforce.Controls.Application
Пример #1
0
        static void Main(string[] args)
        {
            //Setup forms stuff, to possibly be used if an error occurs and a dialog needs to be opened
            System.Windows.Forms.Application.EnableVisualStyles();
            System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

            #if DEBUG
                using (Game game = new Game())
                {
                    game.Run(); //Run the game
                }
            #else
                try
                {
                    using (Game game = new Game())
                    {
                        game.Run(); //Run the game and catch all errors
                    }
                }
                catch (Exception e)
                {
                    //Open all exceptions in an error dialog
                    System.Windows.Forms.Application.Run(new ExceptionForm(e));
                }
                #endif
        }
Пример #2
0
        /// <summary>
        /// Finds content packs and loads the selected one
        /// </summary>
        public static void LoadContentPacks(Game game)
        {
            try
            {
                //Load Content Packs
                List<string> dirs = new List<string>();
                //Load embedded pack directories from Content folder
                dirs.Add(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Content\\Content Packs\\Default");
                //Load packs from Zarknorth folder
                dirs.AddRange(Directory.GetDirectories(Directories["Content Packs"]).ToList<string>());

                //Foreach directory
                for (int x = 0; x < dirs.Count(); x++)
                {
                    string dir = dirs[x];

                    //Load the packs xml data
                    XmlDocument doc = new XmlDocument();
                    doc.Load(dir + "\\pack.xml");

                    //Create the new pack and set it's data
                    ContentPack pack = new ContentPack();
                    pack.Name = doc.ChildNodes[1].ChildNodes[0].ChildNodes[0].Value;
                    pack.Description = doc.ChildNodes[1].ChildNodes[1].ChildNodes[0].Value;
                    pack.Version = doc.ChildNodes[1].ChildNodes[2].ChildNodes[0].Value;
                    pack.Author = doc.ChildNodes[1].ChildNodes[3].ChildNodes[0].Value;
                    pack.Path = dir;
                    pack.Embedded = pack.Name == "Default";

                    //If an embedded pack, load from ContentManager, else FromStream
                    if (pack.Embedded)
                        pack.Icon = Game.TextureLoader.Content.Load<Texture2D>("Content\\Content Packs\\" + pack.Name + "\\icon");
                    else
                        using (FileStream fileStream = new FileStream(dir + "\\icon.png", FileMode.Open))
                            pack.Icon = Texture2D.FromStream(game.GraphicsDevice, fileStream);

                    //Add the pack
                    ContentPacks.Add(pack);

                    //If this pack is the current pack, set the game's data to it, so it is aware of the pack to use
                    if (pack.Name == Game.ContentPackName)
                    {
                        Game.ContentPackIndex = x;
                        Game.ContentPackData = pack;
                    }
                }
                //Load the current pack
                ContentPack.LoadPack(Game.ContentPackData);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.ToString());
            }
        }
Пример #3
0
 /// <summary>
 /// Applies loading settings (Handles logic)
 /// </summary>
 /// <param name="settings"></param>
 private static void ApplySettings(Settings settings, Game game)
 {
     Game.Username = settings.Username;
     Game.ContentPackName = settings.ContentPack;
     Game.MyHue = settings.Color;
     Game.MyColor = Cyral.Extensions.Xna.ColorExtensions.ColorFromHSV(settings.Color, GlobalSettings.ColorSaturation, GlobalSettings.ColorValue);
     Game.Resolution = new Microsoft.Xna.Framework.Rectangle(0, 0, settings.Resolution.X, settings.Resolution.Y);
     game.Graphics.PreferredBackBufferWidth = Game.Resolution.Width;
     game.Graphics.PreferredBackBufferHeight = Game.Resolution.Height;
     game.Graphics.SynchronizeWithVerticalRetrace = settings.UseVSync;
     game.Graphics.ApplyChanges();
 }
Пример #4
0
 private static void SaveAndApplyDefault(Game game)
 {
     SaveSettings(Settings.GetDefaultSettings());
     ApplySettings(Settings.GetDefaultSettings(), game);
 }
Пример #5
0
 /// <summary>
 /// Opens the settings file and loads the values
 /// </summary>
 public static void LoadSettings(Game game)
 {
     try
     {
         Settings settings;
         //If config does not exist, create it and write the default settings
         if (!File.Exists(configFile)) {
             SaveAndApplyDefault(game);
             return;
         }
         string json = File.ReadAllText(configFile);
         //If config is empty, regenerate it
         if (string.IsNullOrWhiteSpace(json)) {
             SaveAndApplyDefault(game);
             return;
         }
         settings = JsonConvert.DeserializeObject<Settings>(json);
         ApplySettings(settings, game);
     }
     catch (Exception ex)
     {
     #if DEBUG
         throw ex;
     #else
         System.Windows.Forms.MessageBox.Show(ex.Message + "\nTry deleting your config file!", game.Window.Title + " Configuration Error");
         //Default fallback settings
         ApplySettings(Settings.GetDefaultSettings(), game);
     #endif
     }
 }