예제 #1
0
        /// <summary>
        /// Creates a new engine with an optional pointer to a draw surface, if none is provided a window is used
        /// </summary>
        /// <param name="game"></param>
        /// <param name="drawSurface"></param>
        public static void Init(Game game, IntPtr?drawSurface = null)
        {
            if (_instance != null)
            {
                throw new InvalidOperationException("Init cannot be called more than once");
            }

            _instance = new Engine();

            // Set engine variables
            Instance.ClearColor  = Color.Black;
            Instance.game        = game;
            game.IsFixedTimeStep = false;
            game.IsMouseVisible  = true;

            // Make sure to stop audio and save the config before exiting
            game.Exiting += (s, a) =>
            {
                AudioManager.Instance.Shutdown();
                ConfigManager.Instance.Write(CONFIG_PATH);
                Environment.Exit(0);
            };

            CultureInfo.CurrentCulture = new CultureInfo("en-GB");
            Instance.Version           = RetrieveLinkerTimestamp().ToString("dd MMM yyyy HH:mm");

            // set up a normal window
            if (drawSurface == null)
            {
                game.Window.Title = $"{ENGINE_NAME} - {Instance.Version}";
                System.Windows.Forms.Form form = (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle(game.Window.Handle);
                form.Location = new System.Drawing.Point(0, 0);
            }
            // set up a custom draw surface
            else
            {
                System.Windows.Forms.Form form = (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle(game.Window.Handle);
                form.Hide();
            }

            // initialise helper classes
            RandomHelper.Init();
            EventInput.Init(game.Window.Handle);

            // set up managers
            AppDomain.CurrentDomain.UnhandledException += (s, a) => Console.WriteLine($"UNHANDLED EXCEPTION: {a.ExceptionObject.ToString()}", MsgType.Failed);
            // run the config file as a script
            ConsoleManager.Instance.RunScript(CONFIG_PATH);
            GraphicsManager.Init(game, drawSurface);

            // set up content manager and load core content
            Instance.Content = new ContentPackManager(game.Content.ServiceProvider, "Content");
            Instance.Content.LoadPack("Core.acp");

            InitGUIDefaults();
            GraphicsHelper.Init(GraphicsManager.Instance.Device);
            RichText.Init();
#if DEBUG
            Console.WriteLine("ASSEMBLY IN DEBUG MODE", MsgType.Warning);
#endif
        }