libgdx Cutting an image

匆匆过客 提交于 2019-12-20 04:30:47

问题


I have been trying to "cut" an image for some time now, I ll explain why and what I tried. So I wanted to create an hp "bar" except it's not a bar but a heart and so I though it would be easy all I had to do is have two pictures draw them on top of each other and then just cut one to make it appear as in hp was being lost, but I was not able to find a way to cut the image.

  • Setting the height just resizes the image as you might have guessed
  • I tried using textureRegion to kind of hack it but it didn't go so well
  • I found a method called clip begin which also uses scissors but for some reason that just doesn't seem to be working.

I might be using the clip begin wrong but I can't really find any real documentation on it, all I'm doing is:

image.clipBegin(x,y,height,weight);
image.clipEnd();

I almost forgot, I'm using a scene2d Image, might be a better way to go around it but not sure what that would be.

I would appreciate any ideas on how to do this, thank you.


回答1:


You want to use the OpenGL Scissor support that Libgdx exposes. See the Libgdx Clipping wiki and the Libgdx ScissorStack documentation.

The API isn't particularly friendly (its designed to support dynamically pushing multiple constraining rectangles, which as far as I've seen, isn't used very often).

The important point to remember with the scissor stack is that it only applies to actual draw commands that get issued. Since most APIs try to batch up draw commands, this means actual drawing might not happen when it looks like it should happen. To ensure clipping is happening you must flush any buffered draws before pushing the scissor (otherwise the wrong thing might get clipped) and you must flush any draw calls before popping the scissor (otherwise things you want clipped might avoid the scissors).

See libgdx ScissorStack not working as expected or libGDX - How to clip or How to draw on just a portion of the screen with SpriteBatch in libgdx? or Making a Group hide Actors outside of its bounds.



来源:https://stackoverflow.com/questions/24275914/libgdx-cutting-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!