From 312e50b50b467b905c5753b39571c0aa63709721 Mon Sep 17 00:00:00 2001 From: Calvin Spealman Date: Mon, 25 May 2020 23:16:15 -0400 Subject: [PATCH] Implement sprite flipping. --- ppb/flags.py | 25 +++++++++++++++++++++++++ ppb/systems/renderer.py | 13 +++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) 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 79e88b70..9821e925 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 @@ -67,6 +68,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): @@ -201,10 +209,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)