0

I'm wondering about if this is correct or if it should be changed?

I plan to call these whenever I want to do a change in how many tiles I'm drawing in my main code, or whenever I want to change my rendering target. Basically I want the freedom to go hey give me a surface and have a surface of any type returned to me. Without having to worry about initiating it

Also another question in this code of mine: Will the surface that I return, instantiate a new COPY, or will I be given the same instance that was created from within the function that instantiated it. Thank you.

I also need to know if I call new_texture, then I do a bunch of spritebatch to that texture surface then I decide, okay, now I dont want to just have it i want to render to it. When I call base.draw() after at the end of my drawing loop from within my draw function what will happen when, I call two sets of spritebatches before game.draw() where the spritebatches have to be rendered to one surface texture, then another one, in a specific order? Will the order of my rendering get messed up or will it not matter? Any alternatives to doing this , im open to suggestions?

one other issue, it tells me the graphics device is null when I call the draw_texture function. I don't know why!!!!

Here's the code

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace TileEngine
{
    class Renderer
    {
        GraphicsDevice mydevice;
        public SpriteBatch spriteBatch;



        RenderTarget2D new_texture(int width, int height, RenderTarget2D Mine)
        {
            Texture2D TEX = new Texture2D(mydevice, width, height);   //create the texture to render to
            mydevice.SetRenderTarget(Mine); //set the render device to the reference provided
                //maybe base.draw can be used with spritebatch. Idk. We'll see if the order of operation
                    //works out. Wish I could call base.draw here.
            return Mine;    //I'm hoping that this returns the same instance and not a copy.

        }

        void draw_texture(int width, int height, RenderTarget2D Mine)
        {
            mydevice.SetRenderTarget(null); //Set the renderer to render to the backbuffer again
            Rectangle drawrect = new Rectangle(0, 0, width, height); //Set the rendering size to what we want
            spriteBatch.Begin();                        //This uses spritebatch to draw the texture directly to the screen  
            spriteBatch.Draw(Mine, drawrect, Color.White); //This uses the color white
            spriteBatch.End();      //ends the spritebatch
            //Call base.draw after this since it doesn't seem to recognize inside the function
            //maybe base.draw can be used with spritebatch. Idk. We'll see if the order of operation
            //works out. Wish I could call base.draw here.
        }


    }
}
Remmie
  • 47
  • 10
  • You *can* use multiple sprite batches on the same render target. `new_texture` returns the same reference that has been passed in. So it does not really make sense to return anything at all. Instead it should create a render target, not just a pure texture. The `SpriteBatch.Draw(Mine, ...)` call in `draw_texture` looks suspicious. Do you really want to draw a render target? You haven't initialized `myDevice` nor `spriteBatch`. Why should they be anything other than `null`? What do you expect `base.Draw()` to do? – Nico Schertler Jun 09 '14 at 08:58
  • I expect base.Draw() to render everything onto it's targets when spritebatch is called. So what intend to do is to have spritebatch.begin, after calling newtexture. Then I intend to draw a bunch of tiles to the rendertarget. Then i intend to pass that same render target into the draw_texture function after spritebatch.end Then I intend to use that draw_texture function to draw what was drawn to the 2d texture, to the backbuffer. I would expect base.draw() to render all of it, simultaneously at once. Sooo base.draw() wouldn't it have to be called twice? between each of these actions? – Remmie Jun 09 '14 at 12:04
  • And where should `base.Draw()` be declared? Your `Renderer` does not inherit from anything (except `Object`). The draw calls are issued on `spriteBatch.End()`. Your texture will contain the data after that. You should clear the render target before rendering anything on it. – Nico Schertler Jun 09 '14 at 13:05

0 Answers0