Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: New example source code to handle sprite sheets  (Read 7736 times)

0 Members and 1 Guest are viewing this topic.

hugoruscitti

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • http://www.losersjuegos.com.ar
New example source code to handle sprite sheets
« on: July 23, 2009, 01:41:12 am »
Hi, i made a simple class to handle image sheets in
the wiki:

  http://www.sfml-dev.org/wiki/en/sources/spritesheets

Bye.

remi.k2620

  • Full Member
  • ***
  • Posts: 186
    • View Profile
    • http://remi.tuxfamily.org
New example source code to handle sprite sheets
« Reply #1 on: July 29, 2009, 10:47:57 pm »
Nice :)
You could have made this a bit differently by making it a drawable. For example :
Code: [Select]

class dImageSheet(sf.Drawable):
 
    def __init__(self, image, cols=1, rows=1):
        self.sprite = sf.Sprite(image)
        self.len_frames = cols * rows
        self.cols = cols
        self.rows = rows
        self.frame_width = image.GetWidth() / cols
        self.frame_height = image.GetHeight() / rows
        self.sub_rect = sf.IntRect(0, 0, self.frame_width, self.frame_height)
        self.SetFrameIndex(0)
 
    def SetFrameIndex(self, index):
        self.index = index
 
        frame_col = index % self.cols
        frame_row = index / self.cols
 
        dx = frame_col * self.frame_width - self.sub_rect.Left
        dy = frame_row * self.frame_height - self.sub_rect.Top
 
        self.sub_rect.Offset(dx, dy)

    def NextFrame(self):
        current_index = self.index + 1
 
        if current_index >= self.len_frames:
            current_index = 0
 
        self.SetFrameIndex(current_index)
 
    def Render(self, window):
self.sprite.SetSubRect(self.sub_rect)
window.Draw(self.sprite)
 
    def GetFrameIndex(self):
        return self.index

hugoruscitti

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • http://www.losersjuegos.com.ar
New example source code to handle sprite sheets
« Reply #2 on: August 01, 2009, 05:29:03 am »
Thanks, it's a good idea. And its more easy to extend.