changeset 719:f52bac7a1c73

fix: few issues with sound and resource loading
author Sam <sam@basx.dev>
date Wed, 24 May 2023 23:24:52 +0700
parents 76af5ffc0cb5
children 96976d3d5364
files src/semicongine/audio.nim src/semicongine/resources.nim
diffstat 2 files changed, 11 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/semicongine/audio.nim	Wed May 24 23:00:21 2023 +0700
+++ b/src/semicongine/audio.nim	Wed May 24 23:24:52 2023 +0700
@@ -9,6 +9,7 @@
 
 import ./core
 import ./platform/audio
+import ./resources
 
 export audiotypes
 
@@ -35,9 +36,6 @@
     buffers: seq[SoundData]
     currentBuffer: int
 
-proc loadSoundResource(resourcePath: string): Sound =
-  assert false, "Not implemented yet"
-
 proc initMixer*(): Mixer =
   result = Mixer(
     tracks: {"": Track(level: 1'f)}.toTable,
@@ -56,7 +54,7 @@
 
 proc loadSound*(mixer: var Mixer, name: string, resource: string) =
   assert not (name in mixer.sounds)
-  mixer.sounds[name] = loadSoundResource(resource)
+  mixer.sounds[name] = loadAudio(resource)
 
 proc addSound*(mixer: var Mixer, name: string, sound: Sound) =
   assert not (name in mixer.sounds)
@@ -66,10 +64,10 @@
   assert (name in mixer.sounds)
   mixer.sounds[name] = sound
 
-proc addTrack*(mixer: var Mixer, name: string) =
+proc addTrack*(mixer: var Mixer, name: string, level=1'f) =
   assert not (name in mixer.tracks)
   mixer.lock.withLock():
-    mixer.tracks[name] = Track(level: 1'f)
+    mixer.tracks[name] = Track(level: level)
 
 proc play*(mixer: var Mixer, soundName: string, track="", stopOtherSounds=false, loop=false, levelLeft, levelRight: Level): uint64 =
   assert track in mixer.tracks
--- a/src/semicongine/resources.nim	Wed May 24 23:00:21 2023 +0700
+++ b/src/semicongine/resources.nim	Wed May 24 23:24:52 2023 +0700
@@ -1,5 +1,6 @@
 import std/streams
 import std/strutils
+import std/strformat
 import std/os
 
 import ./core
@@ -32,6 +33,8 @@
     joinPath(resourceRoot(), selectedMod)
 
   proc loadResource_intern(path: string): Stream =
+    if not path.fileExists():
+      raise newException(Exception, &"Resource {path} not found")
     newFileStream(joinPath(modRoot(), path), fmRead)
 
   proc modList_intern(): seq[string] =
@@ -53,6 +56,8 @@
     joinPath(resourceRoot(), selectedMod)
 
   proc loadResource_intern(path: string): Stream =
+    if not path.fileExists():
+      raise newException(Exception, &"Resource {path} not found")
     let archive = openZipArchive(modRoot() & ".zip")
     # read all here so we can close the stream
     result = newStringStream(archive.extractFile(path))
@@ -89,7 +94,8 @@
   const bundledResources = loadResources()
 
   proc loadResource_intern(path: string): Stream =
-    # TODO: add Lempel–Ziv–Welch compression or something similar simple
+    if not (path in bundledResources[selectedMod]):
+      raise newException(Exception, &"Resource {path} not found")
     newStringStream(bundledResources[selectedMod][path])
 
   proc modList_intern(): seq[string] =