Reputation: 2121
In my code below, I think it's correctly done. But idk, the image called by _bg is does not shown up. The screen only show White. Weirdly, there is 0 error message when I run it.
(The RenderTarget2D is for default-ing my view into landscape mode)
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace GameName2
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager _graphics;
SpriteBatch _spriteBatch;
Texture2D _bg;
RenderTarget2D finalImageTarget;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
_spriteBatch = new SpriteBatch(GraphicsDevice);
_bg = Content.Load<Texture2D>(@"background");
finalImageTarget = new RenderTarget2D(GraphicsDevice, 1280, 720);
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(finalImageTarget);
GraphicsDevice.Clear(Color.White);
// TODO: Add your drawing code here
_spriteBatch.Begin();
_spriteBatch.Draw(_bg,
new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
null,
Color.White,
0,
Vector2.Zero,
SpriteEffects.None,
0);
_spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
_spriteBatch.Begin();
_spriteBatch.Draw(finalImageTarget,
new Vector2(720,0),
null,
Color.White,
MathHelper.PiOver2,
Vector2.Zero,
Vector2.One,
SpriteEffects.None,
0f);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
I use xnb file. And I put it in Content folder in the project. I've set 'Content' and 'Copy if newer' to the file property.
This is the .png file I use before converted into xnb : https://imageshack.us/scaled/large/15/loadingo.png
Or maybe the problem is in my .png file?
Any idea? Thanks
Upvotes: 1
Views: 627
Reputation: 9957
That second sprite batch call is rather curious. It looks to me like you are moving and / or rotating the image off-screen.
Since you are using this overload of draw you are effectively doing this:
Texture2D texture = finalImageTarget;
Vector2 position = new Vector2(720,0);
Rectangle? sourceRectangle = null;
Color color = Color.White;
float rotation = MathHelper.PiOver2;
Vector2 origin = Vector2.Zero;
Vector2 scale = Vector2.One;
SpriteEffects effects = SpriteEffects.None;
float layerDepth = 0f;
_spriteBatch.Draw (texture,position,sourceRectangle,color,rotation,origin,scale,effects,layerDepth,layerDepth);
So that means you are positioning your image 720 pixels to the right of the upper left corner of the screen, then rotating it around the upper left corner of the image 90 degrees.
My suggestion is to start simple and work your way up to what you want to achieve. Use a single sprite batch call without a render target and use source and destination rectangles without rotation.
Edit: Use the debugger to make sure all of the variables are coming out with the values you expect to see. For example, Window.ClientBounds.Width / Height might be coming out as zeros. I think I saw this once when porting to Android and had to change it to use the Viewport Width and Height.
Upvotes: 1
Reputation: 2121
This is the solution (The same question, I posted on different site)
https://gamedev.stackexchange.com/questions/54672/why-spritebatch-draw-shows-blank-output
Upvotes: 0
Reputation: 808
Why are you using Render2D? is there a specific reason?
if not then i would change the resolution of the window by using
_graphics.PreferredBackBuffer.Width = 720;
_graphics.PreferredBackbuffer.Height = 1280;
in your constructor. then you have you freedom to place your Texture2D image using the below;
_spriteBatch.Begin();
_spriteBatch.Draw(_bg,
new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
null,
Color.Black,
0,
Vector2.Zero,
SpriteEffects.None,
0);
_spriteBatch.End();
If your worried about placement over and under the background image then try using layers in your spriteBatch calls.
As i said im not sure why you want it this way so i could be wrong but doing it this simple way will workout whether or not its your texture file or something else.
EDIT
Based upon your comment, id say to try out just positioning the texture2D via coordinates e.g.
_spriteBatch.Draw(_bg, new vector2(0,0), color.black);
or
_spriteBatch.Draw(_bg, new vector2(screen.width/2,screen.height/2), color.black);
if you still cant see it id suggest maybe looking at the properties of the file or changing the file to a jpg for testing.
Upvotes: 0
Reputation: 1936
GraphicsDevice.SetRenderTarget(finalImageTarget);
GraphicsDevice.Clear(Color.White);
// TODO: Add your drawing code here
_spriteBatch.Begin();
_spriteBatch.Draw(_bg,
new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
null,
Color.White,
0,
Vector2.Zero,
SpriteEffects.None,
0);
_spriteBatch.End();
This code is not being drawn to the screen you see. It is being draw to the finalImageTarget
render target. That what a render target is a place to draw to that doesn't show up on screen. Setting it to GraphicsDevice.SetRenderTarget(null);
tells XNA to draw to the visible screen.
Upvotes: 0