-
-
Notifications
You must be signed in to change notification settings - Fork 168
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
Add load_animation and AnimatedSurface #2218
base: main
Are you sure you want to change the base?
Conversation
I tried to review this, but for whatever reason pygame-ce didn't seem to want to build properly with the downloaded SDL_Image 2.7.0 dll. I must be missing a step somewhere (and yes, I tried using the edited Setup file above). It looks like it has built and compiled - but actually it hasn't included SDL_image at all and |
Requires this PR: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm really interested in moving this PR forward, but the exported API may need discussion. Here's what I'm thinking
- We keep the first level API very barebones, so I'm thinking we add something like an
animation: bool
kwarg to the existingload
functions, and the return type could belist[tuple[Surface, float]]
(basically something like[(surface1, delay1), (surface2, delay2), ...]
- Later we may decide to add a higher level abstraction like the
AnimatedSurface
class
Thoughts on this, folks?
4b724ee
to
39e6057
Compare
This works now for me which is great. API wise:
if (not self.attacked and
self.current_anim in [self.anims["sword_attack_right"], self.anims["sword_attack_left"]] and
self.current_anim.frame = 12):
# maximum sword extension frame so check collision and play sound
self.attacked = True and later... if (self.attacked and
self.current_anim in [self.anims["sword_attack_right"], self.anims["sword_attack_left"]] and
self.current_anim.finished):
# sword anim finished, reset attack state and transtion back to idle animation
self.attacked = False
if self.facing = "right":
self.current_anim = self.anims["idle_right"]
else:
self.current_anim = self.anims["idle_left"]
Those are my initial ideas. Perhaps Ankith is right and we should start with getting the loading part right into a list of basic Surfaces and move the I think I'm broadly in favour of us having a basic It should also be possible to construct an |
Hello! I'm not a C programmer but I have some thoughts on the animation work. These are mainly things that I'm currently using that don't look like they'll be supported by this.
Perhaps I've misunderstood and this isn't meant to be a "kitchen sink" approach in which case feel free to ignore me. Thought I'd add my two cents. I've uploaded what I'm currently working with as I've found it's fitting all use cases so far and has evolved several times over the course of a few years to a stable state https://gist.github.com/joereynolds/1c20645bfa54bd2df62133a8fb9de7cb Thanks all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awaiting API discussion or changes.
API discussion: I don't think we should adopt this API, and I don't think this should be one PR. GIF loading should return a list of surfaces, and maybe data about timings. Ideally, the animation loading API would be general enough to also cover loading animated PNG files, short video clips, JSON+PNG sprite sheets, and ASEprite files, that means multiple animations with different frame rates and looping meta-information. The AnimatedSurface or "Clip" class should, if we adopt such a thing, be its own pull request, and it should be optional to use it. I see no reason why animation can't be implemented in pure Python, and I have done it myself multiple times. The only reason to merge this as-is would be, in my opinion, because yunline has already put so much work into it. |
I agree that any animation class would be optional to use, and generic enough to cover various use cases (animated images, videos, even streamed things from pygame.camera or network potentially?) However, I think if we have a load_animation method and an Animation class, that load_animation method should return an Animation by default. The Animation class would need a way to be unwrapped into frames / positions? / timings. Maybe it could be like Rect, a pygame class that also has a "canonical form" of a 4 element sequence. Or maybe it could get an explicit method to "unwrap" it into a frame sequence representation that can be used in any way. |
That's the worst of both worlds! We could just have the Animation class load a GIF from a static method or constructor... Anyway, something I mentioned in Discord but forgot to add here: Loading as a sprite sheet/list of subsurfaces would also make it easier to use with OpenGL/Renderer/future new renderer. Or we'd have to add more special code for loading an animated image into modernGL and so on. |
What's worst about it? If you want an animation, it's already there. If you want a list of frames, treat it as a list of frames. Are you worried about the runtime cost of constructing the class versus a bare list?
That's a good idea. It wouldn't be consistent with Surface, but I've been thinking for a bit about how un-object-ey Surface is. For example, |
If the Animation is just a pure Python data container that holds a surface, subsurfaces and lists of frames, then I might be persuaded. It should probably be able to represent ASEprite animated sprites, JSON sprite sheeps, GIFs, and small WEBM clips. If Animation is a thing that wraps a GIF, talks to the clock and automagically changes the frame, I am against it.
No, purely an API complexity/API support/implementation complexity thing. |
i'd really love to see the idea of gif loading move forward, since there are plenty of use cases for animation in general with pygame. i also think that gif saving would be a good idea if gif loading becomes a thing |
Forgive my ignorance but aren't GIFs quite large in size, would be people want that if better alternatives exist? |
Implementd
image.load_animation()
and some basic functions ofpygame.AnimatedSurface
Issues: #1054 #2181
This PR requires a SDL_image 2.6.0+
You can build it by your self or use my pre-built version.Pre-built SDL_imageSDL2_image-2.7.0-pygame-ce.zip
You may also need to use this customized
Setup
fileExample Code:
cat.gif