From c8a45723ce4cdc261107478824ee08745899d712 Mon Sep 17 00:00:00 2001 From: MyreMylar Date: Fri, 13 Apr 2018 14:03:02 +0100 Subject: [PATCH] Add an optional rectangle parameter to TextureNode so that user's can specify their own area of the shared Texture to use. This allows more flexibility rather than having this functionality restricted to the TextureAtlas class which is specific to one particular approach to texture sharing. --- src/pygame_sdl2/render.pyx | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/pygame_sdl2/render.pyx b/src/pygame_sdl2/render.pyx index c962c3c..6d3be9b 100644 --- a/src/pygame_sdl2/render.pyx +++ b/src/pygame_sdl2/render.pyx @@ -212,13 +212,10 @@ cdef class TextureNode: cdef int source_w cdef int source_h - def __init__(self, tex): + """ Create a TextureNode with a Texture and an optional rectangle that represents the area of the texture to use.""" + def __init__(self, tex, rect=None): if isinstance(tex, Texture): self.texture = tex - to_sdl_rect((0,0,tex.w,tex.h), &self.source_rect) - to_sdl_rect((0,0,tex.w,tex.h), &self.trimmed_rect) - self.source_w = tex.w - self.source_h = tex.h elif isinstance(tex, TextureNode): self.texture = (tex).texture @@ -226,6 +223,17 @@ cdef class TextureNode: else: raise ValueError() + if rect is not None: + to_sdl_rect(rect, &self.source_rect) + to_sdl_rect((0,0,rect[2],rect[3]), &self.trimmed_rect) + self.source_w = rect[2] + self.source_h = rect[3] + else: + to_sdl_rect((0,0,self.texture.w,self.texture.h), &self.source_rect) + to_sdl_rect((0,0,self.texture.w,self.texture.h), &self.trimmed_rect) + self.source_w = self.texture.w + self.source_h = self.texture.h + def render(self, dest=None): cdef SDL_Rect dest_rect