Skip to content

Commit

Permalink
Merge pull request #69 from testercwt/master
Browse files Browse the repository at this point in the history
fix bugs
  • Loading branch information
aviks committed Apr 3, 2023
2 parents 389d613 + 0faea32 commit dc29f0c
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GameZero"
uuid = "9da27670-f782-11e9-1da1-f53579315bfe"
authors = ["Avik Sengupta", "Ahan Sengupta"]
version = "0.3.1"
version = "0.3.2"

[deps]
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Expand Down
8 changes: 6 additions & 2 deletions src/GameZero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module GameZero
using Colors
using Random

export Actor, TextActor, Game, game, draw, schduler, schedule_once, schedule_interval, schedule_unique, unschedule,
export Actor, TextActor, Game, game, draw, scheduler, schedule_once, schedule_interval, schedule_unique, unschedule,
collide, angle, distance, play_music, play_sound, line, clear, rungame, game_include
export Keys, MouseButtons, KeyMods
export Line, Rect, Triangle, Circle
Expand Down Expand Up @@ -76,12 +76,16 @@ game_include(jlf::String) = Base.include(game[].game_module, jlf)

mainloop(g::Ref{Game}) = mainloop(g[])

pollEvent = let event=Ref{SDL_Event}()
()->SDL_PollEvent(event)
end

function mainloop(g::Game)
start!(timer)
while (true)
#Don't run if game is paused by system (resizing, lost focus, etc)
while window_paused[] != 0
_ = pollEvent!()
_ = pollEvent()
sleep(0.5)
end

Expand Down
2 changes: 1 addition & 1 deletion src/actor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function Base.angle(a::Actor, tx, ty)
myx, myy = a.pos
dx = tx - myx
dy = myy - ty
return deg2rad(atan(dy/dx))
return rad2deg(atan(dy,dx))
end

"""
Expand Down
12 changes: 6 additions & 6 deletions src/keyboard.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ using .MouseButtons
# Thus user code to check if X is pressed is written as
# ` if game.keyboard.X`
const singleCharStrings = string.(collect('a':'z'))
struct KeyHolder; end
const Keys = KeyHolder()
function Base.getproperty(k::KeyHolder, s::Symbol)
struct KeyHolder{T} end
const Keys = KeyHolder{:SDLK_}()
const KeyMods = KeyHolder{:KMOD_}()
function Base.getproperty(k::KeyHolder{T}, s::Symbol) where T
st=string(s)
if findfirst(x->x==lowercase(st), singleCharStrings) !== nothing
if length(st)==1 && only(st) in 'A':'Z'
st = lowercase(st)
end
st = "SDLK_" * st
s=Symbol(st)
s=Symbol(T,st)
try
return getproperty(GameZero.SimpleDirectMediaLayer.LibSDL2, s)
catch
Expand Down
44 changes: 28 additions & 16 deletions src/resources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ 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.
"""
function play_sound(name, loops=0, ticks=-1)
sound_file = file_path(name, :sounds)
sample=Mix_LoadWAV_RW(SDL_RWFromFile(sound_file, "rb"), 1);
if sample == C_NULL
@warn "Could not load sound file: $sound_file\n$(getSDLError())"
return
end
r = Mix_PlayChannelTimed(Int32(-1), sample, loops, ticks)
if r == -1
@warn "Unable to play sound $sound_file\n$(getSDLError())"
play_sound = let cache=Dict{Symbol,Ptr}()
function _play_sound(name, loops=0, ticks=-1)
sample=get(cache,Symbol(name)) do
sound_file = file_path(String(name), :sounds)
Mix_LoadWAV(sound_file)
end
if sample == C_NULL
@warn "Could not load sound file: $sound_file\n$(getSDLError())"
return
else
cache[Symbol(name)]=sample
end
r = Mix_PlayChannelTimed(Int32(-1), sample, loops, ticks)
if r == -1
@warn "Unable to play sound $sound_file\n$(getSDLError())"
end
# Mix_FreeChunk(sample) #comment this because we cache the chunks
end
end

Expand All @@ -33,15 +40,20 @@ const resource_ext = Dict(
:music=>"[mp3|ogg|wav]",
:fonts=>"[ttf]")

function image_surface(image::String)
image_file = file_path(image, :images)
sf = IMG_Load(image_file)
if sf == C_NULL
throw("Error loading $image_file")
image_surface = let cache=Dict{Symbol,Ptr}()
function _image_surface(image)
get!(cache,Symbol(image)) do
image_file = file_path(String(image), :images)
sf = IMG_Load(image_file)
if sf == C_NULL
throw("Error loading $image_file")
end
sf
end
end
return sf
end


function file_path(name::String, subdir::Symbol)
path = joinpath(game[].location, String(subdir))
@assert isdir(path)
Expand Down
1 change: 1 addition & 0 deletions src/screen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,4 @@ end

rect(x::Rect) = x
rect(x::Circle) = Rect(x.left, x.top, 2*x.r, 2*x.r)
line(x1,y1,x2,y2)= draw(Line(x1,y1,x2,y2))
12 changes: 4 additions & 8 deletions src/window.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ end
# This function handles all window events.
# We currently do no allow window resizes
function windowEventWatcher(data_ptr::Ptr{Cvoid}, event_ptr::Ptr{SDL_Event})::Cint
global winWidth, winHeight, cam, window_paused, renderer, win
ev = unsafe_load(event_ptr, 1)
#ee = evt.type
#t = UInt32(ee[4]) << 24 | UInt32(ee[3]) << 16 | UInt32(ee[2]) << 8 | ee[1]
t = ev.type
if (t == SDL_WindowEvent)
event = unsafe_load( Ptr{SDL_WindowEvent}(pointer_from_objref(ev)) )
winevent = event.event; # confusing, but that's what the field is called.
# global winWidth, winHeight, cam, window_paused, renderer, win
ev = unsafe_load(event_ptr)
if (ev.type == SDL_WINDOWEVENT)
winevent = ev.window.event
if (winevent == SDL_WINDOWEVENT_FOCUS_LOST || winevent == SDL_WINDOWEVENT_HIDDEN || winevent == SDL_WINDOWEVENT_MINIMIZED)
# Stop game playing when out of focus
window_paused[] = 1
Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,18 @@ end
end
end

@testset "exported symbols" begin
vars = names(GameZero)
@test all(isdefined.(Ref(GameZero),vars))
end

@testset "keys" begin
@test Keys.ESCAPE==27
@test KeyMods.LALT==0x100
end

let ev=GameZero.SDL_Event(ntuple(UInt8,56))
@test ev.window.event==0x0d
end

include("examples.jl")

0 comments on commit dc29f0c

Please sign in to comment.