Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for timer #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/resources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/timer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 89 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,92 @@ let ev=GameZero.SDL_Event(ntuple(UInt8,56))
@test ev.window.event==0x0d
end

include("examples.jl")
@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
Loading