/
hello.hpp
59 lines (48 loc) · 1.34 KB
/
hello.hpp
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
// this file is parsed by SWIG (and compiled by gcc)
class RingRenderer {
public:
int cx, cy;
void render(PyObject * arr, int radius) {
int height, width, x, y;
uint8_t * p;
assert(PyArray_ISCARRAY(arr));
assert(PyArray_NDIM(arr) == 3);
assert(PyArray_DIM(arr, 2) == 3);
height = PyArray_DIM(arr, 0);
width = PyArray_DIM(arr, 1);
p = (uint8_t*)PyArray_DATA(arr);
int radius2 = radius*radius;
for (y=0; y<height; y++) {
for (x=0; x<width; x++) {
int dist2 = (x-cx)*(x-cx) + (y-cy)*(y-cy);
if (dist2 < radius2) {
p[0] = p[0] * dist2 / radius2;
p[1] = p[1] * dist2 / radius2;
p[2] = p[2] * dist2 / radius2;
}
p += 3;
}
}
}
};
// other (incomplete) examples:
class Gradient {
public:
float parm1, parm2;
PyObject * get_color_at(float x, float y) {
int r, g, b;
// ...
return Py_BuildValue("ddd", r, g, b);
}
};
PyObject * render(PyObject * dst_surface) {
PyObject * res;
int x, y, w, h;
// ...
res = PyObject_CallMethod(dst_surface, "mark_dirty",
"(iiii)", x, y, w, h);
if (!res) return NULL; // Python exception in mark_dirty(), just pass it on
Py_DECREF(res); // we own Python reference to res, release it
// ...
Py_RETURN_NONE; // return a new reference to None
}