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

« back to all changes in this revision

Viewing changes to src/sfml/system.pyx

  • 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
#!/usr/bin/env python3
 
2
# -*- coding: utf-8 -*-
 
3
#
 
4
# pySFML - Python bindings for SFML
 
5
# Copyright 2012-2013, Jonathan De Wachter <dewachter.jonathan@gmail.com>
 
6
#
 
7
# This software is released under the LGPLv3 license.
 
8
# You should have received a copy of the GNU Lesser General Public License
 
9
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
10
 
 
11
 
 
12
from __future__ import division
 
13
from numbers import Number, Integral
 
14
from copy import deepcopy
 
15
import threading
 
16
 
 
17
cimport cython
 
18
 
 
19
from libcpp.string cimport string
 
20
from libcpp.vector cimport vector
 
21
 
 
22
cimport libcpp.sfml as sf
 
23
from libcpp.sfml cimport Int8, Int16, Int32, Int64
 
24
from libcpp.sfml cimport Uint8, Uint16, Uint32, Uint64
 
25
 
 
26
#cdef extern from "<string>" namespace "std":
 
27
        #cdef cppclass string:
 
28
                #char* c_str()
 
29
 
 
30
cdef extern from "error.hpp":
 
31
        void restorePythonErrorBuffer()
 
32
        object getLastErrorMessage()
 
33
 
 
34
__all__ = ['Time', 'sleep', 'Clock', 'seconds', 'milliseconds', 'microseconds',
 
35
                        'Vector2', 'Vector3', 'Thread', 'Lock', 'Mutex']
 
36
 
 
37
# expose a function to restore the error handler
 
38
cdef api void restoreErrorHandler():
 
39
        restorePythonErrorBuffer()
 
40
 
 
41
# expose a function to retrieve the last SFML error
 
42
cdef api object popLastErrorMessage():
 
43
        error = getLastErrorMessage()
 
44
 
 
45
        # remove the extra \n character (if any)
 
46
        if error[-1] == '\n':
 
47
                error = error[:-1]
 
48
 
 
49
        return error
 
50
 
 
51
# redirect SFML errors to our stream buffer
 
52
restoreErrorHandler()
 
53
 
 
54
cdef public class Vector2[type PyVector2Type, object PyVector2Object]:
 
55
        cdef public object x
 
56
        cdef public object y
 
57
 
 
58
        def __init__(self, x=0, y=0):
 
59
                self.x = x
 
60
                self.y = y
 
61
 
 
62
        def __repr__(self):
 
63
                return "sf.Vector2({0})".format(self)
 
64
 
 
65
        def __str__(self):
 
66
                return "{0}x, {1}y".format(self.x, self.y)
 
67
 
 
68
        def __richcmp__(Vector2 x, y, op):
 
69
                x1, y1 = x
 
70
                try: x2, y2 = y
 
71
                except TypeError: return False
 
72
 
 
73
                if op == 2: return x1 == x2 and y1 == y2
 
74
                elif op == 3: return not (x1 == x2 and y1 == y2)
 
75
                else: raise NotImplementedError
 
76
 
 
77
        def __iter__(self):
 
78
                return iter((self.x, self.y))
 
79
 
 
80
        def __getitem__(self, key):
 
81
                return (self.x, self.y)[key]
 
82
 
 
83
        def __setitem__(self, key, value):
 
84
                setattr(self, {0: 'x', 1: 'y'}[key], value)
 
85
 
 
86
        def __add__(self, other):
 
87
                if isinstance(other, Number):
 
88
                        return Vector2(self[0] + other, self[1] + other)
 
89
                else:
 
90
                        return Vector2(self[0] + other[0], self[1] + other[1])
 
91
 
 
92
        def __sub__(self, other):
 
93
                if isinstance(other, Number):
 
94
                        return Vector2(self[0] - other, self[1] - other)
 
95
                else:
 
96
                        return Vector2(self[0] - other[0], self[1] - other[1])
 
97
 
 
98
        def __mul__(self, other):
 
99
                if isinstance(other, Number):
 
100
                        return Vector2(self[0] * other, self[1] * other)
 
101
                else:
 
102
                        return Vector2(self[0] * other[0], self[1] * other[1])
 
103
 
 
104
        def __truediv__(self, other):
 
105
                if isinstance(other, Number):
 
106
                        return Vector2(self[0] / other, self[1] / other)
 
107
                else:
 
108
                        return Vector2(self[0] / other[0], self[1] / other[1])
 
109
 
 
110
        def __floordiv__(self, other):
 
111
                if isinstance(other, Number):
 
112
                        return Vector2(self[0] // other, self[1] // other)
 
113
                else:
 
114
                        return Vector2(self[0] // other[0], self[1] // other[1])
 
115
 
 
116
        def __div__(self, other):
 
117
                if isinstance(other, Integral):
 
118
                        return self.__floordiv__(other)
 
119
                elif isinstance(other, Number) and not isinstance(other, Integral):
 
120
                        return self.__truediv__(other)
 
121
                elif all(isinstance(i, Integral) for i in other):
 
122
                        return self.__floordiv__(other)
 
123
                else:
 
124
                        return self.__truediv__(other)
 
125
 
 
126
        def __mod__(self, other):
 
127
                if isinstance(other, Number):
 
128
                        return Vector2(self[0] % other, self[1] % other)
 
129
                else:
 
130
                        return Vector2(self[0] % other[0], self[1] % other[1])
 
131
 
 
132
        def __divmod__(self, other):
 
133
                return self // other, self % other
 
134
 
 
135
        def __iadd__(self, other):
 
136
                if isinstance(other, Number):
 
137
                        self[0] += other
 
138
                        self[1] += other
 
139
                else:
 
140
                        self[0] += other[0]
 
141
                        self[1] += other[1]
 
142
                return self
 
143
 
 
144
        def __isub__(self, other):
 
145
                if isinstance(other, Number):
 
146
                        self[0] -= other
 
147
                        self[1] -= other
 
148
                else:
 
149
                        self[0] -= other[0]
 
150
                        self[1] -= other[1]
 
151
                return self
 
152
 
 
153
        def __imul__(self, other):
 
154
                if isinstance(other, Number):
 
155
                        self[0] *= other
 
156
                        self[1] *= other
 
157
                else:
 
158
                        self[0] *= other[0]
 
159
                        self[1] *= other[1]
 
160
                return self
 
161
 
 
162
        def __itruediv__(self, other):
 
163
                if isinstance(other, Number):
 
164
                        self[0] /= other
 
165
                        self[1] /= other
 
166
                else:
 
167
                        self[0] /= other[0]
 
168
                        self[1] /= other[1]
 
169
                return self
 
170
 
 
171
        def __ifloordiv__(self, other):
 
172
                if isinstance(other, Number):
 
173
                        self[0] //= other
 
174
                        self[1] //= other
 
175
                else:
 
176
                        self[0] //= other[0]
 
177
                        self[1] //= other[1]
 
178
                return self
 
179
 
 
180
        def __idiv__(self, other):
 
181
                if isinstance(other, Integral):
 
182
                        return self.__ifloordiv__(other)
 
183
                elif isinstance(other, Number) and not isinstance(other, Integral):
 
184
                        return self.__itruediv__(other)
 
185
                elif all(isinstance(i, Integral) for i in other):
 
186
                        return self.__ifloordiv__(other)
 
187
                else:
 
188
                        return self.__itruediv__(other)
 
189
 
 
190
        def __imod__(self, other):
 
191
                if isinstance(other, Number):
 
192
                        self[0] %= other
 
193
                        self[1] %= other
 
194
                else:
 
195
                        self[0] %= other[0]
 
196
                        self[1] %= other[1]
 
197
                return self
 
198
 
 
199
        def __neg__(self):
 
200
                cdef Vector2 p = Vector2.__new__(Vector2)
 
201
                p.x, p.y = self
 
202
                p.x, p.y = -p.x, -p.y
 
203
                return p
 
204
 
 
205
        def __pos__(self):
 
206
                cdef Vector2 p = Vector2.__new__(Vector2)
 
207
                p.x, p.y = self
 
208
                p.x, p.y = +p.x, +p.y
 
209
                return p
 
210
 
 
211
        def __copy__(self):
 
212
                cdef Vector2 p = Vector2.__new__(Vector2)
 
213
                p.x, p.y = self
 
214
                return p
 
215
 
 
216
        def __deepcopy__(self):
 
217
                cdef Vector2 p = Vector2.__new__(Vector2)
 
218
                p.x, p.y = deepcopy(self.x), deepcopy(self.y)
 
219
                return p
 
220
 
 
221
 
 
222
cdef public class Vector3[type PyVector3Type, object PyVector3Object]:
 
223
        cdef public object x
 
224
        cdef public object y
 
225
        cdef public object z
 
226
 
 
227
        def __init__(self, x=0, y=0, z=0):
 
228
                self.x = x
 
229
                self.y = y
 
230
                self.z = z
 
231
 
 
232
        def __repr__(self):
 
233
                return "sf.Vector3({0})".format(self)
 
234
 
 
235
        def __str__(self):
 
236
                return "{0}x, {1}y, {2}z".format(self.x, self.y, self.z)
 
237
 
 
238
        def __richcmp__(Vector3 x, y, op):
 
239
                x1, y1, z1 = x
 
240
                try: x2, y2, z2 = y
 
241
                except Exception: return False
 
242
 
 
243
                if op == 2: return x1 == x2 and y1 == y2 and z1 == z2
 
244
                elif op == 3: return not (x1 == x2 and y1 == y2 and z1 == z2)
 
245
                else: raise NotImplementedError
 
246
 
 
247
        def __iter__(self):
 
248
                return iter((self.x, self.y, self.z))
 
249
 
 
250
        def __getitem__(self, key):
 
251
                return (self.x, self.y, self.z)[key]
 
252
 
 
253
        def __setitem__(self, key, value):
 
254
                setattr(self, {0: 'x', 1: 'y', 2: 'z'}[key], value)
 
255
 
 
256
        def __add__(self, other):
 
257
                if isinstance(other, Number):
 
258
                        return Vector3(self[0] + other,
 
259
                                                   self[1] + other, self[2] + other)
 
260
                else:
 
261
                        return Vector3(self[0] + other[0],
 
262
                                                   self[1] + other[1], self[2] + other[2])
 
263
 
 
264
        def __sub__(self, other):
 
265
                if isinstance(other, Number):
 
266
                        return Vector3(self[0] - other,
 
267
                                                   self[1] - other, self[2] - other)
 
268
                else:
 
269
                        return Vector3(self[0] - other[0],
 
270
                                                   self[1] - other[1], self[2] - other[2])
 
271
 
 
272
        def __mul__(self, other):
 
273
                if isinstance(other, Number):
 
274
                        return Vector3(self[0] * other,
 
275
                                                   self[1] * other, self[2] * other)
 
276
                else:
 
277
                        return Vector3(self[0] * other[0],
 
278
                                                   self[1] * other[1], self[2] * other[2])
 
279
 
 
280
        def __truediv__(self, other):
 
281
                if isinstance(other, Number):
 
282
                        return Vector3(self[0] / other,
 
283
                                                   self[1] / other, self[2] / other)
 
284
                else:
 
285
                        return Vector3(self[0] / other[0],
 
286
                                                   self[1] / other[1], self[2] / other[2])
 
287
 
 
288
        def __floordiv__(self, other):
 
289
                if isinstance(other, Number):
 
290
                        return Vector3(self[0] // other,
 
291
                                                   self[1] // other, self[2] // other)
 
292
                else:
 
293
                        return Vector3(self[0] // other[0],
 
294
                                                   self[1] // other[1], self[2] // other[2])
 
295
 
 
296
        def __div__(self, other):
 
297
                if isinstance(other, Integral):
 
298
                        return self.__floordiv__(other)
 
299
                elif isinstance(other, Number) and not isinstance(other, Integral):
 
300
                        return self.__truediv__(other)
 
301
                elif all(isinstance(i, Integral) for i in other):
 
302
                        return self.__floordiv__(other)
 
303
                else:
 
304
                        return self.__truediv__(other)
 
305
 
 
306
        def __mod__(self, other):
 
307
                if isinstance(other, Number):
 
308
                        return Vector3(self[0] % other,
 
309
                                                   self[1] % other, self[2] % other)
 
310
                else:
 
311
                        return Vector3(self[0] % other[0],
 
312
                                                   self[1] % other[1], self[2] % other[2])
 
313
 
 
314
        def __divmod__(self, other):
 
315
                return self // other, self % other
 
316
 
 
317
        def __iadd__(self, other):
 
318
                if isinstance(other, Number):
 
319
                        self[0] += other
 
320
                        self[1] += other
 
321
                        self[2] += other
 
322
                else:
 
323
                        self[0] += other[0]
 
324
                        self[1] += other[1]
 
325
                        self[2] += other[2]
 
326
                return self
 
327
 
 
328
        def __isub__(self, other):
 
329
                if isinstance(other, Number):
 
330
                        self[0] -= other
 
331
                        self[1] -= other
 
332
                        self[2] -= other
 
333
                else:
 
334
                        self[0] -= other[0]
 
335
                        self[1] -= other[1]
 
336
                        self[2] -= other[2]
 
337
                return self
 
338
 
 
339
        def __imul__(self, other):
 
340
                if isinstance(other, Number):
 
341
                        self[0] *= other
 
342
                        self[1] *= other
 
343
                        self[2] *= other
 
344
                else:
 
345
                        self[0] *= other[0]
 
346
                        self[1] *= other[1]
 
347
                        self[2] *= other[2]
 
348
                return self
 
349
 
 
350
        def __itruediv__(self, other):
 
351
                if isinstance(other, Number):
 
352
                        self[0] /= other
 
353
                        self[1] /= other
 
354
                        self[2] /= other
 
355
                else:
 
356
                        self[0] /= other[0]
 
357
                        self[1] /= other[1]
 
358
                        self[2] /= other[2]
 
359
                return self
 
360
 
 
361
        def __ifloordiv__(self, other):
 
362
                if isinstance(other, Number):
 
363
                        self[0] //= other
 
364
                        self[1] //= other
 
365
                        self[2] //= other
 
366
                else:
 
367
                        self[0] //= other[0]
 
368
                        self[1] //= other[1]
 
369
                        self[2] //= other[2]
 
370
                return self
 
371
 
 
372
        def __div__(self, other):
 
373
                if isinstance(other, Integral):
 
374
                        return self.__ifloordiv__(other)
 
375
                elif isinstance(other, Number) and not isinstance(other, Integral):
 
376
                        return self.__itruediv__(other)
 
377
                elif all(isinstance(i, Integral) for i in other):
 
378
                        return self.__ifloordiv__(other)
 
379
                else:
 
380
                        return self.__itruediv__(other)
 
381
 
 
382
        def __imod__(self, other):
 
383
                if isinstance(other, Number):
 
384
                        self[0] %= other
 
385
                        self[1] %= other
 
386
                        self[2] %= other
 
387
                else:
 
388
                        self[0] %= other[0]
 
389
                        self[1] %= other[1]
 
390
                        self[2] %= other[2]
 
391
                return self
 
392
 
 
393
        def __neg__(self):
 
394
                cdef Vector3 p = Vector3.__new__(Vector3)
 
395
                p.x, p.y, p.z = self
 
396
                p.x, p.y, p.z = -p.x, -p.y, -p.z
 
397
                return p
 
398
 
 
399
        def __pos__(self):
 
400
                cdef Vector3 p = Vector3.__new__(Vector3)
 
401
                p.x, p.y, p.z = self
 
402
                p.x, p.y, p.z = +p.x, +p.y, +p.z
 
403
                return p
 
404
 
 
405
        def __copy__(self):
 
406
                cdef Vector3 p = Vector3.__new__(Vector3)
 
407
                p.x, p.y, p.z = self
 
408
                return p
 
409
 
 
410
        def __deepcopy__(self):
 
411
                cdef Vector3 p = Vector3.__new__(Vector3)
 
412
                p.x, p.y, p.z = deepcopy(self.x), deepcopy(self.y), deepcopy(self.z)
 
413
                return p
 
414
 
 
415
 
 
416
cdef api object wrap_vector2f(sf.Vector2f* p):
 
417
        cdef Vector2 r = Vector2.__new__(Vector2)
 
418
        r.x = p.x
 
419
        r.y = p.y
 
420
        return r
 
421
 
 
422
 
 
423
cdef public class Time[type PyTimeType, object PyTimeObject]:
 
424
        ZERO = wrap_time(<sf.Time*>&sf.time.Zero)
 
425
 
 
426
        cdef sf.Time *p_this
 
427
 
 
428
        def __init__(self):
 
429
                self.p_this = new sf.Time()
 
430
 
 
431
        def __dealloc__(self):
 
432
                del self.p_this
 
433
 
 
434
        def __repr__(self):
 
435
                return "sf.Time({0}s, {1}ms, {2}µs)".format(self.seconds, self.milliseconds, self.microseconds)
 
436
 
 
437
        def __str__(self):
 
438
                return "{0} milliseconds".format(self.milliseconds)
 
439
 
 
440
        def __richcmp__(Time x, Time y, int op):
 
441
                if op == 0:   return x.p_this[0] <      y.p_this[0]
 
442
                elif op == 2: return x.p_this[0] == y.p_this[0]
 
443
                elif op == 4: return x.p_this[0] >      y.p_this[0]
 
444
                elif op == 1: return x.p_this[0] <= y.p_this[0]
 
445
                elif op == 3: return x.p_this[0] != y.p_this[0]
 
446
                elif op == 5: return x.p_this[0] >= y.p_this[0]
 
447
 
 
448
        def __add__(Time x, Time y):
 
449
                cdef sf.Time* p = new sf.Time()
 
450
                p[0] = x.p_this[0] + y.p_this[0]
 
451
                return wrap_time(p)
 
452
 
 
453
        def __sub__(Time x, Time y):
 
454
                cdef sf.Time* p = new sf.Time()
 
455
                p[0] = x.p_this[0] - y.p_this[0]
 
456
                return wrap_time(p)
 
457
 
 
458
        def __iadd__(self, Time x):
 
459
                self.p_this[0] = self.p_this[0] + x.p_this[0]
 
460
                return self
 
461
 
 
462
        def __isub__(self, Time x):
 
463
                self.p_this[0] = self.p_this[0] - x.p_this[0]
 
464
                return self
 
465
 
 
466
        property seconds:
 
467
                def __get__(self):
 
468
                        return self.p_this.asSeconds()
 
469
 
 
470
                def __set__(self, float seconds):
 
471
                        self.p_this[0] = sf.seconds(seconds)
 
472
 
 
473
        property milliseconds:
 
474
                def __get__(self):
 
475
                        return self.p_this.asMilliseconds()
 
476
 
 
477
                def __set__(self, Int32 milliseconds):
 
478
                        self.p_this[0] = sf.milliseconds(milliseconds)
 
479
 
 
480
        property microseconds:
 
481
                def __get__(self):
 
482
                        return self.p_this.asMicroseconds()
 
483
 
 
484
                def __set__(self, Int64 microseconds):
 
485
                        self.p_this[0] = sf.microseconds(microseconds)
 
486
 
 
487
        def __copy__(self):
 
488
                cdef sf.Time* p = new sf.Time()
 
489
                p[0] = self.p_this[0]
 
490
                return wrap_time(p)
 
491
 
 
492
        def __deepcopy__(self):
 
493
                cdef sf.Time* p = new sf.Time()
 
494
                p[0] = self.p_this[0]
 
495
                return wrap_time(p)
 
496
 
 
497
 
 
498
cdef api object wrap_time(sf.Time* p):
 
499
        cdef Time r = Time.__new__(Time)
 
500
        r.p_this = p
 
501
        return r
 
502
 
 
503
def sleep(Time duration):
 
504
        with nogil: sf.sleep(duration.p_this[0])
 
505
 
 
506
cdef class Clock:
 
507
        cdef sf.Clock *p_this
 
508
 
 
509
        def __cinit__(self):
 
510
                self.p_this = new sf.Clock()
 
511
 
 
512
        def __dealloc__(self):
 
513
                del self.p_this
 
514
 
 
515
        def __repr__(self):
 
516
                return "sf.Clock({0})".format(self.elapsed_time)
 
517
 
 
518
        def __str__(self):
 
519
                return "{0}".format(self.elapsed_time)
 
520
 
 
521
        property elapsed_time:
 
522
                def __get__(self):
 
523
                        cdef sf.Time* p = new sf.Time()
 
524
                        p[0] = self.p_this.getElapsedTime()
 
525
                        return wrap_time(p)
 
526
 
 
527
        def restart(self):
 
528
                cdef sf.Time* p = new sf.Time()
 
529
                p[0] = self.p_this.restart()
 
530
                return wrap_time(p)
 
531
 
 
532
def seconds(float amount):
 
533
        cdef sf.Time* p = new sf.Time()
 
534
        p[0] = sf.seconds(amount)
 
535
        return wrap_time(p)
 
536
 
 
537
def milliseconds(Int32 amount):
 
538
        cdef sf.Time* p = new sf.Time()
 
539
        p[0] = sf.milliseconds(amount)
 
540
        return wrap_time(p)
 
541
 
 
542
def microseconds(Int64 amount):
 
543
        cdef sf.Time* p = new sf.Time()
 
544
        p[0] = sf.microseconds(amount)
 
545
        return wrap_time(p)
 
546
 
 
547
 
 
548
cdef class Mutex:
 
549
        cdef object _lock
 
550
 
 
551
        def __cinit__(self):
 
552
                self._lock = threading.RLock()
 
553
 
 
554
        def lock(self):
 
555
                self._lock.acquire()
 
556
 
 
557
        def unlock(self):
 
558
                self._lock.release()
 
559
 
 
560
cdef class Lock:
 
561
        cdef Mutex _mutex
 
562
 
 
563
        def __init__(self, Mutex mutex):
 
564
                self._mutex = mutex
 
565
                mutex.lock()
 
566
 
 
567
        def __dealloc__(self):
 
568
                self._mutex.unlock()
 
569
 
 
570
 
 
571
cdef class Thread:
 
572
        cdef object _thread
 
573
 
 
574
        def __init__(self, functor, *args, **kwargs):
 
575
                self._thread = threading.Thread(target=functor, args=args, kwargs=kwargs)
 
576
 
 
577
        def launch(self):
 
578
                self._thread.start()
 
579
 
 
580
        def wait(self):
 
581
                self._thread.join()
 
582
 
 
583
        def terminate(self):
 
584
                try:
 
585
                        self._thread._Thread__stop()
 
586
                except AttributeError:
 
587
                        self._thread._stop()