Skip to content

Commit

Permalink
Merge pull request #246 from SleepProgger/get_component_count
Browse files Browse the repository at this point in the history
Functions to get the active count of components and entities
  • Loading branch information
Kovak committed Aug 8, 2017
2 parents 335b98d + a386047 commit af0994c
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 6 deletions.
4 changes: 0 additions & 4 deletions modules/core/kivent_core/gameworld.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ class GameWorld(Widget):
control the current screen of the gamescreenmanager, as well as which
systems are currently added or removed from canvas or paused.
**number_entities** (NumericProperty): This is the current number of
entities in the system. Do not modify directly, used to generate
entity_ids.
**gamescreenmanager** (ObjectProperty): Reference to the
GameScreenManager your game will use for UI screens.
Expand Down
2 changes: 2 additions & 0 deletions modules/core/kivent_core/managers/entity_manager.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ cdef class EntityManager(GameManager):
cdef void remove_entity(self, unsigned int entity_id)
cdef void set_entity_active(self, unsigned int entity_id)
cdef unsigned int get_size(self)
cpdef unsigned int get_active_entity_count(self)
cpdef unsigned int get_active_entity_count_in_zone(self, str zone) except <unsigned int>-1
24 changes: 24 additions & 0 deletions modules/core/kivent_core/managers/entity_manager.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,27 @@ cdef class EntityManager(GameManager):
self.clear_entity(entity_id)
cdef MemoryZone memory_zone = self.memory_index.memory_zone
memory_zone.free_slot(entity_id)

cpdef unsigned int get_active_entity_count(self):
''' Returns the number of all currently active entities.
**Return**:
unsigned int: active entity count.
'''
cdef MemoryZone memory_zone = self.memory_index.memory_zone
return memory_zone.get_active_slot_count()

cpdef unsigned int get_active_entity_count_in_zone(self, str zone) except <unsigned int>-1:
'''Returns the number of currently active entities for the given zone.
**Args**:
zone (str): The zone name.
**Return**:
unsigned int: active entity count in the given zone.
Will raise a **ValueError** exception if the given zone does not exists.
'''
cdef MemoryZone memory_zone = self.memory_index.memory_zone
cdef unsigned int pool_index = memory_zone.get_pool_index_from_name(zone)
return memory_zone.get_active_slot_count_in_pool(pool_index)
35 changes: 35 additions & 0 deletions modules/core/kivent_core/memory_handlers/indexing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,41 @@ cdef class IndexedMemoryZone:
else:
return self.zone_index.get_component_from_index(value)

def get_active_count_in_zone(self, str zone):
'''Returns the count of all active components of this
**MemoryZone** for the given zone.
**Args**:
zone (str): The zone name.
**Return**:
int: active block count.
Will raise a **ValueError** exception if this **Memoryzone**
does not use the given zone.
.. note:: For Cython access use the \
:func:`kivent_core.memory_handlers.zone.MemoryZone.get_active_slot_count_in_pool` \
method in the :class:`kivent_core.memory_handlers.zone.MemoryZone` directly.
'''
cdef MemoryZone memory_zone = self.memory_zone
cdef unsigned int pool_index = memory_zone.get_pool_index_from_name(zone)
return self.memory_zone.get_active_slot_count_in_pool(pool_index)

def get_active_count(self):
''' Returns the count of all active components
in all used zones.
**Return**:
int: The count of all active components.
.. note:: For Cython access use the \
:func:`kivent_core.memory_handlers.zone.MemoryZone.get_active_slot_count` \
method in the :class:`kivent_core.memory_handlers.zone.MemoryZone` directly.
'''
cdef MemoryZone memory_zone = self.memory_zone
return self.memory_zone.get_active_slot_count()

cdef void* get_pointer(self, unsigned int index) except NULL:
'''Returns a pointer to the data for the slot at index.
Args:
Expand Down
4 changes: 3 additions & 1 deletion modules/core/kivent_core/memory_handlers/zone.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ cdef class MemoryZone:
cdef void* get_pointer(self, unsigned int index) except NULL
cdef unsigned int get_pool_end_from_pool_index(self, unsigned int index)
cdef tuple get_pool_range(self, unsigned int pool_index)
cdef unsigned int get_pool_index_from_name(self, str zone_name)
cdef unsigned int get_pool_index_from_name(self, str zone_name) except <unsigned int>-1
cdef unsigned int get_pool_offset(self, unsigned int pool_index)
cdef unsigned int get_size(self)
cdef unsigned int get_active_slot_count(self)
cdef unsigned int get_active_slot_count_in_pool(self, unsigned int pool_index) except <unsigned int>-1
30 changes: 29 additions & 1 deletion modules/core/kivent_core/memory_handlers/zone.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ cdef class MemoryZone:
range_a((index, index+pool_count-1))
self.count += pool_count

cdef unsigned int get_pool_index_from_name(self, str zone_name):
cdef unsigned int get_pool_index_from_name(self, str zone_name) except <unsigned int>-1:
'''Gets the index of the pool from the zone_name
Arg:
zone_name (str): Name of the zone as passed into the desired_counts
Expand Down Expand Up @@ -309,3 +309,31 @@ cdef class MemoryZone:
pool = pools[key]
size += pool.get_size()
return size

cdef unsigned int get_active_slot_count_in_pool(self, unsigned int pool_index) except <unsigned int>-1:
''' Returns the amount of active (non freed) slots for the given pool.
Args:
pool_index (unsigned int): The pool index for a zone
aquired f.e. by `memory_zone.get_pool_index_from_name(zone)`.
Return:
unsigned int: the slot count.
'''
cdef MemoryPool pool = self.memory_pools[pool_index]
return pool.used - pool.free_count

cdef unsigned int get_active_slot_count(self):
'''Returns the combined amount of all active
(non freed) slots from all the MemoryPools in this MemoryZone.
Return:
unsigned int: the slot count.
'''
cdef dict pools = self.memory_pools
cdef MemoryPool pool
cdef unsigned int count = 0
for key in pools:
pool = pools[key]
count += pool.used - pool.free_count
return count
2 changes: 2 additions & 0 deletions modules/core/kivent_core/systems/gamesystem.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ cdef class GameSystem(CWidget):
cdef unsigned int component_count
cdef object free_indices
cdef dict copied_components
cpdef unsigned int get_active_component_count(self) except <unsigned int>-1
cpdef unsigned int get_active_component_count_in_zone(self, str zone) except <unsigned int>-1
16 changes: 16 additions & 0 deletions modules/core/kivent_core/systems/gamesystem.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,22 @@ cdef class GameSystem(CWidget):
self.py_components[component_index] = None
self.free_indices.append(component_index)

cpdef unsigned int get_active_component_count(self) except <unsigned int>-1:
'''
Returns the number of active components in this system.
'''
return self.component_count - len(self.free_indices)

cpdef unsigned int get_active_component_count_in_zone(self, str zone) except <unsigned int>-1:
'''
Returns the number of active components of this system in the given zone.
Not implemented in the python GameSystem, but overwritten in
StaticMemGameSystems and other GameSystems supporting zones.
Calling this method on GameSystems which did not overwrite this function
will raise a **NotImplementedError**.
'''
raise NotImplementedError("The default python GameSystem does not support zones.")

def on_remove_system(self):
'''Function called when a system is removed during a gameworld state
Expand Down
27 changes: 27 additions & 0 deletions modules/core/kivent_core/systems/staticmemgamesystem.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,33 @@ cdef class StaticMemGameSystem(GameSystem):
recycling'''
pass

cpdef unsigned int get_active_component_count(self) except <unsigned int>-1:
'''Returns the number of all active components in this system.
**Return:**
unsigned int: The number of active components.
'''
cdef IndexedMemoryZone indexed = self.imz_components
cdef MemoryZone memory_zone = indexed.memory_zone
return memory_zone.get_active_slot_count()

cpdef unsigned int get_active_component_count_in_zone(self, str zone) except <unsigned int>-1:
'''Returns the number of active components of this system in the given zone.
**Args:**
zone (str): The name of the zone to get the count from.
**Return:**
unsigned int: The number of active components in the given zone.
Will raise a **ValueError** exception if this **GameSystem**
does not use the given zone.
'''
cdef IndexedMemoryZone indexed = self.imz_components
cdef MemoryZone memory_zone = indexed.memory_zone
cdef unsigned int pool_index = memory_zone.get_pool_index_from_name(zone)
return memory_zone.get_active_slot_count_in_pool(pool_index)


cdef class ZonedAggregator:
'''
Expand Down

0 comments on commit af0994c

Please sign in to comment.