So I accidentally deleted my blog the other week, and after restoring to the current revision found that the backup was from months ago. Brill.
So starting again, with a better gradient Texture2D generator built for use with the XNA Framework. This one does what the previous one did (still published on this blog here, actually) but to a lower level, and dumped a lot of stuff that wasn’t required last time such as rendering the texture for h*w times, when 1*h was required and now handles the alpha itself to gradient down.
public Texture2D createGradientTexture2D(GraphicsDevice g, int height, Color color) { Texture2D backgroundTex = new Texture2D(g, 1, height); Color[] bgc = new Color[height]; Color f = color; //Foreground Color Color b = Color.White; //Background Color float ratio = height / 255; float rc = 0; for (int i = 0; i < bgc.Length; i++) { bgc[i] = f; if (rc >= ratio) { f.A--; rc = 0; } else rc += ratio; } rc = 0; //Reset the ratio count for (int i = bgc.Length-1; i >= 0; i--) { //Explode the background and array color into its RGBA bytes byte[] ba = { bgc[i].R, bgc[i].G, bgc[i].B, bgc[i].A }; byte[] bc = { b.R, b.G, b.B, b.A }; //for each of the bytes... for (int j = 0; j < ba.Length; j++) { //...Bitwise AND them to slowly gradient: ba[j] &= bc[j]; } //Stick it back in the Color array: bgc[i] = new Color(ba[0], ba[1], ba[2], ba[3]); } //And finally build the Texture2D from the data, and return it. backgroundTex.SetData(bgc); return backgroundTex; }
It can currently be found being used here, and if screenshots are your thing, here’s a screenshot of this code currently being used in my video game, “Crunk”.
That’s all for now, a very merry Xmas to all that chance upon my blog, see you in 2012!







