Skip to content

Commit

Permalink
Merge pull request #62 from labuzm/master
Browse files Browse the repository at this point in the history
kaa should use get_engine from kaacore
  • Loading branch information
maniek2332 committed Feb 23, 2020
2 parents 1fb3be3 + 174846e commit aec9228
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 94 deletions.
8 changes: 2 additions & 6 deletions kaa/audio.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,8 @@ cdef Music get_music_wrapper(const CMusic& c_music):

@cython.final
cdef class _AudioManager:
cdef CAudioManager* _get_c_audio_manager(self):
cdef CEngine* c_engine = get_c_engine()
assert c_engine != NULL
cdef CAudioManager* c_audio_manager = c_engine.audio_manager.get()
assert c_audio_manager != NULL
return c_audio_manager
cdef CAudioManager* _get_c_audio_manager(self) except NULL:
return get_c_engine().audio_manager.get()

@property
def master_volume(self):
Expand Down
45 changes: 20 additions & 25 deletions kaa/engine.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ from libcpp.vector cimport vector

from .kaacore.vectors cimport CUVec2
from .kaacore.scenes cimport CScene
from .kaacore.engine cimport CEngine, get_c_engine, CVirtualResolutionMode
from .kaacore.engine cimport (
CEngine, get_c_engine, is_c_engine_initialized, CVirtualResolutionMode
)
from .kaacore.display cimport CDisplay
from .kaacore.log cimport c_log_dynamic, CLogCategory, CLogLevel

Expand All @@ -30,32 +32,26 @@ cdef class _Engine:
_Renderer _renderer
_AudioManager _audio_manager

def __init__(self):
def __cinit__(self):
self._window = _Window()
self._renderer = _Renderer()
self._audio_manager = _AudioManager()

cdef inline CEngine* _get_c_engine(self):
cdef CEngine* c_engine = get_c_engine()
if c_engine == NULL:
raise ValueError("Engine is not running")
return c_engine

@property
def current_scene(self):
return (<CPyScene*>self._get_c_engine().current_scene()).get_py_scene()
return (<CPyScene*>get_c_engine().current_scene()).get_py_scene()

def change_scene(self, Scene scene not None):
self._get_c_engine().change_scene(scene.c_scene)
get_c_engine().change_scene(scene.c_scene)

def run(self, Scene scene not None):
self._get_c_engine().run(<CScene*>scene.c_scene)
get_c_engine().run(<CScene*>scene.c_scene)

def quit(self):
self._get_c_engine().quit()
get_c_engine().quit()

def get_displays(self):
cdef vector[CDisplay] c_displays = self._get_c_engine().get_displays()
cdef vector[CDisplay] c_displays = get_c_engine().get_displays()
cdef CDisplay c_disp
displays_list = []

Expand All @@ -65,31 +61,31 @@ cdef class _Engine:

@property
def virtual_resolution(self):
cdef CUVec2 c_virtual_resolution = self._get_c_engine().virtual_resolution()
cdef CUVec2 c_virtual_resolution = get_c_engine().virtual_resolution()
return Vector(c_virtual_resolution.x,
c_virtual_resolution.y)

@virtual_resolution.setter
def virtual_resolution(self, Vector new_resolution):
self._get_c_engine().virtual_resolution(
get_c_engine().virtual_resolution(
CUVec2(new_resolution.x, new_resolution.y)
)

@property
def virtual_resolution_mode(self):
return VirtualResolutionMode(
<uint32_t>self._get_c_engine().virtual_resolution_mode()
<uint32_t>get_c_engine().virtual_resolution_mode()
)

@property
def virtual_resolution_mode(self):
return VirtualResolutionMode(
<uint32_t>self._get_c_engine().virtual_resolution_mode()
<uint32_t>get_c_engine().virtual_resolution_mode()
)

@virtual_resolution_mode.setter
def virtual_resolution_mode(self, new_mode):
self._get_c_engine().virtual_resolution_mode(
get_c_engine().virtual_resolution_mode(
<CVirtualResolutionMode>(<uint32_t>(int(new_mode)))
)

Expand All @@ -106,8 +102,8 @@ cdef class _Engine:
return self._audio_manager

def stop(self):
if get_c_engine() == NULL:
raise ValueError("Engine is stopped")
if not is_c_engine_initialized():
raise ValueError('Engine is already stopped.')
assert _c_engine_instance != NULL

_c_engine_instance.reset(NULL)
Expand All @@ -124,10 +120,8 @@ cdef _Engine _engine_wrapper = _Engine()

def Engine(Vector virtual_resolution,
virtual_resolution_mode=None, show_window=True):
global _c_engine_instance
if get_c_engine() != NULL:
raise ValueError('Engine was already started.')
assert _c_engine_instance == NULL
if is_c_engine_initialized():
raise ValueError('Engine is already started.')

cdef CUVec2 c_virtual_resolution = CUVec2(
virtual_resolution.x, virtual_resolution.y
Expand All @@ -143,6 +137,7 @@ def Engine(Vector virtual_resolution,
c_virtual_resolution,
)
assert c_engine_ptr != NULL
global _c_engine_instance
_c_engine_instance = unique_ptr[CEngine](c_engine_ptr)

c_log_dynamic(
Expand All @@ -157,7 +152,7 @@ def Engine(Vector virtual_resolution,


def get_engine():
if get_c_engine() != NULL:
if is_c_engine_initialized():
return _engine_wrapper


Expand Down
62 changes: 33 additions & 29 deletions kaa/input.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -761,91 +761,93 @@ cdef class Event(_BaseEvent):


cdef class _BaseInputManager:
cdef CInputManager* c_input_manager

def __cinit__(self):
self.c_input_manager = get_c_engine().input_manager.get()
cdef CInputManager* _get_c_input_manager(self) except NULL:
return get_c_engine().input_manager.get()


@cython.final
cdef class SystemManager(_BaseInputManager):
def get_clipboard_text(self):
self.input_manager.system.get_clipboard_text().c_str()
self._get_c_input_manager().system.get_clipboard_text().c_str()

def set_clipboard_text(self, str text not None):
self.input_manager.system.set_clipboard_text(text)
self._get_c_input_manager().system.set_clipboard_text(text)


@cython.final
cdef class KeyboardManager(_BaseInputManager):
def is_pressed(self, kc not None):
return self.c_input_manager.keyboard.is_pressed(
return self._get_c_input_manager().keyboard.is_pressed(
<CKeycode>(<uint32_t>(kc.value))
)

def is_released(self, kc not None):
return self.c_input_manager.keyboard.is_released(
return self._get_c_input_manager().keyboard.is_released(
<CKeycode>(<uint32_t>(kc.value))
)


@cython.final
cdef class MouseManager(_BaseInputManager):
def is_pressed(self, mc not None):
return self.c_input_manager.mouse.is_pressed(
return self._get_c_input_manager().mouse.is_pressed(
<CMouseButton>(<uint32_t>(mc.value))
)

def is_released(self, mc not None):
return self.c_input_manager.mouse.is_released(
return self._get_c_input_manager().mouse.is_released(
<CMouseButton>(<uint32_t>(mc.value))
)

def get_position(self):
return Vector.from_c_vector(self.c_input_manager.mouse.get_position())
return Vector.from_c_vector(
self._get_c_input_manager().mouse.get_position()
)


@cython.final
cdef class ControllerManager(_BaseInputManager):
def is_connected(self, CControllerID controller_id):
return self.c_input_manager.controller.is_connected(controller_id)
return self._get_c_input_manager().controller.is_connected(
controller_id
)

def is_pressed(self, cb not None, CControllerID controller_id):
return self.c_input_manager.controller.is_pressed(
return self._get_c_input_manager().controller.is_pressed(
<CControllerButton>(<uint32_t>(cb.value)), controller_id
)

def is_released(self, cb not None, CControllerID controller_id):
return self.c_input_manager.controller.is_released(
return self._get_c_input_manager().controller.is_released(
<CControllerButton>(<uint32_t>(cb.value)), controller_id
)

def is_axis_pressed(self, axis not None, CControllerID controller_id):
return self.c_input_manager.controller.is_pressed(
return self._get_c_input_manager().controller.is_pressed(
<CControllerAxis>(<uint32_t>(axis.value)), controller_id
)

def is_axis_released(self, axis not None, CControllerID controller_id):
return self.c_input_manager.controller.is_released(
return self._get_c_input_manager().controller.is_released(
<CControllerAxis>(<uint32_t>(axis.value)), controller_id
)

def get_axis_motion(self, axis not None, CControllerID controller_id):
return self.c_input_manager.controller.get_axis_motion(
return self._get_c_input_manager().controller.get_axis_motion(
<CControllerAxis>(<uint32_t>(axis.value)), controller_id
)

def get_name(self, CControllerID controller_id):
return self.c_input_manager.controller.get_name(controller_id).c_str()
return self._get_c_input_manager().controller.get_name(controller_id).c_str()

def get_triggers(self, CControllerID controller_id):
return Vector.from_c_vector(
self.c_input_manager.controller.get_triggers(controller_id)
self._get_c_input_manager().controller.get_triggers(controller_id)
)

def get_sticks(self, compound_axis not None, CControllerID controller_id):
return Vector.from_c_vector(
self.c_input_manager.controller.get_sticks(
self._get_c_input_manager().controller.get_sticks(
<CCompoundControllerAxis>(<uint32_t>(compound_axis.value)),
controller_id
)
Expand All @@ -855,13 +857,14 @@ cdef class ControllerManager(_BaseInputManager):
cdef int32_t c_event_handler(const CPythonicCallbackWrapper& c_wrapper,
const CEvent& c_event):

cdef object callback = <object>c_wrapper.py_callback
cdef Event event = Event.create(c_event)
cdef:
Event event = Event.create(c_event)
object callback = <object>c_wrapper.py_callback
try:
return 1 if callback(event) is True else 0
except Exception as py_exc:
c_wrap_python_exception(<PyObject*>py_exc)
return 0;
return 0


@cython.final
Expand All @@ -873,16 +876,14 @@ cdef class InputManager(_BaseInputManager):
readonly ControllerManager controller

def __cinit__(self):
assert self.c_input_manager != NULL

self.system = SystemManager()
self.keyboard = KeyboardManager()
self.mouse = MouseManager()
self.controller = ControllerManager()

def events(self):
cdef CEvent c_event
for c_event in self.c_input_manager.events_queue:
for c_event in self._get_c_input_manager().events_queue:
yield Event.create(c_event)

def register_callback(self, object event_type not None, object callback):
Expand All @@ -906,11 +907,14 @@ cdef class InputManager(_BaseInputManager):
cdef CEventType c_event_type = <CEventType>(<uint32_t>(event_type.value))

if callback is None:
self.c_input_manager.register_callback(c_event_type, <CEventCallback>nullptr)
return
return self._get_c_input_manager().register_callback(
c_event_type, <CEventCallback>nullptr
)

cdef CEventCallback bound_callback = bind_cython_event_callback(
c_event_handler,
CPythonicCallbackWrapper(<PyObject*>callback)
)
self.c_input_manager.register_callback(c_event_type, cmove(bound_callback))
self._get_c_input_manager().register_callback(
c_event_type, cmove(bound_callback)
)
15 changes: 5 additions & 10 deletions kaa/kaacore/engine.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ cdef extern from "kaacore/engine.h" nogil:
CVirtualResolutionMode virtual_resolution_mode)

vector[CDisplay] get_displays()
void run(CScene* c_scene) \
except +raise_py_error
void change_scene(CScene* c_scene) \
except +raise_py_error
void quit() \
except +raise_py_error
void run(CScene* c_scene) except +raise_py_error
void change_scene(CScene* c_scene) except +raise_py_error
void quit() except +raise_py_error

CScene* current_scene()
CUVec2 virtual_resolution()
Expand All @@ -45,7 +42,5 @@ cdef extern from "kaacore/engine.h" nogil:
CVirtualResolutionMode virtual_resolution_mode()
void virtual_resolution_mode(CVirtualResolutionMode vr_mode)

CEngine* c_engine "kaacore::engine"

cdef inline CEngine* get_c_engine():
return c_engine
bint is_c_engine_initialized "kaacore::is_engine_initialized"()
CEngine* get_c_engine "kaacore::get_engine"() except +raise_py_error
3 changes: 1 addition & 2 deletions kaa/nodes.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ cdef cppclass CPyNodeWrapper(CForeignNodeWrapper):
this.added_to_parent = True



cdef class NodeBase:
cdef:
# When node is created by class __init__ this member will be filled,
Expand All @@ -50,7 +49,7 @@ cdef class NodeBase:
def __init__(self, **options):
self.setup(**options)

cdef inline CNode* _get_c_node(self):
cdef inline CNode* _get_c_node(self) except NULL:
cdef CNode* c_node = self._c_node_ptr.get()
assert c_node != NULL, \
'Operation on uninitialized or deleted Node. Aborting.'
Expand Down
8 changes: 2 additions & 6 deletions kaa/renderer.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ from .kaacore.engine cimport CEngine, get_c_engine

@cython.final
cdef class _Renderer:
cdef CRenderer* _get_c_renderer(self):
cdef CEngine* c_engine = get_c_engine()
assert c_engine != NULL
cdef CRenderer* c_renderer = c_engine.renderer.get()
assert c_renderer != NULL
return c_renderer
cdef CRenderer* _get_c_renderer(self) except NULL:
return get_c_engine().renderer.get()

@property
def clear_color(self):
Expand Down
Loading

0 comments on commit aec9228

Please sign in to comment.