示例#1
0
        protected override void Initialize()
        {
            GraphicsAdapter.UseDriverType = GraphicsAdapter.DriverType.Hardware;
            //MSAA (Multi-sample Anti Aliasing as requested by lempamo)
            _graphicsDevice.GraphicsProfile     = GraphicsProfile.HiDef;
            _graphicsDevice.PreferMultiSampling = true;
            GraphicsDevice.PresentationParameters.MultiSampleCount = 8; //8x MSAA, should be a configurable thing
            _graphicsDevice.ApplyChanges();

            _instance = this;

            Logger.Log("Peace Engine is now initializing core engine components...");
            List <Type> typesToInit = new List <Type>();

            foreach (var type in ReflectMan.Types.Where(x => x.Assembly == this.GetType().Assembly&& x.GetInterfaces().Contains(typeof(IEngineModule))))
            {
                if (type.GetConstructor(Type.EmptyTypes) == null)
                {
                    Logger.Log($"Found {type.Name}, but it doesn't have a parameterless constructor, so it's ignored.  Probably a mistake.", System.ConsoleColor.Yellow);
                    continue;
                }
                Logger.Log($"Found {type.Name}", System.ConsoleColor.Yellow);
                typesToInit.Add(type);
            }
            foreach (var type in typesToInit)
            {
                var componentInfo = new ComponentInfo
                {
                    IsInitiated = false,
                    Component   = (IEngineModule)Activator.CreateInstance(type, null)
                };
                _components.Add(componentInfo);
            }
            foreach (var component in _components)
            {
                Logger.Log($"{component.Component.GetType().Name}: Injecting dependencies...");
                Inject(component.Component);
            }
            //I know. This is redundant. I'm only doing this as a safety precaution, to prevent crashes with modules that try to access uninitiated modules as they're initiating.
            foreach (var component in _components)
            {
                RecursiveInit(component.Component);
            }
            Logger.Log("Done initiating engine.");

            ResetMouseListener();

            base.Initialize();
        }
示例#2
0
        private void LoadGame()
        {
            _status          = "Retrieving types to load...";
            this._percentage = 0f;
            Logger.Log("Peace Engine is now initializing your game.");
            List <Type> typesToInit = new List <Type>();

            foreach (var type in ReflectMan.Types.Where(x => x.Assembly != this.GetType().Assembly&& x.GetInterfaces().Contains(typeof(IEngineModule))))
            {
                if (type.GetConstructor(Type.EmptyTypes) == null)
                {
                    Logger.Log($"Found {type.Name}, but it doesn't have a parameterless constructor, so it's ignored.  Probably a mistake.", System.ConsoleColor.Yellow);
                    continue;
                }
                Logger.Log($"Found {type.Name}", System.ConsoleColor.Yellow);
                typesToInit.Add(type);
                _status = "Retrieving types to load... [" + typesToInit.Count + "]";
            }
            _status = "Constructing components...";
            foreach (var type in typesToInit)
            {
                _componentLoaded.WaitOne();
                _componentLoaded.Reset();
                Invoke(() =>
                {
                    var componentInfo = new ComponentInfo
                    {
                        IsInitiated = false,
                        Component   = (IEngineModule)Activator.CreateInstance(type, null)
                    };
                    _components.Add(componentInfo);
                    _percentage = (float)typesToInit.IndexOf(type) / typesToInit.Count;
                    _componentLoaded.Set();
                });
            }
            _componentLoaded.WaitOne();
            _status = "Injecting dependencies...";
            var componentsToInject = _components.Where(x => x.IsInitiated == false).ToList();

            foreach (var component in componentsToInject)
            {
                _componentLoaded.Reset();
                Invoke(() =>
                {
                    Logger.Log($"{component.Component.GetType().Name}: Injecting dependencies...");
                    Inject(component.Component);
                    _percentage = (float)componentsToInject.IndexOf(component) / componentsToInject.Count;
                    _componentLoaded.Set();
                });
                _componentLoaded.WaitOne();
            }
            _status = "Initializing components...";
            //I know. This is redundant. I'm only doing this as a safety precaution, to prevent crashes with modules that try to access uninitiated modules as they're initiating.
            foreach (var component in componentsToInject)
            {
                _componentLoaded.WaitOne();
                _componentLoaded.Reset();
                Invoke(() =>
                {
                    RecursiveInit(component.Component);
                    _percentage = (float)componentsToInject.IndexOf(component) / componentsToInject.Count;
                    _componentLoaded.Set();
                });
            }
            _status = "Loading assets...";
            foreach (var component in componentsToInject)
            {
                _componentLoaded.WaitOne();
                _componentLoaded.Reset();
                Invoke(() =>
                {
                    if (component.Component is ILoadable)
                    {
                        (component.Component as ILoadable).Load(Content);
                    }
                    _percentage = (float)componentsToInject.IndexOf(component) / componentsToInject.Count;
                    _componentLoaded.Set();
                });
            }

            Logger.Log("Done initiating engine.");

            Invoke(() =>
            {
                if (StartingSceneType != null)
                {
                    _scene = (GameScene)New(StartingSceneType);
                    _scene.Load();
                }
            });
        }