/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public void Update(GameTime gameTime) { // Get input from the player inputController.Update(); // screencaps if (inputController.ScreenCap.JustPressed) { Draw(gameTime); Texture2D texture = new Texture2D(graphicsDevice, GraphicsConstants.VIEWPORT_WIDTH, GraphicsConstants.VIEWPORT_HEIGHT); Color[] data = new Color[texture.Width * texture.Height]; graphicsDevice.GetBackBufferData<Color>(data); texture.SetData<Color>(data); Stream stream = File.OpenWrite("output.png"); texture.SaveAsPng(stream, texture.Width, texture.Height); stream.Dispose(); texture.Dispose(); } if (currentContext.Exit || currentContext.NextContext != null) { targetOverlayAlpha = 1; if (currentOverlayAlpha == 1) { //audioPlayer.StopSong(); if (currentContext.Exit) exitGame = true; else if (!asyncStarted) { asyncStarted = true; asyncFinished = false; InitializeContextComponents(currentContext.NextContext); asyncFinished = true; } if (asyncStarted && asyncFinished) { //Thread.Sleep(1000); asyncStarted = false; targetOverlayAlpha = 0; currentContext.Dispose(); currentContext = currentContext.NextContext; } } } else { currentContext.Update(gameTime); } currentOverlayAlpha = MathHelper.Lerp(currentOverlayAlpha, targetOverlayAlpha, currentContext.FadeMultiplier); //Console.WriteLine(Math.Abs(currentOverlayAlpha - targetOverlayAlpha)); if (Math.Abs(currentOverlayAlpha - targetOverlayAlpha) < 0.001f || inputController.Zoom.Pressed) currentOverlayAlpha = targetOverlayAlpha; }
void InitializeContextComponents(GameContext gameContext) { if (gameContext == null) return; gameContext.AssetManager = assets; gameContext.AudioPlayer = audioPlayer; gameContext.Canvas = canvas; gameContext.DataCenter = fileManager; gameContext.InputController = inputController; gameContext.Initialize(); }
/// <summary> /// Should be called in load content. /// </summary> public void Initialize(GameContext initialContext) { canvas = new Canvas(graphics, graphicsDevice); #if DEBUG canvas.DebugMode = true; #else canvas.DebugMode = false; #endif // Initialize audio player. audioPlayer = new AudioPlayer(); assets = new AssetManager(); exitGame = false; //launch initialize asynchronously //ThreadPool.QueueUserWorkItem(new WaitCallback(InitializeNextContext)); asyncFinished = true; /*Thread t = new Thread(new ThreadStart(InitializeNextContext)); t.IsBackground = true; t.Start();*/ // load all content assets.LoadContent(content, graphicsDevice); canvas.LoadContent(assets); inputController = new InputController(assets); fileManager = new DataCenter(assets); InitializeContextComponents(initialContext); currentContext = initialContext; inputController.Update(); currentOverlayAlpha = 0; }