残像いっぱい(SpriteBatch編)
■いっぱい出そう、でも
前回の「ジェットストリーム」では、たくさんのパーティクルを出すと処理が重そうなので、描画したフレームバッファを利用して残像のサンプルを作成してみました。
■ソース
Game1.cs
namespace WindowsGame { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content; SpriteBatch sprite; Texture2D textureBall; Texture2D textureBack; const int particleMax = 64; Particle[] particle = new Particle[particleMax]; public Game1() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } protected override void Initialize() { Random ran = new Random(); for (int i = 0; i < particleMax; i++) particle[ i ] = new Particle( (float)ran.Next(700), (float)ran.Next(500), (float)ran.Next(360) * 3.14159f / 180.0f); base.Initialize(); } protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { sprite = new SpriteBatch(graphics.GraphicsDevice); textureBall = content.Load<Texture2D>("ball"); textureBack = content.Load<Texture2D>("back"); } } protected override void Update(GameTime gameTime) { foreach (Particle p in particle) p.Move(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { sprite.Begin(SpriteBlendMode.AlphaBlend); sprite.Draw( textureBack, new Vector2(0, 0), new Rectangle(0, 0, 16, 16), Color.White, 0.0f, new Vector2(0, 0), 100.0f, 0, 0.0f); foreach (Particle p in particle) sprite.Draw(textureBall, p.Pos, Color.White); sprite.End(); base.Draw(gameTime); } } }
Particle.cs
using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; namespace WindowsGame { class Particle { public Vector2 Pos; public Vector2 Direction; public Particle(float x, float y, float ang ) { Pos = new Vector2(x, y); Direction = new Vector2((float)Math.Sin(ang) * 4.0f, (float)Math.Cos(ang) * 4.0f); } public void Move() { Pos += Direction; if ((Pos.X < 0) || (Pos.X > 800 - 64)) Direction.X = -Direction.X; if ((Pos.Y < 0) || (Pos.Y > 600 - 64)) Direction.Y = -Direction.Y; } } }
■ソース説明
通常、描画をする場合画面をクリアするのですが
graphics.GraphicsDevice.Clear(Color.Black);
これを使用せずに自分でポリゴンを描画してクリアします、ただ、そのときに「textureBack」というテクスチャ、アルファー値が「0」ではなく「4」になっているので前回描画したフレームが暗くなった状態で残ります。
今回使用した「back.png」です、サイズが 8 X 6 でα値が4になっています。
[back.png]