summaryrefslogtreecommitdiff
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
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)
-rw-r--r--release-notes.md6
-rw-r--r--src/StardewModdingAPI/Framework/ContentHelper.cs24
2 files changed, 18 insertions, 12 deletions
diff --git a/release-notes.md b/release-notes.md
index 56b41214..3f16c2b0 100644
--- a/release-notes.md
+++ b/release-notes.md
@@ -10,6 +10,12 @@ For mod developers:
images).
-->
+## 1.12
+See [log](https://github.com/Pathoschild/SMAPI/compare/1.11...1.12).
+
+For mod developers:
+* Fixed error when using content API to load a PNG during early game init (e.g. in mod's `Entry`).
+
## 1.11
See [log](https://github.com/Pathoschild/SMAPI/compare/1.10...1.11).
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);
}