c# - XNA RenderTarget Preserve Contents -
i'm trying load texture rendertarget once on first draw , preserve contents draw same texture every frame without recreating it.
this code have doesnt work , shows empty texture area ,
rendertarget2d rtarget = null; /// <summary> /// called when game should draw itself. /// </summary> /// <param name="gametime">provides snapshot of timing values.</param> protected override void draw(gametime gametime) { graphicsdevice.clear(gamebackgroundcolor); spritebatch.begin(); if (rtarget == null) { rtarget = new rendertarget2d(game.graphics.graphicsdevice, game.graphics.graphicsdevice.presentationparameters.backbufferwidth, game.graphics.graphicsdevice.presentationparameters.backbufferheight, false, game.graphics.graphicsdevice.presentationparameters.backbufferformat, depthformat.depth24, 0, rendertargetusage.preservecontents); game.graphics.graphicsdevice.setrendertarget(rtarget); game.graphics.graphicsdevice.clear(color.black); game.spritebatch.draw(contentmanager.load<texture2d>("tiles"), new rectangle(0, 0, 100, 100), color.white); game.graphics.graphicsdevice.setrendertarget(null); } spritebatch.draw(rtarget, new rectangle(0, 0,400, 400), color.white); //draw character character.draw(gametime); spritebatch.end(); base.draw(gametime); }
and end result:
can explain doing wrong??
i think forgot game.spritebatch.begin()
, end()
render target. think should move spritebatch.end()
close draw(rtarget...
method call (especially if game.spritebatch
, spritebatch
same variable).
graphicsdevice.clear(gamebackgroundcolor); if (rtarget == null) { rtarget = new rendertarget2d(game.graphics.graphicsdevice, game.graphics.graphicsdevice.presentationparameters.backbufferwidth, game.graphics.graphicsdevice.presentationparameters.backbufferheight, false, game.graphics.graphicsdevice.presentationparameters.backbufferformat, depthformat.depth24, 0, rendertargetusage.preservecontents); game.graphics.graphicsdevice.setrendertarget(rtarget); game.graphics.graphicsdevice.clear(color.black); game.spritebatch.begin(); game.spritebatch.draw(contentmanager.load<texture2d>("tiles"), new rectangle(0, 0, 100, 100), color.white); game.spritebatch.end(); game.graphics.graphicsdevice.setrendertarget(null); } spritebatch.begin(); spritebatch.draw(rtarget, new rectangle(0, 0,400, 400), color.white); //draw character character.draw(gametime); spritebatch.end(); base.draw(gametime);
Comments
Post a Comment