How to Not refresh the rendertarget after each frame in XNA - xna

Take for example I draw a circle at (10,10) then on the next frame another at (20,20). the circle at (10,10) is no longer visible.
I am not calling graphicsdevice.clear.
is there a way to make the previously drawn graphics not be cleared?
I am drawing these into a seconary rendertarget so I can use it as a background.
Any Ideas?

When you initialize the RenderTarget2D you need to use RenderTargetUsage.PreserveContents.
// Disregard the arguments up to RenderTargetUsage
RenderTarget2D r = new RenderTarget2D(GraphicsDevice,
graphics.PreferredBackBufferWidth,
graphics.PreferredBackBufferHeight,
false,
RenderTargetUsage.PreserveContents);

Related

CAShapeLayer to change the width of different Brushes

I am having different Texture Brushes I am having the image of those texture of each pixel of the stroke I want to assign the texture to the CGPath and change the width .
I need to assign the texture image and change the width to the slider response.
You achieve this in the following order:
Use CGLayerCreateWithContext to create a CGLayer, during this stage, the slider value is obtained and used as the CGSize in the initializer.
Use CGLayerGetContext to get the context of the CGLayer created, then you render your brush texture with the context, for example, CGContextDrawImage.
Use the completed CGLayer as a texture and draw it on screen with CGContextDrawLayerAtPoint, since you got your CGPath, you need to manually calculate the density of your drawing, and generate an array of CGPoint to be used as a parameter in the drawing function.
You may reference the Quartz 2D Programming Guide by Apple.

How to set a outer geometry mask in d2d so d2d only draws outside that geometry

I am working on API which requires me set up a outer geometry mask on ID2D1Rendertarget such that any draw call after that only draws portion of drawings which lies outside this geometry.
https://msdn.microsoft.com/en-us/library/windows/desktop/dd756675(v=vs.85).aspx explains how can we setup a inner geometry mask on ID2D1Rendertarget such that any draw call after that only draws portion of drawings which lies inside this geometry.I want to implement just opposite of that. Is this possible? Any help is deeply appreciated.
One way to do this is to subtract your geometry from a rectangle that fills the entire render target. Check out the MSDN page on combining geometries. I have a small code example below:
ComPtr<ID2D1PathGeometry> invertedGeometry;
ComPtr<ID2D1RectangleGeometry> rectangleGeometry;
d2dFactory->CreateRectangleGeometry(
{ 0, 0, targetWidth, targetHeight },
&rectangleGeometry
);
ComPtr<ID2D1GeometrySink> geometrySink;
d2dFactory->CreatePathGeometry(&invertedGeometry);
invertedGeometry->Open(&geometrySink);
rectangleGeometry->CombineWithGeometry(
pathGeometry.Get(),
D2D1_COMBINE_MODE_EXCLUDE,
D2D1::Matrix3x2F::Identity(),
geometrySink.Get()
);
geometrySink->Close();
Use the inverted geometry as the geometric mask instead of the original path geometry.
A second way to do this is to rasterize your geometry to a bitmap and use it as an opacity mask. You can flip the colors depending on whether or not you want the inside or outside to mask.

Keep pixel aspect with different resolution in xna game

I'm currently developping an old-school game with XNA 4.
My graphics assets are based on 568x320 resolution (16/9 ration), I want to change my window resolution (1136x640 for example) and my graphics are scaled without stretching, that they keep pixel aspect.
How can I reach this ?
You could use a RenderTargetto achieve your goal. It sounds like you don't want to have to render accordingly to every possible screen size, so if your graphics aren't dependant on other graphical features like a mouse, then I would use a RenderTarget and draw all the pixel data to that and afterwards draw it to the actual screen allowing the screen to stretch it.
This technique can be used in other ways too. I use it to draw objects in my game, so I can easily change the rotation and location without having to calculate every sprite for the object.
Example:
void PreDraw()
// You need your graphics device to render to
GraphicsDevice graphicsDevice = Settings.GlobalGraphicsDevice;
// You need a spritebatch to begin/end a draw call
SpriteBatch spriteBatch = Settings.GlobalSpriteBatch;
// Tell the graphics device where to draw too
graphicsDevice.SetRenderTarget(renderTarget);
// Clear the buffer with transparent so the image is transparent
graphicsDevice.Clear(Color.Transparent);
spriteBatch.Begin();
flameAnimation.Draw(spriteBatch);
spriteBatch.Draw(gunTextureToDraw, new Vector2(100, 0), Color.White);
if (!base.CurrentPowerUpLevel.Equals(PowerUpLevels.None)) {
powerUpAnimation.Draw(spriteBatch);
}
// DRAWS THE IMAGE TO THE RENDERTARGET
spriteBatch.Draw(shipSpriteSheet, new Rectangle(105,0, (int)Size.X, (int)Size.Y), shipRectangleToDraw, Color.White);
spriteBatch.End();
// Let the graphics device know you are done and return to drawing according to its dimensions
graphicsDevice.SetRenderTarget(null);
// utilize your render target
finishedShip = renderTarget;
}
Remember, in your case, you would initialize your RenderTarget with dimensions of 568x320 and draw according to that and not worry about any other possible sizes. Once you give the RenderTarget to the spritebatch to draw to the screen, it will "stretch" the image for you!
EDIT:
Sorry, I skimmed through the question and missed that you don't want to "stretch" your result. This could be achieved by drawing the final RenderTarget to your specified dimensions according to the graphics device.
Oh Gosh !!!! I've got it ! Just give SamplerState.PointClamp at your spriteBatch.Begin methods to keep that cool pixel visuel effet <3
spriteBatch.Begin(SpriteSortMode.Immediate,
BlendState.AlphaBlend,
SamplerState.PointClamp,
null,
null,
null,
cam.getTransformation(this.GraphicsDevice));

Drawing off screen using XNA SpriteBatch

I have a SpriteBatch that is set to draw to a RenderTarget2D that is 500px larger in both height and width. However, whenever I call the SpriteBatch.Draw method using a point outside of the physical screen width/height it will not draw the object.
I am performing some 3D transforms on the texture so I need the areas outside the screen to be drawn. I have tried setting culling to None and this had no effect.
Thanks ahead of time for your help.
Modify the Viewport on your GraphicsDevice before drawing to your RenderTarget2D.
GraphicsDevice.SetRenderTarget(myRenderTarget2D);
GraphicsDevice.Viewport.Width += 500; //or a more sensible calculation
GraphicsDevice.ViewPort.Height += 500;
GraphicsDevice.Clear(Color.Transparent);
//Spritebatch operations
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.graphicsdevice.viewport
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.viewport_members

Problem with actionscript 3 erasing drawing

I have a based image and some sprites on top of the basd image movieclip... Some of the sprites can be drawn by the user using graphics api in actionscript 3. I can draw things on the sprites but I can't create an eraser like brush that can remove part of the unwanted drawings. I try using Alpha but no it doesn't work
I have googled about it and come up with the solution:
1) Linebitmapstyle... This solution is not the best one coz I my sprites can be moved so if I use linebitmapstyle, it does draw the pixel from the image to the sprite but if the sprite moved the drawn pixel won't change.
2) Masking may not work for me either....
What is the best way of creating the eraser
You may rather want to use a Bitmap to make such things easier to manipulate (unless you need to do scalable vector graphics of course!). To draw shapes you can still use the graphics API to create the shapes.
To do so, instantiate a "dummy" sprite (or another IBitmapDrawable implementation) to create the graphics and then "copy" them to the BitmapData the bitmapData.draw() function. This way you can for instance draw with the option BlendMode.ERASE in order remove the pixels of the shape.
Example (from the top of my mind) :
// creates a bitmap data canvas
var bitmapData:BitmapData = new BitmapData(500, 500);
// creates a bitmap display object to contain the BitmapData
addChild(new Bitmap(bitmapData));
// creates a dummy object to draw and draws a 10px circle
var brush:Sprite = new Sprite(); // note this is not even added to the stage
brush.graphics.beginFill(0xff0000);
brush.graphics.drawCircle(10, 10, 10);
// the matrix will be used to position the "brush strokes" on the canvas
var matrix:Matrix = new Matrix();
// draws a circle in the middle of the canvas
matrix.translate(250, 250);
bitmapData.draw(brush, matrix
// translates the position 5 pixels to the right to slightly erase the previously
// drawn circle creating a half moon
matrix.translate(5, 0);
bitmapData.draw(brush, matrix,null,BlendMode.ERASE);

Resources