sound_buffer.nim 929 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. when defined(NoSFML) or defined(NoChipmunk):
  2. {.error.}
  3. import sfml_audio, sfml_stuff, sg_assets, chipmunk
  4. const
  5. MinDistance* = 350.0
  6. Attenuation* = 20.0
  7. var
  8. liveSounds: seq[PSound] = @[]
  9. deadSounds: seq[PSound] = @[]
  10. proc playSound*(sound: PSoundRecord, pos: TVector) =
  11. if sound.isNil or sound.soundBuf.isNil: return
  12. var s: PSound
  13. if deadSounds.len == 0:
  14. s = sfml_audio.newSound()
  15. s.setLoop false
  16. s.setRelativeToListener true
  17. s.setAttenuation Attenuation
  18. s.setMinDistance MinDistance
  19. else:
  20. s = deadSounds.pop()
  21. s.setPosition(vec3f(pos.x, 0, pos.y))
  22. s.setBuffer(sound.soundBuf)
  23. s.play()
  24. liveSounds.add s
  25. proc updateSoundBuffer*() =
  26. var i = 0
  27. while i < len(liveSounds):
  28. if liveSounds[i].getStatus == Stopped:
  29. deadSounds.add liveSounds[i]
  30. liveSounds.del i
  31. else:
  32. inc i
  33. proc report*() =
  34. echo "live: ", liveSounds.len
  35. echo "dead: ", deadSounds.len