forked from bastienleonard/pysfml-cython
/
sf.pyx
3218 lines (2322 loc) · 91.6 KB
/
sf.pyx
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- python -*-
# -*- coding: utf-8 -*-
# Copyright 2011, 2012 Bastien Léonard. All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# THIS SOFTWARE IS PROVIDED BY BASTIEN LÉONARD ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BASTIEN LÉONARD OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
"""Python wrapper for the C++ library SFML 2 (Simple and Fast
Multimedia Library)."""
from libc.stdlib cimport malloc, free
from libc.stdio cimport printf
from libcpp.vector cimport vector
from cython.operator cimport preincrement as preinc, dereference as deref
import threading
cimport decl
cimport declaudio
cimport declevent
cimport decljoy
cimport declkey
cimport declmouse
cimport declshader
cimport declstyle
cimport declprimitive
# Forward declarations
cdef class RenderTarget
cdef class RenderWindow
# If you add a class that inherits drawables to the module, you *must*
# add it to this list. It used in RenderTarget.draw(), to know
# whether a drawable is ``built-in'' or user-defined.
cdef sfml_drawables = (Shape, Sprite, Text, VertexArray)
# The shapes built in SFML. This is used to know whether update() can
# be called on a Shape object. (Shape objects wrap a special
# ShapeWithUpdate C++ object that has a public Update() method, but
# built-in Shapes don't support it, as it doesn't seem needed and
# would require extea work.)
cdef sfml_shapes = (RectangleShape, CircleShape, ConvexShape)
cdef error_messages = {}
cdef error_messages_lock = threading.Lock()
# TODO: apparently functions should be static in Python modules, see
# http://docs.python.org/extending/extending.html#providing-a-c-api-for-an-extension-module.
cdef extern void set_error_message(char* message):
ident = threading.current_thread().ident
with error_messages_lock:
error_messages[ident] = message
decl.replace_error_handler()
# Return the last error message for the current thread, or None. Will
# return None after you consumed the latest message, until a new
# message is added. The goal is to avoid showing the same message
# twice.
cdef object get_last_error_message():
ident = threading.current_thread().ident
with error_messages_lock:
if ident in error_messages:
message = error_messages[ident]
del error_messages[ident]
return message
return None
class PySFMLException(Exception):
def __init__(self):
message = get_last_error_message()
if message is None:
Exception.__init__(self)
else:
Exception.__init__(self, message)
BLEND_ALPHA = decl.BlendAlpha
BLEND_ADD = decl.BlendAdd
BLEND_MULTIPLY = decl.BlendMultiply
BLEND_NONE = decl.BlendNone
POINTS = decl.Points
LINES = decl.Lines
LINES_STRIP = decl.LinesStrip
TRIANGLES = decl.Triangles
TRIANGLES_STRIP = decl.TrianglesStrip
TRIANGLES_FAN = decl.TrianglesFan
QUADS = decl.Quads
cdef class Mouse:
LEFT = declmouse.Left
RIGHT = declmouse.Right
MIDDLE = declmouse.Middle
X_BUTTON1 = declmouse.XButton1
X_BUTTON2 = declmouse.XButton2
BUTTON_COUNT = declmouse.ButtonCount
@classmethod
def is_button_pressed(cls, int button):
return declmouse.IsButtonPressed(<declmouse.Button>button)
@classmethod
def get_position(cls, RenderWindow window=None):
cdef decl.Vector2i pos
if window is None:
pos = declmouse.GetPosition()
else:
pos = declmouse.GetPosition((<decl.RenderWindow*>window.p_this)[0])
return (pos.x, pos.y)
@classmethod
def set_position(cls, tuple position, RenderWindow window=None):
cdef decl.Vector2i cpp_pos
cpp_pos.x, cpp_pos.y = position
if window is None:
declmouse.SetPosition(cpp_pos)
else:
declmouse.SetPosition(cpp_pos,
(<decl.RenderWindow*>window.p_this)[0])
cdef class Joystick:
COUNT = decljoy.Count
BUTTON_COUNT = decljoy.ButtonCount
AXIS_COUNT = decljoy.AxisCount
X = decljoy.X
Y = decljoy.Y
Z = decljoy.Z
R = decljoy.R
U = decljoy.U
V = decljoy.V
POV_X = decljoy.PovX
POV_Y = decljoy.PovY
@classmethod
def is_connected(cls, unsigned int joystick):
return decljoy.IsConnected(joystick)
@classmethod
def get_button_count(cls, unsigned int joystick):
return decljoy.GetButtonCount(joystick)
@classmethod
def has_axis(cls, unsigned int joystick, int axis):
return decljoy.HasAxis(joystick, <decljoy.Axis>axis)
@classmethod
def is_button_pressed(cls, unsigned int joystick, unsigned int button):
return decljoy.IsButtonPressed(joystick, button)
@classmethod
def get_axis_position(cls, unsigned int joystick, int axis):
return decljoy.GetAxisPosition(joystick, <decljoy.Axis> axis)
cdef class Keyboard:
A = declkey.A
B = declkey.B
C = declkey.C
D = declkey.D
E = declkey.E
F = declkey.F
G = declkey.G
H = declkey.H
I = declkey.I
J = declkey.J
K = declkey.K
L = declkey.L
M = declkey.M
N = declkey.N
O = declkey.O
P = declkey.P
Q = declkey.Q
R = declkey.R
S = declkey.S
T = declkey.T
U = declkey.U
V = declkey.V
W = declkey.W
X = declkey.X
Y = declkey.Y
Z = declkey.Z
NUM0 = declkey.Num0
NUM1 = declkey.Num1
NUM2 = declkey.Num2
NUM3 = declkey.Num3
NUM4 = declkey.Num4
NUM5 = declkey.Num5
NUM6 = declkey.Num6
NUM7 = declkey.Num7
NUM8 = declkey.Num8
NUM9 = declkey.Num9
ESCAPE = declkey.Escape
L_CONTROL = declkey.LControl
L_SHIFT = declkey.LShift
L_ALT = declkey.LAlt
L_SYSTEM = declkey.LSystem
R_CONTROL = declkey.RControl
R_SHIFT = declkey.RShift
R_ALT = declkey.RAlt
R_SYSTEM = declkey.RSystem
MENU = declkey.Menu
L_BRACKET = declkey.LBracket
R_BRACKET = declkey.RBracket
SEMI_COLON = declkey.SemiColon
COMMA = declkey.Comma
PERIOD = declkey.Period
QUOTE = declkey.Quote
SLASH = declkey.Slash
BACK_SLASH = declkey.BackSlash
TILDE = declkey.Tilde
EQUAL = declkey.Equal
DASH = declkey.Dash
SPACE = declkey.Space
RETURN = declkey.Return
BACK = declkey.Back
TAB = declkey.Tab
PAGE_UP = declkey.PageUp
PAGE_DOWN = declkey.PageDown
END = declkey.End
HOME = declkey.Home
INSERT = declkey.Insert
DELETE = declkey.Delete
ADD = declkey.Add
SUBTRACT = declkey.Subtract
MULTIPLY = declkey.Multiply
DIVIDE = declkey.Divide
LEFT = declkey.Left
RIGHT = declkey.Right
UP = declkey.Up
DOWN = declkey.Down
NUMPAD0 = declkey.Numpad0
NUMPAD1 = declkey.Numpad1
NUMPAD2 = declkey.Numpad2
NUMPAD3 = declkey.Numpad3
NUMPAD4 = declkey.Numpad4
NUMPAD5 = declkey.Numpad5
NUMPAD6 = declkey.Numpad6
NUMPAD7 = declkey.Numpad7
NUMPAD8 = declkey.Numpad8
NUMPAD9 = declkey.Numpad9
F1 = declkey.F1
F2 = declkey.F2
F3 = declkey.F3
F4 = declkey.F4
F5 = declkey.F5
F6 = declkey.F6
F7 = declkey.F7
F8 = declkey.F8
F9 = declkey.F9
F10 = declkey.F10
F11 = declkey.F11
F12 = declkey.F12
F13 = declkey.F13
F14 = declkey.F14
F15 = declkey.F15
PAUSE = declkey.Pause
KEY_COUNT = declkey.KeyCount
@classmethod
def is_key_pressed(cls, int key):
return declkey.IsKeyPressed(<declkey.Key>key)
cdef class Style:
NONE = declstyle.None
TITLEBAR = declstyle.Titlebar
RESIZE = declstyle.Resize
CLOSE = declstyle.Close
FULLSCREEN = declstyle.Fullscreen
DEFAULT = declstyle.Default
cdef class IntRect:
cdef decl.IntRect *p_this
def __init__(self, int left=0, int top=0, int width=0, int height=0):
self.p_this = new decl.IntRect(left, top, width, height)
def __dealloc__(self):
del self.p_this
def __repr__(self):
return ('IntRect(left={0.left!r}, top={0.top!r}, '
'width={0.width!r}, height={0.height!r})'.format(self))
property left:
def __get__(self):
return self.p_this.Left
def __set__(self, int value):
self.p_this.Left = value
property top:
def __get__(self):
return self.p_this.Top
def __set__(self, int value):
self.p_this.Top = value
property width:
def __get__(self):
return self.p_this.Width
def __set__(self, int value):
self.p_this.Width = value
property height:
def __get__(self):
return self.p_this.Height
def __set__(self, int value):
self.p_this.Height = value
def contains(self, int x, int y):
return self.p_this.Contains(x, y)
def intersects(self, IntRect rect, IntRect intersection=None):
if intersection is None:
return self.p_this.Intersects(rect.p_this[0])
else:
return self.p_this.Intersects(rect.p_this[0],
intersection.p_this[0])
cdef IntRect wrap_int_rect_instance(decl.IntRect *p_cpp_instance):
cdef IntRect ret = IntRect.__new__(IntRect)
ret.p_this = p_cpp_instance
return ret
cdef decl.IntRect convert_to_int_rect(value):
if isinstance(value, IntRect):
return (<IntRect>value).p_this[0]
if isinstance(value, tuple):
return decl.IntRect(value[0], value[1], value[2], value[3])
raise TypeError("Expected IntRect or tuple, found {0}".format(type(value)))
cdef class FloatRect:
cdef decl.FloatRect *p_this
def __init__(self, float left=0, float top=0, float width=0,
float height=0):
self.p_this = new decl.FloatRect(left, top, width, height)
def __dealloc__(self):
del self.p_this
def __repr__(self):
return ('FloatRect(left={0.left!r}, top={0.top!r}, '
'width={0.width!r}, height={0.height!r})'.format(self))
property left:
def __get__(self):
return self.p_this.Left
def __set__(self, float value):
self.p_this.Left = value
property top:
def __get__(self):
return self.p_this.Top
def __set__(self, float value):
self.p_this.Top = value
property width:
def __get__(self):
return self.p_this.Width
def __set__(self, float value):
self.p_this.Width = value
property height:
def __get__(self):
return self.p_this.Height
def __set__(self, float value):
self.p_this.Height = value
def contains(self, int x, int y):
return self.p_this.Contains(x, y)
def intersects(self, FloatRect rect, FloatRect intersection=None):
if intersection is None:
return self.p_this.Intersects(rect.p_this[0])
else:
return self.p_this.Intersects(rect.p_this[0],
intersection.p_this[0])
cdef FloatRect wrap_float_rect_instance(decl.FloatRect *p_cpp_instance):
cdef FloatRect ret = FloatRect.__new__(FloatRect)
ret.p_this = p_cpp_instance
return ret
cdef class Vector2f:
cdef decl.Vector2f *p_this
def __cinit__(self, float x=0.0, float y=0.0):
self.p_this = new decl.Vector2f(x, y)
def __dealloc__(self):
del self.p_this
def __repr__(self):
return 'Vector2f({0}, {1})'.format(self.x, self.y)
def __richcmp__(Vector2f a, Vector2f b, int op):
# ==
if op == 2:
return a.x == b.x and a.y == b.y
# !=
elif op == 3:
return a.x != b.x or a.y != b.y
return NotImplemented
def __add__(a, b):
if isinstance(a, Vector2f) and isinstance(b, Vector2f):
return Vector2f(a.x + b.x, a.y + b.y)
return NotImplemented
def __iadd__(self, b):
if isinstance(b, Vector2f):
self.p_this.x += b.x
self.p_this.y += b.y
return self
return NotImplemented
def __sub__(a, b):
if isinstance(a, Vector2f) and isinstance(b, Vector2f):
return Vector2f(a.x - b.x, a.y - b.y)
def __isub__(self, b):
if isinstance(b, Vector2f):
self.p_this.x -= b.x
self.p_this.y -= b.y
return self
return NotImplemented
def __mul__(a, b):
if isinstance(a, Vector2f) and isinstance(b, (int, float)):
return Vector2f(a.x * b, a.y * b)
elif isinstance(a, (int, float)) and isinstance(b, Vector2f):
return Vector2f(b.x * a, b.y * a)
return NotImplemented
def __imul__(self, b):
if isinstance(b, (int, float)):
self.p_this.x *= b
self.p_this.y *= b
return self
return NotImplemented
def __div__(a, b):
if isinstance(a, Vector2f) and isinstance(b, (int, float)):
return Vector2f(a.x / <float>b, a.y / <float>b)
return NotImplemented
def __idiv__(self, b):
if isinstance(b, (int, float)):
self.p_this.x /= <float>b
self.p_this.y /= <float>b
return self
return NotImplemented
def copy(self):
return Vector2f(self.p_this.x, self.p_this.y)
property x:
def __get__(self):
return self.p_this.x
def __set__(self, float value):
self.p_this.x = value
property y:
def __get__(self):
return self.p_this.y
def __set__(self, float value):
self.p_this.y = value
@classmethod
def from_tuple(cls, tuple t):
return Vector2f(t[0], t[1])
cdef extern decl.Vector2f convert_to_vector2f(value):
if isinstance(value, Vector2f):
return (<Vector2f>value).p_this[0]
if isinstance(value, tuple):
return decl.Vector2f(value[0], value[1])
raise TypeError("Expected Vector2f or tuple, found {0}".format(type(value)))
cdef class Transform:
cdef decl.Transform *p_this
IDENTITY = wrap_transform_instance(<decl.Transform*>&decl.Identity)
def __init__(self, float a00, float a01, float a02,
float a10, float a11, float a12,
float a20, float a21, float a22):
self.p_this = new decl.Transform(a00, a01, a02,
a10, a11, a12,
a20, a21, a22)
def __dealloc__(self):
del self.p_this
def __str__(self):
cdef float *p
p = <float*>self.p_this.GetMatrix()
return ('[{0} {4} {8} {12}]\n'
'[{1} {5} {9} {13}]\n'
'[{2} {6} {10} {14}]\n'
'[{3} {7} {11} {15}]'
.format(p[0], p[1], p[2], p[3],
p[4], p[5], p[6], p[7],
p[8], p[9], p[10], p[11],
p[12], p[13], p[14], p[15]))
def __mul__(a, b):
cdef decl.Transform *p_t
cdef decl.Vector2f *p_v
if isinstance(a, Transform):
if isinstance(b, Transform):
p_t = new decl.Transform()
p_t[0] = ((<Transform>a).p_this[0] *
(<Transform>b).p_this[0])
return wrap_transform_instance(p_t)
elif isinstance(b, Vector2f):
p_v = new decl.Vector2f()
p_v[0] = ((<Transform>a).p_this[0] *
(<Vector2f>b).p_this[0])
return Vector2f(p_v.x, p_v.y)
return NotImplemented
property matrix:
def __get__(self):
cdef float *p = <float*>self.p_this.GetMatrix()
cdef ret = []
for i in range(16):
ret.append(p[0])
p += 1
return ret
def combine(self, Transform transform):
cdef decl.Transform *p = new decl.Transform()
p[0] = self.p_this.Combine(transform.p_this[0])
return wrap_transform_instance(p)
def get_inverse(self):
cdef decl.Transform *p = new decl.Transform()
p[0] = self.p_this.GetInverse()
return wrap_transform_instance(p)
def rotate(self, object angle, object center_x=None, object center_y=None):
if center_x is None and center_y is None:
self.p_this.Rotate(<float?>angle)
elif center_x is not None and center_y is not None:
self.p_this.Rotate(<float?>angle, <float?>center_x,
<float?>center_y)
else:
raise PySFMLException(
"You must provide either 1 float or 3 floats are arguments")
return self
def scale(self, float scale_x, float scale_y,
object center_x=None, object center_y=None):
if center_x is None and center_y is None:
self.p_this.Scale(scale_x, scale_y)
elif center_x is not None and center_y is not None:
self.p_this.Scale(scale_x, scale_y, <float?>center_x,
<float?>center_y)
else:
raise PySFMLException(
"You must provide either 2 floats or 4 floats as arguments")
return self
def transform_point(self, float x, float y):
cdef decl.Vector2f v = self.p_this.TransformPoint(x, y)
return (v.x, v.y)
def transform_rect(self, FloatRect rectangle):
cdef decl.FloatRect *p = new decl.FloatRect()
p[0] = self.p_this.TransformRect(rectangle.p_this[0])
return wrap_float_rect_instance(p)
def translate(self, float x, float y):
self.p_this.Translate(x, y)
return self
cdef Transform wrap_transform_instance(decl.Transform *p_cpp_instance):
cdef Transform ret = Transform.__new__(Transform)
ret.p_this = p_cpp_instance
return ret
cdef class Time:
cdef decl.Time *p_this
ZERO = wrap_time_instance(new decl.Time(decl.Time_Zero))
def __init__(self, float seconds=-1.0, int milliseconds=-1,
int microseconds=-1):
self.p_this = new decl.Time()
if seconds != -1.0:
self.p_this[0] = decl.Seconds(seconds)
elif milliseconds != -1:
self.p_this[0] = decl.Milliseconds(milliseconds)
elif microseconds != -1:
self.p_this[0] = decl.Microseconds(microseconds)
def __dealloc__(self):
del self.p_this
def __str__(self):
return 'Time ({0} seconds)'.format(self.as_seconds())
def __richcmp__(Time x, Time y, int op):
# ==
if op == 2:
return x.p_this[0] == y.p_this[0]
# !=
elif op == 3:
return x.p_this[0] != y.p_this[0]
# <
elif op == 0:
return x.p_this[0] < y.p_this[0]
# >
elif op == 4:
return x.p_this[0] > y.p_this[0]
# <=
elif op == 1:
return x.p_this[0] <= y.p_this[0]
# >=
elif op == 5:
return x.p_this[0] >= y.p_this[0]
return NotImplemented
def __add__(a, b):
if isinstance(a, Time) and isinstance(b, Time):
return a.p_this[0] + b.p_this[0]
return NotImplemented
def __sub__(a, b):
if isinstance(a, Time) and isinstance(b, Time):
return a.p_this[0] - b.p_this[0]
return NotImplemented
def __mul__(a, b):
if isinstance(a, (int, float)) and isinstance(b, Time):
a, b = b, a
if isinstance(a, Time):
if isinstance(b, int):
return wrap_time_instance(
new decl.Time((<Time>a).p_this[0] * <decl.Int64>b))
if isinstance(b, float):
return wrap_time_instance(
new decl.Time((<Time>a).p_this[0] * <float>b))
return NotImplemented
def __div__(a, b):
if isinstance(a, (int, float)) and isinstance(b, Time):
a, b = b, a
if isinstance(a, Time):
if isinstance(b, int):
return wrap_time_instance(
new decl.Time((<Time>a).p_this[0] / <decl.Int64>b))
if isinstance(b, float):
return wrap_time_instance(
new decl.Time((<Time>a).p_this[0] / <float>b))
return NotImplemented
def __neg__(self):
return -self.p_this.AsMilliseconds()
def as_seconds(self):
return self.p_this.AsSeconds()
def as_milliseconds(self):
return <int>self.p_this.AsMilliseconds()
def as_microseconds(self):
return self.p_this.AsMicroseconds()
cdef Time wrap_time_instance(decl.Time *p_cpp_instance):
cdef Time ret = Time.__new__(Time)
ret.p_this = p_cpp_instance
return ret
def seconds(float seconds):
cdef decl.Time *p = new decl.Time()
p[0] = decl.Seconds(seconds)
return wrap_time_instance(p)
def milliseconds(int milliseconds):
cdef decl.Time *p = new decl.Time()
p[0] = decl.Milliseconds(milliseconds)
return wrap_time_instance(p)
def microseconds(int microseconds):
cdef decl.Time *p = new decl.Time()
p[0] = decl.Microseconds(microseconds)
return wrap_time_instance(p)
cdef class Clock:
cdef decl.Clock *p_this
def __cinit__(self):
self.p_this = new decl.Clock()
def __dealloc__(self):
del self.p_this
property elapsed_time:
def __get__(self):
cdef decl.Time *p = new decl.Time()
p[0] = self.p_this.GetElapsedTime()
return wrap_time_instance(p)
def restart(self):
self.p_this.Restart()
cdef class Color:
BLACK = Color(0, 0, 0)
WHITE = Color(255, 255, 255)
RED = Color(255, 0, 0)
GREEN = Color(0, 255, 0)
BLUE = Color(0, 0, 255)
YELLOW = Color(255, 255, 0)
MAGENTA = Color(255, 0, 255)
CYAN = Color(0, 255, 255)
cdef decl.Color *p_this
def __init__(self, int r, int g, int b, int a=255):
self.p_this = new decl.Color(r, g, b, a)
def __dealloc__(self):
del self.p_this
def __repr__(self):
return 'Color({0.r}, {0.g}, {0.b}, {0.a})'.format(self)
def __richcmp__(Color x, Color y, int op):
# ==
if op == 2:
return (x.r == y.r and
x.g == y.g and
x.b == y.b and
x.a == y.a)
# !=
elif op == 3:
return not x == y
return NotImplemented
def __add__(x, y):
if isinstance(x, Color) and isinstance(y, Color):
return Color(min(x.r + y.r, 255),
min(x.g + y.g, 255),
min(x.b + y.b, 255),
min(x.a + y.a, 255))
return NotImplemented
def __mul__(x, y):
if isinstance(x, Color) and isinstance(y, Color):
return Color(x.r * y.r / 255,
x.g * y.g / 255,
x.b * y.b / 255,
x.a * y.a / 255)
return NotImplemented
property r:
def __get__(self):
return self.p_this.r
def __set__(self, unsigned int value):
self.p_this.r = value
property g:
def __get__(self):
return self.p_this.g
def __set__(self, unsigned int value):
self.p_this.g = value
property b:
def __get__(self):
return self.p_this.b
def __set__(self, unsigned int value):
self.p_this.b = value
property a:
def __get__(self):
return self.p_this.a
def __set__(self, unsigned int value):
self.p_this.a = value
cdef Color wrap_color_instance(decl.Color *p_cpp_instance):
cdef Color ret = Color.__new__(Color)
ret.p_this = p_cpp_instance
return ret
cdef class SoundBuffer:
cdef declaudio.SoundBuffer *p_this
cdef bint delete_this
def __init__(self):
# self.delete_this = True
raise NotImplementedError("Use static methods like load_from_file "
"to create SoundBuffer instances")
def __dealloc__(self):
if self.delete_this:
del self.p_this
property channel_count:
def __get__(self):
return self.p_this.GetChannelCount()
property duration:
def __get__(self):
cdef decl.Time *p = new decl.Time()
p[0] = self.p_this.GetDuration()
return wrap_time_instance(p)
property sample_rate:
def __get__(self):
return self.p_this.GetSampleRate()
property samples:
def __get__(self):
cdef decl.Int16 *p = <decl.Int16*>self.p_this.GetSamples()
cdef unsigned int i
ret = []
for i in range(self.p_this.GetSampleCount()):
ret.append(int(p[i]))
return ret
property sample_count:
def __get__(self):
return self.p_this.GetSampleCount()
@classmethod
def load_from_file(cls, char* filename):
cdef declaudio.SoundBuffer *p = new declaudio.SoundBuffer()
if p.LoadFromFile(filename):
return wrap_sound_buffer_instance(p, True)
raise PySFMLException()
@classmethod
def load_from_memory(cls, char* data):
cdef declaudio.SoundBuffer *p = new declaudio.SoundBuffer()
if p.LoadFromMemory(data, len(data)):
return wrap_sound_buffer_instance(p, True)
raise PySFMLException()
@classmethod
def load_from_samples(cls, list samples, unsigned int channels_count,
unsigned int sample_rate):