diff --git a/kaa/CMakeLists.txt b/kaa/CMakeLists.txt index ed603463..d5bedaa2 100644 --- a/kaa/CMakeLists.txt +++ b/kaa/CMakeLists.txt @@ -46,9 +46,11 @@ set(CYTHON_FILES kaacore/transitions.pxd kaacore/custom_transitions.pxd kaacore/log.pxd + kaacore/hashing.pxd extra/include/pythonic_callback.h extra/include/python_exceptions_wrapper.h + extra/include/hashing.h ) add_custom_command( diff --git a/kaa/audio.pxi b/kaa/audio.pxi index f08cb3a2..cfa57cb0 100644 --- a/kaa/audio.pxi +++ b/kaa/audio.pxi @@ -8,6 +8,7 @@ from .kaacore.engine cimport get_c_engine from .kaacore.audio cimport ( CAudioManager, CSound, CSoundPlayback, CMusic, CAudioStatus ) +from .kaacore.hashing cimport c_calculate_hash DEF SOUND_FREELIST_SIZE = 30 DEF SOUND_PLAYBACK_FREELIST_SIZE = 10 @@ -31,6 +32,17 @@ cdef class Sound: def __init__(self, str sound_filepath, double volume=1.): self._attach_c_sound(CSound.load(sound_filepath.encode(), volume)) + def __richcmp__(self, Sound other, op): + if op == 2: + return self.c_sound == other.c_sound + elif op == 3: + return not self.c_sound == other.c_sound + else: + return NotImplemented + + def __hash__(self): + return c_calculate_hash[CSound](self.c_sound) + @property def volume(self): return self.c_sound.volume() @@ -103,6 +115,17 @@ cdef class Music: def __init__(self, str music_filepath, double volume=1.): self._attach_c_music(CMusic.load(music_filepath.encode(), volume)) + def __richcmp__(self, Music other, op): + if op == 2: + return self.c_music == other.c_music + elif op == 3: + return not self.c_music == other.c_music + else: + return NotImplemented + + def __hash__(self): + return c_calculate_hash[CMusic](self.c_music) + @staticmethod def get_current(): return get_music_wrapper(CMusic.get_current()) diff --git a/kaa/extra/include/hashing.h b/kaa/extra/include/hashing.h new file mode 100644 index 00000000..fce467a5 --- /dev/null +++ b/kaa/extra/include/hashing.h @@ -0,0 +1,8 @@ +#include + + +template +std::size_t calculate_hash(const T& val) +{ + return std::hash{}(val); +} diff --git a/kaa/fonts.pxi b/kaa/fonts.pxi index 2ef5f0cc..e044018e 100644 --- a/kaa/fonts.pxi +++ b/kaa/fonts.pxi @@ -1,5 +1,6 @@ from .kaacore.nodes cimport CNodeType from .kaacore.fonts cimport CFont, CTextNode +from .kaacore.hashing cimport c_calculate_hash cdef class Font: @@ -11,6 +12,17 @@ cdef class Font: def __init__(self, str font_filepath): self._attach_c_font(CFont.load(font_filepath.encode())) + def __richcmp__(self, Font other, op): + if op == 2: + return self.c_font == other.c_font + elif op == 3: + return not self.c_font == other.c_font + else: + return NotImplemented + + def __hash__(self): + return c_calculate_hash[CFont](self.c_font) + cdef Font get_font_wrapper(const CFont& c_font): cdef Font font = Font.__new__(Font) diff --git a/kaa/kaacore/audio.pxd b/kaa/kaacore/audio.pxd index cf0b9290..12942ded 100644 --- a/kaa/kaacore/audio.pxd +++ b/kaa/kaacore/audio.pxd @@ -18,6 +18,8 @@ cdef extern from "kaacore/audio.h" nogil: CSound load(const char* path, double volume) \ except +raise_py_error + bool operator==(const CSound&) + void play(double volume) \ except +raise_py_error @@ -54,6 +56,8 @@ cdef extern from "kaacore/audio.h" nogil: CMusic get_current() \ except +raise_py_error + bool operator==(const CMusic&) + double volume() except +raise_py_error CAudioStatus status() except +raise_py_error diff --git a/kaa/kaacore/fonts.pxd b/kaa/kaacore/fonts.pxd index 4c0ea306..813415d2 100644 --- a/kaa/kaacore/fonts.pxd +++ b/kaa/kaacore/fonts.pxd @@ -1,4 +1,5 @@ from libcpp.string cimport string +from libcpp cimport bool from .exceptions cimport raise_py_error @@ -9,6 +10,8 @@ cdef extern from "kaacore/fonts.h" nogil: CFont load(const string& font_filepath) \ except +raise_py_error + bool operator==(const CFont&) + cdef cppclass CTextNode "kaacore::TextNode": string content() \ except +raise_py_error diff --git a/kaa/kaacore/hashing.pxd b/kaa/kaacore/hashing.pxd new file mode 100644 index 00000000..3e3bf0c5 --- /dev/null +++ b/kaa/kaacore/hashing.pxd @@ -0,0 +1,2 @@ +cdef extern from "extra/include/hashing.h" nogil: + size_t c_calculate_hash "calculate_hash"[T](const T&) diff --git a/kaa/kaacore/shapes.pxd b/kaa/kaacore/shapes.pxd index df18ce06..50c08339 100644 --- a/kaa/kaacore/shapes.pxd +++ b/kaa/kaacore/shapes.pxd @@ -1,4 +1,5 @@ from libcpp.vector cimport vector +from libcpp cimport bool from .vectors cimport CVector from .geometry cimport CTransformation @@ -20,6 +21,8 @@ cdef extern from "kaacore/shapes.h" nogil: CShape() + bool operator==(const CShape&) + @staticmethod CShape Segment(const CVector a, const CVector b) \ except +raise_py_error diff --git a/kaa/kaacore/sprites.pxd b/kaa/kaacore/sprites.pxd index 45dea5f7..9c2ac81d 100644 --- a/kaa/kaacore/sprites.pxd +++ b/kaa/kaacore/sprites.pxd @@ -17,6 +17,7 @@ cdef extern from "kaacore/sprites.h" nogil: CSprite load(const char* path, uint64_t flags) \ except +raise_py_error + bool operator==(const CSprite&) CSprite crop(CVector new_origin, CVector new_dimensions) \ except +raise_py_error diff --git a/kaa/shapes.pxi b/kaa/shapes.pxi index fd6282c9..8d147c7e 100644 --- a/kaa/shapes.pxi +++ b/kaa/shapes.pxi @@ -2,6 +2,7 @@ from libcpp.vector cimport vector from .kaacore.vectors cimport CVector from .kaacore.shapes cimport CShape, CShapeType +from .kaacore.hashing cimport c_calculate_hash cdef class ShapeBase: @@ -24,6 +25,17 @@ cdef class ShapeBase: self.c_shape_ptr[0].transform(transformation.c_transformation) ) + def __hash__(self): + return c_calculate_hash[CShape](self.c_shape_ptr[0]) + + def __richcmp__(self, ShapeBase other, op): + if op == 2: + return self.c_shape_ptr[0] == other.c_shape_ptr[0] + elif op == 3: + return not self.c_shape_ptr[0] == other.c_shape_ptr[0] + else: + return NotImplemented + cdef class Segment(ShapeBase): def __init__(self, Vector a, Vector b): diff --git a/kaa/sprites.pxi b/kaa/sprites.pxi index 5f2a7eb4..1d1961df 100644 --- a/kaa/sprites.pxi +++ b/kaa/sprites.pxi @@ -4,6 +4,7 @@ from libcpp cimport bool from libcpp.vector cimport vector from .kaacore.sprites cimport CSprite, c_split_spritesheet +from .kaacore.hashing cimport c_calculate_hash DEF SPRITE_FREELIST_SIZE = 250 @@ -18,6 +19,17 @@ cdef class Sprite: def __init__(self, str path): self._set_c_sprite(CSprite.load(path.encode(), 0)) + def __richcmp__(self, Sprite other, op): + if op == 2: + return self.c_sprite == other.c_sprite + elif op == 3: + return not self.c_sprite == other.c_sprite + else: + return NotImplemented + + def __hash__(self): + return c_calculate_hash[CSprite](self.c_sprite) + def crop(self, Vector origin, Vector dimensions): assert self.c_sprite.has_texture() return get_sprite_wrapper(self.c_sprite.crop( diff --git a/kaa/vectors.pxi b/kaa/vectors.pxi index 81b9c0e8..bb062d73 100644 --- a/kaa/vectors.pxi +++ b/kaa/vectors.pxi @@ -6,6 +6,7 @@ from .kaacore.vectors cimport ( CVector_rotate_angle, CVector_oriented_angle ) from .kaacore.math cimport radians, degrees +from .kaacore.hashing cimport c_calculate_hash DEF VECTOR_FREELIST_SIZE = 32 @@ -48,6 +49,9 @@ cdef class Vector: def __repr__(self): return "V[{x}, {y}]".format(x=self.x, y=self.y) + def __hash__(self): + return c_calculate_hash(self.c_vector) + def __richcmp__(self, Vector other, op): if op == 2: return self.c_vector == other.c_vector diff --git a/kaacore b/kaacore index 800ee9b9..71099605 160000 --- a/kaacore +++ b/kaacore @@ -1 +1 @@ -Subproject commit 800ee9b96cc8d2628c5bf63a638712d41ffedfac +Subproject commit 7109960555d74a2184ff6677ad5d6dcdccc604be