Skip to content

Commit

Permalink
feature: added audio cache functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyjor committed Jan 2, 2025
1 parent 1950b13 commit 9cb0d2d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
32 changes: 30 additions & 2 deletions src/engine/Component/SoundSource.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module SoundSourceModule
if length(path) < 1
sound = C_NULL
else
sound = isMusic ? SDL2.Mix_LoadMUS(fullPath) : SDL2.Mix_LoadWAV(fullPath)
sound = load_sound_sdl(path, isMusic)
end
error = unsafe_string(SDL2.SDL_GetError())

Expand Down Expand Up @@ -79,7 +79,7 @@ module SoundSourceModule
function Component.load_sound(this::InternalSoundSource, soundPath::String, isMusic::Bool)
this.isMusic = isMusic
SDL2.SDL_ClearError()
this.sound = this.isMusic ? SDL2.Mix_LoadMUS(joinpath(BasePath, "assets", "sounds", soundPath)) : SDL2.Mix_LoadWAV(joinpath(BasePath, "assets", "sounds", soundPath))
this.sound = load_sound_sdl(soundPath, isMusic)
error = unsafe_string(SDL2.SDL_GetError())
if !isempty(error)
println(string("Couldn't open sound! SDL Error: ", error))
Expand All @@ -90,6 +90,34 @@ module SoundSourceModule
this.path = soundPath
end

function load_sound_sdl(soundPath::String, isMusic::Bool)
if haskey(JulGame.AUDIO_CACHE, get_comma_separated_path(soundPath))
raw_data = JulGame.AUDIO_CACHE[get_comma_separated_path(soundPath)]
rw = SDL2.SDL_RWFromConstMem(pointer(raw_data), length(raw_data))
if rw != C_NULL
@debug("loading sound from cache")
@debug("comma separated path: ", get_comma_separated_path(soundPath))
return isMusic ? SDL2.Mix_LoadMUS_RW(rw, 1) : SDL2.Mix_LoadWAV_RW(rw, 1)
end
end
@debug("loading sound from fs")

fullPath = joinpath(BasePath, "assets", "sounds", soundPath)
return isMusic ? SDL2.Mix_LoadMUS(fullPath) : SDL2.Mix_LoadWAV(fullPath)
end

function get_comma_separated_path(path::String)
# Normalize the path to use forward slashes
normalized_path = replace(path, '\\' => '/')

# Split the path into components
parts = split(normalized_path, '/')

result = join(parts[1:end], ",")

return result
end

function Component.unload_sound(this::InternalSoundSource)
if this.isMusic
SDL2.Mix_FreeMusic(this.sound)
Expand Down
3 changes: 1 addition & 2 deletions src/engine/Component/Sprite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,8 @@ module SpriteModule
return SDL2.IMG_Load_RW(rw, 1)
end
end
image = SDL2.IMG_Load(fullPath)

return image
return SDL2.IMG_Load(fullPath)
end

function get_comma_separated_path(path::String)
Expand Down
6 changes: 2 additions & 4 deletions src/engine/UI/TextBox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,13 @@ module TextBoxModule
raw_data = JulGame.FONT_CACHE[get_comma_separated_path(fontPath)]
rw = SDL2.SDL_RWFromConstMem(pointer(raw_data), length(raw_data))
if rw != C_NULL
@info("loading font from cache")
@debug("loading font from cache")
@debug("comma separated path: ", get_comma_separated_path(fontPath))
return SDL2.TTF_OpenFontRW(rw, 1, fontSize)
end
end

font = CallSDLFunction(SDL2.TTF_OpenFont, joinpath(basePath, fontPath), fontSize)

return font
return CallSDLFunction(SDL2.TTF_OpenFont, joinpath(basePath, fontPath), fontSize)
end

function get_comma_separated_path(path::String)
Expand Down

0 comments on commit 9cb0d2d

Please sign in to comment.