Mercurial > games > semicongine
changeset 1282:3308b88e53a6
did: undo part of stupid API renaming a few weeks back ;(
author | sam <sam@basx.dev> |
---|---|
date | Mon, 29 Jul 2024 16:05:09 +0700 |
parents | c09cdff9a97e |
children | 0369fa1ffbd9 |
files | semicongine/audio.nim semicongine/audio/generators.nim semicongine/audio/mixer.nim semicongine/audio/resources.nim semicongine/contrib/algorithms/collision.nim semicongine/contrib/algorithms/noise.nim semicongine/contrib/algorithms/texture_packing.nim semicongine/text/font.nim tests/test_audio.nim tests/test_text.nim |
diffstat | 10 files changed, 94 insertions(+), 94 deletions(-) [+] |
line wrap: on
line diff
--- a/semicongine/audio.nim Mon Jul 29 15:53:03 2024 +0700 +++ b/semicongine/audio.nim Mon Jul 29 16:05:09 2024 +0700 @@ -2,4 +2,4 @@ include ./audio/generators include ./audio/resources -StartMixerThread() +startMixerThread()
--- a/semicongine/audio/generators.nim Mon Jul 29 15:53:03 2024 +0700 +++ b/semicongine/audio/generators.nim Mon Jul 29 16:05:09 2024 +0700 @@ -3,7 +3,7 @@ sin(x * 2 * Pi * f) result = ret -proc SineSoundData*(f: float, len: float, rate: int, amplitude = 0.5'f32): SoundData = +proc sineSoundData*(f: float, len: float, rate: int, amplitude = 0.5'f32): SoundData = let dt = 1'f / float(rate) var sine = sinewave(f) for i in 0 ..< int(float(rate) * len):
--- a/semicongine/audio/mixer.nim Mon Jul 29 15:53:03 2024 +0700 +++ b/semicongine/audio/mixer.nim Mon Jul 29 16:05:09 2024 +0700 @@ -59,20 +59,20 @@ # assert not (name in mixer.sounds) # mixer.sounds[name] = LoadAudio(resource) -proc AddSound*(mixer: var Mixer, name: string, sound: SoundData) = +proc addSound*(mixer: var Mixer, name: string, sound: SoundData) = assert not (name in mixer.sounds) mixer.sounds[name] = sound -proc ReplaceSound*(mixer: var Mixer, name: string, sound: SoundData) = +proc replaceSound*(mixer: var Mixer, name: string, sound: SoundData) = assert (name in mixer.sounds) mixer.sounds[name] = sound -proc AddTrack*(mixer: var Mixer, name: string, level: Level = 1'f) = +proc addTrack*(mixer: var Mixer, name: string, level: Level = 1'f) = assert not (name in mixer.tracks) mixer.lock.withLock(): mixer.tracks[name] = Track(level: level) -proc Play*(mixer: var Mixer, soundName: string, track = "", stopOtherSounds = false, loop = false, levelLeft, levelRight: Level): uint64 = +proc play*(mixer: var Mixer, soundName: string, track = "", stopOtherSounds = false, loop = false, levelLeft, levelRight: Level): uint64 = assert track in mixer.tracks, &"Track '{track}' does not exists" assert soundName in mixer.sounds, soundName & " not loaded" mixer.lock.withLock(): @@ -89,8 +89,8 @@ result = mixer.playbackCounter inc mixer.playbackCounter -proc Play*(mixer: var Mixer, soundName: string, track = "", stopOtherSounds = false, loop = false, level: Level = 1'f): uint64 = - Play( +proc play*(mixer: var Mixer, soundName: string, track = "", stopOtherSounds = false, loop = false, level: Level = 1'f): uint64 = + play( mixer = mixer, soundName = soundName, track = track, @@ -100,73 +100,73 @@ levelRight = level ) -proc Stop*(mixer: var Mixer) = +proc stop*(mixer: var Mixer) = mixer.lock.withLock(): for track in mixer.tracks.mvalues: track.playing.clear() -proc GetLevel*(mixer: var Mixer): Level = mixer.level -proc GetLevel*(mixer: var Mixer, track: string): Level = mixer.tracks[track].level -proc GetLevel*(mixer: var Mixer, playbackId: uint64): (Level, Level) = +proc getLevel*(mixer: var Mixer): Level = mixer.level +proc getLevel*(mixer: var Mixer, track: string): Level = mixer.tracks[track].level +proc getLevel*(mixer: var Mixer, playbackId: uint64): (Level, Level) = for track in mixer.tracks.mvalues: if playbackId in track.playing: return (track.playing[playbackId].levelLeft, track.playing[playbackId].levelRight) -proc SetLevel*(mixer: var Mixer, level: Level) = mixer.level = level -proc SetLevel*(mixer: var Mixer, track: string, level: Level) = +proc setLevel*(mixer: var Mixer, level: Level) = mixer.level = level +proc setLevel*(mixer: var Mixer, track: string, level: Level) = mixer.lock.withLock(): mixer.tracks[track].level = level -proc SetLevel*(mixer: var Mixer, playbackId: uint64, levelLeft, levelRight: Level) = +proc setLevel*(mixer: var Mixer, playbackId: uint64, levelLeft, levelRight: Level) = mixer.lock.withLock(): for track in mixer.tracks.mvalues: if playbackId in track.playing: track.playing[playbackId].levelLeft = levelLeft track.playing[playbackId].levelRight = levelRight -proc SetLevel*(mixer: var Mixer, playbackId: uint64, level: Level) = - SetLevel(mixer, playbackId, level, level) +proc setLevel*(mixer: var Mixer, playbackId: uint64, level: Level) = + setLevel(mixer, playbackId, level, level) -proc Stop*(mixer: var Mixer, track: string) = +proc stop*(mixer: var Mixer, track: string) = assert track in mixer.tracks mixer.lock.withLock(): mixer.tracks[track].playing.clear() -proc Stop*(mixer: var Mixer, playbackId: uint64) = +proc stop*(mixer: var Mixer, playbackId: uint64) = mixer.lock.withLock(): for track in mixer.tracks.mvalues: if playbackId in track.playing: track.playing.del(playbackId) break -proc Pause*(mixer: var Mixer, value: bool) = +proc pause*(mixer: var Mixer, value: bool) = mixer.lock.withLock(): for track in mixer.tracks.mvalues: for playback in track.playing.mvalues: playback.paused = value -proc Pause*(mixer: var Mixer, track: string, value: bool) = +proc pause*(mixer: var Mixer, track: string, value: bool) = mixer.lock.withLock(): for playback in mixer.tracks[track].playing.mvalues: playback.paused = value -proc Pause*(mixer: var Mixer, playbackId: uint64, value: bool) = +proc pause*(mixer: var Mixer, playbackId: uint64, value: bool) = mixer.lock.withLock(): for track in mixer.tracks.mvalues: if playbackId in track.playing: track.playing[playbackId].paused = value -proc Pause*(mixer: var Mixer) = mixer.Pause(true) -proc Pause*(mixer: var Mixer, track: string) = mixer.Pause(track, true) -proc Pause*(mixer: var Mixer, playbackId: uint64) = mixer.Pause(playbackId, true) -proc Unpause*(mixer: var Mixer) = mixer.Pause(false) -proc Unpause*(mixer: var Mixer, track: string) = mixer.Pause(track, false) -proc Unpause*(mixer: var Mixer, playbackId: uint64) = mixer.Pause(playbackId, false) +proc pause*(mixer: var Mixer) = mixer.pause(true) +proc pause*(mixer: var Mixer, track: string) = mixer.pause(track, true) +proc pause*(mixer: var Mixer, playbackId: uint64) = mixer.pause(playbackId, true) +proc unpause*(mixer: var Mixer) = mixer.pause(false) +proc unpause*(mixer: var Mixer, track: string) = mixer.pause(track, false) +proc unpause*(mixer: var Mixer, playbackId: uint64) = mixer.pause(playbackId, false) -proc FadeTo*(mixer: var Mixer, track: string, level: Level, time: float) = +proc fadeTo*(mixer: var Mixer, track: string, level: Level, time: float) = mixer.tracks[track].targetLevel = level mixer.tracks[track].fadeTime = time mixer.tracks[track].fadeStep = level.float - mixer.tracks[track].level.float / time -proc IsPlaying*(mixer: var Mixer): bool = +proc isPlaying*(mixer: var Mixer): bool = mixer.lock.withLock(): for track in mixer.tracks.mvalues: for playback in track.playing.values: @@ -174,7 +174,7 @@ return true return false -proc IsPlaying*(mixer: var Mixer, track: string): bool = +proc isPlaying*(mixer: var Mixer, track: string): bool = mixer.lock.withLock(): if mixer.tracks.contains(track): for playback in mixer.tracks[track].playing.values: @@ -293,7 +293,7 @@ when defined(linux): import std/posix -proc StartMixerThread() = +proc startMixerThread() = mixer[] = initMixer() audiothread.createThread(audioWorker) debug "Created audio thread"
--- a/semicongine/audio/resources.nim Mon Jul 29 15:53:03 2024 +0700 +++ b/semicongine/audio/resources.nim Mon Jul 29 16:05:09 2024 +0700 @@ -28,7 +28,7 @@ result[1] = result[0] # https://en.wikipedia.org/wiki/Au_file_format -proc ReadAU*(stream: Stream): SoundData = +proc readAU*(stream: Stream): SoundData = var header: AuHeader for name, value in fieldPairs(header): @@ -55,7 +55,7 @@ proc stb_vorbis_decode_memory(mem: pointer, len: cint, channels: ptr cint, sample_rate: ptr cint, output: ptr ptr cshort): cint {.importc.} -proc ReadVorbis*(stream: Stream): SoundData = +proc readVorbis*(stream: Stream): SoundData = var data = stream.readAll() channels: cint @@ -82,11 +82,11 @@ nativeFree(output) raise newException(Exception, "Only support mono and stereo audio at the moment (1 or 2 channels), but found " & $channels) -proc LoadAudio*(path: string, package = DEFAULT_PACKAGE): SoundData = +proc loadAudio*(path: string, package = DEFAULT_PACKAGE): SoundData = if path.splitFile().ext.toLowerAscii == ".au": - loadResource_intern(path, package = package).ReadAU() + loadResource_intern(path, package = package).readAU() elif path.splitFile().ext.toLowerAscii == ".ogg": - loadResource_intern(path, package = package).ReadVorbis() + loadResource_intern(path, package = package).readVorbis() else: raise newException(Exception, "Unsupported audio file type: " & path)
--- a/semicongine/contrib/algorithms/collision.nim Mon Jul 29 15:53:03 2024 +0700 +++ b/semicongine/contrib/algorithms/collision.nim Mon Jul 29 16:05:09 2024 +0700 @@ -14,7 +14,7 @@ func between(value, b1, b2: float32): bool = min(b1, b2) <= value and value <= max(b1, b2) -func Contains*(collider: Collider, x: Vec3f): bool = +func contains*(collider: Collider, x: Vec3f): bool = # from https://math.stackexchange.com/questions/1472049/check-if-a-point-is-inside-a-rectangular-shaped-area-3d case collider.theType: of Box: @@ -309,7 +309,7 @@ result = (normal: vec3(minNormal.x, minNormal.y, 0), penetrationDepth: minDistance + 0.001'f32) -func Intersects*(a, b: Collider, as2D = false): bool = +func intersects*(a, b: Collider, as2D = false): bool = var support = supportPoint(a, b, vec3(0.8153, -0.4239, if as2D: 0.0 else: 0.5786)) # just random initial vector simplex = newSeq[Vec3f]() @@ -328,7 +328,7 @@ direction[0] = 0.0001 inc n -func Collision*(a, b: Collider, as2D = false): tuple[hasCollision: bool, normal: Vec3f, penetrationDepth: float32] = +func collision*(a, b: Collider, as2D = false): tuple[hasCollision: bool, normal: Vec3f, penetrationDepth: float32] = var support = supportPoint(a, b, vec3(0.8153, -0.4239, if as2D: 0.0 else: 0.5786)) # just random initial vector simplex = newSeq[Vec3f]() @@ -348,7 +348,7 @@ direction[0] = 0.0001 inc n -func CalculateCollider*(points: openArray[Vec3f], theType: ColliderType): Collider = +func calculateCollider(points: openArray[Vec3f], theType: ColliderType): Collider = var minX = high(float32) maxX = low(float32)
--- a/semicongine/contrib/algorithms/noise.nim Mon Jul 29 15:53:03 2024 +0700 +++ b/semicongine/contrib/algorithms/noise.nim Mon Jul 29 16:05:09 2024 +0700 @@ -6,7 +6,7 @@ # with Smootherstep (b - a) * ((weight * (weight * 6.0 - 15.0) + 10.0) * weight * weight * weight) + a; -proc Perlin*(pos: Vec2f, seed: int32 = 0): float32 = +proc perlin*(pos: Vec2f, seed: int32 = 0): float32 = let # grid coordinates around target point topleft = vec2(trunc(pos.x), trunc(pos.y))
--- a/semicongine/contrib/algorithms/texture_packing.nim Mon Jul 29 15:53:03 2024 +0700 +++ b/semicongine/contrib/algorithms/texture_packing.nim Mon Jul 29 16:05:09 2024 +0700 @@ -19,7 +19,7 @@ if overlapping: (true, fix.x + fix.w) # next free x coordinate to the right else: (false, newRect.x) # current position is fine -proc find_insertion_position(alreadyPlaced: seq[Rect], area: tuple[i: int, w, h: uint32], maxDim: uint32): (bool, Rect) = +proc findInsertionPosition(alreadyPlaced: seq[Rect], area: tuple[i: int, w, h: uint32], maxDim: uint32): (bool, Rect) = var newRect = (i: area.i, x: 0'u32, y: 0'u32, w: area.w, h: area.h) while newRect.y + newRect.h <= maxDim: @@ -42,7 +42,7 @@ return (false, newRect) -proc Pack*[T: PixelType](images: seq[Image[T]]): tuple[atlas: Image[T], coords: seq[tuple[x: uint32, y: uint32]]] = +proc pack*[T: PixelType](images: seq[Image[T]]): tuple[atlas: Image[T], coords: seq[tuple[x: uint32, y: uint32]]] = const MAX_ATLAS_SIZE = 4096'u32 var areas: seq[tuple[i: int, w, h: uint32]] @@ -54,11 +54,11 @@ var maxDim = 128'u32 for area in areasBySize: - var pos = find_insertion_position(assignedAreas, area, maxDim) + var pos = findInsertionPosition(assignedAreas, area, maxDim) while not pos[0]: # this should actually never loop more than once, but weird things happen ¯\_(ツ)_/¯ maxDim = maxDim * 2 assert maxDim <= MAX_ATLAS_SIZE, &"Atlas gets bigger than {MAX_ATLAS_SIZE}, cannot pack images" - pos = find_insertion_position(assignedAreas, area, maxDim) + pos = findInsertionPosition(assignedAreas, area, maxDim) assignedAreas.add pos[1]
--- a/semicongine/text/font.nim Mon Jul 29 15:53:03 2024 +0700 +++ b/semicongine/text/font.nim Mon Jul 29 16:05:09 2024 +0700 @@ -16,7 +16,7 @@ proc stbtt_GetFontVMetrics(info: ptr stbtt_fontinfo, ascent, descent, lineGap: ptr cint) {.importc, nodecl.} -proc ReadTrueType*(stream: Stream, name: string, codePoints: seq[Rune], lineHeightPixels: float32): Font = +proc readTrueType(stream: Stream, name: string, codePoints: seq[Rune], lineHeightPixels: float32): Font = var indata = stream.readAll() fontinfo: stbtt_fontinfo @@ -74,7 +74,7 @@ nativeFree(data) - let packed = Pack(images) + let packed = pack(images) result.fontAtlas = packed.atlas @@ -111,7 +111,7 @@ cint(codePointAfter) )) * result.fontscale -proc LoadFont*( +proc loadFont*( path: string, name = "", lineHeightPixels = 80'f32, @@ -122,9 +122,9 @@ var thename = name if thename == "": thename = path.splitFile().name - loadResource_intern(path, package = package).ReadTrueType(thename, charset & additional_codepoints.toSeq, lineHeightPixels) + loadResource_intern(path, package = package).readTrueType(thename, charset & additional_codepoints.toSeq, lineHeightPixels) -func TextWidth*(text: seq[Rune], font: FontObj): float32 = +func textWidth*(text: seq[Rune], font: FontObj): float32 = var currentWidth = 0'f32 var lineWidths: seq[float32] for i in 0 ..< text.len: @@ -160,16 +160,16 @@ assert currentWord[^1] != SPACE # if this is the first word of the line and it is too long we need to # split by character - if currentLine.len == 0 and (SPACE & currentWord).TextWidth(font) > maxWidth: + if currentLine.len == 0 and (SPACE & currentWord).textWidth(font) > maxWidth: var subWord = @[currentWord[0]] for c in currentWord[1 .. ^1]: - if (subWord & c).TextWidth(font) > maxWidth: + if (subWord & c).textWidth(font) > maxWidth: break subWord.add c result.add subWord & NEWLINE remaining.add currentWord[subWord.len .. ^1] # process rest of the word in next iteration else: - if (currentLine & SPACE & currentWord).TextWidth(font) <= maxWidth: + if (currentLine & SPACE & currentWord).textWidth(font) <= maxWidth: if currentLine.len == 0: currentLine = currentWord else:
--- a/tests/test_audio.nim Mon Jul 29 15:53:03 2024 +0700 +++ b/tests/test_audio.nim Mon Jul 29 16:05:09 2024 +0700 @@ -6,42 +6,42 @@ proc test1() = - mixer[].AddSound("test1", SineSoundData(1000, 2, 44100)) - mixer[].AddSound("test2", SineSoundData(500, 2, 44100)) + mixer[].addSound("test1", sineSoundData(1000, 2, 44100)) + mixer[].addSound("test2", sineSoundData(500, 2, 44100)) - let s1 = mixer[].Play("test1", loop = true) - let s2 = mixer[].Play("test2", loop = true) + let s1 = mixer[].play("test1", loop = true) + let s2 = mixer[].play("test2", loop = true) let t0 = now() - mixer[].SetLevel(0.5) + mixer[].setLevel(0.5) while true: let runtime = (now() - t0).inMilliseconds() if runtime > 1500: - mixer[].SetLevel(0.2) + mixer[].setLevel(0.2) if runtime > 3000: - mixer[].Stop(s2) + mixer[].stop(s2) if runtime > 6000: - mixer[].Stop("") + mixer[].stop("") if runtime > 8000: break proc test2() = let # notes - c = SineSoundData(261.6256, 0.5, 44100) - d = SineSoundData(293.6648, 0.5, 44100) - e = SineSoundData(329.6276, 0.5, 44100) - f = SineSoundData(349.2282, 0.5, 44100) - g = SineSoundData(391.9954, 0.5, 44100) - a = SineSoundData(440.0000, 0.5, 44100) - b = SineSoundData(493.8833, 0.5, 44100) - bb = SineSoundData(466.1638, 0.5, 44100) - c2 = SineSoundData(523.2511, 0.5, 44100) - d2 = SineSoundData(587.3295, 0.5, 44100) - bbShort = SineSoundData(466.1638, 0.25, 44100) - c2Short = SineSoundData(523.2511, 0.25, 44100) - d2Short = SineSoundData(587.3295, 0.25, 44100) + c = sineSoundData(261.6256, 0.5, 44100) + d = sineSoundData(293.6648, 0.5, 44100) + e = sineSoundData(329.6276, 0.5, 44100) + f = sineSoundData(349.2282, 0.5, 44100) + g = sineSoundData(391.9954, 0.5, 44100) + a = sineSoundData(440.0000, 0.5, 44100) + b = sineSoundData(493.8833, 0.5, 44100) + bb = sineSoundData(466.1638, 0.5, 44100) + c2 = sineSoundData(523.2511, 0.5, 44100) + d2 = sineSoundData(587.3295, 0.5, 44100) + bbShort = sineSoundData(466.1638, 0.25, 44100) + c2Short = sineSoundData(523.2511, 0.25, 44100) + d2Short = sineSoundData(587.3295, 0.25, 44100) # song frerejaquesData = concat( @@ -55,33 +55,33 @@ f, c, f, f, ) - mixer[].AddSound("frerejaques", frerejaquesData) - discard mixer[].Play("frerejaques") + mixer[].addSound("frerejaques", frerejaquesData) + discard mixer[].play("frerejaques") - while mixer[].IsPlaying(): + while mixer[].isPlaying(): sleep(1) proc test3() = - mixer[].AddSound("toccata et fugue", LoadAudio("toccata_et_fugue.ogg")) - mixer[].AddSound("ping", SineSoundData(500, 0.05, 44100)) - mixer[].AddTrack("effects") - discard mixer[].Play("toccata et fugue") + mixer[].addSound("toccata et fugue", loadAudio("toccata_et_fugue.ogg")) + mixer[].addSound("ping", sineSoundData(500, 0.05, 44100)) + mixer[].addTrack("effects") + discard mixer[].play("toccata et fugue") when isMainModule: test1() - mixer[].Stop() + mixer[].stop() test2() - mixer[].Stop() + mixer[].stop() test3() - while mixer[].IsPlaying(): - discard mixer[].Play("ping", track = "effects", stopOtherSounds = true, level = 0.5) + while mixer[].isPlaying(): + discard mixer[].play("ping", track = "effects", stopOtherSounds = true, level = 0.5) # on windows we re-open stdin and this will not work when defined(linux): echo "Press q and enter to exit" if stdin.readLine() == "q": - mixer[].Stop() + mixer[].stop() elif defined(windows): sleep(5) - mixer[].Stop() + mixer[].stop()
--- a/tests/test_text.nim Mon Jul 29 15:53:03 2024 +0700 +++ b/tests/test_text.nim Mon Jul 29 16:05:09 2024 +0700 @@ -14,7 +14,7 @@ var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) - var font = LoadFont("Overhaul.ttf", lineHeightPixels = 160) + var font = loadFont("Overhaul.ttf", lineHeightPixels = 160) var label1 = InitTextbox( renderdata, pipeline.descriptorSetLayouts[0], @@ -42,9 +42,9 @@ var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) - var font1 = LoadFont("Overhaul.ttf", lineHeightPixels = 40) - var font2 = LoadFont("Overhaul.ttf", lineHeightPixels = 160) - var font3 = LoadFont("DejaVuSans.ttf", lineHeightPixels = 160) + var font1 = loadFont("Overhaul.ttf", lineHeightPixels = 40) + var font2 = loadFont("Overhaul.ttf", lineHeightPixels = 160) + var font3 = loadFont("DejaVuSans.ttf", lineHeightPixels = 160) var labels = [ InitTextbox( renderdata, @@ -104,7 +104,7 @@ var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) - var font = LoadFont("DejaVuSans.ttf", lineHeightPixels = 40) + var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 40) var labels: seq[Textbox] for horizontal in HorizontalAlignment: @@ -165,7 +165,7 @@ var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) - var font = LoadFont("DejaVuSans.ttf", lineHeightPixels = 160) + var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 160) var labels: seq[Textbox] for i in 0 ..< 100: labels.add InitTextbox(