From 3d9d9818ed1eee9286f1b53ecf145437757da03c Mon Sep 17 00:00:00 2001
From: Oliver Bock <oliver.bock@aei.mpg.de>
Date: Tue, 8 Apr 2008 16:05:35 +0200
Subject: [PATCH] Added proper timing

* For sphere rotation
* For observatory positioning

Note: the original code seems to have a bug which explains a slight discrepancy between this new and the original visualisation.
In the original code gmt_dtime() is used to get the GMT time, but this function is a mere #define of dtime() under unix, hence any
difference of dtime() and gmt_dtime() will result in 0 instead of the real UTC/GMT offset.

Note: dtime() returns local time...
---
 Starsphere.cpp    | 26 ++++++++++----------------
 Starsphere.h      | 13 ++++++++-----
 WindowManager.cpp |  8 ++++----
 WindowManager.h   |  2 ++
 4 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/Starsphere.cpp b/Starsphere.cpp
index 67d743b..a2e7b2a 100644
--- a/Starsphere.cpp
+++ b/Starsphere.cpp
@@ -26,10 +26,6 @@ Starsphere::Starsphere() : AbstractGraphicsEngine()
 
 	rotation_offset = 0.0;
 	rotation_speed = 180.0;
-
-	/* Time info */
-	gmt_offset=0.0;
-	show_gmt=true;
 	
 	m_RefreshSearchMarker = true;
 }
@@ -224,11 +220,12 @@ void Starsphere::make_obs()
 	GLfloat arm_len_deg=3.000; // lenght of arms, in degrees (not to scale)
 	GLfloat h2=0.400; // slight offset for H2 arms
 
-	double obs_gmt_dtime; // current time in GMT to get zenith position
-
-	obs_gmt_dtime = 0.0; //FIXME: gmt_dtime();  // Current GMT time
-	obs_dtime_drawn = 0.0; //FIXME:dtime();    // dtime() not gmt_dtime(), for difference
-	gmt_offset = obs_gmt_dtime - obs_dtime_drawn; // save for GMT display
+	// get current time and UTC offset (for zenith position)
+	m_ObservatoryDrawTimeLocal = dtime();
+	time_t local = m_ObservatoryDrawTimeLocal;
+	tm *utc = gmtime(&local);
+	double utcOffset = difftime(local, mktime(utc));	
+	double observatoryDrawTimeGMT = m_ObservatoryDrawTimeLocal - utcOffset;
 
 	radius = 1.0*sphRadius; // radius of sphere on which they are drawn
 
@@ -241,7 +238,7 @@ void Starsphere::make_obs()
 	Lat= 30.56377;
 	Lon= 90.77408;
 
-	RAdeg= RAofZenith(obs_gmt_dtime, Lon);
+	RAdeg= RAofZenith(observatoryDrawTimeGMT, Lon);
 	DEdeg= Lat;
 
 	if (!LLOmarker)
@@ -270,7 +267,7 @@ void Starsphere::make_obs()
 	Lat= 46.45510;
 	Lon= 119.40627;
 
-	RAdeg= RAofZenith(obs_gmt_dtime, Lon);
+	RAdeg= RAofZenith(observatoryDrawTimeGMT, Lon);
 	DEdeg= Lat;
 
 	if (!LHOmarker)
@@ -312,7 +309,7 @@ void Starsphere::make_obs()
 	Lon= -9.80683;
 	arm_len_deg=1.50; // not to scale
 
-	RAdeg= RAofZenith(obs_gmt_dtime, Lon);
+	RAdeg= RAofZenith(observatoryDrawTimeGMT, Lon);
 	DEdeg= Lat;
 
 	if (!GEOmarker)
@@ -501,9 +498,6 @@ void Starsphere::resize(const int width, const int height)
 	glLoadIdentity();
 	gluPerspective(95.0, aspect, 0.50, 25.0);
 	glMatrixMode(GL_MODELVIEW);
-
-	// Update in case time (zone?) change
-	gmt_offset = 2; //FIXME: gmt_dtime() - dtime(); 
 }
 
 /**
@@ -661,7 +655,7 @@ void Starsphere::render(const double timeOfDay)
 
 	if (isFeature(OBSERVATORIES)) {
 		glPushMatrix();
-		Zobs = (timeOfDay - obs_dtime_drawn) * 15.0/3600.0;
+		Zobs = (timeOfDay - m_ObservatoryDrawTimeLocal) * 15.0/3600.0;
 		glRotatef(Zobs, 0.0, 1.0, 0.0);
 		glCallList(LLOmarker);
 		glCallList(LHOmarker);
diff --git a/Starsphere.h b/Starsphere.h
index ce4431e..ad23c31 100644
--- a/Starsphere.h
+++ b/Starsphere.h
@@ -3,6 +3,7 @@
 
 #include <cmath>
 #include <cstdio>
+#include <ctime>
 #include <string>
 #include <iostream>
 #include <iomanip>
@@ -12,6 +13,8 @@
 #include <SDL_opengl.h>
 #include <FTGLPolygonFont.h>
 
+#include <util.h>
+
 #include "AbstractGraphicsEngine.h"
 
 /* SIN and COS take arguments in DEGREES */
@@ -93,11 +96,6 @@ private:
 	GLfloat rotation_offset; // so that we can rotate sphere
 	GLfloat rotation_speed; // degrees per minute
 
-	/* Time info */
-	double obs_dtime_drawn; // dtime() when obs were drawn
-	double gmt_offset; // dtime() correction to get GMT
-	bool show_gmt; // show clock or not?
-
 	// Graphics state info:
 	float aspect;
 	
@@ -139,9 +137,14 @@ private:
 	string m_WUPercentDone;
 	string m_WUCPUTime;
 	
+	// search marker info
 	double m_CurrentRightAscension;
 	double m_CurrentDeclination;
 	bool m_RefreshSearchMarker;
+	
+	// observatory movement
+	// (in seconds since 1970 with usec precision)
+	double m_ObservatoryDrawTimeLocal;
 };
 
 /* Constellation & star coordinates are in starlist.C */
diff --git a/WindowManager.cpp b/WindowManager.cpp
index c6e4cf1..2e7189b 100644
--- a/WindowManager.cpp
+++ b/WindowManager.cpp
@@ -152,17 +152,17 @@ void WindowManager::eventLoop()
 
 	SDL_Event event;
 
-	static double i = 0.0;
-
 	while (SDL_WaitEvent(&event) ) {
 		if (event.type == SDL_USEREVENT &&
 			event.user.code == RenderEvent) {
 			
+			static int i = 0;
 #ifdef DEBUG_VALGRIND
-			if(i < 12.5) {
+			if(i < 500) {
+				i++;
 #endif
 				// notify our observers (currently exactly one)
-				eventObservers.front()->render(i += 0.025);
+				eventObservers.front()->render(dtime());
 #ifdef DEBUG_VALGRIND
 			}
 			else {
diff --git a/WindowManager.h b/WindowManager.h
index af954b8..2daa714 100644
--- a/WindowManager.h
+++ b/WindowManager.h
@@ -8,6 +8,8 @@
 
 #include <SDL.h>
 
+#include <util.h>
+
 #include "AbstractGraphicsEngine.h"
 
 using namespace std;
-- 
GitLab