Hai teman-teman, saya punya sedikit masalah.
Saya memiliki 2 DrawableGameComponets
ini (bigApple, smallApple) di keduanya. Saya menggambar menjadi RenderTarget
dan kemudian menggambar RenderTarget
di backbuffer tetapi ini terjadi secara independen di setiap DrawableGameComponent
.
Hal yang ingin saya capai adalah keduanya DrawableGameComponents
menggambar dengan benar satu di atas yang lain.
Sesuatu seperti ini:
Ini adalah layar dengan kedua drawableComponent tanpa target render di setiap komponen.
Tapi alih-alih itu saya mendapatkan ini:
Ini adalah layar dengan drawableComponent dengan target render di setiap komponen.
Ini untuk permainan kecil yang sedang saya kerjakan. Saya berencana untuk menampilkan dalam satu komponen drawable dan gambar dari kamera dan di komponen game drawable lainnya, game itu sendiri. Tapi begitu saya menambahkan GameComponent
lagi ke Daftar Komponen, yang di atas yang terakhir ditambahkan tidak dapat dilihat.
Ini adalah Kode dari setiap Komponen drawable.
apel kecil:
public class SmallApple:DrawableComponent2D
{
Texture2D apple;
public SmallApple(Game game)
: base(game)
{
//Do nothing
}
protected override void LoadContent()
{
apple = Game.Content.Load<Texture2D>("apple");
this.Size = new Vector2(apple.Width,
apple.Height);
renderTarget = new RenderTarget2D(GraphicsDevice,
(int)Size.X,
(int)Size.Y,
false,
SurfaceFormat.Color,
DepthFormat.None,
this.Game.GraphicsDevice.PresentationParameters.MultiSampleCount,
RenderTargetUsage.PreserveContents);
base.LoadContent();
}
public override void Initialize()
{
base.Initialize();
}
public override void Draw(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.Clear(ClearOptions.Target, Color.Transparent, 1f, 0);
this.SharedSpriteBatch.Begin(SpriteSortMode.Immediate, null);
this.SharedSpriteBatch.Draw(this.apple, this.Position, Color.White);
this.SharedSpriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
this.SharedSpriteBatch.Begin();
this.SharedSpriteBatch.Draw(apple, this.Position,Color.White);
this.SharedSpriteBatch.End();
base.Draw(gameTime);
}
}
-- Dan Kelas BigApple
public class BigApple:DrawableComponent2D
{
Texture2D apple;
public BigApple(Game game)
: base(game)
{
}
protected override void LoadContent()
{
base.LoadContent();
apple = Game.Content.Load<Texture2D>("apple");
this.Size = new Vector2(apple.Width, apple.Height);
renderTarget = new RenderTarget2D(GraphicsDevice,
(int)Size.X,
(int)Size.Y,
false,
SurfaceFormat.Color,
DepthFormat.None,
this.Game.GraphicsDevice.PresentationParameters.MultiSampleCount,
RenderTargetUsage.PreserveContents);
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.Clear(ClearOptions.Target, Color.Transparent, 1f, 0);
this.SharedSpriteBatch.Begin(SpriteSortMode.Immediate,null);
this.SharedSpriteBatch.Draw(this.apple, this.Position, Color.White);
this.SharedSpriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
this.SharedSpriteBatch.Begin();
this.SharedSpriteBatch.Draw(renderTarget,new Rectangle((int)Position.X, (int)Position.Y, (int)GraphicsDevice.Viewport.Width, (int)GraphicsDevice.Viewport.Height), Color.White);
this.SharedSpriteBatch.End();
base.Draw(gameTime);
}
}
Kelas DrawableComponent2D adalah kelas yang berisi warisan dari drawablegameComponent dan memiliki beberapa variabel untuk dikerjakan.