value object constructors

This commit is contained in:
Ryan Oldenburg 2021-08-26 22:18:39 -05:00
parent 4bee35753f
commit 38b3f3bc03
4 changed files with 68 additions and 61 deletions

View file

@ -1,12 +1,5 @@
import bindy, pixie, unicode
type
Vector2 = object
x*, y*: float32
Matrix3 = object
a*, b*, c*, d*, e*, f*, g*, h*, i*: float32
var lastError*: ref PixieError
proc takeError*(): string =
@ -16,6 +9,19 @@ proc takeError*(): string =
proc checkError*(): bool =
result = lastError != nil
type
Vector2* = object
x*, y*: float32
Matrix3* = object
a*, b*, c*, d*, e*, f*, g*, h*, i*: float32
proc vector2*(x, y: float32): Vector2 =
Vector2(x: x, y: y)
proc matrix3*(): Matrix3 =
Matrix3(a: 1, b: 0, c: 0, d: 0, e: 1, f: 0, g: 0, h: 0, i: 1)
proc drawImage1*(
ctx: Context, image: Image, dx, dy: float32
) {.raises: [PixieError].} =
@ -51,20 +57,31 @@ exportEnums:
VerticalAlignment,
TextCase
exportObjects:
export
Vector2,
Matrix3,
Rect,
Color,
ColorStop,
TextMetrics
exportProcs:
export
bindings.checkError,
bindings.takeError
exportObject Vector2:
discard vector2(0, 0)
discard
exportObject Matrix3:
discard matrix3()
discard
exportObject Rect:
discard
exportObject Color:
discard
exportObject ColorStop:
discard
exportObject TextMetrics:
discard
exportSeq seq[float32]:
discard

View file

@ -4,6 +4,12 @@ proc pixie_check_error*(): bool {.raises: [], cdecl, exportc, dynlib.} =
proc pixie_take_error*(): cstring {.raises: [], cdecl, exportc, dynlib.} =
takeError().cstring
proc pixie_vector_2*(x: float32, y: float32): Vector2 {.raises: [], cdecl, exportc, dynlib.} =
vector2(x, y)
proc pixie_matrix_3*(): Matrix3 {.raises: [], cdecl, exportc, dynlib.} =
matrix3()
type SeqFloat32* = ref object
s: seq[float32]

View file

@ -83,12 +83,6 @@ type TextCase* = enum
tcLower
tcTitle
type Rect* = object
x*: float32
y*: float32
w*: float32
h*: float32
type ColorStop* = object
color*: Color
position*: float32

View file

@ -86,6 +86,14 @@ TC_UPPER = 1
TC_LOWER = 2
TC_TITLE = 3
def check_error():
result = dll.pixie_check_error()
return result
def take_error():
result = dll.pixie_take_error().decode("utf8")
return result
class Vector2(Structure):
_fields_ = [
("x", c_float),
@ -93,8 +101,9 @@ class Vector2(Structure):
]
def __init__(self, x, y):
self.x = x
self.y = y
tmp = dll.pixie_vector_2(x, y)
self.x = tmp.x
self.y = tmp.y
def __eq__(self, obj):
self.x == obj.x and self.y == obj.y
@ -112,16 +121,17 @@ class Matrix3(Structure):
("i", c_float)
]
def __init__(self, a, b, c, d, e, f, g, h, i):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
self.g = g
self.h = h
self.i = i
def __init__(self):
tmp = dll.pixie_matrix_3()
self.a = tmp.a
self.b = tmp.b
self.c = tmp.c
self.d = tmp.d
self.e = tmp.e
self.f = tmp.f
self.g = tmp.g
self.h = tmp.h
self.i = tmp.i
def __eq__(self, obj):
self.a == obj.a and self.b == obj.b and self.c == obj.c and self.d == obj.d and self.e == obj.e and self.f == obj.f and self.g == obj.g and self.h == obj.h and self.i == obj.i
@ -134,12 +144,6 @@ class Rect(Structure):
("h", c_float)
]
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
def __eq__(self, obj):
self.x == obj.x and self.y == obj.y and self.w == obj.w and self.h == obj.h
@ -151,12 +155,6 @@ class Color(Structure):
("a", c_float)
]
def __init__(self, r, g, b, a):
self.r = r
self.g = g
self.b = b
self.a = a
def __eq__(self, obj):
self.r == obj.r and self.g == obj.g and self.b == obj.b and self.a == obj.a
@ -166,10 +164,6 @@ class ColorStop(Structure):
("position", c_float)
]
def __init__(self, color, position):
self.color = color
self.position = position
def __eq__(self, obj):
self.color == obj.color and self.position == obj.position
@ -178,20 +172,9 @@ class TextMetrics(Structure):
("width", c_float)
]
def __init__(self, width):
self.width = width
def __eq__(self, obj):
self.width == obj.width
def check_error():
result = dll.pixie_check_error()
return result
def take_error():
result = dll.pixie_take_error().decode("utf8")
return result
class SeqFloat32(Structure):
_fields_ = [("ref", c_ulonglong)]
@ -1299,6 +1282,12 @@ dll.pixie_check_error.restype = c_bool
dll.pixie_take_error.argtypes = []
dll.pixie_take_error.restype = c_char_p
dll.pixie_vector_2.argtypes = [c_float, c_float]
dll.pixie_vector_2.restype = Vector2
dll.pixie_matrix_3.argtypes = []
dll.pixie_matrix_3.restype = Matrix3
dll.pixie_seq_float_32_unref.argtypes = [SeqFloat32]
dll.pixie_seq_float_32_unref.restype = None
@ -2021,3 +2010,4 @@ dll.pixie_miter_limit_to_angle.restype = c_float
dll.pixie_angle_to_miter_limit.argtypes = [c_float]
dll.pixie_angle_to_miter_limit.restype = c_float