Skip to content

Commit

Permalink
revamped timers
Browse files Browse the repository at this point in the history
  • Loading branch information
labuzm committed Dec 29, 2020
1 parent 3e2fb2f commit e1b44b3
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 91 deletions.
16 changes: 9 additions & 7 deletions demos/timers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class TtlNode(BodyNode):

def __init__(self, **kwargs):
ttl = kwargs.pop('ttl')
self.ttl_timer = Timer(self.delete, ttl)
self.ttl_timer.start()
self.ttl_timer = Timer(lambda c: self.delete())
self.ttl_timer.start_global(ttl)
super().__init__(**kwargs)


Expand All @@ -28,23 +28,25 @@ def __init__(self):
SpaceNode(position=Vector(0, 0))
)
self.python_image = Sprite(PYTHON_IMAGE_PATH)
self.timer = Timer(self.spawn, 200, single_shot=False)
self.timer.start()
self.timer = Timer(self.spawn)
self.timer.start(0.2, self)
self.spawn()

def spawn(self):
def spawn(self, context=None):
angles = list(range(0, 360, 10))
for angle in angles:
self.space.add_child(
TtlNode(
mass=1e10,
sprite=self.python_image,
angular_velocity=1,
ttl=random.randrange(3000, 6000),
ttl=random.uniform(2, 6),
shape=Polygon.from_box(Vector(20, 20)),
velocity=Vector.from_angle_degrees(angle) * 50
)
)
if context:
return context.interval

def update(self, dt):
for event in self.input.events():
Expand All @@ -55,7 +57,7 @@ def update(self, dt):
if self.timer.is_running:
self.timer.stop()
else:
self.timer.start()
self.timer.start(0.2, self)


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion kaacore
Submodule kaacore updated 51 files
+4 −0 .gitmodules
+8 −8 CMakeLists.txt
+2 −2 demos/basic_scene.cpp
+1 −1 demos/controllers.cpp
+2 −1 demos/drawing_order.cpp
+6 −6 demos/fonts.cpp
+14 −6 demos/physics.cpp
+1 −1 demos/polygon_tester.cpp
+4 −3 demos/spatial_indexing.cpp
+4 −3 demos/sprites.cpp
+11 −9 demos/timers.cpp
+4 −3 demos/transitions.cpp
+2 −2 demos/window.cpp
+41 −0 include/kaacore/clock.h
+13 −1 include/kaacore/embedded_data.h
+6 −0 include/kaacore/engine.h
+0 −1 include/kaacore/input.h
+8 −5 include/kaacore/node_transitions.h
+3 −3 include/kaacore/nodes.h
+4 −3 include/kaacore/physics.h
+2 −1 include/kaacore/renderer.h
+12 −4 include/kaacore/scenes.h
+75 −25 include/kaacore/timers.h
+15 −14 include/kaacore/transitions.h
+40 −0 shaders/CMakeLists.txt
+ shaders/binary/default_glsl_fragment_shader.bin
+ shaders/binary/default_glsl_vertex_shader.bin
+ shaders/binary/default_hlsl_d3d11_fragment_shader.bin
+ shaders/binary/default_hlsl_d3d11_vertex_shader.bin
+ shaders/binary/default_hlsl_d3d9_fragment_shader.bin
+ shaders/binary/default_hlsl_d3d9_vertex_shader.bin
+0 −40 shaders/compile_shaders.sh
+0 −0 shaders/fs_default.sc
+0 −0 shaders/vs_default.sc
+2 −0 src/CMakeLists.txt
+72 −0 src/clock.cpp
+14 −3 src/embedded_data.cpp
+23 −16 src/engine.cpp
+8 −8 src/fonts.cpp
+3 −3 src/nodes.cpp
+8 −5 src/physics.cpp
+62 −29 src/renderer.cpp
+18 −6 src/scenes.cpp
+104 −132 src/timers.cpp
+48 −42 src/transitions.cpp
+16 −0 tests/CMakeLists.txt
+44 −0 tests/runner.cpp
+22 −0 tests/runner.h
+90 −0 tests/test_basics.cpp
+30 −0 tests/test_shapes.cpp
+1 −0 third_party/catch2
1 change: 1 addition & 0 deletions src/kaa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ set(CYTHON_FILES
kaacore/hashing.pxd
kaacore/easings.pxd
kaacore/spatial_index.pxd
kaacore/clock.pxd

extra/include/pythonic_callback.h
extra/include/python_exceptions_wrapper.h
Expand Down
4 changes: 2 additions & 2 deletions src/kaa/custom_transitions.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ cdef cppclass CPyNodeCustomTransition(CNodeTransitionCustomizable):
const CEasing easing):
this.prepare_func = prepare_func
this.evaluate_func = evaluate_func
this.duration = duration * warping.duration_factor()
this.internal_duration = duration
this.duration = CSeconds(duration * warping.duration_factor())
this.internal_duration = CSeconds(duration)
this.warping = warping
this._easing = easing

Expand Down
9 changes: 4 additions & 5 deletions src/kaa/extra/include/pythonic_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ class PythonicCallbackResult {
typedef PythonicCallbackResult<int> (*CythonCollisionHandler)(const PythonicCallbackWrapper&, Arbiter,
CollisionPair, CollisionPair);


CollisionHandlerFunc bind_cython_collision_handler(
const CythonCollisionHandler cy_handler, PythonicCallbackWrapper callback
)
Expand All @@ -168,14 +167,14 @@ CollisionHandlerFunc bind_cython_collision_handler(
}


typedef PythonicCallbackResult<void> (*CythonTimerCallback)(const PythonicCallbackWrapper&);
typedef PythonicCallbackResult<Seconds> (*CythonTimerCallback)(const PythonicCallbackWrapper&, TimerContext);

TimerCallback bind_cython_timer_callback(
const CythonTimerCallback cy_handler, PythonicCallbackWrapper callback
const CythonTimerCallback cy_handler, const PythonicCallbackWrapper callback
)
{
return [cy_handler, callback{std::move(callback)}]() {
std::move(cy_handler(callback)).unwrap_result();
return [cy_handler, callback{std::move(callback)}](TimerContext context) -> Seconds {
return cy_handler(callback, context).unwrap_result();
};
}

Expand Down
10 changes: 10 additions & 0 deletions src/kaa/kaacore/clock.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cdef extern from "<chrono>" namespace "std::chrono" nogil:
cdef cppclass duration[Repr, Period=*]:
duration()
duration(Repr) except +

Repr count()


cdef extern from "kaacore/clock.h" nogil:
ctypedef duration[long double] CSeconds "kaacore::Seconds"
9 changes: 5 additions & 4 deletions src/kaa/kaacore/custom_transitions.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from libcpp.memory cimport unique_ptr
from libcpp.functional cimport function

from .nodes cimport CNodePtr
from .clock cimport CSeconds
from .transitions cimport CTransitionStateBase, CTransitionWarping
from .easings cimport CEasing
from .glue cimport CPythonicCallbackWrapper, CPythonicCallbackResult
Expand All @@ -10,20 +11,20 @@ from .exceptions cimport raise_py_error

cdef extern from "kaacore/transitions.h":
cdef cppclass CNodeTransitionCustomizable "kaacore::NodeTransitionCustomizable":
double duration
double internal_duration
CSeconds duration
CSeconds internal_duration
CTransitionWarping warping
CEasing _easing

CNodeTransitionCustomizable() \
except +raise_py_error
CNodeTransitionCustomizable(const double duration, const CTransitionWarping& warping) \
CNodeTransitionCustomizable(const CSeconds duration, const CTransitionWarping& warping) \
except +raise_py_error

unique_ptr[CTransitionStateBase] prepare_state(CNodePtr node) nogil const
void evaluate(CTransitionStateBase* state, CNodePtr node, const double t) nogil const

ctypedef function[void(CNodePtr)] CNodeTransitionCallbackFunc "kaacore::NodeTransitionCallbackFunc";
ctypedef function[void(CNodePtr)] CNodeTransitionCallbackFunc "kaacore::NodeTransitionCallbackFunc"


cdef extern from "extra/include/pythonic_callback.h":
Expand Down
5 changes: 3 additions & 2 deletions src/kaa/kaacore/nodes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ from .geometry cimport CAlignment, CTransformation, CBoundingBox
from .physics cimport CSpaceNode, CBodyNode, CHitboxNode
from .fonts cimport CTextNode
from .shapes cimport CShape
from .clock cimport CSeconds
from .sprites cimport CSprite
from .scenes cimport CScene
from .transitions cimport CNodeTransitionHandle, CNodeTransitionsManager
Expand Down Expand Up @@ -95,8 +96,8 @@ cdef extern from "kaacore/nodes.h" nogil:
CAlignment origin_alignment() except +raise_py_error
void origin_alignment(const CAlignment& alignment) except +raise_py_error

uint32_t lifetime() except +raise_py_error
void lifetime(const uint32_t& lifetime) except +raise_py_error
CSeconds lifetime() except +raise_py_error
void lifetime(const CSeconds& lifetime) except +raise_py_error

CNodeTransitionHandle transition() except +raise_py_error
void transition(const CNodeTransitionHandle& transition) except +raise_py_error
Expand Down
7 changes: 2 additions & 5 deletions src/kaa/kaacore/scenes.pxd
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from libc.stdint cimport uint32_t

from .nodes cimport CNode
from .clock cimport CSeconds
from .camera cimport CCamera
from .views cimport CViewsManager
from .spatial_index cimport CSpatialIndex
Expand All @@ -14,10 +13,8 @@ cdef extern from "kaacore/scenes.h" nogil:
CSpatialIndex spatial_index

CCamera& camera()
void process_frame(uint32_t dt)
void on_attach()
void on_enter()
void update(uint32_t dt)
void update(CSeconds dt)
void on_exit()
void on_detach()
void process_nodes(uint32_t dt)
25 changes: 13 additions & 12 deletions src/kaa/kaacore/timers.pxd
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
from libcpp cimport bool
from libc.stdint cimport uint32_t
from libcpp.functional cimport function

from .scenes cimport CScene
from .clock cimport CSeconds
from .exceptions cimport raise_py_error
from .glue cimport CPythonicCallbackWrapper, CPythonicCallbackResult


cdef extern from "kaacore/timers.h" nogil:
ctypedef function[void()] CTimerCallback "kaacore::TimerCallback"
cdef cppclass CTimerContext "kaacore::TimerContext":
CSeconds interval
CScene* scene

cdef cppclass CTimerCallback "kaacore::TimerCallback":
pass

cdef cppclass CTimer "kaacore::Timer":
CTimer()
CTimer(const CTimerCallback callback, const uint32_t interval,
const bool single_shot
)
CTimer(const CTimerCallback callback)

void start() except +raise_py_error
void start_global(const CSeconds seconds) except +raise_py_error
void start(const CSeconds seconds, CScene* const scene) except +raise_py_error
bool is_running()
void stop()
uint32_t interval()
void interval(uint32_t) except +raise_py_error
bool single_shot()
void single_shot(bool) except +raise_py_error

cdef extern from "extra/include/pythonic_callback.h":
ctypedef CPythonicCallbackResult[void] (*CythonTimerCallback)(const CPythonicCallbackWrapper&)
CythonTimerCallback bind_cython_timer_callback(
ctypedef CPythonicCallbackResult[CSeconds] (*CythonTimerCallback)(const CPythonicCallbackWrapper&, CTimerContext)
CTimerCallback bind_cython_timer_callback(
const CythonTimerCallback cy_handler,
const CPythonicCallbackWrapper callback
)
7 changes: 4 additions & 3 deletions src/kaa/nodes.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from cymove cimport cymove as cmove
from .extra.optional cimport optional, nullopt

from .kaacore.shapes cimport CShape
from .kaacore.clock cimport CSeconds
from .kaacore.sprites cimport CSprite
from .kaacore.nodes cimport (
CNodeType, CNode, CNodePtr, CNodeOwnerPtr, CForeignNodeWrapper,
Expand Down Expand Up @@ -306,11 +307,11 @@ cdef class NodeBase:

@property
def lifetime(self):
return self._get_c_node().lifetime()
return self._get_c_node().lifetime().count()

@lifetime.setter
def lifetime(self, uint32_t new_lifetime):
self._get_c_node().lifetime(new_lifetime)
def lifetime(self, double new_lifetime):
self._get_c_node().lifetime(CSeconds(new_lifetime))

@property
def transition(self):
Expand Down
15 changes: 8 additions & 7 deletions src/kaa/scenes.pxi
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from libc.stdint cimport uint32_t
from libcpp.memory cimport unique_ptr
from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF
from cpython.weakref cimport PyWeakref_NewRef
from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF

from .kaacore.glue cimport CPythonicCallbackResult
from .kaacore.nodes cimport CNodePtr
from .kaacore.scenes cimport CScene
from .kaacore.clock cimport CSeconds
from .kaacore.nodes cimport CNodePtr
from .kaacore.glue cimport CPythonicCallbackResult
from .kaacore.engine cimport is_c_engine_initialized
from .kaacore.log cimport c_emit_log_dynamic, CLogLevel, _log_category_wrapper
from .kaacore.views cimport views_default_z_index
Expand All @@ -32,7 +33,7 @@ cdef cppclass CPyScene(CScene):
void on_enter() nogil:
this._call_py_on_enter().unwrap_result()

void update(uint32_t dt) nogil:
void update(CSeconds dt) nogil:
this._call_py_update(dt).unwrap_result()

void on_exit() nogil:
Expand All @@ -42,9 +43,9 @@ cdef cppclass CPyScene(CScene):
with gil:
Py_DECREF(this.get_py_scene())

CPythonicCallbackResult[void] _call_py_update(uint32_t dt) with gil:
CPythonicCallbackResult[void] _call_py_update(CSeconds dt) with gil:
try:
this.get_py_scene().update(dt)
this.get_py_scene().update(dt.count())
except BaseException as exc:
return CPythonicCallbackResult[void](<PyObject*>exc)
return CPythonicCallbackResult[void]()
Expand Down Expand Up @@ -86,12 +87,12 @@ cdef class Scene:
assert c_scene != NULL
self._c_scene = unique_ptr[CPyScene](c_scene)

self.input_ = InputManager()
self.views = _ViewsManager.create(self)
self.spatial_index = _SpatialIndexManager.create(self)
self._root_node_wrapper = get_node_wrapper(
CNodePtr(&self._c_scene.get().root_node)
)
self.input_ = InputManager()

def on_enter(self):
pass
Expand Down
Loading

0 comments on commit e1b44b3

Please sign in to comment.