Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sprite flipping. #484

Open
wants to merge 1 commit into
base: canon
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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):
Expand Down Expand Up @@ -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)
Expand Down