Skip to content

Commit d8b341f

Browse files
committed
Implemented mul and div operators in Time class (#38)
1 parent ec76e94 commit d8b341f

File tree

4 files changed

+85
-10
lines changed

4 files changed

+85
-10
lines changed

include/Includes/sfml/sfml.pxd

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,20 @@ cdef extern from "SFML/System.hpp" namespace "sf":
5757
Int32 asMilliseconds() const
5858
Int64 asMicroseconds() const
5959
Time operator-()
60-
bint operator==(Time&)
61-
bint operator!=(Time&)
62-
bint operator<(Time&)
63-
bint operator>(Time&)
64-
bint operator<=(Time&)
65-
bint operator>=(Time&)
66-
Time operator+(Time&)
67-
Time operator-(Time&)
60+
bint operator==(Time)
61+
bint operator!=(Time)
62+
bint operator<(Time)
63+
bint operator>(Time)
64+
bint operator<=(Time)
65+
bint operator>=(Time)
66+
Time operator+(Time)
67+
Time operator-(Time)
6868
Time operator*(float)
6969
Time operator*(Int64)
7070
Time operator/(float)
7171
Time operator/(Int64)
72-
Time operator%(Time&)
72+
float operator/(Time)
73+
Time operator%(Time)
7374

7475
cdef void sleep(Time) nogil
7576

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def remove_if_exist(filename):
9292

9393
system = extension(
9494
'system',
95-
[sources['system'], 'src/sfml/error.cpp', 'src/sfml/NumericObject.cpp'],
95+
[sources['system'], 'src/sfml/error.cpp', 'src/sfml/NumericObject.cpp', 'src/sfml/hacks.cpp'],
9696
['sfml-system'])
9797

9898
window = extension(

src/sfml/hacks.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,21 @@
2222
// 3. This notice may not be removed or altered from any source distribution.
2323
//------------------------------------------------------------------------------
2424

25+
#ifndef PYSFML_HACKS_HPP
26+
#define PYSFML_HACKS_HPP
27+
2528
#include <Python.h>
29+
#include <SFML/System.hpp>
2630

2731
#if PY_VERSION_HEX >= 0x03000000
2832
#define PyString_AsString PyBytes_AsString
2933
#endif
34+
35+
sf::Time Time_div_int(sf::Time left, sf::Int64 right);
36+
sf::Time Time_div_float(sf::Time left, float right);
37+
float Time_div_Time(sf::Time left, sf::Time right);
38+
39+
void Time_idiv_int(sf::Time& left, sf::Int64 right);
40+
void Time_idiv_float(sf::Time& left, float right);
41+
42+
#endif // PYSFML_HACKS_HPP

src/sfml/system.pyx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ cdef extern from "error.hpp":
5050
void restorePythonErrorBuffer()
5151
object getLastErrorMessage()
5252

53+
cdef extern from "hacks.h":
54+
sf.Time Time_div_int(sf.Time left, Int64)
55+
sf.Time Time_div_float(sf.Time left, float)
56+
float Time_div_Time(sf.Time, sf.Time)
57+
void Time_idiv_int(sf.Time&, Int64)
58+
void Time_idiv_float(sf.Time&, float)
59+
5360
__all__ = ['Time', 'sleep', 'Clock', 'seconds', 'milliseconds', 'microseconds',
5461
'Vector2', 'Vector3']
5562

@@ -440,6 +447,38 @@ cdef public class Time[type PyTimeType, object PyTimeObject]:
440447
p[0] = x.p_this[0] - y.p_this[0]
441448
return wrap_time(p)
442449

450+
def __mul__(Time self, other):
451+
cdef sf.Time* p = new sf.Time()
452+
453+
if isinstance(other, (int, long)):
454+
p[0] = self.p_this[0] * <Int64>other
455+
elif isinstance(other, float):
456+
p[0] = self.p_this[0] * <float>other
457+
else:
458+
return NotImplemented
459+
460+
return wrap_time(p)
461+
462+
def __truediv__(Time self, other):
463+
cdef sf.Time* p
464+
465+
if isinstance(other, Time):
466+
# return self.p_this[0] / (<Time>other).p_this[0]
467+
return Time_div_Time(self.p_this[0], (<Time>other).p_this[0])
468+
else:
469+
p = new sf.Time()
470+
if isinstance(other, (int, long)):
471+
# p[0] = self.p_this[0] / <Int64>other
472+
p[0] = Time_div_int(self.p_this[0], <Int64>other)
473+
elif isinstance(other, float):
474+
# p[0] = self.p_this[0] / <float>other
475+
p[0] = Time_div_float(self.p_this[0], <float>other)
476+
else:
477+
del p
478+
return NotImplemented
479+
480+
return wrap_time(p)
481+
443482
def __mod__(Time x, Time y):
444483
cdef sf.Time* p = new sf.Time()
445484
p[0] = x.p_this[0] % y.p_this[0]
@@ -453,6 +492,28 @@ cdef public class Time[type PyTimeType, object PyTimeObject]:
453492
self.p_this[0] -= x.p_this[0]
454493
return self
455494

495+
def __imul__(Time self, other):
496+
if isinstance(other, (int, long)):
497+
self.p_this[0] *= <Int64>other
498+
elif isinstance(other, float):
499+
self.p_this[0] *= <float>other
500+
else:
501+
return NotImplemented
502+
503+
return self
504+
505+
def __itruediv__(Time self, other):
506+
if isinstance(other, (int, long)):
507+
# self.p_this[0] /= <Int64>other
508+
Time_idiv_int(self.p_this[0], <Int64>other)
509+
elif isinstance(other, float):
510+
# self.p_this[0] /= <float>other
511+
Time_idiv_float(self.p_this[0], <float>other)
512+
else:
513+
return NotImplemented
514+
515+
return self
516+
456517
def __imod__(self, Time x):
457518
self.p_this[0] %= x.p_this[0]
458519
return self

0 commit comments

Comments
 (0)