Skip to content

Commit d3de11a

Browse files
committed
Update to reflect changes in Vectors.
1 parent 50a9c0c commit d3de11a

File tree

1 file changed

+80
-111
lines changed

1 file changed

+80
-111
lines changed

README.md

Lines changed: 80 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ More information on how to build raylib can be found in the [raylib wiki pages](
4848

4949
## raylib vs raylibpy
5050

51+
Below are the differences in usage between raylib and raylibpy. Note, though that these
52+
differences are being worked to make raylibpy as pythonic as possible, so changes may
53+
occur without notification.
54+
5155
### Constant values
5256

5357
All C `#define`s got translated to Python 'constants'. Enums got translated to
@@ -57,19 +61,9 @@ Python [enums](https://docs.python.org/3/library/enum.html).
5761

5862
#### In general
5963

60-
All structures inherit from `ctypes.Structure` class. Unlike functions, constructors require
61-
the exact argument types, so `int`s can't be passed where `float`s are expected (although the
62-
argument can be omitted):
63-
64-
```python
65-
# ways of creating a Vector3 instance:
66-
vec_a = Vector3()
67-
vec_b = Vector3(0., 1., 0.)
68-
vec_c = Vector3.one()
69-
70-
# the following will raise an exception:
71-
vec_d = Vector3(10, 0, 100)
72-
```
64+
All structures inherit from `ctypes.Structure` class. At the moment, constructors
65+
(except for vectors) require the exact argument types, so `int`s can't be passed
66+
where `float`s are expected (although the argument can be omitted).
7367

7468
All structures have `__str__()` implemented, so they have a very basic textual representation:
7569
```python
@@ -95,109 +89,84 @@ assignment is also supported; the right hand side operand can be any sequence of
9589
number of components:
9690

9791
```python
98-
>>> vec_a = Vector3(3., 5., 7.)
99-
>>> vec_b = Vector3(4., 2., 0.)
100-
>>> vec_a * vec_b
101-
"(12.0, 10.0, 0.0)"
102-
>>> vec_a + (8, 100, -1)
103-
"(11.0, 105.0, 6.0)"
104-
>>> vec_a %= 2
105-
>>> vec_a
106-
"(1.0, 1.0, 1.0)"
92+
vec_a = Vector3(3., 5., 7.)
93+
vec_b = Vector3(4., 2., 0.)
94+
vec_a * vec_b # outputs (12.0, 10.0, 0.0)
95+
vec_a + (8, 100, -1) # outputs (11.0, 105.0, 6.0)
96+
vec_a %= 2 # augmented assignment (modulo)
97+
vec_a # outputs (1.0, 1.0, 0.0)
10798
```
10899

109-
Vectors have also a feature that tries to emulate the GLSL vector swizzling, but
110-
its done through subscription:
100+
Vectors also support GLSL vector swizzling. Also, `x`, `y`, `z` and `w` coordinates maps to
101+
normalized color values (`r`, `g`, `b` and `a`; only for `Vector3` and `Vector4`) and
102+
texture coordinates (`u` and `v`):
103+
111104
```python
112-
>>> vec_a = Vector4(10.0, 20.0, 50.0, 1.0)
113-
>>> # create a Vector2 from it:
114-
...
115-
>>> vec_b = vec_a['xy']
116-
>>> vec_b
117-
"(10.0, 20.0)"
118-
>>> # create a Vector3 from it, setting 1.0 to the z axis:
119-
...
120-
>>> vec_c = vec_b['xy1']
121-
>>> vec_c
122-
"(10.0, 20.0, 0.0)"
123-
>>> # another Vector2, perpendicular to vec_b:
124-
...
125-
>>> vec_d = vec_b['Yx']
126-
>>> vec_d
127-
"(-20.0, 10.0)"
128-
>>> # a Vector4 from the y axis:
129-
...
130-
>>> vec_e = vec_b['yyyy']
131-
>>> # vec_d with signs flipped:
132-
...
133-
>>> vec_d['XY']
134-
"(20.0, -10.0)"
135-
>>> # moving vec_a in the y axis by one:
136-
...
137-
>>> vec_a += vec_a['0010']
105+
# Reading (__getattr__)
106+
vec3 = Vector3(123.0, 467.0, 789.0)
107+
vec2 = vec3.uv # x and y respectively as u and v
108+
vec3 = vec3.bgr # x, y and z respectively as r, g and b ( rgb is not available in Vector 2)
109+
vec4 = vec2.rrrg # for attribute reading, is ok to repeat components
110+
111+
112+
# Writing (__setattr__)
113+
vec3 = Vector3(123.0, 467.0, 789.0)
114+
vec4.yxwz = 10, 0, -1, vec3.z # sequences of ints and/or floats are accepted as value
115+
vec2.vu = vec3.xy # x and y respectively as u and v
116+
vec3.bgr = 12, vec4.x # x, y and z respectively as r, g and b ( rgb is not available in Vector 2)
117+
118+
# the following raises an exception:
119+
vec3.rrr = vec4.yxw # for attribute writing, is _not_ ok to repeat components
120+
vec2.br = vec4.uv # r, g and b is not available in Vector2
121+
vec4.brxy = (0., 0., vec2.x, vec3.z) # can't mix component name groups (rgba, xywz and uv)
138122
```
139-
That's not all! Other component-wise operations (or tricks) can be made this way (note,
140-
though that these operators apply in positional fashion):
123+
124+
Constructors and swizzled attributes now accept any combination of numbers,
125+
vectors and sequences, as long as the total number of arguments are preserved:
141126
```python
142-
>>> vec_a = Vector3(-10.0, 20.5, 15.840)
143-
>>> # divide all components by 2:
144-
...
145-
>>> vec_a['///']
146-
"(-5.0, 10.25, 7.920)"
147-
>>>
148-
>>>
149-
>>> # multiply all components by 2:
150-
...
151-
>>> vec_a['***']
152-
"(-20.0, 41.0, 31.680)"
153-
>>>
154-
>>>
155-
>>> # all components raised to the power of 2:
156-
...
157-
>>> vec_a['^^^']
158-
"(-1e-10, 7.779544182561597e+26, 1.0095364584490473e+19)"
159-
>>>
160-
>>>
161-
>>> # inverse of all components (1/x):
162-
...
163-
>>> vec_a['...']
164-
"(-0.1, 0.04878048780487805, 0.06313131313131314)"
165-
>>>
166-
>>>
167-
>>> # sign of components (-1 if < 0, 1 if > 0, 0 otherwise):
168-
...
169-
>>> vec_a['+++']
170-
"(-1.0, 1.0, 1.0)"
171-
>>>
172-
>>>
173-
>>> # nonzero components (1 if != 0, 0 otherwise):
174-
...
175-
>>> vec_a['???']
176-
"(1.0, 1.0, 1.0)"
177-
>>>
178-
>>>
179-
>>> # the component of vec_a with largest value:
180-
...
181-
>>> vec_a['>>>']
182-
"(15.840, 15.840, 15.840)"
183-
>>>
184-
>>>
185-
>>> # the component of vec_a with smallest value:
186-
...
187-
>>> vec_a['<<<']
188-
"(-10.0, -10.0, -10.0)"
189-
>>>
190-
>>>
191-
>>> # component values rounded:
192-
...
193-
>>> vec_a['###']
194-
"(-10.0, 20.0, 15.0)"
195-
>>>
196-
>>>
197-
>>> # fractional part of component values:
198-
...
199-
>>> vec_a['%%%']
200-
"(0.0, 0.5, 0.840)"
127+
# all these results in the same Vector4
128+
a = Vector4(3, 4, 5, 6)
129+
b = Vector4(a.xy, 5, 6)
130+
c = Vector4(b.x, b.yz, b.w)
131+
d = Vector4(c.x, c.y, c.zw)
132+
e = Vector4(d.xy, d.zw)
133+
f = Vector4(e.xyz, 6)
134+
g = Vector4(f.x, f.yzw)
135+
h = Vector4(g)
136+
```
137+
138+
Setting attributes also works:
139+
140+
```python
141+
a = Vector4(Vector2(10, 0), 100, 20)
142+
b = Vector4.zero()
143+
b.rgba = 0.0, vec4.rg, 1.0
144+
a.xyzw = (10, b.uv), 1.0
145+
```
146+
147+
This became available by dropping a previous feature wich allowed for a very basic
148+
swizzling emulation. A feature more similas to GLSL vectors is implemented on
149+
top of Python container emulation magic functions:
150+
151+
```python
152+
vec = Vector4(0., 1., 2., 3.)
153+
154+
# __len__()
155+
print(len(vec)) # outputs 4
156+
157+
# __iter__()
158+
for comp in v:
159+
print(comp) # iterates on Vector4 components
160+
161+
# __getitem__()
162+
x = vec[0] # key as int
163+
y = vec['y'] # key as str
164+
zw = vec[2:] # key as slice; returns a List[float]
165+
166+
# __setitem__()
167+
vec[0] = 10
168+
vec['y'] = 20
169+
# vec[2:] = zw # <--- not supported; will raise TypeError
201170
```
202171

203172
## extras

0 commit comments

Comments
 (0)