Skip to content

Instantly share code, notes, and snippets.

@kungfooman
Last active February 19, 2018 00:50
Show Gist options
  • Save kungfooman/dd5ddae12cf5eafcf1aeb0ea35b574d1 to your computer and use it in GitHub Desktop.
Save kungfooman/dd5ddae12cf5eafcf1aeb0ea35b574d1 to your computer and use it in GitHub Desktop.
# this file can be generated via:
# source = generateCallback("player_damage", Int32, Arg[Arg(Ptr{Int64}, "targ", Entity), Arg(Ptr{Int64}, "inflictor", Entity), Arg(Ptr{Int64}, "attacker", Entity), Arg(Ptr{Float32}, "dir", Vec3), Arg(Ptr{Float32}, "point", Vec3), Arg(Int32, "damage", nothing), Arg(Int32, "dflags", nothing), Arg(Int32, "mod", nothing)])
# file_put_contents("julia/callbacks/player_damage.jl", source)
#=
int (*callback_player_damage)(int *targ, int *inflictor, int *attacker, float *dir, float *point, int damage, int dflags, int mod) = NULL;
CCALL void set_callback_player_damage(int (*cb)(int *targ, int *inflictor, int *attacker, float *dir, float *point, int damage, int dflags, int mod)) {
callback_player_damage = cb;
}
// cb returns 1 == handled, return...
// cb returns 0 == handled, but still do the normal C stuff
if (callback_player_damage && callback_player_damage((int *)targ, (int *)inflictor, (int *)attacker, (float *)dir, (float *)point, (int)damage, (int)dflags, (int)mod)) {
return 0;
}
=#
function callback_player_damage(targ::Entity, inflictor::Entity, attacker::Entity, dir::Vec3, point::Vec3, damage::Int32, dflags::Int32, mod::Int32)::Int32
log(console, "player_damage $targ $inflictor $attacker $dir $point $damage $dflags $mod")
zero(Int32)
end
function wrapper_callback_player_damage(targ::Ptr{Int64}, inflictor::Ptr{Int64}, attacker::Ptr{Int64}, dir::Ptr{Float32}, point::Ptr{Float32}, damage::Int32, dflags::Int32, mod::Int32, )::Int32
ret = zero(Int32)
# whatever happens, make sure we return to C what it expects
try
# for some reason cfunction returns always the first compiled one, so make sure we call the latest function here
ret = Int32( Base.invokelatest(callback_player_damage, convert(Entity, targ), convert(Entity, inflictor), convert(Entity, attacker), convert(Vec3, dir), convert(Vec3, point), damage, dflags, mod) )
catch ex
log(console, ex)
end
return ret
end
c_callback_player_damage = cfunction(wrapper_callback_player_damage, Int32, (Ptr{Int64}, Ptr{Int64}, Ptr{Int64}, Ptr{Float32}, Ptr{Float32}, Int32, Int32, Int32, ))
ccall(("set_callback_player_damage", lib), Void, (Ptr{Int64}, ), c_callback_player_damage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment