Skip to content

Commit

Permalink
implement SoundAlias fix #87
Browse files Browse the repository at this point in the history
  • Loading branch information
planetis-m committed Sep 23, 2023
1 parent 80364e3 commit 1a94c2d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/raylib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1519,14 +1519,13 @@ proc isWaveReady*(wave: Wave): bool {.importc: "IsWaveReady".}
## Checks if wave data is ready
proc loadSoundPriv(fileName: cstring): Sound {.importc: "LoadSound".}
proc loadSoundFromWavePriv(wave: Wave): Sound {.importc: "LoadSoundFromWave".}
proc loadSoundAlias*(source: Sound): Sound {.importc: "LoadSoundAlias".}
## Create a new sound that shares the same sample data as the source sound, does not own the sound data
proc loadSoundAliasPriv(source: Sound): Sound {.importc: "LoadSoundAlias".}
proc isSoundReady*(sound: Sound): bool {.importc: "IsSoundReady".}
## Checks if a sound is ready
proc updateSoundPriv(sound: Sound, data: pointer, sampleCount: int32) {.importc: "UpdateSound".}
proc unloadWave(wave: Wave) {.importc: "UnloadWave".}
proc unloadSound(sound: Sound) {.importc: "UnloadSound".}
proc unloadSoundAlias*(alias: Sound) {.importc: "UnloadSoundAlias".}
proc unloadSoundAlias(alias: Sound) {.importc: "UnloadSoundAlias".}
## Unload a sound alias (does not deallocate sample data)
proc exportWave*(wave: Wave, fileName: cstring): bool {.importc: "ExportWave".}
## Export wave data to file, returns true on success
Expand Down Expand Up @@ -1627,6 +1626,7 @@ type
WeakFont* = distinct Font

ShaderLocsPtr* = distinct typeof(Shader.locs)
SoundAlias* = distinct Sound

proc `=destroy`*(x: WeakImage) = discard
proc `=dup`*(source: WeakImage): WeakImage {.nodestroy.} = source
Expand Down Expand Up @@ -1719,6 +1719,9 @@ proc `=destroy`*(x: Sound) =
proc `=dup`*(source: Sound): Sound {.error.}
proc `=copy`*(dest: var Sound; source: Sound) {.error.}

proc `=destroy`*(x: SoundAlias) =
unloadSoundAlias(Sound(x))

proc `=destroy`*(x: Music) =
unloadMusicStream(x)
proc `=dup`*(source: Music): Music {.error.}
Expand Down Expand Up @@ -2163,6 +2166,11 @@ proc loadSound*(fileName: string): Sound =
result = loadSoundPriv(fileName.cstring)
if not isSoundReady(result): raiseRaylibError("Failed to load Sound from " & fileName)

proc loadSoundAlias*(source: Sound): SoundAlias =
## Create a new sound that shares the same sample data as the source sound, does not own the sound data
result = SoundAlias(loadSoundAliasPriv(source))
if not isSoundReady(Sound(result)): raiseRaylibError("Failed to load SoundAlias from source")

proc loadSoundFromWave*(wave: Wave): Sound =
## Load sound from wave data
result = loadSoundFromWavePriv(wave)
Expand Down
2 changes: 2 additions & 0 deletions tools/raylib_gen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ const
"UnloadModelAnimation",
"UnloadWave",
"UnloadSound",
"UnloadSoundAlias",
"UnloadMusicStream",
"UnloadAudioStream",
]
Expand Down Expand Up @@ -472,6 +473,7 @@ const
"LoadModelFromMesh",
"LoadWave",
"LoadSound",
"LoadSoundAlias",
"LoadSoundFromWave",
"LoadWaveFromMemory",
"LoadMusicStream",
Expand Down
4 changes: 4 additions & 0 deletions tools/raylib_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type
WeakFont* = distinct Font

ShaderLocsPtr* = distinct typeof(Shader.locs)
SoundAlias* = distinct Sound

proc `=destroy`*(x: WeakImage) = discard
proc `=dup`*(source: WeakImage): WeakImage {.nodestroy.} = source
Expand Down Expand Up @@ -97,6 +98,9 @@ proc `=destroy`*(x: Sound) =
proc `=dup`*(source: Sound): Sound {.error.}
proc `=copy`*(dest: var Sound; source: Sound) {.error.}

proc `=destroy`*(x: SoundAlias) =
unloadSoundAlias(Sound(x))

proc `=destroy`*(x: Music) =
unloadMusicStream(x)
proc `=dup`*(source: Music): Music {.error.}
Expand Down
5 changes: 5 additions & 0 deletions tools/raylib_wrap.nim
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ proc loadSound*(fileName: string): Sound =
result = loadSoundPriv(fileName.cstring)
if not isSoundReady(result): raiseRaylibError("Failed to load Sound from " & fileName)

proc loadSoundAlias*(source: Sound): SoundAlias =
## Create a new sound that shares the same sample data as the source sound, does not own the sound data
result = SoundAlias(loadSoundAliasPriv(source))
if not isSoundReady(Sound(result)): raiseRaylibError("Failed to load SoundAlias from source")

proc loadSoundFromWave*(wave: Wave): Sound =
## Load sound from wave data
result = loadSoundFromWavePriv(wave)
Expand Down

0 comments on commit 1a94c2d

Please sign in to comment.