From 69e8a8c0f17ca91d0cb9f61126751936ee64f7a0 Mon Sep 17 00:00:00 2001 From: testercwt <67111993+testercwt@users.noreply.github.com> Date: Wed, 31 May 2023 21:58:52 +0800 Subject: [PATCH 1/3] add test for timer fix timer.jl typo --- src/resources.jl | 31 ++++++++++++----- src/timer.jl | 2 +- test/runtests.jl | 90 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 113 insertions(+), 10 deletions(-) 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 From d480760def82a9d4fbc338b708ee9df90318a78b Mon Sep 17 00:00:00 2001 From: testercwt <67111993+testercwt@users.noreply.github.com> Date: Tue, 25 Jul 2023 14:18:18 +0800 Subject: [PATCH 2/3] Update runtests.jl --- test/runtests.jl | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 8da95a4..b1ac6fc 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -121,17 +121,24 @@ end GameZero.scheduler[] = GameZero.Scheduler() flag=false f()= (flag=true;nothing) + function _sleep(t) + t1=Base.time_ns() + while true + sleep(0.01) + Base.time_ns()-t1 > t * 1.0e9 && break + end + end @testset "once" begin flag=false schedule_once(f,1) schedule_once(f,0.5) @test flag==false - sleep(0.5) + _sleep(0.5) GameZero.tick!(GameZero.scheduler[]) @test flag==true flag=false - sleep(0.5) + _sleep(0.5) GameZero.tick!(GameZero.scheduler[]) @test flag==true end @@ -141,11 +148,11 @@ end schedule_once(f,1) schedule_unique(f,0.5) @test flag==false - sleep(0.5) + _sleep(0.5) GameZero.tick!(GameZero.scheduler[]) @test flag==true flag=false - sleep(0.5) + _sleep(0.5) GameZero.tick!(GameZero.scheduler[]) @test flag==false end @@ -154,11 +161,11 @@ end flag=false schedule_interval(f,1) @test flag==false - sleep(1) + _sleep(1) GameZero.tick!(GameZero.scheduler[]) @test flag==true flag=false - sleep(1) + _sleep(1) GameZero.tick!(GameZero.scheduler[]) @test flag==true GameZero.clear!(GameZero.scheduler[]) @@ -172,18 +179,18 @@ end flag=false schedule_conti(f,1) @test flag==false - sleep(1) + _sleep(1) GameZero.tick!(GameZero.scheduler[]) @test flag==true flag=false schedule_conti(f2,1) @test flag==false - sleep(1) + _sleep(1) GameZero.tick!(GameZero.scheduler[]) @test flag==true flag=false - sleep(0.5) + _sleep(0.5) GameZero.tick!(GameZero.scheduler[]) @test flag==true end From 636fe07e6ed0fd98f6b56b8a9a105dc4ddea2a85 Mon Sep 17 00:00:00 2001 From: testercwt <67111993+testercwt@users.noreply.github.com> Date: Tue, 25 Jul 2023 23:28:36 +0800 Subject: [PATCH 3/3] Revert "Update runtests.jl" This reverts commit d480760def82a9d4fbc338b708ee9df90318a78b. --- test/runtests.jl | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index b1ac6fc..8da95a4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -121,24 +121,17 @@ end GameZero.scheduler[] = GameZero.Scheduler() flag=false f()= (flag=true;nothing) - function _sleep(t) - t1=Base.time_ns() - while true - sleep(0.01) - Base.time_ns()-t1 > t * 1.0e9 && break - end - end @testset "once" begin flag=false schedule_once(f,1) schedule_once(f,0.5) @test flag==false - _sleep(0.5) + sleep(0.5) GameZero.tick!(GameZero.scheduler[]) @test flag==true flag=false - _sleep(0.5) + sleep(0.5) GameZero.tick!(GameZero.scheduler[]) @test flag==true end @@ -148,11 +141,11 @@ end schedule_once(f,1) schedule_unique(f,0.5) @test flag==false - _sleep(0.5) + sleep(0.5) GameZero.tick!(GameZero.scheduler[]) @test flag==true flag=false - _sleep(0.5) + sleep(0.5) GameZero.tick!(GameZero.scheduler[]) @test flag==false end @@ -161,11 +154,11 @@ end flag=false schedule_interval(f,1) @test flag==false - _sleep(1) + sleep(1) GameZero.tick!(GameZero.scheduler[]) @test flag==true flag=false - _sleep(1) + sleep(1) GameZero.tick!(GameZero.scheduler[]) @test flag==true GameZero.clear!(GameZero.scheduler[]) @@ -179,18 +172,18 @@ end flag=false schedule_conti(f,1) @test flag==false - _sleep(1) + sleep(1) GameZero.tick!(GameZero.scheduler[]) @test flag==true flag=false schedule_conti(f2,1) @test flag==false - _sleep(1) + sleep(1) GameZero.tick!(GameZero.scheduler[]) @test flag==true flag=false - _sleep(0.5) + sleep(0.5) GameZero.tick!(GameZero.scheduler[]) @test flag==true end