Analog clock

From FSDeveloper Wiki
Revision as of 06:28, 5 November 2023 by Dragonflightdesign (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they 'flick over' - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy'n'paste code, but check that you haven't already declared the module_vars under different names.

Edited 22 August 2020 to replace the menu time change with a more reliable trap.


//----------------------------------------------------------
// Time - 12 hour analog clock
// Driven from seconds only to get better movement of the hands
// 3,600 seconds to an hour
// 43,200 seconds to twelve hours
//----------------------------------------------------------

MODULE_VAR localsecond = { CLOCK_SECOND };
MODULE_VAR tick18 = { TICK18 };

// Global clock hand variables for the gauge display 
double second_hand = 0;
double minute_hand = 0;
double hour_hand = 0;
 
//----------------------------------------------------------
// Function to be called from the gauge update -
// Can be used in any project without having to change anything
//----------------------------------------------------------
void analogClock()
{
  int x = 0;
 
  static double mins = -1;
  static double hours = -1;
  static double prev_secs = -1;
  static double set_clock = 0;
  static double prev_tick = -1;
  int min_check = 0; 
  int force_update_period = 5;  // Number of minutes difference between menu and clock that will force a clock update
 
  // Set clock to the current simulator time
  if (set_clock == 0)
  {
    set_clock = 1;
    mins = localminutes.var_value.n;
    // Apply the current minutes to the check variable
    min_check = mins;
    hours = localhours.var_value.n;
    // 24 hour trap
    if (hours > 12) hours -= 12;
    // Adjust for needle positioning
    hours *= 3600;
    mins *= 60;
    hours += mins;
  }
  else
  {
    // Resume normal updates
    x = localseconds.var_value.n;
    if (x != prev_secs)
    {
      prev_secs = x;
      // Minute hand
      mins += 1;
      if (mins >= 3599)mins = 0;
      // Apply the actual current minutes to the check variable
      min_check = (int)mins/60;
      // Hour hand
      hours += 1;
      if (hours >= 43199)hours = 0;
    }
  }

  // Trap a time change by menu. 
  // A difference of five minutes or more will trigger a clock update
  x = (int)localminutes.var_value.n;
  if (abs(x - min_check) > force_update_period)set_clock = 0;
 
  return;
}