Cut a Sprite

72 views
Skip to first unread message

Nitneroc

unread,
Aug 18, 2012, 4:05:13 PM8/18/12
to cocos-...@googlegroups.com
After reading those threads, I tried another way to "cut" a sprite (ie only display some part of it) :
https://groups.google.com/forum/?fromgroups#!topic/cocos-discuss/Ks83VdBj9Wk%5B1-25%5D 

Here's what I came up with for cutting the top of a sprite (the only thing I'm actually interested in) :

class Cut_Sprite(cocos.sprite.Sprite):
    
    def __init__(self,*args,**kwargs):
        super(Cut_Sprite,self).__init__(*args,**kwargs)
        self.tex_scale = self.width/self._vertex_list.tex_coords[3] #used to convert pixels to texture coords
        
    def cut(self,desired_height):
        desired_height=int(desired_height)#needs int
        self._vertex_list.vertices[5]=self.y+desired_height
        self._vertex_list.vertices[7]=self.y+desired_height
        self._vertex_list.tex_coords[7]=desired_height/self.tex_scale
        self._vertex_list.tex_coords[10]=desired_height/self.tex_scale

What do you think of such a solution ? I'm a newbie, I don't know anything about openGL, so I guess there are other/better solutions (some are mentionned in the threads I mentionned earlier), but this one works both inside and outside a batch.
here's a small test-app : http://pastebin.com/xmn0KERe

Nitneroc

unread,
Aug 18, 2012, 4:36:49 PM8/18/12
to cocos-...@googlegroups.com
Ok, I see now this only works because the image is the only one used at the moment, and I'd get to see other images in the atlas (I guess ?) with such a code, but there has to be a workaround.

claudio canepa

unread,
Aug 18, 2012, 6:50:35 PM8/18/12
to cocos-...@googlegroups.com
The big problem is that you modify the vertex_list in parallel with other code in Sprite:
if you change position, scale, rotation or image after the creation, the vertex list will be changed by other code in Sprite and it will not look as a cut sprite.

The proper general way to implement this should be:
   1. add a property desired_height to sprite
   2. make the setter for this property call _update_position
   3. modify _update_position to account for desired_height when rebuilding the vertex list, and also add the texcoord change there

Another way to attack the problem is thinking one level up: a cluster of textured quads.
Suitability for the task depends on the use case, which I don't know.
The implementation can be based on the ParticleSystem code.
The advantage is that theres no long chain of classes interacting,

 

claudio canepa

unread,
Aug 18, 2012, 6:56:03 PM8/18/12
to cocos-...@googlegroups.com
On Sat, Aug 18, 2012 at 5:36 PM, Nitneroc <corent...@gmail.com> wrote:
Ok, I see now this only works because the image is the only one used at the moment, and I'd get to see other images in the atlas (I guess ?) with such a code, but there has to be a workaround.
--
 

You can load an image as a single texture with

    pic = pyglet.image.load('fire.png', file=pyglet.resource.file('fire.png'))
    texture = pic.get_texture()

Another way is to change the texcoords recalculation to account for the offset that an image in an atlas has.

Nitneroc

unread,
Aug 19, 2012, 9:06:02 AM8/19/12
to cocos-...@googlegroups.com
Thank you once more Claudio !

I didn't realize this would pose problems with sprite positions, since I actually don't move them around in my game (they are cells in a grid), but your solutions seem perfect.

I think I'll go with the texture offset, I guess there has to be a downside to have images in separate textures...

I'll have a look at the ParticleSystem code, this should be instructive !
Reply all
Reply to author
Forward
0 new messages