Add animated start menu and level music config
This commit is contained in:
+33
-1
@@ -11,7 +11,7 @@ except ImportError:
|
||||
sdlmixer = None
|
||||
from sdl2.ext.compat import byteify
|
||||
from sdl2 import SDL_AudioSpec
|
||||
from PIL import Image
|
||||
from PIL import Image, ImageSequence
|
||||
|
||||
from runtime_paths import resolve_bundle_path
|
||||
|
||||
@@ -259,6 +259,38 @@ class GameWindow:
|
||||
sdl2.SDL_FreeSurface(temp_surface)
|
||||
return texture
|
||||
|
||||
def load_animation(self, path):
|
||||
"""Load a GIF animation as SDL textures with frame durations."""
|
||||
image_path = resolve_bundle_path(os.path.join("assets", path))
|
||||
image = Image.open(image_path)
|
||||
frames = []
|
||||
durations = []
|
||||
|
||||
for frame in ImageSequence.Iterator(image):
|
||||
rgba_frame = frame.convert("RGBA")
|
||||
duration = int(frame.info.get("duration", 83))
|
||||
duration = max(20, duration)
|
||||
temp_surface = sdl2.ext.pillow_to_surface(rgba_frame)
|
||||
texture = self.factory.from_surface(temp_surface)
|
||||
sdl2.SDL_FreeSurface(temp_surface)
|
||||
frames.append(texture)
|
||||
durations.append(duration)
|
||||
|
||||
if not frames:
|
||||
rgba_frame = image.convert("RGBA")
|
||||
temp_surface = sdl2.ext.pillow_to_surface(rgba_frame)
|
||||
texture = self.factory.from_surface(temp_surface)
|
||||
sdl2.SDL_FreeSurface(temp_surface)
|
||||
frames.append(texture)
|
||||
durations.append(83)
|
||||
|
||||
return {
|
||||
"frames": frames,
|
||||
"durations": durations,
|
||||
"total_duration": sum(durations),
|
||||
"size": frames[0].size,
|
||||
}
|
||||
|
||||
def get_image_size(self, image):
|
||||
"""Get the size of an image sprite"""
|
||||
return image.size
|
||||
|
||||
Reference in New Issue
Block a user