# HG changeset patch # User Sam # Date 1683826404 -25200 # Node ID 28e704ca739badfcb3fe045d1e7ce53da62a49ad # Parent d9a0023dd0faed3ac429d25b617b16f0041e7fe9 did: some refactoring for proper integration of image/audio resources with resource system diff -r d9a0023dd0fa -r 28e704ca739b src/semicongine/audio.nim --- 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 diff -r d9a0023dd0fa -r 28e704ca739b src/semicongine/core/audiotypes.nim --- 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 -f s16le -ac 2 -ar 48000 -acodec pcm_s16le +const AUDIO_SAMPLE_RATE* = 48000 + type Level* = 0'f .. 1'f Sample* = (int16, int16) diff -r d9a0023dd0fa -r 28e704ca739b src/semicongine/resources.nim --- 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() diff -r d9a0023dd0fa -r 28e704ca739b src/semicongine/resources/audio.nim --- 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 diff -r d9a0023dd0fa -r 28e704ca739b src/semicongine/resources/image.nim --- 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))