Skip to content

Commit

Permalink
Merge #484
Browse files Browse the repository at this point in the history
484: Implement sprite flipping. r=pathunstrom a=ironfroggy



Co-authored-by: Calvin Spealman <[email protected]>
  • Loading branch information
bors[bot] and ironfroggy authored May 17, 2021
2 parents ba91643 + 312e50b commit 92b3e05
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
25 changes: 25 additions & 0 deletions ppb/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
13 changes: 11 additions & 2 deletions ppb/systems/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 92b3e05

Please sign in to comment.