12

So what I'm trying to do is to generate a background image for my game by drawing pixmaps to a texture. So far I can do that, but now I need to draw the pixmaps flipped in the X or Y axis to the texture. However I can't find anything to do so. The pixmap class does not provide that functionality. Then I thought I could draw a flipped texture region to a texture, but so far I haven't found how to do so. So I was wondering how can I do such a thing, would it be possible to flip a png image with other java libraries and then create a pixmap from that flipped image?

Daahrien
  • 10,190
  • 6
  • 39
  • 71
Leonso Medina Lopez
  • 777
  • 1
  • 10
  • 21

2 Answers2

9

I also don't see other option except to iterate the pixels:

public Pixmap flipPixmap(Pixmap src) {
    final int width = src.getWidth();
    final int height = src.getHeight();
    Pixmap flipped = new Pixmap(width, height, src.getFormat());

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            flipped.drawPixel(x, y, src.getPixel(width - x - 1, y));
        }
    }
    return flipped;
}
Petko Petkov
  • 324
  • 3
  • 6
  • Remember to dispose the original Pixmap – Will Kru Mar 06 '14 at 01:58
  • 2
    Thx! Btw, if you want to flip on the Y axis instead you just do `flipped.drawPixel(x, y, src.getPixel(x, height - y - 1));`. – Sawny Jun 01 '14 at 12:33
  • Just in case you need to rotate a pixmap by 90 degrees: I created a method to do this inspired by your idea, see http://stackoverflow.com/a/34362685/2399024 – donfuxx Dec 18 '15 at 19:35
1

Here's a solution that doesn't require the creation of a new Pixmap. This code can also be modified to flip a Pixmap horizontally and vertically by swapping corners of a pixmap image instead of swapping pixels on opposite sides of the image.

public static void flipPixmap( Pixmap p ){
    int w = p.getWidth();
    int h = p.getHeight();
    int hold;

    //change blending to 'none' so that alpha areas will not show
      //previous orientation of image
    p.setBlending(Pixmap.Blending.None);
    for (int y = 0; y < h / 2; y++) {
        for (int x = 0; x < w / 2; x++) {
            //get color of current pixel
            hold = p.getPixel(x,y);
            //draw color of pixel from opposite side of pixmap to current position
            p.drawPixel(x,y, p.getPixel(w-x-1, y));
            //draw saved color to other side of pixmap
            p.drawPixel(w-x-1,y, hold);
            //repeat for height/width inverted pixels
            hold = p.getPixel(x, h-y-1);
            p.drawPixel(x,h-y-1, p.getPixel(w-x-1,h-y-1));
            p.drawPixel(w-x-1,h-y-1, hold);
        }
    }
    //set blending back to default
    p.setBlending(Pixmap.Blending.SourceOver);
}
jigritsn
  • 63
  • 2
  • 6