summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-05-02 00:43:15 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-05-02 00:43:15 -0400
commite4357c3c7d0bb3de0494e7c3547ae0dd39ee966b (patch)
tree551743b5f3f384336c5594c9a250156da1511984 /src/StardewModdingAPI
parent482a91962ac02cf83c2647fd7e5ba8627bd0bb0b (diff)
downloadSMAPI-e4357c3c7d0bb3de0494e7c3547ae0dd39ee966b.tar.gz
SMAPI-e4357c3c7d0bb3de0494e7c3547ae0dd39ee966b.tar.bz2
SMAPI-e4357c3c7d0bb3de0494e7c3547ae0dd39ee966b.zip
fix error when using content API to load a PNG during early game init (#280)
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r--src/StardewModdingAPI/Framework/ContentHelper.cs24
1 files changed, 12 insertions, 12 deletions
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);
}