1
////////////////////////////////////////////////////////////
3
// PySFML - Python binding for SFML (Simple and Fast Multimedia Library)
4
// Copyright (C) 2007, 2008 Rémi Koenig (remi.k2620@gmail.com)
6
// This software is provided 'as-is', without any express or implied warranty.
7
// In no event will the authors be held liable for any damages arising from the use of this software.
9
// Permission is granted to anyone to use this software for any purpose,
10
// including commercial applications, and to alter it and redistribute it freely,
11
// subject to the following restrictions:
13
// 1. The origin of this software must not be misrepresented;
14
// you must not claim that you wrote the original software.
15
// If you use this software in a product, an acknowledgment
16
// in the product documentation would be appreciated but is not required.
18
// 2. Altered source versions must be plainly marked as such,
19
// and must not be misrepresented as being the original software.
21
// 3. This notice may not be removed or altered from any source distribution.
23
////////////////////////////////////////////////////////////
27
#include <structmember.h>
30
#include "offsetof.hpp"
33
static PyMemberDef PySfIntRect_members[] = {
34
{(char *)"Left", T_INT, offsetof(PySfIntRect, Left), 0, (char *)"Left coordinate of the rectangle."},
35
{(char *)"Top", T_INT, offsetof(PySfIntRect, Top), 0, (char *)"Top coordinate of the rectangle."},
36
{(char *)"Right", T_INT, offsetof(PySfIntRect, Right), 0, (char *)"Right coordinate of the rectangle."},
37
{(char *)"Bottom", T_INT, offsetof(PySfIntRect, Bottom), 0, (char *)"Bottom coordinate of the rectangle."},
41
static PyMemberDef PySfFloatRect_members[] = {
42
{(char *)"Left", T_FLOAT, offsetof(PySfFloatRect, Left), 0, (char *)"Left coordinate of the rectangle."},
43
{(char *)"Top", T_FLOAT, offsetof(PySfFloatRect, Top), 0, (char *)"Top coordinate of the rectangle."},
44
{(char *)"Right", T_FLOAT, offsetof(PySfFloatRect, Right), 0, (char *)"Right coordinate of the rectangle."},
45
{(char *)"Bottom", T_FLOAT, offsetof(PySfFloatRect, Bottom), 0, (char *)"Bottom coordinate of the rectangle."},
50
PySfIntRect_dealloc(PySfIntRect* self)
58
PySfFloatRect_dealloc(PySfFloatRect* self)
66
PySfIntRect_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
68
const char *kwlist[] = {"Left", "Top", "Right", "Bottom", NULL};
70
self = (PySfIntRect *)type->tp_alloc(type, 0);
74
if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiii:IntRect.__new__", (char **)kwlist, &(self->Left), &(self->Top), &(self->Right), &(self->Bottom)))
76
self->obj = new sf::IntRect(self->Left, self->Top, self->Right, self->Bottom);
78
return (PyObject *)self;
82
PySfFloatRect_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
84
const char *kwlist[] = {"Left", "Top", "Right", "Bottom", NULL};
86
self = (PySfFloatRect *)type->tp_alloc(type, 0);
90
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffff:FloatRect.__new__", (char **)kwlist, &(self->Left), &(self->Top), &(self->Right), &(self->Bottom)))
92
self->obj = new sf::FloatRect(self->Left, self->Top, self->Right, self->Bottom);
94
return (PyObject *)self;
98
PySfIntRect_GetWidth(PySfIntRect *self)
100
return PyLong_FromLong(self->obj->GetWidth());
104
PySfIntRect_GetHeight(PySfIntRect *self)
106
return PyLong_FromLong(self->obj->GetHeight());
110
PySfIntRect_Contains(PySfIntRect* self, PyObject *args);
113
PySfIntRect_Intersects(PySfIntRect* self, PyObject *args);
116
PySfFloatRect_GetWidth(PySfFloatRect *self)
118
return PyFloat_FromDouble(self->obj->GetWidth());
122
PySfFloatRect_GetHeight(PySfFloatRect *self)
124
return PyFloat_FromDouble(self->obj->GetHeight());
128
PySfFloatRect_Contains(PySfFloatRect* self, PyObject *args);
131
PySfFloatRect_Intersects(PySfFloatRect* self, PyObject *args);
134
PySfIntRect_Offset(PySfIntRect* self, PyObject *args)
136
int OffsetX, OffsetY;
138
if (!PyArg_ParseTuple(args, "ii:IntRect.Offset", &OffsetX, &OffsetY))
141
self->obj->Offset(OffsetX, OffsetY);
142
PySfIntRectUpdateSelf(self);
147
PySfFloatRect_Offset(PySfFloatRect* self, PyObject *args)
149
float OffsetX, OffsetY;
151
if (!PyArg_ParseTuple(args, "ff:FloatRect.Offset", &OffsetX, &OffsetY))
154
self->obj->Offset(OffsetX, OffsetY);
155
PySfFloatRectUpdateSelf(self);
160
static PyMethodDef PySfIntRect_methods[] = {
161
{"Offset", (PyCFunction)PySfIntRect_Offset, METH_VARARGS, "Offset(OffsetX, OffsetY)\n\
162
Move the whole rectangle by the given offset.\n\
163
OffsetX : Horizontal offset\n\
164
OffsetY : Vertical offset\n\
166
{"GetWidth", (PyCFunction)PySfIntRect_GetWidth, METH_NOARGS, "GetWidth()\n\
167
Get the width of the rectangle."},
168
{"GetHeight", (PyCFunction)PySfIntRect_GetHeight, METH_NOARGS, "GetHeight()\n\
169
Get the height of the rectangle."},
170
{"Contains", (PyCFunction)PySfIntRect_Contains, METH_VARARGS, "Contains(X, Y)\n\
171
Check if a point is inside the rectangle's area.\n\
172
X : X coordinate of the point to test\n\
173
Y : Y coordinate of the point to test"},
174
{"Intersects", (PyCFunction)PySfIntRect_Intersects, METH_VARARGS, "Intersects(Rectangle, OverlappingRect=None)\n\
175
Check intersection between two rectangles.\n\
176
Rectangle : Rectangle to test\n\
177
OverlappingRect : Rectangle to be filled with overlapping rect (None by default)"},
178
{NULL} /* Sentinel */
182
static PyMethodDef PySfFloatRect_methods[] = {
183
{"Offset", (PyCFunction)PySfFloatRect_Offset, METH_VARARGS, "Offset(OffsetX, OffsetY)\n\
184
Move the whole rectangle by the given offset.\n\
185
OffsetX : Horizontal offset\n\
186
OffsetY : Vertical offset\n\
188
{"GetWidth", (PyCFunction)PySfFloatRect_GetWidth, METH_NOARGS, "GetWidth()\n\
189
Get the width of the rectangle."},
190
{"GetHeight", (PyCFunction)PySfFloatRect_GetHeight, METH_NOARGS, "GetHeight()\n\
191
Get the height of the rectangle."},
192
{"Contains", (PyCFunction)PySfFloatRect_Contains, METH_VARARGS, "Contains(X, Y)\n\
193
Check if a point is inside the rectangle's area.\n\
194
X : X coordinate of the point to test\n\
195
Y : Y coordinate of the point to test"},
196
{"Intersects", (PyCFunction)PySfFloatRect_Intersects, METH_VARARGS, "Intersects(Rectangle, OverlappingRect=None)\n\
197
Check intersection between two rectangles.\n\
198
Rectangle : Rectangle to test\n\
199
OverlappingRect : Rectangle to be filled with overlapping rect (None by default)"},
200
{NULL} /* Sentinel */
204
PySfIntRect_setattro(PyObject* self, PyObject *attr_name, PyObject *v)
206
int result = PyObject_GenericSetAttr(self, attr_name, v);
207
PySfIntRect *Rect = (PySfIntRect *)self;
208
PySfIntRectUpdateObj(Rect);
213
PySfFloatRect_setattro(PyObject* self, PyObject *attr_name, PyObject *v)
215
int result = PyObject_GenericSetAttr(self, attr_name, v);
216
PySfFloatRect *Rect = (PySfFloatRect *)self;
217
PySfFloatRectUpdateObj(Rect);
221
PyTypeObject PySfIntRectType = {
223
"IntRect", /*tp_name*/
224
sizeof(PySfIntRect), /*tp_basicsize*/
226
(destructor)PySfIntRect_dealloc, /*tp_dealloc*/
233
0, /*tp_as_sequence*/
239
PySfIntRect_setattro, /*tp_setattro*/
241
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
242
"sf.IntRect is an utility class for manipulating rectangles.", /* tp_doc */
245
0, /* tp_richcompare */
246
0, /* tp_weaklistoffset */
249
PySfIntRect_methods, /* tp_methods */
250
PySfIntRect_members, /* tp_members */
254
0, /* tp_descr_get */
255
0, /* tp_descr_set */
256
0, /* tp_dictoffset */
259
PySfIntRect_new, /* tp_new */
263
PyTypeObject PySfFloatRectType = {
265
"FloatRect", /*tp_name*/
266
sizeof(PySfFloatRect), /*tp_basicsize*/
268
(destructor)PySfFloatRect_dealloc, /*tp_dealloc*/
275
0, /*tp_as_sequence*/
281
PySfFloatRect_setattro, /*tp_setattro*/
283
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
284
"sf.FloatRect is an utility class for manipulating rectangles.", /* tp_doc */
287
0, /* tp_richcompare */
288
0, /* tp_weaklistoffset */
291
PySfFloatRect_methods, /* tp_methods */
292
PySfFloatRect_members, /* tp_members */
296
0, /* tp_descr_get */
297
0, /* tp_descr_set */
298
0, /* tp_dictoffset */
301
PySfFloatRect_new, /* tp_new */
306
PySfFloatRect_Contains(PySfFloatRect* self, PyObject *args)
310
if (!PyArg_ParseTuple(args, "ff:FloatRect.Contains", &x, &y))
313
return PyBool_FromLong(self->obj->Contains(x,y));
317
PySfFloatRect_Intersects(PySfFloatRect* self, PyObject *args)
319
PySfFloatRect *Rect=NULL, *OverlappingRect=NULL;
322
if (!PyArg_ParseTuple(args, "O!|O!:FloatRect.Intersects", &PySfFloatRectType, &Rect, &PySfFloatRectType, &OverlappingRect))
326
result = self->obj->Intersects(*(Rect->obj), (OverlappingRect->obj));
328
result = self->obj->Intersects(*(Rect->obj));
330
return PyBool_FromLong(result);
335
PySfIntRect_Contains(PySfIntRect* self, PyObject *args)
337
unsigned int x=0, y=0;
339
if (!PyArg_ParseTuple(args, "II:IntRect.Contains", &x, &y))
342
return PyBool_FromLong(self->obj->Contains(x,y));
346
PySfIntRect_Intersects(PySfIntRect* self, PyObject *args)
348
PySfIntRect *Rect=NULL, *OverlappingRect=NULL;
351
if (!PyArg_ParseTuple(args, "O!|O!:IntRect.Intersects", &PySfIntRectType, &Rect, &PySfIntRectType, &OverlappingRect))
355
result = self->obj->Intersects(*(Rect->obj), (OverlappingRect->obj));
357
result = self->obj->Intersects(*(Rect->obj));
359
return PyBool_FromLong(result);
363
PySfIntRectUpdateObj(PySfIntRect *self)
365
self->obj->Left = self->Left;
366
self->obj->Right = self->Right;
367
self->obj->Top = self->Top;
368
self->obj->Bottom = self->Bottom;
372
PySfFloatRectUpdateObj(PySfFloatRect *self)
374
self->obj->Left = self->Left;
375
self->obj->Right = self->Right;
376
self->obj->Top = self->Top;
377
self->obj->Bottom = self->Bottom;
381
PySfIntRectUpdateSelf(PySfIntRect *self)
383
self->Left = self->obj->Left;
384
self->Right = self->obj->Right;
385
self->Top = self->obj->Top;
386
self->Bottom = self->obj->Bottom;
390
PySfFloatRectUpdateSelf(PySfFloatRect *self)
392
self->Left = self->obj->Left;
393
self->Right = self->obj->Right;
394
self->Top = self->obj->Top;
395
self->Bottom = self->obj->Bottom;
401
return PyObject_New(PySfIntRect, &PySfIntRectType);
405
GetNewPySfFloatRect()
407
return PyObject_New(PySfFloatRect, &PySfFloatRectType);