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

Changed the way games are run. #38

Merged
merged 5 commits into from
Jul 20, 2021
Merged
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
1 change: 0 additions & 1 deletion example/BasicGame/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ function update(g::Game)
dx = 2
end


end

# If the "space" key is pressed, change the displayed image to the "hurt" variant.
Expand Down
57 changes: 36 additions & 21 deletions src/GameZero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using Colors
using Random

export Actor, Game, game, draw, schduler, schedule_once, schedule_interval, schedule_unique, unschedule,
collide, angle, distance, play_music, play_sound, line, clear, rungame
collide, angle, distance, play_music, play_sound, line, clear, rungame, game_include
export Keys, MouseButtons, KeyMods
export Line, Rect, Circle

Expand All @@ -22,6 +22,7 @@ include("actor.jl")
const HEIGHTSYMBOL = :HEIGHT
const WIDTHSYMBOL = :WIDTH
const BACKSYMBOL = :BACKGROUND
const LOCATION = pwd()



Expand Down Expand Up @@ -74,6 +75,9 @@ end

getifdefined(m, s, v) = isdefined(m, s) ? getfield(m, s) : v

game_include(jlf::String) = Base.include(game[].game_module, jlf)


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

function mainloop(g::Game)
Expand Down Expand Up @@ -178,9 +182,9 @@ getMouseClickY(e) = bitcat(Int32, e[28:-1:25])
getMouseMoveX(e) = bitcat(Int32, e[24:-1:21])
getMouseMoveY(e) = bitcat(Int32, e[28:-1:25])

function rungame(jlf::String)
function rungame(jlf::String, external::Bool=true)
global playing, paused
g = initgame(jlf::String)
g = initgame(jlf::String, external)
try
playing[] = paused[] = true
mainloop(g)
Expand All @@ -193,38 +197,49 @@ function rungame(jlf::String)
end
end

function initgame(jlf::String)
function rungame()
rungame(abspath(PROGRAM_FILE), false)
end

function initgame(jlf::String, external::Bool)
if !isfile(jlf)
ArgumentError("File not found: $jlf")
end
name = titlecase(replace(basename(jlf), ".jl"=>""))
module_name = Symbol(name*"_"*randstring(5))
game_module = Module(module_name)
@debug "Initialised Anonymous Game Module" module_name
if external
module_name = Symbol(name*"_"*randstring(5))
game_module = Module(module_name)
@debug "Initialised Anonymous Game Module" module_name
end
initSDL()
game[] = Game()
scheduler[] = Scheduler()
g = game[]
g.game_module = game_module
g.location = dirname(jlf)
g.keyboard = Keyboard()
if external
g.game_module = game_module
else
g.game_module = Main
end

if external
Base.include_string(g.game_module, "using GameZero")
Base.include_string(g.game_module, "import GameZero.draw")
Base.include_string(g.game_module, "using Colors")
Base.include(g.game_module, jlf)
end

Base.include_string(game_module, "using GameZero")
Base.include_string(game_module, "import GameZero.draw")
Base.include_string(game_module, "using Colors")
Base.include(game_module, jlf)

g.update_function = getfn(game_module, :update, 2)
g.render_function = getfn(game_module, :draw, 1)
g.onkey_function = getfn(game_module, :on_key_down, 3)
g.onmouseup_function = getfn(game_module, :on_mouse_up, 3)
g.onmousedown_function = getfn(game_module, :on_mouse_down, 3)
g.onmousemove_function = getfn(game_module, :on_mouse_move, 2)
g.screen = initscreen(game_module, "GameZero::"*name)
g.update_function = getfn(g.game_module, :update, 2)
g.render_function = getfn(g.game_module, :draw, 1)
g.onkey_function = getfn(g.game_module, :on_key_down, 3)
g.onmouseup_function = getfn(g.game_module, :on_mouse_up, 3)
g.onmousedown_function = getfn(g.game_module, :on_mouse_down, 3)
g.onmousemove_function = getfn(g.game_module, :on_mouse_move, 2)
g.screen = initscreen(g.game_module, "GameZero::"*name)
clear(g.screen)
return g
end
end


function getfn(m::Module, s::Symbol, maxargs=3)
Expand Down
2 changes: 1 addition & 1 deletion src/resources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function image_surface(image::String)
end

function file_path(name::String, subdir::Symbol)
path = joinpath(game[].location, String(subdir))
path = joinpath(LOCATION, String(subdir))
@assert isdir(path)
allfiles = readdir(path)
allexts = resource_ext[subdir]
Expand Down