|
| 1 | +""" |
| 2 | +Particles Stress Demo - SpritePro |
| 3 | +
|
| 4 | +Spawns 10k image particles every second and shows timings. |
| 5 | +""" |
| 6 | + |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | +import time |
| 10 | + |
| 11 | +import pygame |
| 12 | + |
| 13 | +current_dir = Path(__file__).parent |
| 14 | +parent_dir = current_dir.parent.parent |
| 15 | +if str(parent_dir) not in sys.path: |
| 16 | + sys.path.insert(0, str(parent_dir)) |
| 17 | + |
| 18 | +import spritePro as s # noqa: E402 |
| 19 | +from spritePro.particles import ParticleConfig, ParticleEmitter # noqa: E402 |
| 20 | +from spritePro.readySprites import Text_fps # noqa: E402 |
| 21 | +from spritePro.resources import resource_cache # noqa: E402 |
| 22 | + |
| 23 | + |
| 24 | +TEXTURE_PATH = "spritePro/demoGames/Sprites/c.png" |
| 25 | +BURST_COUNT = 1_00 |
| 26 | +BURST_INTERVAL = 0.1 |
| 27 | + |
| 28 | + |
| 29 | +def _load_particle_image() -> pygame.Surface: |
| 30 | + img = resource_cache.load_texture(TEXTURE_PATH) |
| 31 | + if img is None: |
| 32 | + img = pygame.Surface((24, 24), pygame.SRCALPHA) |
| 33 | + img.fill((255, 255, 255, 255)) |
| 34 | + return img |
| 35 | + |
| 36 | + |
| 37 | +def main() -> None: |
| 38 | + s.init() |
| 39 | + screen = s.get_screen((900, 600), "Particles Stress Demo") |
| 40 | + center = screen.get_rect().center |
| 41 | + |
| 42 | + image = _load_particle_image() |
| 43 | + cfg = ParticleConfig( |
| 44 | + amount=BURST_COUNT, |
| 45 | + lifetime_range=(1.5, 2.0), |
| 46 | + speed_range=(40.0, 160.0), |
| 47 | + fade_speed=220.0, |
| 48 | + image=image, |
| 49 | + image_scale_range=(0.05, 0.4), |
| 50 | + angle_range=(0.0, 360.0), |
| 51 | + spawn_circle_radius=30, |
| 52 | + ) |
| 53 | + emitter = ParticleEmitter(cfg, use_pool=True, max_pool_size=BURST_COUNT * 2) |
| 54 | + |
| 55 | + fps_counter = Text_fps( |
| 56 | + pos=(10, 8), color=(255, 255, 0), prefix="FPS: ", precision=1 |
| 57 | + ) |
| 58 | + stats = s.TextSprite("", 20, (200, 220, 255), (450, 40)) |
| 59 | + _hint = s.TextSprite( |
| 60 | + f"Spawn: {BURST_COUNT} particles every {BURST_INTERVAL:.1f}s", |
| 61 | + 18, |
| 62 | + (180, 180, 180), |
| 63 | + (450, 70), |
| 64 | + ) |
| 65 | + |
| 66 | + total_emitted = 0 |
| 67 | + last_emit_ms = 0.0 |
| 68 | + last_emit_count = 0 |
| 69 | + |
| 70 | + def emit_burst() -> None: |
| 71 | + nonlocal total_emitted, last_emit_ms, last_emit_count |
| 72 | + start = time.perf_counter() |
| 73 | + particles = emitter.emit(center) |
| 74 | + last_emit_ms = (time.perf_counter() - start) * 1000.0 |
| 75 | + last_emit_count = len(particles) |
| 76 | + total_emitted += last_emit_count |
| 77 | + stats.set_text( |
| 78 | + f"Burst: {last_emit_count} | Total: {total_emitted} | Emit: {last_emit_ms:.2f}ms" |
| 79 | + ) |
| 80 | + |
| 81 | + stats.set_text("Waiting for burst...") |
| 82 | + s.Timer(BURST_INTERVAL, callback=emit_burst, repeat=True, autostart=True) |
| 83 | + emit_burst() |
| 84 | + |
| 85 | + while True: |
| 86 | + fps_counter.update_fps() |
| 87 | + s.update(fill_color=(12, 12, 20)) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments