0
\$\begingroup\$

I have a method that creates rectangles, it takes a texture

 for (Rectangle rect : rect) {
            game.batch.draw(rectImage, rect.x, rect.y);
        }

The texture is simple, I could just draw it instead. I created for example

shapeRenderer.set(ShapeRenderer.ShapeType.Filled);
shapeRenderer.rect(
        rect.x,
        rect.y,
        rect.width,
        rect.height,
        Color.DARK_GRAY,
);

Now what? game.batch.draw doesn't accept shapeRenderer instead of rectImage and doesn't accept only rect.x, rect.y

I'm just wondering how to call it?

\$\endgroup\$
  • \$\begingroup\$ By the way, you can't draw Rect through sprite batch, to draw with ShapeRenderer, you need to do it after spriteBatch.end() or before spriteBatch.begin() (depends of what need to be draw on top). Then : shapeRenderer.begin(); shapeRenderer.setColor(Color.DARK_GRAY); shapeRenderer.rect(rect); shapeRenderer.end(); Sorry i don't know how to pull code correctly into comment :/ \$\endgroup\$ – Sebastien Servouze Feb 7 '17 at 16:28
  • \$\begingroup\$ @SebastienServouze I want to draw it before, I want to draw inside the method which I use to create the rectangles, but then, how to pass the shape to batch.draw()? If I dont have to pass it then what to do? it takes 3 arguments \$\endgroup\$ – Lynob Feb 7 '17 at 18:17
  • \$\begingroup\$ You draw in a loop, you can't create your rectangles in this loop ! So you have to declare it in your game class juste once and then use this declaration in your render loop to draw, why do you want to draw them at the same place where your make them ? \$\endgroup\$ – Sebastien Servouze Feb 9 '17 at 8:07
3
\$\begingroup\$

To draw Shapes over sprites and textures:

  1. Make a shape renderer

    ShapeRenderer shapeRenderer = new ShapeRenderer();
    
  2. Define your shape(s)

    Rectangle rect = new Rectangle(x, y, width, height);
    
  3. Open your batch and draw all the stuff which needs to be UNDER your shape. Then close your batch.

    batch.begin();
    // Draw your stuff
    batch.end()
    
  4. Draw your shape(s)

    shapeRenderer.begin(ShapeType.Line);
    shapeRenderer.setColor(yourColor);
    shapeRenderer.rect(rect.x, rect.y, rect.width, rect.height);
    shapeRenderer.end();
    

If you want gradients and stuff for your shape, ShapeRenderer.rect() also has a method where you can put to four colors in (one by top of rectangle).

If you want border for your shape (which can also be gradients) it's also possible with ShapeRenderer

So if you want a specific shape with gradient and stuff, i'll recommend you to build a class which is very similar to sprite, but where the draw method look like the .4 above.

\$\endgroup\$
1
\$\begingroup\$

Why you are using shapeRenderer if you want to draw rect type object having different color.

Draw Rectangle using Pixmap. create Texture with given method and use

Texture texture=getPixmapTexture(Color.WHITE);

    Sprite sprite=new Sprite(texture); //Used for drawing 2D sprites.
    or
    Image image=new Image(texture);  //2D scene graph node.

    public static Texture getPixmapTexture(Color color){
       return new Texture(PixmapBuilder.getPixmapRectangle(1, 1, color));
    }     

   public static Pixmap getPixmapRectangle(int width, int height, Color color){
       Pixmap pixmap=new Pixmap(width, height, Pixmap.Format.RGBA8888);
       pixmap.setColor(color);
       pixmap.fillRectangle(0,0, pixmap.getWidth(), pixmap.getHeight());
       return pixmap;
   }
\$\endgroup\$
  • \$\begingroup\$ Can I add border and gradient to it? \$\endgroup\$ – Lynob Feb 5 '17 at 18:24
  • \$\begingroup\$ you need to use shader for border and gradient \$\endgroup\$ – Aryan Feb 5 '17 at 18:25
  • \$\begingroup\$ Ok so it turns out I don't need a pixmap, nor a shaperenderer, i need a shader since I want a rectangle with borders and gradient right? the question now is if I use shader, would I have the same problem I'm facing with shaperenderer? if yes then how to solve it \$\endgroup\$ – Lynob Feb 5 '17 at 18:29
  • \$\begingroup\$ @Lynob Consider using a NinePatch if you want to draw rectangles with borders. \$\endgroup\$ – bornander Feb 5 '17 at 20:07
  • \$\begingroup\$ @bornander can you elaborate how i can achieve by using NinePatch. Need some useful codes. \$\endgroup\$ – Aryan Feb 5 '17 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.