Skip to content

Commit

Permalink
idea for a busy update loop
Browse files Browse the repository at this point in the history
  • Loading branch information
smallcluster committed Feb 7, 2022
1 parent 6ded682 commit 8579898
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.9)
project(BasicBool)

set(CMAKE_CXX_STANDARD 17)

set(CMAKE_EXE_LINKER_FLAGS -static)

add_executable(
Expand Down Expand Up @@ -68,4 +69,4 @@ target_link_libraries(${PROJECT_NAME} PRIVATE glad)
find_package(OpenMP REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE OpenMP::OpenMP_CXX)

target_include_directories(${PROJECT_NAME} PUBLIC external/stb)
target_include_directories(${PROJECT_NAME} PUBLIC external/stb)
63 changes: 30 additions & 33 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "node/nodes.hpp"
#include "gui/gui.hpp"
#include <chrono>
#include <cstdint>
#include <ratio>
#include <thread>
#include <cmath>
Expand Down Expand Up @@ -273,30 +272,7 @@ int main(int argc, char const *argv[]) {

// ---- simulation ----

// start cycle
if (!unlimitedTickTime) {
if (!beginUpdateDone) {
NodeManager.beginUpdate();
beginUpdateDone = true;
simStartTime = std::chrono::steady_clock::now();
}
auto time = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(time - simStartTime);
auto t = (float) ((double) duration.count() / (double) tickTime.count());
if (t > 1)
t = 1;
// update animation
NodeManager.setProgress(t);
// end cycle
if (duration >= tickTime) {
NodeManager.endUpdate();
beginUpdateDone = false;
}
} else {
NodeManager.beginUpdate();
NodeManager.setProgress(1);
NodeManager.endUpdate();
}


NodeManager.render(pmat, view, (invView * vec4(0, 0, 0, 1)).xy,
(invView * vec4((float) width, (float) height, 0, 1)).xy);
Expand Down Expand Up @@ -405,21 +381,42 @@ int main(int argc, char const *argv[]) {
}



// TODO : Use system clock to increase sleep resolution

auto endTime = std::chrono::steady_clock::now();
auto workTime = endTime-startTime;
auto targetTime = std::chrono::duration<uint64_t , std::ratio<1, 80>>{1}; // 80 Hz
if(workTime < targetTime){
std::this_thread::sleep_for(targetTime - workTime);
static auto targetTime = std::chrono::duration<long long , std::ratio<1, 60>>{1}; // 60 Hz

// TODO : improve this busy update loop
while (std::chrono::steady_clock::now()-startTime < targetTime){
// start cycle
if (!unlimitedTickTime) {
if (!beginUpdateDone) {
NodeManager.beginUpdate();
beginUpdateDone = true;
simStartTime = std::chrono::steady_clock::now();
}
auto time = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(time - simStartTime);
auto t = (float) ((double) duration.count() / (double) tickTime.count());
if (t > 1)
t = 1;
// update animation
NodeManager.setProgress(t);
// end cycle
if (duration >= tickTime) {
NodeManager.endUpdate();
beginUpdateDone = false;
}
} else {
NodeManager.beginUpdate();
NodeManager.setProgress(1);
NodeManager.endUpdate();
}
}

// FPS info
auto afterSleep = std::chrono::steady_clock::now();
long long finalElapsedTime = std::chrono::duration_cast<std::chrono::microseconds>(afterSleep-startTime).count();

int fps = (int) (1000000.0 / (double) finalElapsedTime);

string text = std::to_string(fps);
font.text("fps : " + text, vec2(0, height-font.getHeight("fps : " + text, 20)), 20, vec3(1));
font.render(pmat);
Expand Down

0 comments on commit 8579898

Please sign in to comment.