Example #1
0
        public static int Main(string[] args)
        {
            EngineLaunchOptions launchOptions = new EngineLaunchOptions(args);

            ProjectManifest projectManifest;
            string          currentDir   = AppContext.BaseDirectory;
            string          manifestName = null;

            foreach (var file in Directory.EnumerateFiles(currentDir))
            {
                if (file.EndsWith("manifest"))
                {
                    if (manifestName != null)
                    {
                        Console.WriteLine("Error: Multiple project manifests in this directory: " + currentDir);
                        return(-1);
                    }

                    manifestName = file;
                }
            }

            using (var fs = File.OpenRead(manifestName))
                using (var sr = new StreamReader(fs))
                    using (var jtr = new JsonTextReader(sr))
                    {
                        var js = new JsonSerializer();
                        try
                        {
                            projectManifest = js.Deserialize <ProjectManifest>(jtr);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("An error was encountered while loading the project manifest.");
                            Console.WriteLine(e);
                            return(-1);
                        }
                    }

            Game game = new Game();

            AssemblyLoadSystem als = new AssemblyLoadSystem();

            als.LoadFromProjectManifest(projectManifest, AppContext.BaseDirectory);
            game.SystemRegistry.Register(als);

            GraphicsPreferencesProvider graphicsProvider;
            string graphicsProviderName = projectManifest.GraphicsPreferencesProviderTypeName;

            if (graphicsProviderName != null)
            {
                graphicsProvider = GetProvider(als, graphicsProviderName);
            }
            else
            {
                graphicsProvider = new DefaultGraphicsPreferencesProvider();
            }

            var          desiredInitialState = GraphicsPreferencesUtil.MapPreferencesState(graphicsProvider.WindowStatePreference);
            OpenTKWindow window = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                ? (OpenTKWindow) new DedicatedThreadWindow(960, 540, desiredInitialState)
                : new SameThreadWindow(960, 540, desiredInitialState);

            window.Title = "ge.Main";
            GraphicsSystem gs = new GraphicsSystem(window, graphicsProvider);

            game.SystemRegistry.Register(gs);
            window.Closed += game.Exit;

            game.LimitFrameRate = false;

            InputSystem inputSystem = new InputSystem(window);

            inputSystem.RegisterCallback((input) =>
            {
                if (input.GetKeyDown(Key.F4) && (input.GetKey(Key.AltLeft) || input.GetKey(Key.AltRight)))
                {
                    game.Exit();
                }
                if (input.GetKeyDown(Key.F11))
                {
                    window.WindowState = window.WindowState == WindowState.Normal ? WindowState.BorderlessFullScreen : WindowState.Normal;
                }
            });
            game.SystemRegistry.Register(inputSystem);

            SceneLoaderSystem sls = new SceneLoaderSystem(game, game.SystemRegistry.GetSystem <GameObjectQuerySystem>());

            game.SystemRegistry.Register(sls);
            sls.AfterSceneLoaded += () => game.ResetDeltaTime();

            EngineLaunchOptions.AudioEnginePreference?audioPreference = launchOptions.AudioPreference;
            AudioEngineOptions audioEngineOptions =
                !audioPreference.HasValue ? AudioEngineOptions.Default
                : audioPreference == EngineLaunchOptions.AudioEnginePreference.None ? AudioEngineOptions.UseNullAudio
                : AudioEngineOptions.UseOpenAL;
            AudioSystem audioSystem = new AudioSystem(audioEngineOptions);

            game.SystemRegistry.Register(audioSystem);

            ImGuiRenderer imGuiRenderer = new ImGuiRenderer(gs.Context, window.NativeWindow, inputSystem);

            gs.SetImGuiRenderer(imGuiRenderer);

            AssetSystem assetSystem = new AssetSystem(Path.Combine(AppContext.BaseDirectory, projectManifest.AssetRoot), als.Binder);

            game.SystemRegistry.Register(assetSystem);

            BehaviorUpdateSystem bus = new BehaviorUpdateSystem(game.SystemRegistry);

            game.SystemRegistry.Register(bus);
            bus.Register(imGuiRenderer);

            PhysicsSystem ps = new PhysicsSystem(projectManifest.PhysicsLayers);

            game.SystemRegistry.Register(ps);

#if DEBUG
            ConsoleCommandSystem ccs = new ConsoleCommandSystem(game.SystemRegistry);
            game.SystemRegistry.Register(ccs);
#endif

            game.SystemRegistry.Register(new SynchronizationHelperSystem());

            SceneAsset scene;
            AssetID    mainSceneID = projectManifest.OpeningScene.ID;
            if (mainSceneID.IsEmpty)
            {
                var scenes = assetSystem.Database.GetAssetsOfType(typeof(SceneAsset));
                if (!scenes.Any())
                {
                    Console.WriteLine("No scenes were available to load.");
                    return(-1);
                }
                else
                {
                    mainSceneID = scenes.First();
                }
            }

            scene = assetSystem.Database.LoadAsset <SceneAsset>(mainSceneID);
            scene.GenerateGameObjects();

            RunStartupFunctions(projectManifest, als, game);

            game.RunMainLoop();

            return(0);
        }