1
\$\begingroup\$

[SOLVED] - See linked post in the P.S.

I have a set of Texture2Ds generated in the Load method with this function:

Texture Generation:

private Texture2D[,] createTextureSet(TileInfo[,] map)
    {
        Texture2D[,] texSet = new Texture2D[map.GetLength(0), map.GetLength(1)];

        using (SpriteBatch sprb = new SpriteBatch(GraphicsDevice))
        {
            for (int row = 0; row < map.GetLength(0); row++)
            {
                System.Threading.Thread.Sleep(10);
                for (int col = 0; col < map.GetLength(1); col++)
                {
                    RenderTarget2D renderer = new RenderTarget2D(this.GraphicsDevice, 300, 300);
                    System.Threading.Thread.Sleep(2);
                    GraphicsDevice.SetRenderTarget(renderer);
                    renderer.Name = map[row, col].buildings[0].buildingType;
                    GraphicsDevice.Clear(Color.Transparent);
                    sprb.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
                    sprb.Draw(TextureFromFile(resourceDirectory + @"Images\Tile Backgrounds\" + map[row, col].tileType + @"\" + map[row, col].tileBackground + ".png"), renderer.Bounds, Color.White);
                    sprb.Draw(TextureFromFile(resourceDirectory + @"Images\Buildings\" + map[row, col].buildings[0].buildingType + ".png"), (Vector2)map[row, col].buildings[0].pointWithinTile, Color.White);
                    sprb.End();
                    texSet[row, col] = renderer;
                }
            }
        }

        GraphicsDevice.SetRenderTarget(null);
        return texSet;
    }

The tiles are drawn on the screen using the following code:

Texture Rendering

for (int tx = -1; tx < 2; tx++)
        {
            for (int ty = -1; ty < 2; ty++)
            {
                gameSpriteBatch.Draw(mapTextureSet[gameCharacter.characterTile.tileX + tx, gameCharacter.characterTile.tileY + ty], determinePositionToDrawTile(gameCharacter, map[gameCharacter.characterTile.tileX + tx, gameCharacter.characterTile.tileY + ty], tx, ty, resizeX, resizeY), null, Color.White, 0f, Vector2.Zero, new Vector2(resizeX, resizeY), SpriteEffects.None, 0f);
            }
         }

These textures are then drawn on the screen based on where on the map the player is. The game runs fine, until the game window is resized by the user. After that, only the character sprite remains on the screen during the redraws, and the screen remains the default Color.BlanchedAlmond background of my game. Any help is thanked in advance.

[ ! ] P.S. [ ! ]

Before marking this as a duplicate! I have looked at this and tried most of the applicable solutions from it, none of which have worked, so I assume that this error is deeper than that one.

\$\endgroup\$
6
  • \$\begingroup\$ Why are you calling Thread.Sleep? \$\endgroup\$ – Andrew Russell Feb 20 '14 at 1:21
  • \$\begingroup\$ Is that still in there? Oh, it is. This is supposed to randomly generate textures using a Random(), but it was always getting the same texture, so I added the Sleep() to see if giving it a delay would allow the Random.Next() time to change, but it didn't. I guess I just left it in there, I'll take it out. \$\endgroup\$ – ApachePilotMPE Feb 20 '14 at 1:28
  • \$\begingroup\$ Are you using Random correctly? See this answer. \$\endgroup\$ – Andrew Russell Feb 20 '14 at 1:30
  • \$\begingroup\$ Well, I've fixed the Random texture issue, its now just the resize/minimize texture error that's the problem. \$\endgroup\$ – ApachePilotMPE Feb 20 '14 at 1:30
  • \$\begingroup\$ What does your TextureFromFile method do? Also, my SO answer that you linked does seem to exactly describe the problem you are having. Did you try implementing "Option 3" from that answer? Can you show the code you used? \$\endgroup\$ – Andrew Russell Feb 20 '14 at 1:34
1
\$\begingroup\$

I'm pretty sure that this is exactly the problem described and solved in my answer that you linked. In which case, "Option 3" in that answer is probably the easiest way to get your code working.

(And, indeed, from comments, it seems like that has worked for you.)

But to finish what I was typing up, you can verify that this is the problem by adding this code right after you create your RenderTarget2D renderer:

renderer.ContentLost += (s,a) => System.Diagnostics.Debug.Assert(false);

If that assertion triggers, then you know that your texture has gone missing due to the device reset.

\$\endgroup\$
1
  • \$\begingroup\$ Oh, thanks! If I run into this again, I'll be sure to remember that! \$\endgroup\$ – ApachePilotMPE Feb 20 '14 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.