Call GLFW functions via a dynamic dispatch table.

This allows us to use GLFW without needing to introduce a link-time dependency on the library, which in turn enables us to get GLFW through Python.

PiperOrigin-RevId: 470100999
Change-Id: I27c84ad3310afdbebd582ab0033b6a963ba6307e
This commit is contained in:
Saran Tunyasuvunakool
2022-08-25 15:41:14 -07:00
committed by Copybara-Service
parent d26501c0f3
commit 3240596c3a
7 changed files with 294 additions and 94 deletions
+8 -5
View File
@@ -23,6 +23,7 @@
#include <mujoco/mujoco.h>
#include <mujoco/mjxmacro.h>
#include "glfw_dispatch.h"
#include "simulate.h"
#include "array_safety.h"
@@ -30,6 +31,8 @@ namespace {
namespace mj = ::mujoco;
namespace mju = ::mujoco::sample_util;
using ::mujoco::Glfw;
// constants
const double syncmisalign = 0.1; // maximum time mis-alignment before re-sync
const double refreshfactor = 0.7; // fraction of refresh available for simulation
@@ -158,7 +161,7 @@ void PhysicsLoop(mj::Simulate& sim) {
// running
if (sim.run) {
// record cpu time at start of iteration
double tmstart = glfwGetTime();
double tmstart = Glfw().glfwGetTime();
// inject noise
if (sim.ctrlnoisestd) {
@@ -195,8 +198,8 @@ void PhysicsLoop(mj::Simulate& sim) {
// in-sync
else {
// step while simtime lags behind cputime, and within safefactor
while ((d->time*sim.slow_down-simsync) < (glfwGetTime()-cpusync) &&
(glfwGetTime()-tmstart) < refreshfactor/sim.vmode.refreshRate) {
while ((d->time*sim.slow_down-simsync) < (Glfw().glfwGetTime()-cpusync) &&
(Glfw().glfwGetTime()-tmstart) < refreshfactor/sim.vmode.refreshRate) {
// clear old perturbations, apply new
mju_zero(d->xfrc_applied, 6*m->nbody);
sim.applyposepertubations(0); // move mocap bodies only
@@ -268,7 +271,7 @@ int main(int argc, const char** argv) {
auto sim = std::make_unique<mj::Simulate>();
// init GLFW
if (!glfwInit()) {
if (!Glfw().glfwInit()) {
mju_error("could not initialize GLFW");
}
@@ -286,7 +289,7 @@ int main(int argc, const char** argv) {
// terminate GLFW (crashes with Linux NVidia drivers)
#if defined(__APPLE__) || defined(_WIN32)
glfwTerminate();
Glfw().glfwTerminate();
#endif
return 0;