+ static bool oldButtonDown = false;
+ static int oldx, oldy;
+ if (buttons == 3 && !oldButtonDown)
+ {
+ oldButtonDown = true;
+ view->ToggleGPUTransform();
+ oldx = x;
+ oldy = y;
+ return;
+ }
+ if (buttons == 2 && !oldButtonDown)
+ {
+ oldButtonDown = true;
+ view->ToggleGPURendering();
+ oldx = x;
+ oldy = y;
+ }
+ if (buttons && !oldButtonDown)
+ {
+ // We're beginning a drag.
+ oldButtonDown = true;
+ oldx = x;
+ oldy = y;
+ scr->SetMouseCursor(Screen::CursorMove);
+ }
+ if (buttons)
+ {
+ view->Translate(Real(oldx-x)/Real(scr->ViewportWidth()), Real(oldy-y)/Real(scr->ViewportHeight()));
+ }
+ else
+ {
+ oldButtonDown = false;
+ scr->SetMouseCursor(Screen::CursorArrow);
+ }
+ oldx = x;
+ oldy = y;
+
+ if (wheel)
+ {
+ view->ScaleAroundPoint(Real(x)/Real(scr->ViewportWidth()),Real(y)/Real(scr->ViewportHeight()), Real(expf(-wheel/20.f)));
+ }
+}
+
+
+void MainLoop(Document & doc, Screen & scr, View & view, int max_frames = -1)
+{
+ // order is important... segfaults occur when screen (which inits GL) is not constructed first -_-
+
+
+ //scr.DebugFontInit("fonts/DejaVuSansMono.ttf", 12);
+ scr.DebugFontInit("fonts/DejaVuSansMono.ttf", 18);
+ scr.SetMouseHandler(RatCatcher);
+
+ ifstream tmp;
+ istream * script_input = NULL;
+ if (script_filename != NULL)
+ {
+ if (strcmp(script_filename, "stdin") == 0)
+ script_input = &cin;
+ else
+ {
+ tmp.open(script_filename);
+ script_input = &tmp;
+ }
+ }
+ DebugScript script(script_input);
+
+ double total_cpu_time = 0;
+ double total_gpu_time = 0;
+ double total_real_time = 0;
+
+ // MINGW doesn't support a lot of ctime stuff here
+ #ifndef __MINGW32__
+ struct timespec real_clock_start;
+ struct timespec real_clock_now;
+ struct timespec real_clock_prev;
+ clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_start);
+ real_clock_now = real_clock_start;
+ #endif
+
+
+ double frames = 0;
+ double data_rate = 0; // period between data output to stdout (if <= 0 there will be no output)
+ uint64_t data_points = 0;
+ setbuf(stdout, NULL);
+ int frame_number = 0;
+ while (scr.PumpEvents() && (max_frames < 0 || frame_number++ < max_frames))