大家好,我有一个小问题。
我在两个(bigApple,smallApple)中都有这个 2 DrawableGameComponets
(bigApple,smallApple),我正在绘制到 a中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);
}
}
——还有大苹果班
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 的继承,并且有一些变量可以使用。