diff --git a/ppb/flags.py b/ppb/flags.py index af48506a..312030d6 100644 --- a/ppb/flags.py +++ b/ppb/flags.py @@ -81,3 +81,28 @@ class BlendModeNone(BlendMode): """ Indicate a sprite, if translucent, should be rendered in NONE mode. """ + +class Flip(Flag, abstract=True): + """ + Indicate sprite flipping at render time. + """ + +class FlipNone(Flip): + """ + Do not flip the sprite. + """ + +class FlipVertical(Flip): + """ + Flip the sprite vertically. + """ + +class FlipHorizontal(Flip): + """ + Flip the sprite horizontally. + """ + +class FlipBoth(Flip): + """ + Flip the sprite both horizontally and vertically. + """ diff --git a/ppb/systems/renderer.py b/ppb/systems/renderer.py index 3e439224..bc104cc1 100644 --- a/ppb/systems/renderer.py +++ b/ppb/systems/renderer.py @@ -11,7 +11,8 @@ rw_from_object, # https://pysdl2.readthedocs.io/en/latest/modules/sdl2.html#sdl2.sdl2.rw_from_object SDL_Window, SDL_Renderer, SDL_Rect, # https://wiki.libsdl.org/SDL_Rect - SDL_INIT_VIDEO, SDL_BLENDMODE_BLEND, SDL_FLIP_NONE, + SDL_INIT_VIDEO, SDL_BLENDMODE_BLEND, + SDL_FLIP_NONE, SDL_FLIP_HORIZONTAL, SDL_FLIP_VERTICAL, SDL_CreateWindowAndRenderer, # https://wiki.libsdl.org/SDL_CreateWindowAndRenderer SDL_DestroyRenderer, # https://wiki.libsdl.org/SDL_DestroyRenderer SDL_DestroyWindow, # https://wiki.libsdl.org/SDL_DestroyWindow @@ -68,6 +69,13 @@ flags.BlendModeNone: SDL_BLENDMODE_NONE, } +FLIP = { + flags.FlipNone: SDL_FLIP_NONE, + flags.FlipVertical: SDL_FLIP_VERTICAL, + flags.FlipHorizontal: SDL_FLIP_HORIZONTAL, + flags.FlipBoth: SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL, +} + # TODO: Move Image out of the renderer so sprites can type hint appropriately. class Image(assets.Asset): @@ -213,10 +221,11 @@ def on_render(self, render_event, signal): src_rect, dest_rect, angle = self.compute_rectangles( texture.inner, game_object, camera ) + flip = FLIP[getattr(game_object, 'flip', flags.FlipNone)] sdl_call( SDL_RenderCopyEx, self.renderer, texture.inner, ctypes.byref(src_rect), ctypes.byref(dest_rect), - angle, None, SDL_FLIP_NONE, + angle, None, flip, _check_error=lambda rv: rv < 0 ) sdl_call(SDL_RenderPresent, self.renderer)