Skip to content

Commit

Permalink
Merge branch 'maniek2332-ft/hashing'
Browse files Browse the repository at this point in the history
  • Loading branch information
labuzm committed Mar 14, 2020
2 parents 0917523 + c062f10 commit 04e4372
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 1 deletion.
2 changes: 2 additions & 0 deletions kaa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
23 changes: 23 additions & 0 deletions kaa/audio.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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())
Expand Down
8 changes: 8 additions & 0 deletions kaa/extra/include/hashing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <functional>


template<typename T>
std::size_t calculate_hash(const T& val)
{
return std::hash<T>{}(val);
}
12 changes: 12 additions & 0 deletions kaa/fonts.pxi
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions kaa/kaacore/audio.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions kaa/kaacore/fonts.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from libcpp.string cimport string
from libcpp cimport bool

from .exceptions cimport raise_py_error

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions kaa/kaacore/hashing.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cdef extern from "extra/include/hashing.h" nogil:
size_t c_calculate_hash "calculate_hash"[T](const T&)
3 changes: 3 additions & 0 deletions kaa/kaacore/shapes.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from libcpp.vector cimport vector
from libcpp cimport bool

from .vectors cimport CVector
from .geometry cimport CTransformation
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions kaa/kaacore/sprites.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions kaa/shapes.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down
12 changes: 12 additions & 0 deletions kaa/sprites.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions kaa/vectors.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 04e4372

Please sign in to comment.