Dynamic texture2D

Aug 28, 2009 at 9:14 PM

I'm currently working on a project with FlatSilverBall, which is the Silverlight extension of the FlatRedBall engine. It uses the silversprite library. I am trying to accomplish a 2D terrain random generation. I used code from Reimer's 2D shooter tutorial (which is for straight XNA), which uses random numbers and sine waves to determine the heightmap. This is then copied into a Texture2D using the SurfaceFormat.Color. I can get the code to compile fine for FSB, but I get nothing on the screen, and getting into the texture2D during runtime, I see that there is an error for myTexture.Format saying that the SilverArcade.SilverSprite.Graphics.SurfaceFormat.Color throws a NotImplemented exception. Same thing for SilverArcade.SilverSprite.Graphics.TextureUsage.None. Does anybody have an idea as to what SurfaceFormats are implemented yet, or how to go about implementing the SurfaceFormat.Color?

Aug 29, 2009 at 4:22 AM

The Texture2D.Format and Texture2D.TextureUsage properties are not implemented. So it's not that any particular SurfaceFormat is not implemented; the SurfaceFormats are all there, but the Texture2D constructor ignores them, and the readonly Texture2D.Format property throws a NotImplementedException.

I'm not actually on this project, and am only getting familiar with it, so you should wait for someone else to answer your other question, how to go about implementing the properties.

You can always download the source code and play around with it yourself. The Texture2D class is in SilverArcade.SilverSprite/Graphics/Texture2D. You can try creating private members for the format and textureUsage and adjusting the constructor and properties accordingly. If your code doesn't really care what their values are, you can, as a temporary solution, set the relevant properties to return a constant default value (e.g. SurfaceFormat.Color and TextureUsage.None), rather than throwing.

Aug 29, 2009 at 9:58 PM

Thanks for the help. I had been playing around with the Joe Stegman's PNG encoder and found that it would do the trick for me. That PngEncoder is included in SilverSpriteArcade. For those interested in dynamic terrain creation, I created an EditableImage, filled in with EditableImage.SetPixel, copied the EditableImage to a BitmapImage and then created the new Texture2D using the BitmapImage.

EditableImage ei = new EditableImage(screenWidth, screenHeight);
            BitmapImage image = new BitmapImage();

            for (int x = 0; x < screenWidth; x++)
            {
                for (int y = 0; y < screenHeight; y++)
                {
                    if (y > terrainContour[x])
                    {
                        ei.SetPixel(x, y, Colors.Green);
                    }
                    else
                    {
                        ei.SetPixel(x, y, Colors.Black);
                    }
                }
            }

            image.SetSource(ei.GetStream());

            foregroundTexture = new Texture2D(image, Game1.graphics.GraphicsDevice, new SilverArcade.SilverSprite.Vector2(500, 500), "terrain");