From e4357c3c7d0bb3de0494e7c3547ae0dd39ee966b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 2 May 2017 00:43:15 -0400 Subject: fix error when using content API to load a PNG during early game init (#280) --- src/StardewModdingAPI/Framework/ContentHelper.cs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src/StardewModdingAPI/Framework') diff --git a/src/StardewModdingAPI/Framework/ContentHelper.cs b/src/StardewModdingAPI/Framework/ContentHelper.cs index 762b7e35..425e0f8c 100644 --- a/src/StardewModdingAPI/Framework/ContentHelper.cs +++ b/src/StardewModdingAPI/Framework/ContentHelper.cs @@ -181,14 +181,13 @@ namespace StardewModdingAPI.Framework throw new NotSupportedException("Can't load a PNG file while the game is drawing to the screen. Make sure you load content outside the draw loop."); // process texture + SpriteBatch spriteBatch = Game1.spriteBatch; + GraphicsDevice gpu = Game1.graphics.GraphicsDevice; using (RenderTarget2D renderTarget = new RenderTarget2D(Game1.graphics.GraphicsDevice, texture.Width, texture.Height)) - using (SpriteBatch spriteBatch = new SpriteBatch(Game1.graphics.GraphicsDevice)) { - //Viewport originalViewport = Game1.graphics.GraphicsDevice.Viewport; - - // create blank slate in render target - Game1.graphics.GraphicsDevice.SetRenderTarget(renderTarget); - Game1.graphics.GraphicsDevice.Clear(Color.Black); + // create blank render target to premultiply + gpu.SetRenderTarget(renderTarget); + gpu.Clear(Color.Black); // multiply each color by the source alpha, and write just the color values into the final texture spriteBatch.Begin(SpriteSortMode.Immediate, new BlendState @@ -214,16 +213,17 @@ namespace StardewModdingAPI.Framework spriteBatch.Draw(texture, texture.Bounds, Color.White); spriteBatch.End(); - // release the GPU - Game1.graphics.GraphicsDevice.SetRenderTarget(null); - //Game1.graphics.GraphicsDevice.Viewport = originalViewport; + // release GPU + gpu.SetRenderTarget(null); - // store data from render target because the RenderTarget2D is volatile + // extract premultiplied data Color[] data = new Color[texture.Width * texture.Height]; renderTarget.GetData(data); - // unset texture from graphic device and set modified data back to it - Game1.graphics.GraphicsDevice.Textures[0] = null; + // unset texture from GPU to regain control + gpu.Textures[0] = null; + + // update texture with premultiplied data texture.SetData(data); } -- cgit