สวัสดีเพื่อนๆ ฉันมีปัญหานิดหน่อย
ฉันมี 2 DrawableGameComponets
นี้ (bigApple, SmallApple) ทั้งคู่ ฉันกำลังวาดเป็น RenderTarget
แล้ววาด RenderTarget
ใน backbuffer แต่สิ่งนี้เกิดขึ้นอย่างอิสระใน DrawableGameComponent
แต่ละตัว
สิ่งที่ฉันต้องการบรรลุคือทั้ง DrawableGameComponents
วาดอย่างถูกต้องจากกัน
บางอย่างเช่นนี้:
นี่คือหน้าจอที่มีทั้ง DrawableComponent โดยไม่มีเป้าหมายการแสดงผลในแต่ละองค์ประกอบ
แต่แทนที่จะเป็นเช่นนั้น ฉันกลับได้รับสิ่งนี้:
นี่คือหน้าจอที่มีทั้ง DrawableComponent พร้อม rendertargets ในแต่ละองค์ประกอบ
นี่เป็นเกมเล็กๆ น้อยๆ ที่ฉันกำลังทำอยู่ ฉันวางแผนที่จะแสดงในองค์ประกอบที่วาดได้และรูปภาพจากกล้อง และในองค์ประกอบเกมที่วาดได้อีกอันหนึ่งของตัวเกมเอง แต่เมื่อฉันเพิ่มอีก GameComponent
ในรายการส่วนประกอบ จะไม่สามารถมองเห็นอันที่อยู่เหนือการเพิ่มครั้งล่าสุดได้
นี่คือรหัสจากแต่ละองค์ประกอบที่สามารถวาดได้
สมอลแอปเปิ้ล:
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);
}
}
-- และคลาส 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);
}
}
คลาส DrawableComponent2D เป็นคลาสที่บรรจุมรดกจาก DrawablegameComponent และมีตัวแปรบางตัวให้ใช้งาน