diff --git a/src/engine/Component/SoundSource.jl b/src/engine/Component/SoundSource.jl index 4af38ae..18db976 100644 --- a/src/engine/Component/SoundSource.jl +++ b/src/engine/Component/SoundSource.jl @@ -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()) @@ -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)) @@ -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) diff --git a/src/engine/Component/Sprite.jl b/src/engine/Component/Sprite.jl index a1a381f..73097dd 100644 --- a/src/engine/Component/Sprite.jl +++ b/src/engine/Component/Sprite.jl @@ -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) diff --git a/src/engine/UI/TextBox.jl b/src/engine/UI/TextBox.jl index 0e7dcfd..787f17e 100644 --- a/src/engine/UI/TextBox.jl +++ b/src/engine/UI/TextBox.jl @@ -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)