在Microsoft XNA中使用spritebatch和base.draw

时间:2014-06-09 08:39:41

标签: c# .net xna spritebatch

我想知道这是否正确或是否应该更改?

每当我想要更改我在主代码中绘制的图块数量时,或者每当我想要更改渲染目标时,我都计划调用这些图片。 基本上我想要自由嘿给我一个表面,并有一个任何类型的表面返回给我。 无需担心启动它

我的代码中还有另一个问题:我将返回的表面,实例化一个新的COPY,或者我将被赋予从实例化它的函数中创建的相同实例。谢谢。

我还需要知道我是否调用了new_texture,然后我对该纹理表面做了一堆spritebatch 然后我决定,好吧,现在我不想只是拥有它我想渲染它。 当我在绘图循环结束后从绘图函数中调用base.draw()之后 什么会发生的时候,我在game.draw()之前调用两组spritebatches,其中spritebatches必须以特定顺序呈现给一个表面纹理,然后再呈现另一个表面纹理? 我渲染的顺序会搞砸了还是无关紧要? 这样做的任何替代方案,我都可以接受建议吗?

另一个问题,它告诉我当我调用draw_texture函数时图形设备为null。 我不知道为什么!!!!

这是代码

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.
        }


    }
}

0 个答案:

没有答案