~ubuntu-branches/ubuntu/trusty/python-sfml/trusty

« back to all changes in this revision

Viewing changes to python/src/Rect.cpp

  • Committer: Package Import Robot
  • Author(s): James Cowgill
  • Date: 2013-12-09 17:50:52 UTC
  • mfrom: (1.1.3) (8.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20131209175052-11v6drpb6g3yksst
Tags: 1.5.1.is.1.3+dfsg-1
* New upstream version 1.3 (from python-sfml.org)
  - This is a complete rewrite of the Python bindings for SFML2, and
    the new maintainer is using a different version numbering scheme.
* Added myself to the list of uploaders
* Change package priority from extra to optional
* Bumped standards version (to 3.9.5) and debhelper compat (to 9)
* Added Python 3 and documentation packages
* Improve package description for debug packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
////////////////////////////////////////////////////////////
2
 
//
3
 
// PySFML - Python binding for SFML (Simple and Fast Multimedia Library)
4
 
// Copyright (C) 2007, 2008 Rémi Koenig (remi.k2620@gmail.com)
5
 
//
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.
8
 
//
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:
12
 
//
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.
17
 
//
18
 
// 2. Altered source versions must be plainly marked as such,
19
 
//    and must not be misrepresented as being the original software.
20
 
//
21
 
// 3. This notice may not be removed or altered from any source distribution.
22
 
//
23
 
////////////////////////////////////////////////////////////
24
 
 
25
 
#include "Rect.hpp"
26
 
 
27
 
#include <structmember.h>
28
 
 
29
 
#include "compat.hpp"
30
 
#include "offsetof.hpp"
31
 
 
32
 
 
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."},
38
 
        {NULL}  /* Sentinel */
39
 
};
40
 
 
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."},
46
 
        {NULL}  /* Sentinel */
47
 
};
48
 
 
49
 
static void
50
 
PySfIntRect_dealloc(PySfIntRect* self)
51
 
{
52
 
        if (self->Owner)
53
 
                delete self->obj;
54
 
        free_object(self);
55
 
}
56
 
 
57
 
static void
58
 
PySfFloatRect_dealloc(PySfFloatRect* self)
59
 
{
60
 
        if (self->Owner)
61
 
                delete self->obj;
62
 
        free_object(self);
63
 
}
64
 
 
65
 
static PyObject *
66
 
PySfIntRect_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
67
 
{
68
 
        const char *kwlist[] = {"Left", "Top", "Right", "Bottom", NULL};
69
 
        PySfIntRect *self;
70
 
        self = (PySfIntRect *)type->tp_alloc(type, 0);
71
 
        if (self != NULL)
72
 
        {
73
 
                self->Owner = true;
74
 
                if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiii:IntRect.__new__", (char **)kwlist, &(self->Left), &(self->Top), &(self->Right), &(self->Bottom)))
75
 
                        return NULL;
76
 
                self->obj = new sf::IntRect(self->Left, self->Top, self->Right, self->Bottom);
77
 
        }
78
 
        return (PyObject *)self;
79
 
}
80
 
 
81
 
static PyObject *
82
 
PySfFloatRect_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
83
 
{
84
 
        const char *kwlist[] = {"Left", "Top", "Right", "Bottom", NULL};
85
 
        PySfFloatRect *self;
86
 
        self = (PySfFloatRect *)type->tp_alloc(type, 0);
87
 
        if (self != NULL)
88
 
        {
89
 
                self->Owner = true;
90
 
                if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffff:FloatRect.__new__", (char **)kwlist, &(self->Left), &(self->Top), &(self->Right), &(self->Bottom)))
91
 
                        return NULL;
92
 
                self->obj = new sf::FloatRect(self->Left, self->Top, self->Right, self->Bottom);
93
 
        }
94
 
        return (PyObject *)self;
95
 
}
96
 
 
97
 
static PyObject *
98
 
PySfIntRect_GetWidth(PySfIntRect *self)
99
 
{
100
 
        return PyLong_FromLong(self->obj->GetWidth());
101
 
}
102
 
 
103
 
static PyObject *
104
 
PySfIntRect_GetHeight(PySfIntRect *self)
105
 
{
106
 
        return PyLong_FromLong(self->obj->GetHeight());
107
 
}
108
 
 
109
 
static PyObject *
110
 
PySfIntRect_Contains(PySfIntRect* self, PyObject *args);
111
 
 
112
 
static PyObject *
113
 
PySfIntRect_Intersects(PySfIntRect* self, PyObject *args);
114
 
 
115
 
static PyObject *
116
 
PySfFloatRect_GetWidth(PySfFloatRect *self)
117
 
{
118
 
        return PyFloat_FromDouble(self->obj->GetWidth());
119
 
}
120
 
 
121
 
static PyObject *
122
 
PySfFloatRect_GetHeight(PySfFloatRect *self)
123
 
{
124
 
        return PyFloat_FromDouble(self->obj->GetHeight());
125
 
}
126
 
 
127
 
static PyObject *
128
 
PySfFloatRect_Contains(PySfFloatRect* self, PyObject *args);
129
 
 
130
 
static PyObject *
131
 
PySfFloatRect_Intersects(PySfFloatRect* self, PyObject *args);
132
 
 
133
 
static PyObject *
134
 
PySfIntRect_Offset(PySfIntRect* self, PyObject *args)
135
 
{
136
 
        int OffsetX, OffsetY;
137
 
 
138
 
        if (!PyArg_ParseTuple(args, "ii:IntRect.Offset", &OffsetX, &OffsetY))
139
 
                return NULL; 
140
 
 
141
 
        self->obj->Offset(OffsetX, OffsetY);
142
 
        PySfIntRectUpdateSelf(self);
143
 
        Py_RETURN_NONE;
144
 
}
145
 
 
146
 
static PyObject *
147
 
PySfFloatRect_Offset(PySfFloatRect* self, PyObject *args)
148
 
{
149
 
        float OffsetX, OffsetY;
150
 
 
151
 
        if (!PyArg_ParseTuple(args, "ff:FloatRect.Offset", &OffsetX, &OffsetY))
152
 
                return NULL; 
153
 
 
154
 
        self->obj->Offset(OffsetX, OffsetY);
155
 
        PySfFloatRectUpdateSelf(self);
156
 
        Py_RETURN_NONE;
157
 
}
158
 
 
159
 
 
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\
165
 
"},
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 */
179
 
};
180
 
 
181
 
 
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\
187
 
"},
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 */
201
 
};
202
 
 
203
 
int
204
 
PySfIntRect_setattro(PyObject* self, PyObject *attr_name, PyObject *v)
205
 
{
206
 
        int result = PyObject_GenericSetAttr(self, attr_name, v);
207
 
        PySfIntRect *Rect = (PySfIntRect *)self;
208
 
        PySfIntRectUpdateObj(Rect);
209
 
        return result;
210
 
}
211
 
 
212
 
int
213
 
PySfFloatRect_setattro(PyObject* self, PyObject *attr_name, PyObject *v)
214
 
{
215
 
        int result = PyObject_GenericSetAttr(self, attr_name, v);
216
 
        PySfFloatRect *Rect = (PySfFloatRect *)self;
217
 
        PySfFloatRectUpdateObj(Rect);
218
 
        return result;
219
 
}
220
 
 
221
 
PyTypeObject PySfIntRectType = {
222
 
        head_init
223
 
        "IntRect",                              /*tp_name*/
224
 
        sizeof(PySfIntRect),    /*tp_basicsize*/
225
 
        0,                                              /*tp_itemsize*/
226
 
        (destructor)PySfIntRect_dealloc, /*tp_dealloc*/
227
 
        0,                                              /*tp_print*/
228
 
        0,                                              /*tp_getattr*/
229
 
        0,                                              /*tp_setattr*/
230
 
        0,                                              /*tp_compare*/
231
 
        0,                                              /*tp_repr*/
232
 
        0,                                              /*tp_as_number*/
233
 
        0,                                              /*tp_as_sequence*/
234
 
        0,                                              /*tp_as_mapping*/
235
 
        0,                                              /*tp_hash */
236
 
        0,                                              /*tp_call*/
237
 
        0,                                              /*tp_str*/
238
 
        0,                                              /*tp_getattro*/
239
 
        PySfIntRect_setattro,   /*tp_setattro*/
240
 
        0,                                              /*tp_as_buffer*/
241
 
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
242
 
        "sf.IntRect is an utility class for manipulating rectangles.", /* tp_doc */
243
 
        0,                                              /* tp_traverse */
244
 
        0,                                              /* tp_clear */
245
 
        0,                                              /* tp_richcompare */
246
 
        0,                                              /* tp_weaklistoffset */
247
 
        0,                                              /* tp_iter */
248
 
        0,                                              /* tp_iternext */
249
 
        PySfIntRect_methods,    /* tp_methods */
250
 
        PySfIntRect_members,    /* tp_members */
251
 
        0,                                              /* tp_getset */
252
 
        0,                                              /* tp_base */
253
 
        0,                                              /* tp_dict */
254
 
        0,                                              /* tp_descr_get */
255
 
        0,                                              /* tp_descr_set */
256
 
        0,                                              /* tp_dictoffset */
257
 
        0,                                              /* tp_init */
258
 
        0,                                              /* tp_alloc */
259
 
        PySfIntRect_new,                /* tp_new */
260
 
};
261
 
 
262
 
 
263
 
PyTypeObject PySfFloatRectType = {
264
 
        head_init
265
 
        "FloatRect",                    /*tp_name*/
266
 
        sizeof(PySfFloatRect),  /*tp_basicsize*/
267
 
        0,                                              /*tp_itemsize*/
268
 
        (destructor)PySfFloatRect_dealloc, /*tp_dealloc*/
269
 
        0,                                              /*tp_print*/
270
 
        0,                                              /*tp_getattr*/
271
 
        0,                                              /*tp_setattr*/
272
 
        0,                                              /*tp_compare*/
273
 
        0,                                              /*tp_repr*/
274
 
        0,                                              /*tp_as_number*/
275
 
        0,                                              /*tp_as_sequence*/
276
 
        0,                                              /*tp_as_mapping*/
277
 
        0,                                              /*tp_hash */
278
 
        0,                                              /*tp_call*/
279
 
        0,                                              /*tp_str*/
280
 
        0,                                              /*tp_getattro*/
281
 
        PySfFloatRect_setattro, /*tp_setattro*/
282
 
        0,                                              /*tp_as_buffer*/
283
 
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
284
 
        "sf.FloatRect is an utility class for manipulating rectangles.", /* tp_doc */
285
 
        0,                                              /* tp_traverse */
286
 
        0,                                              /* tp_clear */
287
 
        0,                                              /* tp_richcompare */
288
 
        0,                                              /* tp_weaklistoffset */
289
 
        0,                                              /* tp_iter */
290
 
        0,                                              /* tp_iternext */
291
 
        PySfFloatRect_methods,  /* tp_methods */
292
 
        PySfFloatRect_members,  /* tp_members */
293
 
        0,                                              /* tp_getset */
294
 
        0,                                              /* tp_base */
295
 
        0,                                              /* tp_dict */
296
 
        0,                                              /* tp_descr_get */
297
 
        0,                                              /* tp_descr_set */
298
 
        0,                                              /* tp_dictoffset */
299
 
        0,                                              /* tp_init */
300
 
        0,                                              /* tp_alloc */
301
 
        PySfFloatRect_new,              /* tp_new */
302
 
};
303
 
 
304
 
 
305
 
static PyObject *
306
 
PySfFloatRect_Contains(PySfFloatRect* self, PyObject *args)
307
 
{
308
 
        float x=0, y=0;
309
 
 
310
 
        if (!PyArg_ParseTuple(args, "ff:FloatRect.Contains", &x, &y))
311
 
                return NULL; 
312
 
 
313
 
        return PyBool_FromLong(self->obj->Contains(x,y));
314
 
}
315
 
 
316
 
static PyObject *
317
 
PySfFloatRect_Intersects(PySfFloatRect* self, PyObject *args)
318
 
{
319
 
        PySfFloatRect *Rect=NULL, *OverlappingRect=NULL;
320
 
        bool result;
321
 
 
322
 
        if (!PyArg_ParseTuple(args, "O!|O!:FloatRect.Intersects", &PySfFloatRectType, &Rect, &PySfFloatRectType, &OverlappingRect))
323
 
                return NULL; 
324
 
 
325
 
        if (OverlappingRect)
326
 
                result = self->obj->Intersects(*(Rect->obj), (OverlappingRect->obj));
327
 
        else
328
 
                result = self->obj->Intersects(*(Rect->obj));
329
 
 
330
 
        return PyBool_FromLong(result);
331
 
}
332
 
 
333
 
 
334
 
static PyObject *
335
 
PySfIntRect_Contains(PySfIntRect* self, PyObject *args)
336
 
{
337
 
        unsigned int x=0, y=0;
338
 
 
339
 
        if (!PyArg_ParseTuple(args, "II:IntRect.Contains",  &x, &y))
340
 
                return NULL;
341
 
 
342
 
        return PyBool_FromLong(self->obj->Contains(x,y));
343
 
}
344
 
 
345
 
static PyObject *
346
 
PySfIntRect_Intersects(PySfIntRect* self, PyObject *args)
347
 
{
348
 
        PySfIntRect *Rect=NULL, *OverlappingRect=NULL;
349
 
        bool result;
350
 
 
351
 
        if (!PyArg_ParseTuple(args, "O!|O!:IntRect.Intersects", &PySfIntRectType, &Rect, &PySfIntRectType, &OverlappingRect))
352
 
                return NULL; 
353
 
 
354
 
        if (OverlappingRect)
355
 
                result = self->obj->Intersects(*(Rect->obj), (OverlappingRect->obj));
356
 
        else
357
 
                result = self->obj->Intersects(*(Rect->obj));
358
 
 
359
 
        return PyBool_FromLong(result);
360
 
}
361
 
 
362
 
void
363
 
PySfIntRectUpdateObj(PySfIntRect *self)
364
 
{
365
 
        self->obj->Left = self->Left;
366
 
        self->obj->Right = self->Right;
367
 
        self->obj->Top = self->Top;
368
 
        self->obj->Bottom = self->Bottom;
369
 
}
370
 
 
371
 
void
372
 
PySfFloatRectUpdateObj(PySfFloatRect *self)
373
 
{
374
 
        self->obj->Left = self->Left;
375
 
        self->obj->Right = self->Right;
376
 
        self->obj->Top = self->Top;
377
 
        self->obj->Bottom = self->Bottom;
378
 
}
379
 
 
380
 
void
381
 
PySfIntRectUpdateSelf(PySfIntRect *self)
382
 
{
383
 
        self->Left = self->obj->Left;
384
 
        self->Right = self->obj->Right;
385
 
        self->Top = self->obj->Top;
386
 
        self->Bottom = self->obj->Bottom;
387
 
}
388
 
 
389
 
void
390
 
PySfFloatRectUpdateSelf(PySfFloatRect *self)
391
 
{
392
 
        self->Left = self->obj->Left;
393
 
        self->Right = self->obj->Right;
394
 
        self->Top = self->obj->Top;
395
 
        self->Bottom = self->obj->Bottom;
396
 
}
397
 
 
398
 
PySfIntRect *
399
 
GetNewPySfIntRect()
400
 
{
401
 
        return PyObject_New(PySfIntRect, &PySfIntRectType);
402
 
}
403
 
 
404
 
PySfFloatRect *
405
 
GetNewPySfFloatRect()
406
 
{
407
 
        return PyObject_New(PySfFloatRect, &PySfFloatRectType);
408
 
}
409
 
 
410