Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Game" /> class.
        /// </summary>
        public Game()
        {
            // Internals
            drawableGameSystems          = new List <IDrawable>();
            currentlyContentGameSystems  = new List <IContentable>();
            currentlyDrawingGameSystems  = new List <IDrawable>();
            pendingGameSystems           = new List <IGameSystem>();
            updateableGameSystems        = new List <IUpdateable>();
            currentlyUpdatingGameSystems = new List <IUpdateable>();
            contentableGameSystems       = new List <IContentable>();
            contentCollector             = new DisposeCollector();
            gameTime                 = new GameTime();
            totalGameTime            = new TimeSpan();
            timer                    = new TimerTick();
            IsFixedTimeStep          = false;
            maximumElapsedTime       = TimeSpan.FromMilliseconds(500.0);
            TargetElapsedTime        = TimeSpan.FromTicks(10000000 / 60); // target elapsed time is by default 60Hz
            lastUpdateCount          = new int[4];
            nextLastUpdateCountIndex = 0;

            // Calculate the updateCountAverageSlowLimit (assuming moving average is >=3 )
            // Example for a moving average of 4:
            // updateCountAverageSlowLimit = (2 * 2 + (4 - 2)) / 4 = 1.5f
            const int BadUpdateCountTime = 2; // number of bad frame (a bad frame is a frame that has at least 2 updates)
            var       maxLastCount       = 2 * Math.Min(BadUpdateCountTime, lastUpdateCount.Length);

            updateCountAverageSlowLimit = (float)(maxLastCount + (lastUpdateCount.Length - maxLastCount)) / lastUpdateCount.Length;

            // Externals
            Services         = new GameServiceRegistry();
            Content          = new ContentManager(Services);
            LaunchParameters = new LaunchParameters();
            GameSystems      = new GameSystemCollection();

            // Create Platform
            gamePlatform                = GamePlatform.Create(this);
            gamePlatform.Activated     += gamePlatform_Activated;
            gamePlatform.Deactivated   += gamePlatform_Deactivated;
            gamePlatform.Exiting       += gamePlatform_Exiting;
            gamePlatform.WindowCreated += GamePlatformOnWindowCreated;

            // By default, add a FileResolver for the ContentManager
            Content.Resolvers.Add(new FileSystemContentResolver(gamePlatform.DefaultAppDirectory));

            // Setup registry
            Services.AddService(typeof(IServiceRegistry), Services);
            Services.AddService(typeof(IContentManager), Content);
            Services.AddService(typeof(IGamePlatform), gamePlatform);

            // Register events on GameSystems.
            GameSystems.ItemAdded   += GameSystems_ItemAdded;
            GameSystems.ItemRemoved += GameSystems_ItemRemoved;

            IsActive = true;
        }
Ejemplo n.º 2
0
        private void DXControl_Loaded(object sender, EventArgs args)
        {
            _services = new GameServiceRegistry();
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                InitializeGraphicsDevice();
                InitializeContentManager();

                if (LoadContent != null)
                    LoadContent(this, new DXGraphicsDeviceEventArgs(GraphicsDevice));
            }
            _imageSourcePresenter = new RenderTargetGraphicsPresenter(GraphicsDevice, _imageSource.RenderTarget);
            CompositionTarget.Rendering += CompositionTarget_Rendering;
            _ready = true;
        }