This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
base.lua
93 lines (84 loc) · 2.5 KB
/
base.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
local env = {
assert = assert,
type = type,
tonumber = tonumber,
tostring = tostring,
require = require,
error = error,
getmetatable = getmetatable,
setmetatable = setmetatable,
string = string,
table = table,
love = love,
jit = jit,
}
setfenv(1, env)
local path = (...):gsub("[^%.]*$", "")
local M = require(path .. "master")
local ffi = require("ffi")
local C = M.C
local _common = M._common
-- add metamethods to ImVec2 and ImVec4
local ct = ffi.typeof("ImVec2")
local ImVec2 = {}
function ImVec2.__add(u, v)
assert(type(u) == type(v) and ffi.istype(u, v), "One of the summands in not an ImVec2.")
return ct(u.x + v.x, u.y + v.y)
end
function ImVec2.__sub(u, v)
assert(type(u) == type(v) and ffi.istype(u, v), "One of the summands in not an ImVec2.")
return ct( u.x - v.x, u.y - v.y)
end
function ImVec2.__unm(u)
return ct(-u.x, -u.y)
end
function ImVec2.__mul(u, v)
local nu, nv = tonumber(u), tonumber(v)
if nu then
return ct(nu*v.x, nu*v.y)
elseif nv then
return ct(nv*u.x, nv*u.y)
else
error("ImVec2 can only be multipliead by a numerical type.")
end
end
function ImVec2.__div(u, a)
a = assert(tonumber(a), "ImVec2 can only be divided by a numerical type.")
return ct(u.x/a, u.y/a)
end
local ct = ffi.typeof("ImVec4")
local ImVec4 = {}
function ImVec4.__add(u, v)
assert(type(u) == type(v) and ffi.istype(u, v), "One of the summands in not an ImVec4.")
return ct(u.x + v.x, u.y + v.y, u.z + v.z, u.w + v.w)
end
function ImVec4.__sub(u, v)
assert(type(u) == type(v) and ffi.istype(u, v), "One of the summands in not an ImVec4.")
return ct(u.x - v.x, u.y - v.y, u.z - v.z, u.w - v.w)
end
function ImVec4.__unm(u)
return ct(-u.x, -u.y, -u.z, -u.w)
end
function ImVec4.__mul(u, v)
local nu, nv = tonumber(u), tonumber(v)
if nu then
return v:__new(nu*v.x, nu*v.y, nu*v.z, nu*v.w)
elseif nv then
return ct(nv*u.x, nv*u.y, nv*u.z, nv*u.w)
else
error("ImVec4 can only be multipliead by a numerical type.")
end
end
function ImVec4.__div(u, a)
a = assert(tonumber(a), "ImVec4 can only be divided by a numerical type.")
return ct(u.x/a, u.y/a, u.z/a, u.w/a)
end
-- wrap FLT_MIN, FLT_MAX
local FLT_MIN, FLT_MAX = C.igGET_FLT_MIN(), C.igGET_FLT_MAX()
M.FLT_MIN, M.FLT_MAX = FLT_MIN, FLT_MAX
-- handwritten functions
M.ImVector_ImWchar = function()
jit.off(true)
local p = C.ImVector_ImWchar_create()
return ffi.gc(p[0], C.ImVector_ImWchar_destroy)
end