Operator overloading.

This commit is contained in:
TSnake41 2020-05-08 17:07:34 +02:00
parent 72a32e09d5
commit 10fe371292
2 changed files with 115 additions and 3 deletions

View File

@ -17,8 +17,6 @@ else
LDFLAGS += -ldl -lX11 -lpthread LDFLAGS += -ldl -lX11 -lpthread
endif endif
BOOT_FILES := src/raylib.lua src/raylua.lua
all: raylua_s raylua_e all: raylua_s raylua_e
%.o: %.c %.o: %.c
@ -51,7 +49,7 @@ src/raylua.o: luajit raylib src/autogen/boot.c src/autogen/bind.c
src/raylua_builder.o: src/autogen/builder.c src/raylua_builder.o: src/autogen/builder.c
src/autogen/boot.c: src/raylib.lua src/raylua.lua src/autogen/boot.c: src/raylib.lua src/compat.lua src/raylua.lua
$(LUA) tools/lua2str.lua $@ raylua_boot_lua $^ $(LUA) tools/lua2str.lua $@ raylua_boot_lua $^
src/autogen/bind.c: src/autogen/bind.c:

114
src/compat.lua Normal file
View File

@ -0,0 +1,114 @@
--[[
Copyright (C) 2020 Astie Teddy
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
]]
-- math metamethods
local new = ffi.new
ffi.metatype("Vector2", {
__add = function (a, b)
if ffi.istype("Vector2", b) then
return new("Vector2", a.x + b.x, a.y + b.y)
else
error "Invalid operation."
end
end,
__sub = function (a, b)
if ffi.istype("Vector2", b) then
return new("Vector2", a.x - b.x, a.y - b.y)
else
error "Invalid operation."
end
end,
__unm = function (a)
return new("Vector2", -a.x, -a.y)
end,
__len = function (a)
return math.sqrt(a.x * a.x + a.y * a.y)
end,
__mul = function (a, b)
if type(a) == "number" then
-- swap if a is a number (a * vector)
a, b = b, a
end
if ffi.istype("Vector2", b) then -- dot product
return a.x * b.x + a.y * b.y
elseif type(b) == "number" then
return new("Vector2", a.x * b, a.y * b)
else
error "Invalid operation."
end
end,
__div = function (a, b)
if type(b) == "number" then
return new("Vector2", a.x / b, a.y / b)
else
error "Invalid operation"
end
end,
__tostring = function (a)
return string.format("Vector2: (%g %g)", a.x, a.y)
end
})
ffi.metatype("Vector3", {
__add = function (a, b)
if ffi.istype("Vector3", b) then
return new("Vector3", a.x + b.x, a.y + b.y, a.z + b.z)
else
error "Invalid operation."
end
end,
__sub = function (a, b)
if ffi.istype("Vector3", b) then
return new("Vector2", a.x - b.x, a.y - b.y, a.z - b.z)
else
error "Invalid operation."
end
end,
__unm = function (a)
return new("Vector2", -a.x, -a.y, -a.z)
end,
__len = function (a)
return math.sqrt(a.x * a.x + a.y * a.y + a.z * a.z)
end,
__mul = function (a, b)
if type(a) == "number" then
-- swap if a is a number (a * vector)
a, b = b, a
end
if ffi.istype("Vector2", b) then -- dot product
return a.x * b.x + a.y * b.y + a.z * b.z
elseif type(b) == "number" then
return new("Vector2", a.x * b, a.y * b, a.z * b)
else
error "Invalid operation."
end
end,
__div = function (a, b)
if type(b) == "number" then
return new("Vector3", a.x / b, a.y / b, a.z / b)
else
error "Invalid operation"
end
end,
__tostring = function (a)
return string.format("Vector3: (%g %g %g)", a.x, a.y, a.z)
end
})
-- TODO: Matrix and Quaternion