# Spritebatch and Shaperenderer

When I am using the same spritebatch to draw texture and calling shaperenderer inside it, it is kind of misbehaving, like texture is not getting drawn or font is not getting drawn, Why is it happening?

For example(pseusdo code):

spritebatch.begin()
draw texture
shaperenderer.begin()
shaperenderer.end()
spritebatch.end()


but when I use it with different ends and begins it is drawing the way I want it to, but drawing so many spritebatches is reducing the performance of the phone.Reason I have to draw many spritebatches is because I want some texture to overlap on on some other shaperender's function.Any suggestions?

• You can't explain an issue with such pseudo code. Yes you should be using only one sprite batch but you are doing something wrong that we can't see. – Winter Nov 27 '16 at 5:17
• but is it possible to draw font and texture in the same begin-end? – Vamsi Rao Nov 27 '16 at 5:27
• As I said yes. I'm not a pro but I'm doing it without any problem. Look at a tutorial/example code if you want to be sure. Then, if you can't find your issue post your code. – Winter Nov 27 '16 at 5:28
• I guess you were right, the problem was not with font but with shaperenderer and spritebatch, you can't call shaperenderer inside a spritebatch, Do you know how to fix it? – Vamsi Rao Nov 27 '16 at 7:01

You should try not to do the following: (Quote from one of the GDX creators)

batch.begin();
shapes.begin();

.. draw stuff from both ...

shapes.end();
batch.end();


And from me - Don't use ShapeRenderer for drawing - it's for debugging purposes.

Instead of this create a 1x1px image with a white color and create a class like CustomLine which extends Actor and make it's draw() look like this:

public void draw(batch, parentalpha){
batch.begin();
batch.draw(smallpixelimage, getX(), getY(), getWidth(), getHeight());
batch.end();
}


Just create an object of your new class CustomLine, add it to a stage/table or what you want and set it's size to 1x50 or something you like and you have your full-performance shaperenderer imitation ;)

But, if you really want a have a reason to use ShapeRenderer in your application, do it like:

batch.begin();
// draw graphics
batch.end();

shaperenderer.begin();
// draw shapes
shaperenderer.end();


and it will work.

• First of all thanks for answering, I guess your method doesn't work for drawing circles and that is why I am using shaperenderer. I did figure that I'll have to use different begins and ends for batch and renderer, it's just that frame rate is dropping while running it on a phone as I am using around 4 begin-ends. – Vamsi Rao Nov 27 '16 at 11:48
• 4 begins()/ends() shouldn't drop your fps, maybe there's something else breaking this. For drawing circles you can always create some white circles in photoshop and use them as textures (and scale) ;) – Jacob Nov 27 '16 at 11:59

Use the functions in the following order

shaperenderer.begin()
draw the shape
shaperenderer.end()

spritebatch.begin()
draw some texture
spritebatch.end()

• Why does this work and not the version of the OP? This answer would be improved with a bit more details. – Vaillancourt Jan 28 at 12:58