From 5bb8f8845b3ba89aacacca025799e29e9397fa5f Mon Sep 17 00:00:00 2001 From: omigamedev Date: Mon, 8 Oct 2018 12:03:57 +0200 Subject: [PATCH] separate global tick from draw so on tick any node can request a redraw --- PanoPainter-OSX/main.cpp | 23 ++++++++--------------- PanoPainter/GameViewController.m | 9 +++++++-- src/app.cpp | 2 -- src/app.h | 1 + src/app_events.cpp | 5 +++++ src/node.cpp | 5 +++++ src/node.h | 1 + src/node_text_input.cpp | 3 +++ 8 files changed, 30 insertions(+), 19 deletions(-) diff --git a/PanoPainter-OSX/main.cpp b/PanoPainter-OSX/main.cpp index 44fc531..7727835 100644 --- a/PanoPainter-OSX/main.cpp +++ b/PanoPainter-OSX/main.cpp @@ -155,11 +155,14 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTime { static double _timeFreq = CVGetHostClockFrequency(); static double _prevTime = (double)outputTime->hostTime / _timeFreq; + static double elapsed = 0; double hostTime = (double)outputTime->hostTime; double now = hostTime / _timeFreq; - + double dt = now - _prevTime; + _prevTime = now; + // this will not update unless 1/30th of a second has passed since the last update - if (1 /*now < _prevTime + (1.0 / 30.0) &&*/ ) + //if (now < _prevTime + (1.0 / 30.0)) { std::deque> working_list; { @@ -180,17 +183,14 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTime working_list.pop_front(); } - double dt = now - _prevTime; - - if (dt > 0.1) - App::I.redraw = true; + App::I.tick(dt); if (App::I.redraw) { App::I.clear(); - App::I.update(dt); + App::I.update(elapsed); + elapsed = 0; CGLFlushDrawable([glctx CGLContextObj]); - _prevTime = now; } //[[self openGLContext] flushBuffer]; @@ -199,13 +199,6 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTime CGLUnlockContext([glctx CGLContextObj]); return NO; } - else - { - // change whatever you want to change here, as a function of time elapsed - _prevTime = now; - // return YES to have your layer redrawn - return YES; - } return kCVReturnSuccess; } diff --git a/PanoPainter/GameViewController.m b/PanoPainter/GameViewController.m index 565a590..210d66f 100644 --- a/PanoPainter/GameViewController.m +++ b/PanoPainter/GameViewController.m @@ -427,8 +427,11 @@ std::set ignored_touch; - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { static auto time = std::chrono::steady_clock::now(); + static double elapsed = 0; auto now = std::chrono::steady_clock::now(); auto dt = std::chrono::duration(now - time); + time = now; + elapsed += dt.count(); std::deque> working_list; if (!tasklist.empty()) @@ -436,6 +439,8 @@ std::set ignored_touch; std::lock_guard lock(task_mutex); working_list = std::move(tasklist); } + + App::I.tick(dt.count()); [self async_lock]; if (!(App::I.redraw || App::I.animate || !working_list.empty())) @@ -451,10 +456,10 @@ std::set ignored_touch; working_list.pop_front(); } App::I.clear(); - App::I.update(dt.count()); + App::I.update(elapsed); [self.context presentRenderbuffer:GL_FRAMEBUFFER]; [self async_unlock]; - time = now; + elapsed = 0; } @end diff --git a/src/app.cpp b/src/app.cpp index 3ab40dd..360cd55 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -442,8 +442,6 @@ void App::update(float dt) if (canvas && canvas->m_canvas) canvas->m_canvas->stroke_draw(); - layout[main_id]->tick(dt); - if (!(redraw || animate)) return; diff --git a/src/app.h b/src/app.h index 14b72f8..2a537bd 100644 --- a/src/app.h +++ b/src/app.h @@ -112,6 +112,7 @@ public: bool request_close(); void terminate(); void clear(); + void tick(float dt); void update(float dt); void async_start(); void async_update(); diff --git a/src/app_events.cpp b/src/app_events.cpp index 58546d7..1f53c21 100644 --- a/src/app_events.cpp +++ b/src/app_events.cpp @@ -11,6 +11,11 @@ std::string win32_open_file(); using namespace ui; +void App::tick(float dt) +{ + layout[main_id]->tick(dt); +} + void App::resize(float w, float h) { redraw = true; diff --git a/src/node.cpp b/src/node.cpp index 77f57fa..561d4e0 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -35,6 +35,11 @@ #include "node_changelog.h" #include "node_usermanual.h" +void Node::app_redraw() +{ + App::I.redraw = true; +} + void Node::async_start() { App::I.async_start(); diff --git a/src/node.h b/src/node.h index b09ae48..d40c32b 100644 --- a/src/node.h +++ b/src/node.h @@ -211,6 +211,7 @@ public: virtual void removed(Node* parent); const Node* init_template(const char* id); void tick(float dt); + void app_redraw(); void async_start(); void async_update(); void async_end(); diff --git a/src/node_text_input.cpp b/src/node_text_input.cpp index 9aa34da..9554596 100644 --- a/src/node_text_input.cpp +++ b/src/node_text_input.cpp @@ -16,7 +16,10 @@ void NodeTextInput::on_tick(float dt) { timer = 0; if (m_cursor) + { m_cursor->m_display = !m_cursor->m_display; + app_redraw(); + } } }