diff --git a/src/resources.jl b/src/resources.jl index 67777e6..966d24e 100644 --- a/src/resources.jl +++ b/src/resources.jl @@ -3,8 +3,11 @@ play_sound(filename::String, loops::Integer) Plays a sound effect from the `sounds` subdirctory. It will play the specified number of times. If not specified, it will default to once. """ -play_sound = let cache=Dict{Symbol,Ptr}() - function _play_sound(name, loops=0, ticks=-1) +function play_sound end +let cache=Dict{Symbol,Ptr}() + global play_sound + function play_sound(name, loops=0, ticks=-1) + sound_file="" sample=get(cache,Symbol(name)) do sound_file = file_path(String(name), :sounds) Mix_LoadWAV(sound_file) @@ -13,7 +16,7 @@ play_sound = let cache=Dict{Symbol,Ptr}() @warn "Could not load sound file: $sound_file\n$(getSDLError())" return else - cache[Symbol(name)]=sample + get!(cache,Symbol(name),sample) end r = Mix_PlayChannelTimed(Int32(-1), sample, loops, ticks) if r == -1 @@ -28,10 +31,22 @@ play_music(name::String, loops::Integer) Plays music from the `sounds` subdirectory. It will play the file the specified number of times. If not specified, it will default to infinitely. """ -function play_music(name, loops=-1) - music_file = file_path(name, :music) - music = Mix_LoadMUS(music_file) - Mix_PlayMusic( music, Int32(loops) ) +function play_music end +let cache=Dict{Symbol,Ptr}() + global play_music + function play_music(name, loops=-1) + music_file = "" + music = get(cache,Symbol(name)) do + music_file = file_path(name, :music) + Mix_LoadMUS(music_file) + end + if music == C_NULL + error( "Could not load music file: $music_file\n$(getSDLError())" ) + else + get!(cache,Symbol(name),music) + end + Mix_PlayMusic( music, Int32(loops) ) + end end const resource_ext = Dict( @@ -46,7 +61,7 @@ image_surface = let cache=Dict{Symbol,Ptr}() image_file = file_path(String(image), :images) sf = IMG_Load(image_file) if sf == C_NULL - throw("Error loading $image_file") + error("Error loading $image_file") end sf end diff --git a/src/timer.jl b/src/timer.jl index 1777022..fec742d 100644 --- a/src/timer.jl +++ b/src/timer.jl @@ -135,7 +135,7 @@ function tick(x::ContingentScheduled, elapsed, s=scheduler[]) if r == nothing return else - push!(s, ContingetScheduled(x.action, 1e9*r+elapsed)) + push!(s, ContingentScheduled(x.action, 1e9*r+elapsed)) end end end diff --git a/test/runtests.jl b/test/runtests.jl index 37afb84..8da95a4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -117,4 +117,92 @@ let ev=GameZero.SDL_Event(ntuple(UInt8,56)) @test ev.window.event==0x0d end -include("examples.jl") \ No newline at end of file +@testset "timer" begin + GameZero.scheduler[] = GameZero.Scheduler() + flag=false + f()= (flag=true;nothing) + + @testset "once" begin + flag=false + schedule_once(f,1) + schedule_once(f,0.5) + @test flag==false + sleep(0.5) + GameZero.tick!(GameZero.scheduler[]) + @test flag==true + flag=false + sleep(0.5) + GameZero.tick!(GameZero.scheduler[]) + @test flag==true + end + + @testset "unique" begin + flag=false + schedule_once(f,1) + schedule_unique(f,0.5) + @test flag==false + sleep(0.5) + GameZero.tick!(GameZero.scheduler[]) + @test flag==true + flag=false + sleep(0.5) + GameZero.tick!(GameZero.scheduler[]) + @test flag==false + end + + @testset "interval" begin + flag=false + schedule_interval(f,1) + @test flag==false + sleep(1) + GameZero.tick!(GameZero.scheduler[]) + @test flag==true + flag=false + sleep(1) + GameZero.tick!(GameZero.scheduler[]) + @test flag==true + GameZero.clear!(GameZero.scheduler[]) + end + + function schedule_conti(f::Function, interval) + push!(GameZero.scheduler[], GameZero.ContingentScheduled(WeakRef(f), GameZero.elapsed(scheduler[])+interval*1e9)) + end + f2()=(flag=true;0.5) + @testset "contingent" begin + flag=false + schedule_conti(f,1) + @test flag==false + sleep(1) + GameZero.tick!(GameZero.scheduler[]) + @test flag==true + + flag=false + schedule_conti(f2,1) + @test flag==false + sleep(1) + GameZero.tick!(GameZero.scheduler[]) + @test flag==true + flag=false + sleep(0.5) + GameZero.tick!(GameZero.scheduler[]) + @test flag==true + end + +end + + +include("examples.jl") + +@testset "File not exist" begin + GameZero.file_path(name::String,subdir::Symbol)=throw(ArgumentError("File not exist")) + @test_throws ArgumentError play_music("filenotexist") + @test_throws ArgumentError play_sound("filenotexist") + @test_throws ArgumentError GameZero.image_surface("filenotexist") +end + +@testset "Invalid file" begin + GameZero.file_path(name::String,subdir::Symbol)=@__FILE__ + @test_throws ErrorException play_music("invalidfile") + @test_logs (:warn,r"Could not load") play_sound("invalidfile") + @test_throws ErrorException GameZero.image_surface("invalidfile") +end \ No newline at end of file