changeset 679:28e704ca739b

did: some refactoring for proper integration of image/audio resources with resource system
author Sam <sam@basx.dev>
date Fri, 12 May 2023 00:33:24 +0700
parents d9a0023dd0fa
children d3e62cd055d1
files src/semicongine/audio.nim src/semicongine/core/audiotypes.nim src/semicongine/resources.nim src/semicongine/resources/audio.nim src/semicongine/resources/image.nim
diffstat 5 files changed, 31 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/semicongine/audio.nim	Fri May 12 00:13:30 2023 +0700
+++ b/src/semicongine/audio.nim	Fri May 12 00:33:24 2023 +0700
@@ -52,7 +52,7 @@
     mixer.buffers.add newSeq[Sample](BUFFERSAMPLECOUNT)
   for i in 0 ..< mixer.buffers.len:
     bufferaddresses.add (addr mixer.buffers[i])
-  mixer.device = openSoundDevice(44100, bufferaddresses)
+  mixer.device = openSoundDevice(AUDIO_SAMPLE_RATE, bufferaddresses)
 
 proc loadSound*(mixer: var Mixer, name: string, resource: string) =
   assert not (name in mixer.sounds)
@@ -169,7 +169,6 @@
           track.playing.del(id)
       mixer.buffers[mixer.currentBuffer][i] = currentSample
   # send data to sound device
-  # mixer.device.writeSoundData((mixer.currentBuffer - 1) %% mixer.buffers.len)
   mixer.device.writeSoundData(mixer.currentBuffer)
   mixer.currentBuffer = (mixer.currentBuffer + 1) mod mixer.buffers.len
 
--- a/src/semicongine/core/audiotypes.nim	Fri May 12 00:13:30 2023 +0700
+++ b/src/semicongine/core/audiotypes.nim	Fri May 12 00:33:24 2023 +0700
@@ -5,6 +5,8 @@
 #
 # ffmpeg -i <infile> -f s16le -ac 2 -ar 48000 -acodec pcm_s16le <outfile>
 
+const AUDIO_SAMPLE_RATE* = 48000
+
 type
   Level* = 0'f .. 1'f
   Sample* = (int16, int16)
--- a/src/semicongine/resources.nim	Fri May 12 00:13:30 2023 +0700
+++ b/src/semicongine/resources.nim	Fri May 12 00:33:24 2023 +0700
@@ -1,10 +1,15 @@
+import std/streams
 import std/strutils
 import std/os
 
 import ./core
+import ./resources/image
+import ./resources/audio
+
+export image
+export audio
 
 type
-  Binary* = seq[uint8]
   ResourceBundlingType = enum
     Dir # Directories
     Zip # Zip files
@@ -22,8 +27,8 @@
   proc modRoot(): string =
     joinPath(resourceRoot(), selectedMod)
 
-  proc loadResource_intern(path: string): Binary =
-    cast[Binary](readFile(joinPath(modRoot(), path)))
+  proc loadResource_intern(path: string): Stream =
+    newFileStream(joinPath(modRoot(), path), fmRead)
 
   proc modList_intern(): seq[string] =
     for kind, file in walkDir(resourceRoot(), relative=true):
@@ -43,9 +48,9 @@
   proc modRoot(): string =
     joinPath(resourceRoot(), selectedMod)
 
-  proc loadResource_intern(path: string): Binary =
+  proc loadResource_intern(path: string): Stream =
     let reader = openZipArchive(modRoot() & ".zip")
-    result = cast[Binary](reader.extractFile(path))
+    result = newStringStream(reader.extractFile(path))
     reader.close()
 
   proc modList_intern(): seq[string] =
@@ -77,9 +82,9 @@
           result[modname][resourcefile] = staticRead(joinPath(moddir, resourcefile))
   const bundledResources = loadResources()
 
-  proc loadResource_intern(path: string): Binary =
+  proc loadResource_intern(path: string): Stream =
     # TODO: add Lempel–Ziv–Welch compression or something similar simple
-    cast[seq[uint8]](bundledResources[selectedMod][path])
+    newStringStream(bundledResources[selectedMod][path])
 
   proc modList_intern(): seq[string] =
     result = bundledResources.keys().toSeq()
@@ -88,9 +93,14 @@
     for i in bundledResources[selectedMod].keys:
       yield i
 
-proc loadResource*(path: string): ref Binary =
-    result = new Binary
-    result[] = loadResource_intern(path)
+proc loadResource*(path: string): Stream =
+    loadResource_intern(path)
+
+proc loadImage*(path: string): Image =
+    loadResource_intern(path).readBMP()
+
+proc loadAudio*(path: string): Sound =
+    loadResource_intern(path).readAU()
 
 proc modList*(): seq[string] =
   modList_intern()
--- a/src/semicongine/resources/audio.nim	Fri May 12 00:13:30 2023 +0700
+++ b/src/semicongine/resources/audio.nim	Fri May 12 00:33:24 2023 +0700
@@ -1,2 +1,7 @@
+import std/streams
 
+import ../core/audiotypes
 
+# https://en.wikipedia.org/wiki/Au_file_format
+proc readAU*(stream: Stream): Sound =
+  result
--- a/src/semicongine/resources/image.nim	Fri May 12 00:13:30 2023 +0700
+++ b/src/semicongine/resources/image.nim	Fri May 12 00:33:24 2023 +0700
@@ -36,13 +36,15 @@
     gammaGreen: uint32 # not used yet
     gammaBlue: uint32 # not used yet
 
-proc loadBitmap(stream: Stream): Image =
+proc readBMP*(stream: Stream): Image =
   var
     bitmapFileHeader: BitmapFileHeader
     dibHeader: DIBHeader
 
   for name, value in fieldPairs(bitmapFileHeader):
     stream.read(value)
+  if bitmapFileHeader.magicbytes != ['B', 'M']:
+    raise newException(Exception, "Cannot open image, invalid magic bytes (is this really a BMP bitmap?)")
   for name, value in fieldPairs(dibHeader):
 
     when name in ["bitMaskRed", "bitMaskGreen", "bitMaskBlue"]:
@@ -100,6 +102,3 @@
     stream.setPosition(stream.getPosition() + padding)
 
   result = newImage(width=uint32(dibHeader.width), height=uint32(abs(dibHeader.height)), imagedata=data)
-
-proc loadBitmap(file: string): Image =
-  loadBitmap(newFileStream(file, fmRead))